var currentScrollingAd = 0;
var loadingInterval = 1;
function showLoadingGraphic() {
	jQuery("div#loadingGraphic").show();
	if(jQuery("div#loadingGraphic").is(":visible")) { setTimeout("animateLoadingGraphic()", 75); }
	else { jQuery("div#loadingGraphic").children("div").css("top", "0"); return; }
}
function animateLoadingGraphic() {
	jQuery("div#loadingGraphic").children("div").css('top', (loadingInterval * -40) + 'px');
	loadingInterval = (loadingInterval + 1) % 12;
	if(jQuery("div#loadingGraphic").is(":visible")) { showLoadingGraphic(); }
}
function shiftRight() {
	loadingSlide = 1;
	jQuery("div.middleAdColumn").animate({marginLeft: "-736px"}, 350, function() {
		jQuery("div.middleAdColumn").remove();
		jQuery("div.rightAdColumn").addClass("middleAdColumn");
		jQuery("div.rightAdColumn").removeClass("rightAdColumn");
		jQuery("div.middleAdColumn").after("\n<div class=\"homeAdBlock rightAdColumn\">\n<div class=\"homeAdBlockImage\">\n</div>\n<div class=\"homeAdBlockText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>");
		loadingSlide = 0;
	});
}
function shiftLeft() {
	loadingSlide = 1;
	jQuery("div.rightAdColumn").remove();
	jQuery("div.historyCaptionRight").remove();
	jQuery("div.leftAdColumn").animate({marginLeft: "736px"}, 350, function() {
		jQuery("div.leftAdColumn").css("margin-left", "0");
		jQuery("div.middleAdColumn").remove();
		jQuery("div.leftAdColumn").addClass("middleAdColumn");
		jQuery("div.leftAdColumn").removeClass("leftAdColumn");
		jQuery("div.middleAdColumn").before("<div class=\"homeAdBlock leftAdColumn\">\n<div class=\"homeAdBlockImage\">\n</div>\n<div class=\"homeAdBlockText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>\n");
		jQuery("div.middleAdColumn").after("\n<div class=\"homeAdBlock rightAdColumn\">\n<div class=\"homeAdBlockImage\">\n</div>\n<div class=\"homeAdBlockText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>");
		loadingSlide = 0;
	});
}
function generateTab(tab) {
	largeImage = scrollingAds[tab]['imagePath']+"c_"+scrollingAds[tab]['imageBlankName'];
	adImage = '<img src="/'+largeImage+'.jpg" alt="'+scrollingAds[tab]['hpaTitle']+'" />';
	adContent = "<h2>"+scrollingAds[tab]['hpaTitle']+"</h2>";
	adContent += ""+scrollingAds[tab]['hpaDescription']+"";
	if(scrollingAds[tab]['hpaLinkText'] != '' && scrollingAds[tab]['hpaLink'] != '' && scrollingAds[tab]['hpaLinkText'] != null && scrollingAds[tab]['hpaLink'] != null) {
		adContent += "<p><a href=\""+scrollingAds[tab]['hpaLink']+"\">"+scrollingAds[tab]['hpaLinkText']+"</a></p>";
	}
}
function clickArrowRight() {
	if (loadingSlide == 1) { return false; }
	showLoadingGraphic();
	currentScrollingAd = currentScrollingAd + 1;
	if(currentScrollingAd == scrollingAds.length) { currentScrollingAd = 0; }
	generateTab(currentScrollingAd);
	jQuery("div.rightAdColumn").children("div.homeAdBlockImage").html(adImage);
	jQuery("div.rightAdColumn").children("div.homeAdBlockText").html(adContent);		
	var urls = [ largeImage ];
	var siteBase = location.href;
	var dotCom = siteBase.indexOf('.com');
	siteBase = siteBase.substr(0, (dotCom + 4));
	siteBase = siteBase+"/";
	jQuery.preload( urls, {
		base: siteBase,
		ext:'.jpg',
		onComplete:function( data ) {
			var img = new Image();
			img.src = data.image;
		},
		onFinish:function() {
			shiftRight();
			jQuery("div#loadingGraphic").hide();	
		}
	});
}
function clickArrowLeft() {
	if (loadingSlide == 1) { return false; }
	showLoadingGraphic();
	currentScrollingAd = currentScrollingAd - 1;
	if(currentScrollingAd < 0) { currentScrollingAd = scrollingAds.length - 1; }
	generateTab(currentScrollingAd);
	jQuery("div.leftAdColumn").children("div.homeAdBlockImage").html(adImage);
	jQuery("div.leftAdColumn").children("div.homeAdBlockText").html(adContent);	
	var urls = [ largeImage ];
	var siteBase = location.href;
	var dotCom = siteBase.indexOf('.com');
	siteBase = siteBase.substr(0, (dotCom + 4));
	siteBase = siteBase+"/";
	jQuery.preload( urls, {
		base: siteBase,
		ext:'.jpg',
		onComplete:function( data ) {
			var img = new Image();
			img.src = data.image;
		},
		onFinish:function() {
			shiftLeft();
			jQuery("div#loadingGraphic").hide();
		}
	});
}
