//Fade image
fade = 0;
slide = 0;
fader.prototype = {
	init : function(){
		this.idx = 0;
		this.item = this._name;
		this.under = getElement(this.item);
		if(this.under!=null){
			this.opacity = 0;
			this.call = "this.fadeIn();";
			this.done = false;
			this.images = Array();
			this.img = '';
			if(this.list.length==0){
				this.img = new Image();
				this.over = getElement(this._name);
				this.img.src = this.over.src;
			} else if(this.list.length>1) {
				this.container = getElement(this._name).parentNode.parentNode; //DIV CONTAINING IMG AND HREF
				if(this.link==null){
					a = createElement("a","style","position:absolute;top:0px;left:0px;z-index:2","name",'fade'+this.idx,"title",this.title[this.idx]); //,"rel","lightbox"
				} else {
					if(this.title[this.idx]!='' && typeof this.title[this.idx]!='undefined'){
						a = createElement("a","style","position:absolute;top:0px;left:0px;z-index:2","href",this.link[this.idx],"title",this.title[this.idx]); //,"rel","lightbox"
					} else {
						a = createElement("a","style","position:absolute;top:0px;left:0px;z-index:2","href",this.link[this.idx],"title",''); //,"rel","lightbox"
					}
				}
				i= createElement("img","id","fader-"+this._name,"class","fader-over");
				a.appendChild(i);
				this.img = new Image();
				this.img.src = this.list[this.idx];
				this.images[this.list[this.idx]] = this.img;
				this.container.appendChild(a);
				this.over=getElement("fader-"+this._name);
			} else if(this.list.length==1 && this.fadein==true){
				this.over = getElement(this._name);
			} else { return; }
			this.isLoaded();
		}
	},

	startFading : function(){
		if(typeof this.images[this.list[this.idx]]=='undefined'){
			this.img = new Image();
			this.img.src = this.list[this.idx];
			this.images[this.list[this.idx]] = this.img;
		} else {
			this.img = this.images[this.list[this.idx]];
			this.img.src = this.list[this.idx];
		}
		this.isLoaded();
	},
	
	isLoaded : function(){
		if(this._wait && this._wait.done==false){
			idTimer = setTimeout(this.isLoaded.bind(this),50);
		} else {
			if(this.img!='' && typeof this.over.src!='undefined' && this.img.complete!=true){
				idTimer = setTimeout(this.isLoaded.bind(this),50);
			} else {
				if(this.img!=''){
					this.over.src = this.img.src;
					this.over.width = this.img.width;
					this.over.height = this.img.height;
					if(this.title.length>0){
						this.over.parentNode.title = this.title[this.idx];
						this.over.alt = this.title[this.idx];
					}
					if(this.link!=null && this.link.length>0){
						this.over.parentNode.href = this.link[this.idx];
						myRE = new RegExp("(\.[png|gif|jpg|jpeg]{1})$","gi");
						if(this.link[this.idx].match(myRE)){
							this.over.parentNode.setAttribute('rel','lightbox');
						} else {
							this.over.parentNode.removeAttribute('rel');
						}
						this.lightbox.updateImageList();
					}
				}
				eval(this.call);
			}
		}
	},

	startFader : function(){
		this.under.src = this.img.src;
		this.under.width = this.img.width;
		this.under.height = this.img.height;
		//if(inOut=='out'){
			this.fadeOut();
		//}
	},

	fadeOut : function(){
		if(this.over){
			if (this.opacity >= 0) {
				if (this.over.style.MozOpacity!=null) {
					/* Mozilla's pre-CSS3 proprietary rule */
					this.over.style.MozOpacity = (this.opacity/100)-.001;
					/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
				} else if (this.over.style.opacity!=null) {
					/* CSS3 compatible */
					this.over.style.opacity = (this.opacity/100)-.001;
				} else if (this.over.style.filter!=null) {
					/* IE's proprietary filter */
					this.over.style.filter = "alpha(opacity="+this.opacity+")";
					/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
				}
				this.opacity -= 4;
				setTimeout(this.fadeOut.bind(this), 80);
			} else {
				this.over.src = this.under.src;
				this.over.width = this.under.width;
				this.over.height = this.under.height;
				this.opacity = 100;
				if (this.over.style.MozOpacity!=null) {
					/* Mozilla's pre-CSS3 proprietary rule */
					this.over.style.MozOpacity = (this.opacity/100)-.001;
					/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
				} else if (this.over.style.opacity!=null) {
					/* CSS3 compatible */
					this.over.style.opacity = (this.opacity/100)-.001;
				} else if (this.over.style.filter!=null) {
					/* IE's proprietary filter */
					this.over.style.filter = "alpha(opacity="+this.opacity+")";
					/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
				}
				setTimeout(this.startFading.bind(this), this.time);
			}
		}
	},
	
	fadeIn : function(){
		if (this.opacity < 100) {
			if (this.over.style.MozOpacity!=null) {
				/* Mozilla's pre-CSS3 proprietary rule */
				this.over.style.MozOpacity = (this.opacity/100)+.001;
				/* the .001 fixes a glitch in the opacity calculation which normally results in a flash when reaching 1 */
			} else if (this.over.style.opacity!=null) {
				/* CSS3 compatible */
				this.over.style.opacity = (this.opacity/100)+.001;
			} else if (this.over.style.filter!=null) {
				/* IE's proprietary filter */
				this.over.style.filter = "alpha(opacity="+this.opacity+")";
				/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
			}
			this.opacity += 5;
			//setTimeout(this.fadeOut.bind(this), 30);
			setTimeout(this.fadeIn.bind(this), 50);
		} else {
			this.done = true;
			this.idx++;
			if(this.idx==this.list.length){
				this.idx = 0;
			}
			this.opacity = 0;
			if(this.list!='' && this.list.length>1){
				this.under.style.MozOpacity = 1;
				this.under.style.opacity = 1;
				this.under.style.filter = 100;
				this.under.src=this.over.src;
				this.under.width=this.over.width;
				this.under.height=this.over.height;
				setTimeout(this.startFading.bind(this), this.time);
			}
		}
	}
}

