/*
CAB Javascript - Homepage
*/

//*** INITIALIZE OBJECTS
$(document).ready(function(){ 

	Testimonials.init(); 
	SponsorsTop.init();
	Sponsors.init();
	
});

//*** TESTIMONIALS
var Testimonials = {
	
	// you can touch these
	fadeSpeed: 500,
	intervalSpeed: 5000,
	elements: "div.info blockquote",
	
	// you shouldn't touch these
	current: 0,
	total: 0,
	delayFade: 500,
	delayFadeIn: null,
	rotateInterval: null,
	
	// start it
	init: function(){
		
		$(this.elements).hide();
		this.total = $(this.elements).length; // amount of testimonials
		var random = Math.floor(Math.random() * ((this.total-1) - 0 + 1)); // randomize it
		
		this.current = random;
		$(this.elements+":eq("+random+")").show();
		this.rotateInterval = window.setInterval("Testimonials.rotate();",this.intervalSpeed);
	
	},
	
	// rotate through the testimonials
	rotate: function(){
		
		this.checkNext(); // we get the appropriate testimonial if 
		
		/*
		$(this.elements).fadeOut(this.fadeSpeed,function(){
			clearTimeout(Testimonials.delayFadeIn);
			Testimonials.delayFadeIn = setTimeout("Testimonials.delayit();",Testimonials.delayFade);
		});
		*/
		$(this.elements).hide();
		$(this.elements+":eq("+this.current+")").fadeIn(this.fadeSpeed);

	},
	
	// delay the fade in so the other testimonial has time to fade out.. found inside the fadeout callback
	/*delayit: function(){

		$(this.elements+":eq("+this.current+")").fadeIn(this.fadeSpeed);
		
	},*/
	
	// call this function before you use the "this.current" variable.. it will insure you are on the correct testimonial
	checkNext: function(){
	
		if((this.total - 1) > this.current){
			this.current++;
		} else {
			this.current = 0;
		}
		
	},
	
	checkPrevious: function(){
		
		if(this.current > 0){
			this.current--;
		} 
		else {
			this.current = (this.total -1);
		}
		
	},
	
	// if they want to manually scroll through the testimonials
	next: function(){
	
		clearInterval(this.rotateInterval);
		this.checkNext();
		
		$(this.elements).hide();
		$(this.elements+":eq("+this.current+")").show();
		
	},
	
	// if they want to manually scroll through the testimonials
	previous: function(){
		
		clearInterval(this.rotateInterval);
		this.checkPrevious();
		
		$(this.elements).hide();
		$(this.elements+":eq("+this.current+")").show();
		
	}


}

