// on mouseOut, delay restart of rotation
theBanner.restartDelay = 500; 

// array of all banners
allTheBanners = {};
var mainBanner = null;
// reset text and link arrays to 0 based.
// they 1 based in the files to assist non-technical content editors
for(i = 0; i < txtArray.length-1; i++) {
	txtArray[i] = txtArray[i+1];
	linkArray[i] = linkArray[i+1];
}

// init initial banner elements
var link = linkArray[0];
var txt = txtArray[0];
var banner = document.getElementById("banner");
// for happening now images
var imgArray = new Array(txtArray.length-1);

// pre-load all banner images
for(i = 0; i < imgArray.length; i++) {
    imgArray[i] = "img/banner/store_banner_" + (i+1) + ".jpg";
}

// define a banner
function theBanner(id, linkId, delay, bMouse) {
    this.id = id; 
    this.linkId = linkId;
    this.delay = delay; 
    this.items = []; 
    this.ctr = 0; 
    this.timer = null;
    allTheBanners[this.id] = this; 
    this.animString = "allTheBanners." + this.id;
    if (bMouse) 
    	theBanner.setMouseEvents(this.id);
};

function anItem(sHtml, sLink, sImg) {
	this.html = sHtml;
	this.link = sLink;
	this.image = new Image(255, 150);
	this.image.src = sImg;
}

// banner.addItem() function to add a new item to be rotated
theBanner.prototype.addItem = function(sHtml, sLink, sImg) {
 	this.items[this.items.length] = new anItem(sHtml, sLink, sImg);
};

// banner.rotate() to rotate from one item to the next
theBanner.prototype.rotate = function() {
    //clearTimeout(this.timer); 
    //this.timer = null;
    // set the text
    var el = document.getElementById(this.id);
    el.innerHTML = this.items[this.ctr].html;
    // set the link
    var linkEl = document.getElementById(this.linkId);
    linkEl.innerHTML = this.items[this.ctr].link + "&nbsp;<a style='color:#CCCC9A; font-weight:bold; font-size:1.1em'>&gt;</a>";
    // set the image
    document.banner.src = this.items[this.ctr].image.src; 
    // rotate the counter
    if (this.ctr < this.items.length-1) this.ctr++;
    else this.ctr = 0;
    //this.timer = setTimeout(this.animString + ".rotate()", this.delay);
};

// MOUSE FUNCTIONS
// banner.setMouseEvents() to control behavior onMouseOver and onMouseOut
theBanner.setMouseEvents = function(id) {
    var el = document.getElementById(id);
    if (el) {
        el.onmouseover = theBanner.pause;
        el.onmouseout = theBanner.resume;
    }
};

// pause rotation
theBanner.pause = function() { 
    var curObj = allTheBanners[this.id];
    if (curObj) { clearTimeout(curObj.timer); curObj.timer = null; }
};
 
theBanner.resume = function(e) {
    e = e? e: window.event;
    var toEl = e.relatedTarget? e.relatedTarget: e.toElement;
    if ( this != toEl && !dw_contained(toEl, this) ) {
        var curObj = allTheBanners[this.id];
        if (curObj)
            curObj.timer = setTimeout(curObj.animString + ".rotate()", theBanner.restartDelay);
    }
};

function printArray(arr) {
	for(i = 0; i < arr.length; i++) {
		alert(i + ": " + arr[i]);
	}
}
function printElement(arr,i) {
	alert(i + ": " + arr[i]);
}
// returns true of oNode is contained by oCont (container)
function contained(oNode, oCont) {
    if (!oNode) return; // in case alt-tab away while hovering (prevent error)
    while ( oNode = oNode.parentNode ) if ( oNode == oCont ) return true;
    return false;
};

var imageHandler = { 
    path:"", imgs:[], preload:function() { for(var i=0;arguments[i];i++) {
    var img=new Image(255, 150); img.src=this.path+arguments[i]; this.imgs[this.imgs.length]=img;}}
};