function fader(name,wait,list,link,time,title,fadein){
	this._name = name;
	this.lightbox = new Lightbox();
	this._wait = wait || false;
	this.list = Array();
	if(typeof list!='undefined' && list!=null && list!=''){
		list = list.split("|");
		this.list = list;
	}
	this.link = Array();
	if(typeof link!='undefined' && link!=null && link!=''){
		link = link.split("|");
		this.link = link;
	} else {
		this.link=null;
	}
	if(typeof time!='undefined' && time!=null && time!=''){
		this.time = time;
	} else {
		this.time = 2500;
	}
	if(typeof fadein!='undefined' && fadein!=null && fadein!=''){
		this.fadein = fadein;
	} else {
		this.fadein = true;
	}
	this.title = Array();
	if(typeof title!='undefined' && title!=null && title!=''){
		title = title.split("|");
		this.title = title;
	}
	this.init();
}

slider.prototype = {
	init : function(){
		this.item = this._name;
		this.over = getElement(this.item);
		if(this.over==null){
			setTimeout("slider('"+this._name+"','"+this._wait+"')",500);
		} else {
			this.opacity = 0;
			this.call = "this.slideDown();";
			this.isLoaded();
			this.done=false;
		}
	},
	isLoaded : function(){
		if(this._wait && this._wait.done==false){
			idTimer = setTimeout(this.isLoaded.bind(this),800);
		} else {
			eval(this.call);
		}
	},
	slideDown : function(){
		mt = parseInt(this.over.style.top);
		if(mt<35){
			this.over.style.top = (mt+2)+"px";
			setTimeout(this.slideDown.bind(this), 25);
		} else {
			this.over.style.top = "35px";
			this.done=true;
		}
	}
}


function slider(name,wait){
	this._name = name;
	this._wait = wait || false;
	this.done = false;
	this.init();
}

function preloadImages(arr){
	x = Array();
	for(i=0;i<arr.length;i++){
		x[i] = new Image();
		x[i].src = arr[i];
	}
}
