/*
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)}

$(document).ready(function() {
   $.imgpreload("http://www.impactgiveback.org/servlet/servlet.FileDownload?file=015A0000001HO0wIAG",function(){
    // Determine the current height and set it to allow all columns to be same height
    var h = $("body").height();
    $("body").height(h);
  });
});
var hs = {
	hId: null,
	hNavId: null,
	hNavItemId: null,
	hList: new Array(),
	hImgList: new Array(),
	hCurrIndex: 0,
	hDelay: 14000,
	heroTimer: null, // hero spot timer
	heroTimer_is_on: false,
	debug: false,
	initHero: function() {
		$.ajax({
			type: "GET",
			url: "http://www.impactgiveback.org/slidesJSON",
			dataType: "xml",
			success: function(xml) {
				$(xml).find('hero').each(function(){
					hs.hId = $(this).find('hero_id').text();
					hs.hNavId = $(this).find('hero_nav_id').text();
					hs.hNavItemId = $(this).find('hero_nav_parts_id').text();

					$(this).find('heroes').each(function(){
						var path = $(this).find('path').text();
						var target = $(this).find('target').text();
						var title = $(this).find('title').text();
						var url = $(this).find('url').text();
						var myObj = new feedObj(path, target, title, url);
						hs.hList.push(myObj);
      					hs.hImgList.push(path);

					});
					hs.preload(hs.hImgList);
			        if(hs.debug) { console.log(hs.hList); console.log("testing hs.debug"); }
			     	hs.createNav();
				});
			}
		});
	},
	createNav: function() {
		for(i = 0; i < hs.hList.length; i++) {
		  var e = "<li id='" + hs.hNavItemId + i +"'><a href='javascript:hs.updateNav(" + i + ");'></a></li>";
		  $("#" + hs.hNavId).append(e);
		}
	hs.updateNav(0);
	},
	updateNav: function(ind) {
		hs.hCurrIndex = ind;
		for(i = 0; i < hs.hList.length; i++) { $("#" + hs.hNavItemId + i + " a").removeClass("active"); }
		$("#" + hs.hNavItemId + hs.hCurrIndex + " a").addClass("active");
		hs.rotate();
	},
	rotate: function() {
		var hero;
		$("#" + hs.hId).fadeOut("slow", function(){
		  	hero = hs.hList[hs.hCurrIndex];
		  	$("#hs_info a").fadeOut(function(){
		    	$("#hs_info a").attr("href", hs.hList[hs.hCurrIndex].url).fadeIn();
		  	});
		  	$(this).css("backgroundImage", "url('" + hs.hList[hs.hCurrIndex].path + "')");
		  	$("#" + hs.hId).fadeIn("slow");
		});
		hs.setTimer();
	},
	setTimer: function() {
		if (hs.heroTimer_is_on == true) clearInterval(hs.heroTimer);
		hs.heroTimer = setInterval('hs.nextHero()', hs.hDelay);
		hs.heroTimer_is_on = true;
	},
	nextHero: function() {
		if(++hs.hCurrIndex >= hs.hList.length) { hs.hCurrIndex = 0; }
		hs.updateNav(hs.hCurrIndex);
	},
	preload: function(arr) {
		$.imgpreload(arr, {
		  all: function() { if (hs.debug) console.log('preloaded all ' + this.length + ' images'); }
		});
	}
}

function feedObj(path, target, title, url){
	this.path = path;
	this.target = target;
	this.title = title;
	this.url = url;
}

function imageObj(path){
	this.path = path;
}