/***********************************************
* Ultimate Fade-In Slideshow (v1.5): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
var fadebgcolor="white"
 
////NO need to edit beyond here/////////////
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(banner, theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder){
	this.banner = banner
	this.pausecheck=pause
	this.mouseovercheck=0
	this.delay=delay
	this.degree=10 //initial opacity degree (10%)
	this.curimageindex=0
	this.nextimageindex=1
	fadearray[fadearray.length]=this
	this.slideshowid=fadearray.length-1
	this.canvasbase="canvas"+this.slideshowid
	this.curcanvas=this.canvasbase+"_0"
	if (typeof displayorder!="undefined")
		theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
	this.theimages=theimages
	this.imageborder=parseInt(borderwidth)
	this.postimages=new Array() //preload images

	for (p=0;p<theimages.length-1;p++){
		this.postimages[p]=new Image()
		this.postimages[p].src=theimages[p+1][0]
	}
 
	var fadewidth=fadewidth+this.imageborder*2
	var fadeheight=fadeheight+this.imageborder*2
	 
	if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
		document.write('<div id="master'+this.slideshowid+'" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:'+fadebgcolor+'"></div></div>')
	else
		document.write('<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>')
	 
	if (iebrowser&&dom||dom) //if IE5+ or modern browsers such as Firefox
		this.startit()
	else{
			this.curimageindex++
			setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
		}
}

function fadepic(obj){
	if (obj.degree<100){
		obj.degree+=10
	if (obj.tempobj.filters&&obj.tempobj.filters[0]){
		if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
			obj.tempobj.filters[0].opacity=obj.degree
		else //else if IE5.5-
			obj.tempobj.style.filter="alpha(opacity="+obj.degree+")"
	}
	else if (obj.tempobj.style.MozOpacity)
		obj.tempobj.style.MozOpacity=obj.degree/101
	else if (obj.tempobj.style.KhtmlOpacity)
		obj.tempobj.style.KhtmlOpacity=obj.degree/100
	}
	else{
		clearInterval(fadeclear[obj.slideshowid])
		obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
		obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
		obj.populateslide(obj.tempobj, obj.nextimageindex)
		obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
		setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
	}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
	var slideHTML=""
	if (this.theimages[picindex][1]!="") //if associated link exists for image
		slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
		slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
	if (this.theimages[picindex][1]!="") //if associated link exists for image
		slideHTML+='</a>'
		picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(banner){
	if (this.pausecheck==1) //if pause onMouseover enabled, cache object
		var cacheobj=this
	if (this.mouseovercheck==1)
		setTimeout(function(){cacheobj.rotateimage()}, 100)
	else if (iebrowser&&dom||dom){
		this.resetit()
		var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
		crossobj.style.zIndex++
		fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
		this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
		mainBanner.rotate()
	}
	else{
		var ns4imgobj=document.images['defaultslide'+this.slideshowid]
		ns4imgobj.src=this.postimages[this.curimageindex].src
		mainBanner.rotate()
	}
	this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
}
 
fadeshow.prototype.resetit=function(){
	this.degree=10
	var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
	if (crossobj.filters&&crossobj.filters[0]){
		if (typeof crossobj.filters[0].opacity=="number") //if IE6+
			crossobj.filters(0).opacity=this.degree
		else //else if IE5.5-
			crossobj.style.filter="alpha(opacity="+this.degree+")"
	}
	else if (crossobj.style.MozOpacity)
		crossobj.style.MozOpacity=this.degree/101
	else if (crossobj.style.KhtmlOpacity)
		crossobj.style.KhtmlOpacity=this.degree/100
}

fadeshow.prototype.startit=function(){
	var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
	this.populateslide(crossobj, this.curimageindex)
	if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
		var cacheobj=this
		var crossobjcontainer=iebrowser? iebrowser["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
		crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
		crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
	}
	this.rotateimage()
}

// on load function
function loadBanner() {
	initBanner();
}

// initialize the banner
function initBanner() { 
    // arguments: id, delay (amount of time in milliseconds you linger on each item)
    // set up pause onmouseover and resume onmouseout? (boolean) 
    mainBanner = new theBanner('bannerDiv', 'linkDiv', 10000, false);
    // add as many items as you like
    for(i = 0; i < imgArray.length; i++) {
    	mainBanner.addItem(txtArray[i],linkArray[i],imgArray[i]);
    }
	new fadeshow(mainBanner,fadeimages, 255, 150, 0, 8000, 0);
}