// *** SPONSORS TOP (PLATINUM PLUS / PLATINUM)
var SponsorsTop = {

	rotInt: null,
	rotSpeed: 6000,
	target: "ul#sponsor-bar li.sponsor-top", // this gets altered through the script
	default_target: "ul#sponsor-bar li.sponsor-top", // do not touch this, it will hurt you. ( should be same value as above)
	platinum_plus: "ul#sponsor-bar li.platinum-plus",
	platinum: "ul#sponsor-bar li.platinum",
	title: "ul#sponsor-bar li h4",
	lang_platinum_plus_text: "#platinum-plus-text",
	lang_platinum_text: "#platinum-text",
	shownImages: 4,
	startAt: 4,
	currentlyAt: 4,
	reset: 0, // when we need to reset the interval
	rowSections: 0,
	intervalCount: 0,
	amountOfItemsPlatinumPlus: 0,
	amountOfItemsPlatinum: 0,
	intervalsBeforeResetPlatinumPlus: 0,
	intervalsBeforeResetPlatinum: 0,
	intervalsTotal: 0,
	
	init: function(){
		
		this.rotInt = setInterval("SponsorsTop.startRot();",this.rotSpeed);
		
		// platinum plus
		this.amountOfItemsPlatinumPlus = $(this.platinum_plus).length;
		this.intervalsBeforeResetPlatinumPlus = (Math.ceil(this.amountOfItemsPlatinumPlus/this.shownImages) * this.shownImages) / this.shownImages; // 2 cycles before we reset
		
		// platinum
		this.amountOfItemsPlatinum = $(this.platinum).length;
		this.intervalsBeforeResetPlatinum = (Math.ceil(this.amountOfItemsPlatinum/this.shownImages) * this.shownImages) / this.shownImages; // 2 cycles before we reset
		
		this.intervalsTotal = this.intervalsBeforeResetPlatinum + this.intervalsBeforeResetPlatinumPlus;
		
	},
	
	startRot: function(){
		
		this.intervalCount++;
		
		this.target = (this.intervalCount < this.intervalsBeforeResetPlatinumPlus) ? this.platinum_plus : this.default_target;
		
		// hide all the lis
		$(this.target).hide();
		
		// do we need to switch to platinum now?
		if(this.intervalCount == this.intervalsBeforeResetPlatinumPlus){
			this.currentlyAt = this.amountOfItemsPlatinumPlus;
			this.startAt = this.amountOfItemsPlatinumPlus;
			this.target = this.default_target;
			$(this.title).text($(this.lang_platinum_text).text());
		}
		
		// do we need to start over?
		if(this.intervalCount == this.intervalsTotal){
			this.currentlyAt = 0;
			this.startAt = 0;
			this.intervalCount = 0;
			this.target = this.platinum_plus;
			$(this.title).text($(this.lang_platinum_plus_text).text());
		}
		
		
		while(this.currentlyAt < (this.startAt + this.shownImages)){ // 0 < (6)
			//alert("current: " + this.currentlyAt + " | startAt: " + this.startAt + " | shownImages: " + this.shownImages + " | total: " + (this.startAt + this.shownImages)); 
			$(this.target + ":eq("+this.currentlyAt+")").fadeIn("fast");
			this.currentlyAt++;
		}
		
		this.startAt = this.currentlyAt;
		
	}
}



//*** SPONSORS 
var Sponsors4Col = {
	
	imagePath: Workspace.RootUrl+'/media/sponsors',
	rotationInt: null,
	rotationSpeed: 5000,
	currentlyAt: 0,
	cols: 4,
	amountMax: 0,
	emptyCellFiller: 18,

	init: function(){
		
		var tmp_arr_max = 0;
		
		for(var i=1;i<=this.cols;i++){
			var length = $('table#featured ul#col-'+i+' li').length;
			tmp_arr_max = tmp_arr_max + parseInt(length);
		}
		
		// get the one with the most amount of li's
		this.amountMax = tmp_arr_max;
		this.startRotation();
		this.rotationInt = setInterval("Sponsors4Col.startRotation();",this.rotationSpeed);
		
	},
	
	startRotation: function(){
		
		var obj = this;
		
		$('table#featured ul.list').each(function(){

			var col_img = $(this).parent().find('img');

			var image_href = $('table#featured ul.list li:eq('+obj.currentlyAt+') a').attr('title');
			var image_to_show = obj.imagePath+'/'+image_href;
			
			if(image_href){
				col_img.attr('src',image_to_show);
			}
			
			else { // random image if it can't find one.
				var random = Math.floor(Math.random() * ((obj.amountMax-1) - 0 + 1)); // randomize it
				var image_href = $('table#featured ul.list li:eq('+random+') a').attr('title');
				var image_to_show = obj.imagePath+'/'+image_href;
				col_img.attr('src',image_to_show); // random
			}
			
			if(obj.currentlyAt == obj.amountMax){
				//col_img.fadeOut();
				//clearInterval(this.rotationInt);
				obj.currentlyAt = 0;
				//this.rotationInt = setInterval("Sponsors4Col.startRotation();",this.rotationSpeed);
			} else {
				obj.currentlyAt++;
			}
			//$("#test").text(obj.currentlyAt);
			
		});
	
	}	

}

