var currentSlide = 1;
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.middleHistoryColumn").animate({marginLeft: "-779px"}, 350, function() {
		jQuery("div.middleHistoryColumn").remove();
		jQuery("div.rightHistoryColumn").addClass("middleHistoryColumn");
		jQuery("div.rightHistoryColumn").removeClass("rightHistoryColumn");
		jQuery("div.middleHistoryColumn").after("\n<div class=\"historyBlock rightHistoryColumn\">\n<div class=\"historyImage\">\n</div>\n<div class=\"historyText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>");
		loadingSlide = 0;
	});
}
function shiftLeft() {
	loadingSlide = 1;
	jQuery("div.rightHistoryColumn").remove();
	jQuery("div.historyCaptionRight").remove();
	jQuery("div.leftHistoryColumn").animate({marginLeft: "779px"}, 350, function() {
		jQuery("div.leftHistoryColumn").css("margin-left", "0");
		jQuery("div.middleHistoryColumn").remove();
		jQuery("div.leftHistoryColumn").addClass("middleHistoryColumn");
		jQuery("div.leftHistoryColumn").removeClass("leftHistoryColumn");
		jQuery("div.middleHistoryColumn").before("<div class=\"historyBlock leftHistoryColumn\">\n<div class=\"historyImage\">\n</div>\n<div class=\"historyText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>\n");
		jQuery("div.middleHistoryColumn").after("\n<div class=\"historyBlock rightHistoryColumn\">\n<div class=\"historyImage\">\n</div>\n<div class=\"historyText\">\n</div>\n<div class=\"clear\">&nbsp;</div>\n</div>");
		loadingSlide = 0;
	});
}
function generateTab(tab) {
	largeImage = historySlides[tab]['historyImage'];
	adImage = '<img src="/imgs/history/slides/'+largeImage+'.jpg" alt="Chianti History Slide '+currentSlide+'" />';
	adContent = "<p>"+historySlides[tab]['historyDetails']+"</p>";
	adContent += '<p class="historyWhere">'+currentSlide+' of '+(historySlides.length - 1)+'</p>';
}
function clickArrowRight() {
	if (loadingSlide == 1) { return false; }
	showLoadingGraphic();
	currentSlide = currentSlide + 1;
	if(currentSlide >= (historySlides.length - 1)) { jQuery('div.historyArrowRight > a').addClass("noShow"); }
	else { jQuery('div.historyArrowLeft > a').removeClass("noShow"); }
	generateTab(currentSlide);
	jQuery("div.rightHistoryColumn").children("div.historyImage").html(adImage);
	jQuery("div.rightHistoryColumn").children("div.historyText").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();
	currentSlide = currentSlide - 1;
	if(currentSlide <= 1) { jQuery('div.historyArrowLeft > a').addClass("noShow"); }
	else { jQuery('div.historyArrowRight > a').removeClass("noShow"); }
	generateTab(currentSlide);
	jQuery("div.leftHistoryColumn").children("div.historyImage").html(adImage);
	jQuery("div.leftHistoryColumn").children("div.historyText").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();
		}
	});
}