/*

imgpreload Copyright (c) 2009 Dimas Begunoff, http://www.farinspace.com

Licensed under the MIT license
http://en.wikipedia.org/wiki/MIT_License

*/

function imgpreload(imgs,settings){if(settings instanceof Function){settings={all:settings}}if(typeof imgs=="string"){imgs=[imgs]}var loaded=[];var t=imgs.length;var i=0;for(i;i<t;i++){var img=new Image();$(img).bind("load",null,function(){loaded.push(this);if(settings.each instanceof Function){settings.each.call(this)}if(loaded.length>=t&&settings.all instanceof Function){settings.all.call(loaded)}});img.src=imgs[i]}}if(typeof jQuery!="undefined"){(function($){$.imgpreload=imgpreload;$.fn.imgpreload=function(settings){settings=$.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings);this.each(function(){var elem=this;imgpreload($(this).attr('src'),function(){if(settings.each instanceof Function){settings.each.call(elem)}})});var urls=[];this.each(function(){urls.push($(this).attr('src'))});var selection=this;imgpreload(urls,function(){if(settings.all instanceof Function){settings.all.call(selection)}});return this};$.fn.imgpreload.defaults={each:null,all:null}})(jQuery)}

var Browser = {
	Version: function() {
		var version = 999; // we assume a sane browser
		if (navigator.appVersion.indexOf("MSIE") != -1)
			version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
	}
}

/*** Hero Spot Cycle ***/

var hs = {
	queHero: 0,
	fadeIndex: 0,
	heroTimer: null, // hero spot timer
	heroTimer_is_on: false,
	lastIndex: null, // last available hero spot index
	preloadArray: new Array(),
	intervalTime: 6000,
	$heroDiv: null,
	json: null,
	init: function(data) {
		if (data != null) {
			hs.json = data;
			$('#hero-spot').empty(); // remove noscript content
			$.each(hs.json.heroes, function(index) {
				hs.$heroDiv = $('<div></div>');
				if (Modernizr.multiplebgs) {
					$(hs.$heroDiv).css('backgroundImage','url(http://netdrive.bobcat.com/home/heroshadow.png), url(' + this.path + ')');
				} else {
					$(hs.$heroDiv).css('backgroundImage','url(' + this.path + ')');
				};
				$(hs.$heroDiv).append('<a href="' + this.url + '" target="' + this.target + '"></a>');
				$('#hero-spot').append(hs.$heroDiv);
				hs.preloadArray.push(this.path);
			});
			if (hs.json.heroes.length > 1) hs.preload(hs.preloadArray); // preload hero spots and start cycle if more than one
			hs.adapt();
		}	
	},
	preload: function(arr) {
		$.imgpreload(arr,
		{
			all: function()
			{
				hs.setup(this.length);
			}
		});	
	},
	setup: function(totalHeroes) {
		hs.lastIndex = (totalHeroes-1);
		hs.buildNav(totalHeroes);
		$('#hero-spot div').eq(0).show();
		hs.setTimer(1);  // show second hero after first interval
		hs.updateNav(0);
	},
	buildNav:function(links)
	{
		$('#widescreen').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() {
				hs.cycle(index); // immediately switch to selected hero
				
				// set up timer for following hero
				if (index < hs.lastIndex) {
					hs.setTimer(index+1);
				} else if (index == hs.lastIndex) {
					hs.setTimer(0);
				}
				return false;
			});
		});
	},
	setTimer: function(hero) {
		hs.queHero = hero;
		if (hs.heroTimer_is_on == true) clearInterval(hs.heroTimer);
		hs.heroTimer = setInterval('hs.cycle(hs.queHero)', hs.intervalTime);
		hs.heroTimer_is_on = true;
	},
	cycle: function(heroIndex) {
		
		hs.updateNav(heroIndex);
		
		if (hs.fadeIndex != heroIndex) { // do not change if active hero is selected
			if (Browser.Version() < 7) { // check for IE6, fix fade flicker
				$('#hero-spot div').eq(heroIndex).show();
				$('#hero-spot div').eq(hs.fadeIndex).hide();
			} else {
				$('#hero-spot div').eq(heroIndex).fadeIn('slow');
				$('#hero-spot div').eq(hs.fadeIndex).fadeOut('slow');
			}
			
			hs.fadeIndex = heroIndex; // current index will be targeted to fade out
		
			if (heroIndex < hs.lastIndex) {
				hs.queHero++; // que up next hero
			} else if (heroIndex == hs.lastIndex) {
				hs.queHero = 0; // start over at the beginning
			}
		}
	},
	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);
	}
}

/***  Home Content Cycle ***/

