// For promo rollover text
var jq = jQuery.noConflict();
jq(document).ready(function() {
	var sensitivity = 5;
	var interval = 25;
	var timeout = 0;
	var speed = 200;
							
	// Rotating/Dedicated Promos
	var hoverConfig = {    
		sensitivity: sensitivity,
		interval: interval,
		over: function() {
			move = jq(this).children(".promoRollover").height() + 1;
			jq(this).children(".promoRollover").animate({top : "+="+move+"px"}, speed);
		},
		timeout: timeout,
		out: function() {
			move = jq(this).children(".promoRollover").height() + 1;
			jq(this).children(".promoRollover").animate({top : "-="+move+"px"}, speed);
		}
	};
	jq(".promoImage").hoverIntent(hoverConfig);
});


// BIG BOX
var BigBox = function() {
	this.features = new Array();
	this.current = 0;
	this.features = [];
	this.images = [];
}

BigBox.prototype = {
	imgfolder:'/images/homepage/bigbox/',
	container:'bigBox',
	interval: 5000,
	features: [],
	current:0,
	duration:'0.2',
	eventHandler:null,
	pause:false,
	
	start: function() {
		var afterLoad = function() {
			if (this.features.length == 1) this.pause = true;
			this.pick(0);
		}.bind(this);
		
		//lets preload the first feature before we start up the rotation:
		var first = this.features[0];
		if(first) {
			this.images[0] = new Image();
			this.images[0].onload = afterLoad;
			this.images[0].src = this.imgfolder + first.image;
		}
	},
	
	startTimeout: function() {
		this.interval = setInterval(this.next.bind(this), 5000);
	},
	
	cancelTimeout: function() {
		clearInterval(this.interval);
	},
	
	next: function() {
		this.current++;
		if(this.current >= this.features.length) {
			this.current = 0;
		}
		this.choose(this.current);
	},
	
	previous: function() {
		this.current--;
		if(this.current < 0) {
			this.current = this.features.length - 1;
		}
		this.choose(this.current);
	},
	
	pick: function(id) {
		id = parseInt(id);
		
		if(id >= this.features.length) {
			id = this.features.length - 1;
		} else if(id < 0) {
			id = 0;
		}
		this.current = id;
		this.choose(this.current);
	},
	
	choose: function(id) {
		this.cancelTimeout();
		this.swapSelected(id);
		
		//get the next feature:
		var feature = this.features[id];
		
		//load up the image after this one:
		this.loadNextImage(id);
	
		var complete = function() {
			// set the image:
			var theContent = '';
			var img = $('bigBoxImage');
			img.innerHTML = '';
			
			// create link attributes
			link_attr = '';
			if (feature.newwindow != 0) {
				link_attr += "target='_blank'";
			}
			if (feature.click_name != "") {
				link_attr += 'onclick="var s=s_gi(\'astralfamilyca\'); s.tl(this,\'o\',\'Homepage: '+feature.click_name+'\');"';
			}
			
			// create the content
			theContent = '<img src="'+this.imgfolder+feature.image+'"';
			if (feature.alt != "") {
				 theContent += 'alt="'+feature.alt+'"';
			}
			theContent += ' />';
			
			
			theContent = '<a href="'+feature.url+'" '+link_attr+'>'+theContent+'</a>';
			img.innerHTML = theContent;
			
			this.fadeCurtain(); // fade-in the new feature
		}.bind(this);
	
		//first fade out the old feature:
		this.showCurtain(complete);
		if (!this.pause) this.startTimeout();
	},
	
	showCurtain: function(onComplete) {
		var div = $('bigBoxCurtain');
		new Effect.Appear(div, {afterFinish:onComplete, duration:this.duration});
	},
	
	fadeCurtain: function() {
		var div = $('bigBoxCurtain');
		new Effect.Fade(div, {duration:this.duration});
	},
	
	swapSelected: function(toSelect) {
		var BTNs = $$('#bigBoxButtons li a img');
		var len = BTNs.length;
		for(var i = 0; i < len; i++) {
			src = BTNs[i].readAttribute('src');
			if (src.match('bb_btn_on') && i != toSelect) {
				BTNs[i].writeAttribute('src', '/images/homepage/bb_btn.png');
			} else if (i == toSelect)  {
				BTNs[i].writeAttribute('src', '/images/homepage/bb_btn_on.png');
			}
		}
	},
	
	loadNextImage: function(id) {
		var next = id + 1;
		//we don't need to worry about wrapping around back
		//to 0 as those images should already have been loaded
		//(unless they clear their cache... in that case forget them!):
		if(next < this.features.length && !this.images[next]) {
			this.images[next] = new Image();
			this.images[next].src = this.features[next].file;
		}
	}
};