/**
 * effects_lib.js v2.1, builded by Sergey Shuchkin <shuchkin@mail.ru>
 *
 * Features:
 * fade,
 * slide,slideTop,slideRight,slideBottom,slideLeft,
 * glide,glideTop,glideRight,glideBottom,glideLeft,
 * wipe,
 * unfurl,
 * grow,
 * shrink,
 * highlight
 *
 * Examples:
 * 1) ... onclick="Effects.hide(this,['fade','slide'], 5);"
 * 2) Effects.show("myDiv","shrink",10);
 * 3) Effects.hide("myDiv","glide",8);
 * 4) Effects.show("myDiv");
 */

/* Effect hierarchy contains functions for working with visual effects.
 * These are called to progressively style the DOM elements as menus show
 * and hide. They do not have to set item visibility, but may want to set DOM
 * properties like clipping, opacity and position to create custom effects.
 *
 * @param ref [HTMLElement] -- target DOM element.
 * @param counter [number] -- an animation progress value, from 0 (start) to 100 (end).
 */

function getAbsolutePos(el, scrollOff) {
	var SL = 0, ST = 0;
	if (!scrollOff) {
		var is_div = /^div$/i.test(el.tagName);
		if (is_div && el.scrollLeft)
			SL = el.scrollLeft;
		if (is_div && el.scrollTop)
			ST = el.scrollTop;
	}
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};

Effect = []

/*
* Constant stores value for removing clip effect
*/
Effect.NO_CLIP = ((window.opera || navigator.userAgent.indexOf('KHTML') > -1) ?
	'' : 'rect(auto, auto, auto, auto)');

/**
 * \internal Sometimes is useful to execute some action for elements and all 
 * his childs.
 *
 * @param ref [HTMLElement] -- target DOM element.
 * @param funcToDo [function] -- function, that would be applied to ref.
 */

Effect.applyFunc = function(ref, funcToDo) {
	funcToDo(ref);

	for(var i = 0; i < ref.childNodes.length; i++) {
		Effect.applyFunc(ref.childNodes[i], funcToDo);
	}
}

Effect.fade = function(ref, counter) {
	if(ref.efOriginalOpacity == null && ref.__ef_opacitySaved == null){
		ref.efOpacitySaved = true;
		ref.efOriginalOpacity = document.all ? 
			ref.style.filter : ref.style.opacity != null ? 
				ref.style.opacity : ref.style.MozOpacity
		;
	}

	var md = null;

	var currentOpacity = 
		(!isNaN(parseFloat(ref.efOriginalOpacity || 1)) ?
			parseFloat(ref.efOriginalOpacity || 1) : (
				(md = ref.efOriginalOpacity.match(/alpha\(opacity=(\d+)\)/i)) ?
					parseInt(md[1]) / 100 : 1
			)
		) * counter / 100;

	if (ref.filters) {
		if (!ref.style.filter.match(/alpha/i)) {
			ref.style.filter += ' alpha(opacity=' + (currentOpacity * 100) + ')';
		} else if (ref.filters.length && ref.filters.alpha) {
			ref.style.filter = ref.style.filter.replace(/alpha\(opacity=\d+\)/ig, 'alpha(opacity=' + (Math.floor(currentOpacity * 100)) + ')')
		}
	} else {      
		if(counter > 0 && counter < 100){
			ref.style.opacity = ref.style.MozOpacity = currentOpacity;
		}
	}

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.filter = ref.style.opacity = ref.style.MozOpacity = ref.efOriginalOpacity;
		ref.efOriginalOpacity = null;
		ref.efOpacitySaved = null;
	}

	if(counter >= 100 && ref.efOpacitySaved != null) {
		ref.style.filter = ref.efOriginalOpacity;

		// FF blinks if set opacity=1 to element which already have it.
//		if(ref.efOriginalOpacity != "" && parseFloat(ref.efOriginalOpacity) != 1) {
			ref.style.opacity = ref.style.MozOpacity = ref.efOriginalOpacity;
//		}
			
		ref.efOriginalOpacity = null;
		ref.efOpacitySaved = null;
	}
};

Effect.slideTop = function(ref, counter) {
	return Effect.slide(ref, counter, 'top');
}

Effect.slideRight = function(ref, counter) {
	return Effect.slide(ref, counter, 'right');
}

Effect.slideBottom = function(ref, counter) {
	return Effect.slide(ref, counter, 'bottom');
}

Effect.slideLeft = function(ref, counter) {
	return Effect.slide(ref, counter, 'left');
}

