jQuery.noConflict();

//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 = findAncestorDiv(getElement(this._name)); //DIV CONTAINING IMG AND HREF
				i = createElement("img","id","fader-"+this._name,"class","fader-over",'style','position:absolute;');
				if(typeof this.link[this.idx]!='undefined' && this.link[this.idx]!=''){
					a = createElement("a","href",this.link[this.idx]); //,"title",this.title[this.idx]
					this.container.insertBefore(a,this.container.firstChild);
					a.appendChild(createElement("img","id","fader-"+this._name,"class","fader-over",'style','position:absolute;'));
					//i = a;
				} else {
					this.container.insertBefore(i,this.container.firstChild);
				}
				
				this.img = new Image();
				this.img.src = this.list[this.idx];
				this.images[this.list[this.idx]] = this.img;
				
				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!='' && this.over && typeof this.over.src!='undefined' && this.img.complete!=true){
				idTimer = setTimeout(this.isLoaded.bind(this),50);
			} else {
				if(this.img!=''){
					if(this.over){
						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.length>0){
							this.over.parentNode.href = this.link[this.idx];
							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;
	}
	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();
}

//Fade image
function fader2(names,wait,list,link,time,title,fadein){
	this._name = names;
	this.items = names.split("|")
	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;
	}
	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();
}

fade = 0;
slide = 0;
fader2.prototype = {
	init : function(){
		this.idx = 0;
		this.under = new Array();
		this.over = new Array();
		this.container = new Array();
		this.img = new Array();
		//this.item = this._name;
		for(y=0;y<this.items.length;y++){
			this.under[y] = getElement(this.items[y]);
			if(this.under[y]!=null){
				this.opacity = 0;
				this.call = "this.fadeIn();";
				this.done = false;
				this.images = Array();
				this.img[y] = '';
				if(this.list.length==0){
					this.img[y] = new Image();
					this.over[y] = getElement(this.items[y]);
					this.img[y].src = this.over.src;
				} else if(this.list.length>1) {
					this.container[y] = findAncestorDiv(getElement(this.items[y])); //DIV CONTAINING IMG AND HREF
					im = createElement("img","id","faders-"+this.items[y],"class","fader-over",'style','position:absolute;');
					if(typeof this.link[this.idx+y]!='undefined' && this.link[this.idx+y]=='#'){
						this.link[this.idx+y] = '#a-fader-'+this.items[y];
					}
					a = createElement("a","href",this.link[this.idx+y],'name','a-fader-'+this.items[y]); //,"title",this.title[this.idx]
					this.container[y].insertBefore(a,this.container[y].firstChild);
					a.appendChild(im);
						//i = a;
					/*} else {
						this.container[y].insertBefore(im,this.container[y].firstChild);
					}*/
					
					this.img[y] = new Image();
					this.img[y].src = this.list[this.idx+y];
					this.images[this.list[this.idx]] = this.img[y];
					
					this.over[y]=getElement("faders-"+this.items[y]);
				} else if(this.list.length==1 && this.fadein==true){
					this.over[y] = getElement(this.items[y]);
				} else { return; }
				
			} else { return; }
		}
		this.isLoaded();
	},

	startFading : function(){
		i=0;
		for(y=0;y<this.items.length;y++){
			idx = (this.idx+y>=this.list.length)?i++:this.idx+y;
			//alert(idx+" "+(this.idx+y));
			if(typeof this.images[this.list[idx]]=='undefined'){
				this.img[y] = new Image();
				this.img[y].src = this.list[idx];
				this.images[this.list[idx]] = this.img[y];
			} else {
				this.img[y] = this.images[this.list[idx]];
				this.img[y].src = this.list[idx];
			}
		}
		this.isLoaded();
	},
	
	isLoaded : function(){
		if(this._wait && this._wait.done==false){
			idTimer = setTimeout(this.isLoaded.bind(this),50);
		} else {
			i=0;
			for(y=0;y<this.items.length;y++){
				idx = (this.idx+y>=this.list.length)?i++:this.idx+y;
				if(this.img[y]!='' && this.over[y] && typeof this.over[y].src!='undefined' && this.img[y].complete!=true){
					idTimer = setTimeout(this.isLoaded.bind(this),50);
					return;
				} else {
					if(this.img[y]!=''){
						if(this.over[y]){
							this.over[y].src = this.img[y].src;
							this.over[y].width = this.img[y].width;
							this.over[y].height = this.img[y].height;
							if(this.title.length>0){
								this.over[y].parentNode.title = this.title[this.idx];
								this.over[y].alt = this.title[this.idx];
							}
							if(this.link.length>0){
								if(typeof this.link[idx]!='undefined'){
									this.over[y].parentNode.href = this.link[idx];
								} else {
									this.over[y].parentNode.href = "#";
								}
								this.lightbox.updateImageList();
							}
						}
					} else {
						return;
					}
				}
			}
			
			eval(this.call);
		}
	},
	fadeIn : function(){
		for(y=0;y<this.items.length;y++){
			if (this.opacity < 100) {
				if (this.over[y].style.MozOpacity!=null) {
					/* Mozilla's pre-CSS3 proprietary rule */
					this.over[y].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[y].style.opacity!=null) {
					/* CSS3 compatible */
					this.over[y].style.opacity = (this.opacity/100)+.001;
				} else if (this.over[y].style.filter!=null) {
					/* IE's proprietary filter */
					this.over[y].style.filter = "alpha(opacity="+this.opacity+")";
					/* worth noting: IE's opacity needs values in a range of 0-100, not 0.0 - 1.0 */ 
				}
				if(y+1==this.items.length){
					this.opacity += 5;
					setTimeout(this.fadeIn.bind(this), 50);
				}
				//
			} else {
				this.done = true;
				if(this.list!='' && this.list.length>1){
					this.under[y].style.MozOpacity = 1;
					this.under[y].style.opacity = 1;
					this.under[y].style.filter = 100;
					this.under[y].src=this.over[y].src;
					this.under[y].width=this.over[y].width;
					this.under[y].height=this.over[y].height;
					if(y+1==this.items.length){
						this.opacity = 0;
						this.idx++;
						if(this.idx==this.list.length){
							this.idx = 0;
						}
						setTimeout(this.startFading.bind(this), this.time);
					}
				}
			}
		}
	}
}

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];
	}
}