// new.. last one i hope
var Sponsors = {

	rotInt: null,
	rotSpeed: 8000,
	nav: "#sponsors-list ul.supplemental-nav li",
	types: ['gold','bronze','patron'],
	target: "#sponsors-list div.info ul li,#patron", // this gets altered through the script
	default_target: "#sponsors-list div.info ul li,#patron", //do not touch this, it will hurt you. ( should be same value as above)
	target_ul: "#sponsors-list div.info ul,#patron",
	gold: "ul#gold li",
	bronze: "ul#bronze li",
	patron: "#patron",
	top_nav_gold: "#top-nav-gold",
	top_nav_bronze: "#top-nav-bronze",
	top_nav_patron: "#top-nav-patron",
	title: "ul#sponsor-bar li h4",
	lang_platinum_plus_text: "#platinum-plus-text",
	lang_platinum_text: "#platinum-text",
	shownImages: 4,
	startAt: 4,
	currentlyAt: 4,
	reset: 0, // when we need to reset the interval
	rowSections: 0,
	intervalCount: 0,
	amountOfItemsGold: 0,
	amountOfItemsBronze: 0,
	intervalsBeforeResetGold: 0,
	intervalsBeforeResetBronze: 0,
	intervalsTotal: 0,
	hasLooped: false,
	
	init: function(){
		
		this.rotInt = setInterval("Sponsors.startRot();",this.rotSpeed);
		
		// gold
		this.amountOfItemsGold = $(this.gold).length;
		this.intervalsBeforeResetGold = (Math.ceil(this.amountOfItemsGold/this.shownImages) * this.shownImages) / this.shownImages; // 2 cycles before we reset
		
		// bronze
		this.amountOfItemsBronze = $(this.bronze).length;
		this.intervalsBeforeResetBronze = (Math.ceil(this.amountOfItemsBronze/this.shownImages) * this.shownImages) / this.shownImages; // 2 cycles before we reset
		
		this.intervalsTotal = this.intervalsBeforeResetGold + this.intervalsBeforeResetBronze + 1;
		
	},
	
	addSelected: function(type){
		
		$(this.nav).removeClass('selected');
		var e = eval("this.top_nav_" + type);
		$("#top-nav-"+type).addClass('selected');
		
	},

	
	startRot: function(){
		
		// hide all the lis
		$(this.target).hide();
		$(this.target_ul).hide();
		
		var is_patron = false;
		
		if(this.hasLooped){
			
			$(this.patron).hide();	
			
			$(this.gold).parent().show();
			this.addSelected('gold');
					
			for(var i = 0;i<this.shownImages;i++){
				$(this.gold + ":eq("+i+")").show();
			}
			
			this.target = this.gold;
			
			this.hasLooped = false;
		}
		
		if(this.intervalCount == 0){ 
			this.target = this.gold;
			$(this.nav).removeClass('selected');
			this.addSelected('gold');
		}
		
		if(this.intervalCount == (this.intervalsBeforeResetGold - 1)){
			this.target = this.bronze;
			this.currentlyAt = 0;
			this.startAt = 0;
			this.addSelected('bronze');
		}
		
		if(this.intervalCount == (this.intervalsBeforeResetBronze + this.intervalsBeforeResetGold - 1)){
			this.target = this.patron;
			this.currentlyAt = 0;
			this.startAt = 0;
			is_patron = true;
			this.addSelected('patron');
		}	
		
		//$("#test").html("INTERVAL COUNT: " + this.intervalCount + "<br/>IBR GOLD: " + this.intervalsBeforeResetGold + "<br/>IBR BRONZE: " + this.intervalsBeforeResetBronze + "<br/> INT TOTAL: " + this.intervalsTotal  + "<br/>TARGET: " + this.target);
		
		this.intervalCount++; 
		
		if(!is_patron){
		
			while(this.currentlyAt < (this.startAt + this.shownImages)){ // 0 < (6)
				//alert("current: " + this.currentlyAt + " | startAt: " + this.startAt + " | shownImages: " + this.shownImages + " | total: " + (this.startAt + this.shownImages)); 
				$(this.target).parent().show();
				$(this.target + ":eq("+this.currentlyAt+")").show();
				this.currentlyAt++;
			}
			
			this.startAt = this.currentlyAt;

		}
		else {
			$(this.patron).show();
			$(this.patron + " > *").show();
			this.rotInt = clearInterval(this.rotInt);
			this.hasLooped = true;
			this.currentlyAt = 0;
			this.startAt = 0;
			this.intervalCount = -1;
			this.rotInt = setInterval("Sponsors.startRot();",this.rotSpeed);
		}
		
	}
}