Effect.slide = function(ref, counter, direction) {
	if(typeof(direction) != 'string'){
		return false;
	}

	direction = direction.toLowerCase();

	var direct = "";

	switch(direction){
		case 'top':
			// fall through
		case 'bottom':
			direct = "marginTop";
			break;
		case 'right':
			// fall through
		case 'left':
			direct = "marginLeft";
			break;
		default:
			return false;
	}

	var cP = Math.pow(Math.sin(Math.PI*counter/200),0.75);

	if (typeof ref.__ef_origmargin == 'undefined') {
		ref.__ef_origmargin = ref.style[direct];
	}

	if(counter == 100){
		ref.style.clip = Effect.NO_CLIP;
		ref.style[direct] = ref.__ef_origmargin;
	} else {
		switch(direction){
			case 'top':
				ref.style.clip = 'rect(' +
					(ref.offsetHeight*(1 - cP)) + 'px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';

				ref.style.marginTop = '-' + (ref.offsetHeight * (1 - cP)) + 'px';
				break;
			case 'right':
				ref.style.clip = 'rect(' +
					'0px, ' +
					(ref.offsetWidth * cP) + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';

				ref.style.marginLeft = (ref.offsetWidth * (1 - cP)) + 'px';
				break;
			case 'bottom':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight*cP + 'px, ' +
					'0px' +
				')';

				ref.style.marginTop = (ref.offsetHeight * (1 - cP)) + 'px';
				break;
			case 'left':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					ref.offsetWidth*(1 - cP) + 'px' +
				')';

				ref.style.marginLeft = "-" + (ref.offsetWidth * (1 - cP)) + 'px';
				break;
		}
	}

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
		ref.style[direct] = ref.__ef_origmargin;
	}
};

Effect.glideTop = function(ref, counter) {
	return Effect.glide(ref, counter, 'top');
}

Effect.glideRight = function(ref, counter) {
	return Effect.glide(ref, counter, 'right');
}

Effect.glideBottom = function(ref, counter) {
	return Effect.glide(ref, counter, 'bottom');
}

Effect.glideLeft = function(ref, counter) {
	return Effect.glide(ref, counter, 'left');
}

Effect.glide = function(ref, counter, direction) {
	if(typeof(direction) != 'string'){
		return false;
	}

	direction = direction.toLowerCase();

	var cP = Math.pow(Math.sin(Math.PI*counter/200),0.75);

	if(counter == 100){
		ref.style.clip = Effect.NO_CLIP;
	} else {
		switch(direction){
			case 'top':
				ref.style.clip = 'rect(' +
					(ref.offsetHeight*(1 - cP)) + 'px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';
				break;
			case 'right':
				ref.style.clip = 'rect(' +
					'0px, ' +
					(ref.offsetWidth * cP) + 'px, ' +
					ref.offsetHeight + 'px, ' +
					'0px' +
				')';
				break;
			case 'bottom':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight*cP + 'px, ' +
					'0px' +
				')';
				break;
			case 'left':
				ref.style.clip = 'rect(' +
					'0px, ' +
					ref.offsetWidth + 'px, ' +
					ref.offsetHeight + 'px, ' +
					ref.offsetWidth*(1 - cP) + 'px' +
				')';
				break;
		}
	}

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
	}
};

Effect.wipe = function(ref, counter) {
	ref.style.clip = (counter==100) ? Effect.NO_CLIP :
		'rect(0, ' + (ref.offsetWidth*(counter/100)) + 'px, ' +
		(ref.offsetHeight*(counter/100)) + 'px, 0)';

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
	}
};

Effect.unfurl = function(ref, counter) {
	if (counter <= 50) {
		ref.style.clip = 'rect(0, ' + (ref.offsetWidth*(counter/50)) +
			'px, 10px, 0)';
	} else if (counter < 100) {
		ref.style.clip =  'rect(0, ' + ref.offsetWidth + 'px, ' +
			(ref.offsetHeight*((counter-50)/50)) + 'px, 0)';
	} else {
		ref.style.clip = Effect.NO_CLIP;
	}

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
	}
};

Effect.shrink = function(ref, counter) {
	var paddingWidth = Math.floor(ref.offsetWidth * counter / 200);
	var paddingHeight = Math.floor(ref.offsetHeight * counter / 200);

	ref.style.clip = (counter >= 100) ? 
		Effect.NO_CLIP : "rect(" + (ref.offsetHeight / 2 - paddingHeight) + "px, " + (ref.offsetWidth/2 + paddingWidth) + "px, "
			+ (ref.offsetHeight / 2 + paddingHeight) + "px, " + (ref.offsetWidth/2 - paddingWidth) + "px)";

	if(counter <= 0){
		ref.style.display = 'none';
		ref.style.clip = Effect.NO_CLIP;
	}
}

Effect.grow = function(ref, counter) {
	Effect.shrink(ref, 100 - counter);
}

