/*  Copyright 2010 by Marco Stocco <http://www.my-w.org>
 * -----------------------------------------------------------
 *
 * Effects for the ajax loading
 *
 * This script is distributed under the GNU General Public License.
 *
 * Read the entire license text here: http://www.gnu.org/licenses/gpl.html
 */

myw.ajaxAnimate = {

	/* opacity, loading image */
	animate_start_myw_default : function(_vars) {
		var d = document.getElementById(_vars.div);			
		if (d.innerHTML != '' && d.style.display != 'none') {
			jQuery(d).animate(
				myw.ajaxAnimate.getAnimations(0,_vars),
				'medium',
				function() {
					myw.ajaxAnimate.loadingImg(_vars);
					myw.ajax.execute2(_vars);
				}
			);
		} else {
			if (_vars && _vars.effects && _vars.effects.loadingImg) {
				myw.ajaxAnimate.loadingImg(_vars);
				var d = document.getElementById(_vars.div);
				jQuery(d).animate({height:'show'},'medium', function() {
					myw.ajax.execute2(_vars);
				});
			} else {
				myw.ajax.execute2(_vars);
			}
		}
	},
	animate_end_myw_default : function(_vars) {
		jQuery(document.getElementById(_vars.div)).animate(
			myw.ajaxAnimate.getAnimations(1,_vars),
			'medium');
		myw.ajaxAnimate.loadingImgDelete(_vars);
	},
			
	/* mask and show in the middle of the screen a text */
	animate_start_myw_warning : function(_vars) {
		myw.mask_show(function(){void(0);});
		var newDiv = document.createElement('div');
		newDiv.id = 'myw_mask_loading';
		newDiv.style.position = 'absolute';
		myw.popup_center(newDiv,300,30);
		newDiv.style.backgroundColor = '#fff';
		newDiv.style.border = '1px #b22222 solid';
		newDiv.style.padding = '10px';
		newDiv.style.textAlign = 'center';
		newDiv.style.zIndex = 21;
		newDiv.innerHTML = '<span style="font-size: 18px; color: #b22222;">'+myw.lang.frontend.texts.WAIT + ' ...</font>'
		if (_vars.effects.loadingImg)
			newDiv.innerHTML += myw.ajaxAnimate.insertLoadingImage();
		document.body.appendChild(newDiv);
		myw.ajax.execute2(_vars);
	},
	animate_end_myw_warning : function() {
		if (document.getElementById('myw_mask_loading'))
			document.body.removeChild(document.getElementById('myw_mask_loading'));
		myw.mask_hide();
	},
	
	/* -------------------------- */
	
	insertLoadingImage : function() {
		return '<img src="'+myw.config.website.htdocs.net+'/admin/img/loading.gif" />';
	},
	
	loadingImg : function(_vars) {
		if (_vars && _vars.effects && _vars.effects.loadingImg) {
			var d = document.getElementById(_vars.div);
			if (d) {
				if (d.innerHTML != '' && d.style.display != 'none') {
					var newDiv = document.createElement('div');
					newDiv.id = _vars.div+'_myw_isChild';
					newDiv.style.zIndex = 100;
					newDiv.style.position = 'absolute';
					newDiv.innerHTML = myw.ajaxAnimate.insertLoadingImage();
					newDiv.style.left = Math.round(myw.mouse_x-20)+'px';
					newDiv.style.top = Math.round(myw.mouse_y+5)+'px';
					document.body.appendChild(newDiv);
				} else {
					d.style.display = 'none';
					/* <!--_MYW:LOADINGIMAGE--> does not work in IE. IE deletes the comment. */
					d.innerHTML = '<span style="display:none;">_MYW:LOADINGIMAGE</span><div style="text-align:left;">'+myw.ajaxAnimate.insertLoadingImage()+'</div>';
				}
			}
		}
	},
	
	loadingImgDelete : function(_vars) {
		if (document.getElementById(_vars.div+'_myw_isChild'))
			document.body.removeChild(document.getElementById(_vars.div+'_myw_isChild'));
	},
	
	getAnimations : function(option,_vars) {	/* option: 0 (close), 1 (open) */
		var d = document.getElementById(_vars.div);	
		var o = new Object();
		if (_vars && _vars.effects && _vars.effects.funcParams) {
			if (_vars.effects.funcParams == 'left') {
				if (option == 0) {
					if (d.parentNode)
						d.parentNode.style.overflow = 'auto';
					d.style.position='relative';
					o.left = -$('#'+_vars.div).outerWidth()-50;
				} else {
					o.left = 0;
				}
			} else if (_vars.effects.funcParams == 'top') {
				if (option == 0) {
					if (d.parentNode)
						d.parentNode.style.overflow = 'auto';
					d.style.position='relative';
					o.top = -$('#'+_vars.div).outerHeight()-50;
				} else {
					o.top = 0;
				}
			} else if (_vars.effects.funcParams == 'width' || _vars.effects.funcParams == 'height') {
				if (option == 0) {
					o[_vars.effects.funcParams] = 'hide';
				} else {
					o[_vars.effects.funcParams] = 'show';
				}
			} else {
				option == 0 ?
					o.opacity = 0.3 :
					o.opacity = 1;
			}
		} else {
			option == 0 ?
				o.opacity = 0.3 :
				o.opacity = 1;
		}
					
		return o;
	}

};

