/*
	uniAnimation Item Effect Fading

	Version:	1.0.0.
*/
uniAnimationItemEffect=function(obj, param) {
	var eList, tList;
	var self=this;
	this.obj		= obj;
	this.current		= 0;
	this.destination	= (param.destination ? param.destination : 1);
	this.width		= (param.width ? param.width : 0);
	this.height		= (param.height ? param.height : 0);
	this.duration		= (param.duration ? param.duration : 15);
	this.autorun		= (param.autorun ? param.autorun : false);
	this.status		= '';
	this.opacityAttr	= this.getOpacityAttr();
/*
	eList=obj.getElementsByClassName("item");
	this.count=eList.length;
	for (var i=0; i<eList.length; i++) {
		this.setMethods(eList[i]);
		if (!i) {
			if (!this.width) this.width=eList[i].clientWidth;
			if (!this.height) this.height=eList[i].clientHeight;
			eList[i].alpha(1);
			eList[i].animation.position=eList[i].animation.length;
		} else eList[i].alpha(0);

		// Searching TextBox
		if (param.textBox.animation) {
			tList=eList[i].getElementsByClassName('text_box');
			eList[i].textBox=(tList.length ? tList[0] : null);
			if (eList[i].textBox) {
				param.textBox.show=(!i);
				eList[i].textBox.animation=new uniAnimationTextBoxEffect(eList[i].textBox, (param.textBox ? param.textBox : {}));
				eList[i].textBox.animation.addEventListener('ShowCompleted', function() {self.onShowCompleted()});
				eList[i].textBox.animation.addEventListener('HideCompleted', function() {self.run()});
			}
		}
	}
	this.items=eList;
*/
	this.items=Array();
	eList=obj.getElementsByTagName("*");
	for (var i=0; i<eList.length; i++) {
		if (eList[i].className == 'item') {
			pos=this.items.length;
			this.items[pos]=eList[i];
			this.setMethods(this.items[pos]);
			if (!pos) {
				if (!this.width) this.width=this.items[pos].clientWidth;
				if (!this.height) this.height=this.items[pos].clientHeight;
				this.items[pos].alpha(1);
				this.items[pos].animation.position=this.items[pos].animation.length;
			} else this.items[pos].alpha(0);

			// Searching TextBox
			if (param.textBox.animation) {
				tList=this.items[pos].getElementsByTagName('*');
				for (var j=0; j<tList.length; j++) {
					if (tList[j].className == 'text_box') {
						this.items[pos].textBox=tList[j];
						break;
					}
				}
				if (this.items[pos].textBox) {
					param.textBox.show=(!pos);
					this.items[pos].textBox.animation=new uniAnimationTextBoxEffect(this.items[pos].textBox, (param.textBox ? param.textBox : {}));
					this.items[pos].textBox.animation.addEventListener('ShowCompleted', function() {self.onShowCompleted()});
					this.items[pos].textBox.animation.addEventListener('HideCompleted', function() {self.run()});
				}
			}
		}
	}
	this.count		= this.items.length;
	this.obj.style.left	= '0px';
	this.obj.style.top	= '0px';
	this.obj.style.width	= this.width+'px';
	this.obj.style.height	= this.height+'px';
	if (this.destination && this.autorun) this.durationTimeout = setTimeout(function() {self.stopDuration();}, (this.getElement(0).getAttribute('duration') ? this.getElement(0).getAttribute('duration') : this.duration)*1000);
}

uniAnimationItemEffect.prototype.getOpacityAttr=function() {

	if (typeof document.body.style.opacity == 'string') // CSS3 compliant (Moz 1.7+, Safari 1.2+, Opera 9)
		return 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') // Mozilla 1.6 ט לכאהרו, Firefox 0.8 
		return 'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') // Konqueror 3.1, Safari 1.1
		return 'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) // Internet Exploder 5.5+
		return 'filter';
	return false;
}

uniAnimationItemEffect.prototype.getElement=function(n) {
	return this.items[(n%this.count)];
}

uniAnimationItemEffect.prototype.getNextId=function() {
	var res=this.current+this.destination;
	return (res<0 ? this.count-1 : (res>=this.count ? 0 : res));
}

uniAnimationItemEffect.prototype.setMethods=function(obj) {
	var self=this;

	obj.animation={
		position	: 0,
		length		: 100,
		destination	: 1
	}

	obj.f=function(x) {
		return x/this.animation.length;
	}

	obj.alpha=function(val) {
		this.style.zIndex=(val<1 ? 0 : 1);
		
		if (self.opacityAttr=="filter") {
			val *= 100;
			// If opacity allready exist, then use "filters" for change it, else use "style.filter" for inserting it
    			var oAlpha = this.filters['DXImageTransform.Microsoft.alpha'] || this.filters.alpha;
			if (oAlpha) oAlpha.opacity = val;
    			else this.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+val+")"; // Use "+=" if we don't want delete already exist filters
  		} else this.style[self.opacityAttr] = val;
	}

	obj.show=function() {
		this.animation.destination=1;
		this.fading();
	}

	obj.hide=function() {
		this.animation.destination=-1;
		this.fading();
	}

	obj.fading=function() {
		this.alpha(this.f(this.animation.position));
		if ((this.animation.position==0 && this.animation.destination<0) || (this.animation.position==this.animation.length && this.animation.destination>0)) {
			if (this.textBox && this.textBox.animation && this.animation.destination>0) this.textBox.animation.show();
		} else {
			this.animation.position+=this.animation.destination;
			setTimeout(function() {obj.fading();}, 10);
		}
	}

	obj.onClick=function() {
		if (url=this.getAttribute('url')) window.location.href=url;
	}

}

uniAnimationItemEffect.prototype.onShowCompleted=function() {
	var self=this;
	if (this.autorun) this.durationTimeout = setTimeout(function() {self.stopDuration()}, (this.getElement(this.current).getAttribute('duration') ? this.getElement(this.current).getAttribute('duration') : this.duration)*1000);
}


uniAnimationItemEffect.prototype.stopDuration=function() {
	clearTimeout(this.durationTimeout);
	if (this.destination) {
		if (this.getElement(this.current).textBox && this.getElement(this.current).textBox.animation) this.getElement(this.current).textBox.animation.hide();
		else this.run();
	}
}

uniAnimationItemEffect.prototype.run=function() {
	this.getElement(this.current).hide();
	this.current=this.getNextId();
	this.getElement(this.current).show();
}

uniAnimationItemEffect.prototype.prev=function() {
	this.destination = -1;
	this.stopDuration();
}

uniAnimationItemEffect.prototype.next=function() {
	this.destination = 1;
	this.stopDuration();
}

uniAnimationItemEffect.prototype.pause=function() {
	this.destination = 0;
}

uniAnimationItemEffect.prototype.onClick=function() {
	this.getElement(this.current).onClick();
}
