/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);

var currentSlide = 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.slideshowMiddle").animate({'marginLeft': "-578px"}, 500, function() {
		jQuery("div.slideshowMiddle").remove();
		jQuery("div.slideshowRight").addClass("slideshowMiddle");
		jQuery("div.slideshowRight").removeClass("slideshowRight");
		jQuery("div.slideshowMiddle").after("\n<div class=\"slideshowBlock slideshowRight\">\n<div class=\"slideshowText\">\n</div>\n</div>");
		loadingSlide = 0;
	});
}
function shiftLeft() {
	loadingSlide = 1;
	jQuery("div.slideshowRight").remove();
	jQuery("div.historyCaptionRight").remove();
	jQuery("div.slideshowLeft").animate({'marginLeft': "578px"}, 500, function() {
		jQuery("div.slideshowLeft").css("margin-left", "0");
		jQuery("div.slideshowMiddle").remove();
		jQuery("div.slideshowLeft").addClass("slideshowMiddle");
		jQuery("div.slideshowLeft").removeClass("slideshowLeft");
		jQuery("div.slideshowMiddle").before("<div class=\"slideshowBlock slideshowLeft\">\n<div class=\"slideshowText\">\n</div>\n</div>\n");
		jQuery("div.slideshowMiddle").after("\n<div class=\"slideshowBlock slideshowRight\">\n<div class=\"slideshowText\">\n</div>\n</div>");
		loadingSlide = 0;
	});
}
function generateTab(tab) {
	slideContent = slideshow[tab]['text'];
	slideImage = slideshow[tab]['image'];
}
function clickArrowRight() {
	if (loadingSlide == 1) { return false; }
	showLoadingGraphic();
	currentSlide = currentSlide + 1;
	if(currentSlide == slideshow.length) { currentSlide = 0; }
	generateTab(currentSlide);
	jQuery("div.slideshowRight").children("div.slideshowText").html(slideContent);	
	jQuery("div.slideshowRight").css('background-image', 'url(/wordpress/wp-content/themes/atl/imgs/slideshow/'+slideImage+'.jpg)');		
	var urls = [ slideImage ];
	var siteBase = location.href;
	var dotCom = siteBase.indexOf('.com');
	siteBase = siteBase.substr(0, (dotCom + 4));
	siteBase = siteBase+"/wordpress/wp-content/themes/atl/imgs/slideshow/";
	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 < 0) { currentSlide = slideshow.length - 1; }
	generateTab(currentSlide);
	jQuery("div.slideshowLeft").children("div.slideshowText").html(slideContent);	
	jQuery("div.slideshowLeft").css('background-image', 'url(/wordpress/wp-content/themes/atl/imgs/slideshow/'+slideImage+'.jpg)');
	var urls = [ slideImage ];
	var siteBase = location.href;
	var dotCom = siteBase.indexOf('.com');
	siteBase = siteBase.substr(0, (dotCom + 4));
	siteBase = siteBase+"/wordpress/wp-content/themes/atl/imgs/slideshow/";
	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();
		}
	});
}