Effect.highlight = function(ref, counter) {
	if(ref.origbackground == null) {
		Effect.applyFunc(ref, function(){ 
			var el = arguments[0];

			if(el.nodeType == 1) {
				el.origbackground = el.style.backgroundColor;
			}
		});
	}

	Effect.applyFunc(ref, function(){ 
		var el = arguments[0];

		if(el.nodeType == 1) {
			el.style.backgroundColor = "#FFFF" + (255 - Math.floor(counter*1.5)).toString(16);
		}
	});

	if(counter <= 0 || counter >= 100) {
		Effect.applyFunc(ref, function(){ 
			var el = arguments[0];

			if(el.nodeType == 1) {
				el.style.backgroundColor = el.origbackground;
				el.origbackground = null;
			}
		});
	}
}

Effect.roundCorners = function(ref, outerColor, innerColor){
	if(!document.getElementById || !document.createElement){
	    return;
	}

	var ua = navigator.userAgent.toLowerCase();

	if(ua.indexOf("msie 5") != -1 && ua.indexOf("opera") == -1){
	    return;
	}

	var top = document.createElement("div");
	top.className = "rtop";
	top.style.backgroundColor = outerColor;
		
	for(var i = 1; i <= 4; i++){
		var child = document.createElement("span");
		child.className = "r" + i;
		child.style.backgroundColor = innerColor;
		top.appendChild(child);
	}

	ref.firstChild == null ? 
		ref.appendChild(top) : ref.insertBefore(top, ref.firstChild);

	var bottom = document.createElement("div");
	bottom.className = 'rbottom';
	bottom.style.backgroundColor = outerColor;

	for(var i = 4; i >= 1; i--){
		var child = document.createElement("span");
		child.className = 'r' + i;
		child.style.backgroundColor = innerColor;
		bottom.appendChild(child);
	}

	ref.appendChild(bottom);
	ref.__ef_roundCorners = true;
	ref.__ef_outerColor = outerColor;

	// if element has shadow - 
	if(ref.__ef_dropshadow != null){
		document.body.removeChild(ref.__ef_dropshadow);
		ref.__ef_dropshadow = null;
		Effect.dropShadow(ref, ref.__ef_deep);
	}
}

Effect.dropShadow = function(ref, deep) {
	// if element already have shadow or element is not visible - do nothing
	if(ref.__ef_dropshadow != null || ref.style.display == 'none'){
		return false;
	}

	// parse deep parameter.
	if(deep == null || isNaN(parseInt(deep))) {
		deep = 5;
	}

	ref.__ef_deep = deep;
	var shadow = document.createElement("div");
	
	shadow.style.position = "absolute";
	shadow.style.backgroundColor = "#666666";
	shadow.style.MozOpacity = 0.50;
	shadow.style.filter = "Alpha(Opacity=50)";
	var pos = getAbsolutePos(ref);
	shadow.style.left = (pos.x + deep) + "px";
	shadow.style.top = (pos.y + deep) + "px";
	shadow.style.width = ref.offsetWidth + "px";
	shadow.style.height = ref.offsetHeight + "px";
	shadow.style.visibility = ref.style.visibility;
	shadow.style.display = ref.style.display;

	ref.__ef_dropshadow = shadow;
		
	document.body.insertBefore(shadow, document.body.firstChild);

	if(ref.__ef_roundCorners){
		Effects.apply(shadow, 'roundCorners', {outerColor: ref.__ef_outerColor, innerColor: "#666666"});
	}

	return true;
}

Effects = []

/**
 * This method is used to show HTML element with some visual effects.
 *
 * @param ref [HTMLElement] -- the DOM element that contains the menu items.
 * @param effects [String or array] -- what effects apply to element. May be a 
 * string(when only one effect would be applied) or array of strings
 * @param animSpeed [number] -- animation speed. From 1(low speed) to 100(high speed)
 * @param onFinish[function] -- function to call when effect ends
 */

Effects.show = function(ref, effects, animSpeed, onFinish) {
	Effects.init(ref, true, effects, animSpeed, onFinish);
}

/**
 * This method is used to hide HTML element with some visual effects.
 *
 * @param ref [HTMLElement] -- the DOM element that contains the menu items.
 * @param effects [String or array] -- what effects apply to element. May be a 
 * string(when only one effect would be applied) or array of strings
 * @param animSpeed [number] -- animation speed. From 1(low speed) to 100(high speed)
 * @param onFinish[function] -- function to call when effect ends
 */

Effects.hide = function(ref, effects, animSpeed,  onFinish) {
	Effects.init(ref, false, effects, animSpeed, onFinish);
}

