/*
Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com
Licensed under the MIT license
http://en.wikipedia.org/wiki/MIT_License
*/
function imgpreload(a,b){if(b instanceof Function){b={all:b}}if(typeof a=="string"){a=[a]}var c=[];var t=a.length;var i=0;for(i;i<t;i++){var d=new Image();d.onload=function(){c.push(this);if(b.each instanceof Function){b.each.call(this)}if(c.length>=t&&b.all instanceof Function){b.all.call(c)}};d.src=a[i]}}if(typeof jQuery!="undefined"){(function($){$.imgpreload=imgpreload;$.fn.imgpreload=function(b){b=$.extend({},$.fn.imgpreload.defaults,(b instanceof Function)?{all:b}:b);this.each(function(){var a=this;imgpreload($(this).attr('src'),function(){if(b.each instanceof Function){b.each.call(a)}})});var c=[];this.each(function(){c.push($(this).attr('src'))});var d=this;imgpreload(c,function(){if(b.all instanceof Function){b.all.call(d)}});return this};$.fn.imgpreload.defaults={each:null,all:null}})(jQuery)}
/* 
 Hero Spot Cycle
 Author: Darin Livdahl
 Created: November, 2010
*/
var queHero = 0;
var fadeIndex = 0;
var heroTimer; // hero spot timer
var heroTimer_is_on = false;
var lastIndex; // last available hero spot index
var countTimer; // count timer var for checking time delay
var countTimer_is_on = false;
var sec;
var preloadArray = new Array();
var intervalArray = new Array();
var intervalTime = 5000;
var $heroDiv;
var debug = false;

var heroSpot = {
	init: function() {
		$.getJSON('/index.php/shared/generate_home_herospot', function(data) {
			$('#hero-spot').empty(); // remove noscript content
			$.each(data.heroes, function(index) {
				$heroDiv = $('<div></div>');
				var browserName=navigator.appName;
				if (Modernizr.multiplebgs) {
					$($heroDiv).css('backgroundImage','url(/includes/css/cssimages/heroshadow.png), url(' + this.path + ')');
				} else {
					$($heroDiv).css('backgroundImage','url(' + this.path + ')');
				};
				$($heroDiv).append('<a href="' + this.url + '" target="' + this.target + '"><p style="margin-left:' + this.testimonial_left_margin + 'px; margin-top:' + this.testimonial_top_margin + 'px; width:' + this.hero_width +'px; color:#fff; line-height: 20px; position: absolute; z-index: 9999;"> ' + String(this.testimonial) + '</p></a>');
				$('#hero-spot').append($heroDiv);
				preloadArray.push(this.path);
				intervalArray.push(this.interval);
			});
			if (data.heroes.length > 1) heroSpot.preload(preloadArray); // preload hero spots and start cycle if more than one
			heroSpot.adapt();
		});
	},
	preload: function(arr) {
		$.imgpreload(arr, {
			all: function() {
				if (debug) console.log('preloaded all ' + this.length + ' images');
				heroSpot.setup(this.length);
			}
		});	
	},
	setup: function(totalHeroes) {
		lastIndex = (totalHeroes-1);
		heroSpot.buildNav(totalHeroes);
		// intervalTime = intervalArray[1] * 1000;
		heroSpot.setTimer(1);  // show second hero after first interval
		heroSpot.updateNav(0);
		if (debug) heroSpot.countdown();
	},
	buildNav:function(links) {
		$('#slideshow').append('<div id="hero-nav" style="display: none;"><div id="hero-links-' + links + '"></div></div>');
		$('#hero-nav').fadeIn('slow');
		var i;
		for (i = 1; i <= links; i++) {
			$('#hero-nav div').append('<a href="#">Hero ' + i + '</a>');
		}
		// iterate through number of hero spots to build navigation interface
		$('#hero-nav a').each(function(index) {
			$(this).click(function() {
				heroSpot.cycle(index); // immediately switch to selected hero				
				// set up timer for following hero
				if (index < lastIndex) {
					heroSpot.setTimer(index+1);
				} else if (index == lastIndex) {
					heroSpot.setTimer(0);
				}
				if (debug) console.log('link #' + (index+1) + ' and total links are ' + links);
				return false;
			});
		});
	},
	setTimer: function(hero) {
		queHero = hero;
		if (heroTimer_is_on == true) clearInterval(heroTimer);
		heroTimer = setInterval('heroSpot.cycle(queHero)', intervalTime);
		heroTimer_is_on = true;
	},
	countdown: function() {
		if (countTimer_is_on == true) clearInterval(countTimer);
		countTimer = setInterval('heroSpot.countInterval()', 1000); // count down to check time
		countTimer_is_on = true;
		sec = 1;
	},
	countInterval: function() {
		console.log(sec);
		sec++;
	},
	cycle: function(heroIndex) {		
		heroSpot.updateNav(heroIndex);		
		if (debug) console.log('switching to hero ' + (heroIndex+1));		
		if (fadeIndex != heroIndex) { // do not change if active hero is selected
			$('#hero-spot div').eq(fadeIndex).fadeOut('slow');
			$('#hero-spot div').eq(heroIndex).fadeIn('slow');			
			fadeIndex = heroIndex; // current index will be targeted to fade out		
			if (heroIndex < lastIndex) {
				queHero++; // que up next hero
				// intervalTime = intervalArray[queHero] * 1000;
			} else if (heroIndex == lastIndex) {
				queHero = 0; // start over at the beginning
				// intervalTime = intervalArray[0] * 1000;
			}
			if (debug) console.log('interval time for next hero is: ' + intervalTime);
		}		
		if (debug) heroSpot.countdown(); // reset countdown
	},
	updateNav: function(navIndex) {
		$('#hero-nav div a').eq(navIndex).siblings('a').removeClass('active');
		$('#hero-nav div a').eq(navIndex).addClass('active');
	},
	adapt: function() {
		var adaptHero = function() {
			if ($(window).width() < 1024) {
				$('#hero-spot div a').width(987);
			} else if ($(window).width() > 1300) {
				$('#hero-spot div a').width(1280);
			}
		};
		adaptHero();
		$(window).resize(adaptHero);
	}
}
// Move this to common.js
var hoverNav = {
	init: function() {
		$('#equip-nav ul li').mouseenter(function() {
			$(this).children('.hovernav').fadeIn(200);
		});
		$('#equip-nav ul li').mouseleave(function() {
			$(this).children('.hovernav').fadeOut(400);
		});
		$('#equip-nav ul li a.equip-btn').click(function() {
			$(this).parent('li').siblings('li').children('.hovernav').removeClass('activeHoverNav');
			$(this).parent('li').children('.hovernav').addClass('activeHoverNav');
			return false;
		});
	}
}
$(document).ready(function() {
	heroSpot.init();
	hoverNav.init();
});