//*** SPONSORS
var Sponsors_OLD_LAST_BEFORE_REDO = {
	
	container: "#sponsors-list",
	elements: "#sponsors-list .list",
	all_sponsor_uls: "#sponsors-list .info ul",
	sponsorImage: "#sponsors-list #featured img",
	startElement: null,
	imagePath: Workspace.RootUrl+'/media/sponsors',
	imageToShow: null,
	rotationInt: null,
	rotationSpeed: 4000,
	liTotal: 0,
	currentlyAt: 0,
	holdSlidesArray: new Array(),
	
	init: function(){
		
		// second UL (the first one isn't a sponsors list)
		this.startElement = $(this.container+" ul.list:eq(0)").attr('id');
		this.rotationInt = setInterval("Sponsors.startRotation();",this.rotationSpeed);
		this.liTotal = $("#"+this.startElement+" li").length;
		
		this.showStartImage(null);
		
		var total = 0;
		var slides = new Array();
		
		$("#"+this.startElement+" li").each(function(){
			slides.push(total);
			total++;
		});
		
		this.holdSlidesArray = slides;
		//alert(this.holdSlidesArray);
		
		// set the first anchor to an italic style
		$("#"+this.startElement+" li:eq(0) a").css('font-style','italic')
		
	},
	
	showStartImage: function(section){ // section can be "gold", "bronze", "patron"
	
		if(section == null){
			// show the first image
			var imageTitle = $("#"+this.startElement+" li:eq(0) a:eq(0)").attr('title');
		}
		else {
			// set the first one italic and show the image
			$(this.container+" ul#"+section+" li:eq(0) a:eq(0)").css('font-style','italic');
			var imageTitle = $(this.container+" ul#"+section+" li:eq(0) a:eq(0)").attr('title');
		}
		
		this.imageToShow = this.imagePath+'/'+imageTitle;
		this.showImage();
		
	},
	
	startRotation: function(){
		
		this.calcNext();
		// default font
		$("#"+this.startElement+" li a").css('font-style','normal');
		var a_element = $("#"+this.startElement+" li:eq("+this.currentlyAt+") a");
		a_element.css('font-style','italic')
		this.showNext(a_element);
		
	},
	
	// show a specific category.. gold, bronze...
	show: function(id){
	
		// set all fonts normal
		$(this.all_sponsor_uls+" li a").css('font-style','normal');
		clearInterval(this.rotationInt);
		$(this.elements).hide();
		$(this.container+" #"+id).show();
		this.showStartImage(id);
	
	},
	
	showNext: function(e){
		
		var image = this.imagePath+'/'+e.attr('title');
		this.imageToShow = image;
		this.showImage();
		
	},
	
	// show a specific sponsor
	showsponsor: function(e){
		
		clearInterval(this.rotationInt);
		// change the font
		$(this.all_sponsor_uls+" li a").css('font-style','normal');
		$(e).css('font-style','italic');
		// show the image
		var image = this.imagePath+'/'+$(e).attr('title');
		this.imageToShow = image;
		this.showImage();
		
	},
	
	showImage: function(){
		
		$(this.sponsorImage).attr('src',this.imageToShow);
		
	},
	
	// call this function before you use the "this.currentlyAt" variable.. it will insure you are on the correct sponsor
	calcNext: function(){
	
		if((this.liTotal - 1) > this.currentlyAt){
			this.currentlyAt++;
		} else {
			this.currentlyAt = 0;
		}
		
	}

}