/**
 * This method is used to show/hide HTML element with some visual effects.
 *
 * @param ref [HTMLElement] -- the DOM element that contains the menu items.
 * @param show [boolean] -- if true - show element, false - hide element.
 * @param animSpeed [number] -- animation speed. From 1(low speed) to 100(high speed)
 * @param effects [String or array] -- what effects apply to element. May be a 
 * string(when only one effect would be applied) or array of strings
 * @param onFinish[function] -- function to call when effect ends
 */

Effects.init = function(ref, show, effects, animSpeed, onFinish){
	// checking input parameters
	if(ref == null ) return null;
	if(typeof ref == "string"){
		ref = document.getElementById(ref);
	}
	if(ref == null){
		return null;
	}
	if (typeof show == "undefined") show = true;
	if (typeof effects == "undefined") effects = "fade";
	if (typeof animSpeed == "undefined") animSpeed = 15;

	ref.animations = [];

	// if effects is given as string - replace it with array with one value
	if(typeof effects == "string")
		effects = [effects];

	for(var i = 0; i < effects.length; i++){
		var effect = null;
	    
		// analyzing given effects names
		switch(effects[i]){
			case 'fade':
				effect = Effect.fade;
				break;
			case 'slide':
				effect = Effect.slideTop;
				break;
			case 'slideTop':
				effect = Effect.slideTop;
				break;
			case 'slideRight':
				effect = Effect.slideRight;
				break;
			case 'slideBottom':
				effect = Effect.slideBottom;
				break;
			case 'slideLeft':
				effect = Effect.slideLeft;
				break;
			case 'glide':
				effect = Effect.glideTop;
				break;
			case 'glideTop':
				effect = Effect.glideTop;
				break;
			case 'glideRight':
				effect = Effect.glideRight;
				break;
			case 'glideBottom':
				effect = Effect.glideBottom;
				break;
			case 'glideLeft':
				effect = Effect.glideLeft;
				break;
			case 'wipe':
				effect = Effect.wipe;
				break; 
			case 'unfurl':
				effect = Effect.unfurl;
				break;
			case 'grow':
				effect = Effect.grow;
				break;
			case 'shrink':
				effect = Effect.shrink;
				break;
			case 'highlight':
				effect = Effect.highlight;
				break;
		}

		if(effect != null)
			ref.animations[ref.animations.length] = effect;
	}

	if(ref.animations.length != 0 && ref.running == null) {
		ref.running = true;
		Effects.run(ref, animSpeed, show, null, onFinish);
	}
}

/**
 * \internal is called from Effects.init. Runs periodically
 * updating element properties.
 *
 * @param ref [HTMLElement] -- the DOM element that contains the menu items.
 * @param animSpeed [number] -- animation speed. From 1(low speed) to 100(high speed)
 * @param show [boolean] -- if true - show element, false - hide element.
 * @param currVal [number] -- current progress - from 0 to 100.
 * @param onFinish[function] -- function to call when effect ends
 */

Effects.run = function(ref, animSpeed, show, currVal, onFinish) {
	if(animSpeed == null)
		animSpeed = 10;

	if(currVal < 0){
		currVal = 0;
	}

	if(currVal > 100){
		currVal = 100;
	}

	if(currVal == null) {
		if(show){
			currVal = 0

			if(ref.style.display == "none"){
				ref.style.display = '';

				if(ref.__ef_dropshadow != null) {
					ref.__ef_dropshadow.style.display = '';
				}
			}
		}
		else {
			currVal = 100;
		}
	}

	currVal += (show ? 1 : -1) * animSpeed;
	
	// run attached effects
	for (var i = 0; i < ref.animations.length; i++) {
		ref.animations[i](ref, currVal);

		if(ref.__ef_dropshadow != null) {
			ref.animations[i](ref.__ef_dropshadow, currVal);
		}
	}

	if (currVal <= 0 || currVal >= 100) {
		ref.running = null;

		if(onFinish != null){
			onFinish();
		}
		
		return;
	}
	else {
		setTimeout(function() {
			Effects.run(ref, animSpeed, show, currVal, onFinish);
		}, 50);
	}
}

Effects.apply = function(ref, effect, params){
	if(ref == null || effect == null) {
		return;
	}

	if(typeof ref == "string") {
		ref = document.getElementById(ref);
	}

	if(ref == null) {
		return;
	}

	switch(effect) {
		case 'roundCorners':
			if (typeof params == "undefined")
				return Effects.roundCorners(ref);
			else
				return Effect.roundCorners(ref, params['outerColor'], params['innerColor']);
		case 'dropShadow':
			if (typeof params == "undefined")
				return Effect.dropShadow(ref);
			else
				return Effect.dropShadow(ref, params['deep']);
	}
}
;