var hc = {
	openContent1: new Array(),
	openContent2: new Array(),
	menu1Length: null,
	menu2Length: null,
	nextIndex: 0,
	timer: null,
	intervalTime: hs.intervalTime * 2,
	timer_is_on: false,

	init: function() {
		
		// check news items for content and add to array
		$('#hdetail-news .home-content').each(function() {
			var content = $(this).find('h4').text();
			if (content != '') {
				$(this).addClass('open-content');
				hc.openContent1.push($(this).index());
			}
		});
		
		// check news items for content and add to array
		$('#hdetail-subscribe .home-content').each(function() {
			var content = $(this).find('h4').text();
			if (content != '') {
				$(this).addClass('open-content');
				hc.openContent2.push($(this).index());
			}
		});
		
		hc.menu1Length = hc.openContent1.length;
		hc.menu2Length = hc.openContent2.length;
		
		// use content array to mark open menu items
		$.each(hc.openContent1, function(i,value) {
			$('#news-menu').children('li').eq(value).addClass('open-menu-item');
		});
		$.each(hc.openContent2, function(i,value) {
			$('#subscribe-menu').children('li').eq(value).addClass('open-menu-item');
		});
		
		hc.change(hc.openContent1[0],1); // show first open content item	
		hc.startTimer(); // start timer to start rotation of next open content items
		hc.nextIndex = 1; // set next open content array index position for timer rotation
		hc.bindMenuEvents(); // set up menu events
		
		hc.emailCheck(); // form validation on email address signup
	},
	bindMenuEvents: function() {
		$('#news-menu .open-menu-item a').bind('click', function() {
			var selectedMenu = $(this).parent('li').index();
			hc.select(selectedMenu,1);
			return false;
		});
		$('#subscribe-menu .open-menu-item a').bind('click', function() {
			var selectedMenu = $(this).parent('li').index();
			hc.select(selectedMenu,2);
			return false;
		});
	},
	startTimer: function() {
		hc.timer = window.setInterval("hc.cycle(hc.openContent1[hc.nextIndex],1)", hc.intervalTime);
		hc.timer_is_on = true;
	},
	cycle: function(i,menu) {
		hc.change(i,menu);
		if (hc.nextIndex < (hc.menu1Length-1)) {
			hc.nextIndex++;
		} else {
			hc.nextIndex = 0;
		}
	},
	select: function(i,menu) {
		if (hc.timer_is_on == true) {
			clearInterval(hc.timer);
			hc.timer_is_on = false;
		}
		hc.change(i,menu);
	},
	change: function(i,menu) {
		$('.home-content').hide();
		if (menu == 1) {
			$('#hdetail-news .home-content').eq(i).fadeIn();
		} else if (menu == 2) {
			$('#hdetail-subscribe .home-content').eq(i).fadeIn();
		}
		$('#hmenu ul li').removeClass('active');
		if (menu == 1) {
			$('#news-menu').children('li').eq(i).addClass('active');
		} else if (menu == 2) {
			$('#subscribe-menu').children('li').eq(i).addClass('active');
		}
	},
	emailCheck: function() {
		$('#email-form').submit(function() {
			if ($('#email-signup').val() == '') {
				alert('Please enter an email address.');
				return false;
			}
		});
	}
}

/***  Home Badge Rotation ***/

var hb = {
	json: null,
	init: function(data) {
		if ($('#badges dd').length) {
			if (data != null) {
				hb.json = data;
				var badges1Length = hb.json.badges1.length;
				hb.randomize(1,badges1Length);
				var badges2Length = hb.json.badges2.length;
				hb.randomize(2,badges2Length);
			}
		}
	},
	randomize: function(badge,groupTotal) {
		if (groupTotal > 1) {
			var badgeIndex = (Math.floor(Math.random() * groupTotal)); // generate random number
		} else {
			var badgeIndex = 0;
		}
		if (badge == 1) {
			$('#badges dd').eq(0).find('a').attr('style','visibility: visible; background-image: url(http://www.bobcat.com/publicadmin/getImage.do?id=' + hb.json.badges1[badgeIndex].fmsid + '&amp;width=295)');
			$('#badges dd').eq(0).find('a').attr('href',hb.json.badges1[badgeIndex].url);
			$('#badges dd').eq(0).find('a').attr('target',hb.json.badges1[badgeIndex].target);
			$('#badges dd').eq(0).find('a').text(hb.json.badges1[badgeIndex].text);
		} else if (badge == 2) {
			$('#badges dd').eq(1).find('a').attr('style','visibility: visible; background-image: url(http://www.bobcat.com/publicadmin/getImage.do?id=' + hb.json.badges2[badgeIndex].fmsid + '&amp;width=295)');
			$('#badges dd').eq(1).find('a').attr('href',hb.json.badges2[badgeIndex].url);
			$('#badges dd').eq(1).find('a').attr('target',hb.json.badges2[badgeIndex].target);
			$('#badges dd').eq(1).find('a').text(hb.json.badges2[badgeIndex].text);
		}
	}
}

$(document).ready(function() {
	$.getJSON('/publicadmin/elements/herospot.js', function(data) {
		hs.init(data);
		hb.init(data);
	});
	hc.init();
});
