/* Thanks to Build for this http://www.wearebuild.com */

runOnLoad(function(){
	
	$('img.showcaseimg').pngfix();
		
	page_overlay();
	
	if (750 < 1024) {
		$('body').css('overflow','auto');
	}
	
	var measures = new Array();
	var i=0;
	while (i<$('img.showcaseimg').length) {	
		measures[i]=new Array($('img.showcaseimg:eq('+i+')').width(),$('img.showcaseimg:eq('+i+')').height());
		i=i+1;
	}

	function myCenterPos (o) {
		// returns the centred position of the image
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = (750-mymeasures[0])/2;
		var top = 0;
		
		return new Array(left, top);
	}
	
	function myHiddenRightPos (o) {
		// returns the right position of the image before it is animated
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = 750;
		var top = 0;
		
		return new Array(left, top);
	}
	
	function myLeftPos (o) {
		// returns the left position of the image that is being replaced
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = mymeasures[0];
		var top = 0;

	
		return new Array(left, top);
	}
	
	function left_in () {
		$('img.showcaseimg:first').stop();
		var pos = $('img.showcaseimg:first').offset()['left']+15;
	   	$('img.showcaseimg:first').animate({'left':pos}, 'slow','expoout');
		
	}
	
	function out () {
		$('img.showcaseimg:first').stop();
	 	var pos = myCenterPos($('img.showcaseimg:first'));
		$('img.showcaseimg:first').animate({'left':pos[0]}, 'normal','expoout');		
	}
	
	function right_in () {
		$('img.showcaseimg:first').stop();
		var pos = $('img.showcaseimg:first').offset()['left']-15;
	   	$('img.showcaseimg:first').animate({'left':pos}, 'slow','expoout');
		
	}
	
	function previous () {
		li = 0;
		unbindpads();
		$('img.showcaseimg:first').stop();
		$('.image_gallery_nav').block({ backgroundColor: 'transparent', color: 'transparent'});
		$('img.showcaseimg:first').stop({gotolaststep:true});
		pos = myLeftPos($('img.showcaseimg:first'));
		$('img.showcaseimg:first').pause(50);
		$('img.showcaseimg:first').animate({'left':'-'+pos[0]+'px'},400,null,function (){
				$('img.showcaseimg:first').hide().remove().insertAfter('img.showcaseimg:last');
				});	
		pos = myHiddenRightPos($('img.showcaseimg:eq(1)'));
		$('img.showcaseimg:eq(1)').css({'position':'absolute','top':pos[1]+'px','left':pos[0]+'px'});
		left =0;
		$('img.showcaseimg:eq(1)').animate({'left':left, 'display':'block'},1200,'expoout',bindpads);
		$('.image_gallery_nav').unblock();
	}

	function next () {
		li = 0;
		unbindpads();
		$('img.showcaseimg:first').stop();
		$('.image_gallery_nav').block({ backgroundColor: 'transparent', color: 'transparent'});
		$('img.showcaseimg:first').stop({gotolaststep:true});
		pos = 750;
		$('img.showcaseimg:first').pause(50);
		$('img.showcaseimg:first').animate({'left':pos},400,null,function (){
			$('img.showcaseimg:first').hide();
			$.each($('img.showcaseimg:hidden'),function (i,n) { $(n).remove().insertAfter('img.showcaseimg:last'); });
		});	
		pos = myLeftPos($('img.showcaseimg:last'));
		$('img.showcaseimg:last').css({'top':pos[1],'left':'-'+pos[0]+'px'});
		left = 0;
		$('img.showcaseimg:last').animate({'left':left, 'display':'block'},1200,'expoout',bindpads);
		$('.image_gallery_nav').unblock();
	}	

	function bindpads(){
		$('div#leftpad').mouseover(left_in);
		$('div#leftpad').mouseout(out);
		$('div#rightpad').mouseover(right_in);
		$('div#rightpad').mouseout(out);
	}

	function unbindpads() {
		$('div#leftpad').unbind('mouseover');
		$('div#leftpad').unbind('mouseout');
		$('div#rightpad').unbind('mouseover');
		$('div#rightpad').unbind('mouseout');
	}
	
	bindpads();

	$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: 'transparent' });
			
	$('img.showcaseimg').hide();
	
	pos = myHiddenRightPos($('img.showcaseimg:first'));
	$('img.showcaseimg:first').css({'top':pos[1],'left':pos[0],'position':'absolute'});
	left = 0;
	$('img.showcaseimg:first').animate({'left':left, 'display':'block'},1200,'expoout');

	$('.previous').click(previous);
	$('.next').click(next);
	
	
	// insert timeout here //
	var li = -5000;

	$(document).mousemove(function () {
		li = 0;
	});

    function loop () {
    	if (li == 6000) {
    		if ($('div.jqmOverlay').length == 0) {
    			previous();
    			li = 0;
    		}
    	} else {
    		li = li + 1000;
    	}
    	setTimeout(loop,1000);
    }	

	loop();


	
	// handle iframed overlays sizes
	function page_overlay() {
		$('.clippingsWindow').width(750-5);
		$('.clippingsWindow').height($(window).height()-20);
		$('.clippingsWindow#header').width(750-20);
		$('.eventsWindow').width(750-20);
		$('.eventsWindow').height($(window).height()-20);
	}
	
	$(window).resize(function() {
		page_overlay();
		if (750 < 1024) {
			$('body').css('overflow','auto');
		}
	});
	
});