﻿	var li = -5000;
	var currentTagText = "not set";
	var nextTagText = "not set";
	var measures = new Array();
	var currentTag;
	var animating_bln;
	
	
function sortOnName()
{
	var i;
	var a = $('div.imageholder');
	var l = a.length;
	var newa = new Array();
	var abort;
	
	while (newa.length < l && abort!= true)
	{
		var ni = newa.length-1;
		for (i = 0; i<a.length; ++i)
		{
			
			
			var testStr = newa[ni] ? $(newa[ni]).attr('name') : "unefined";

		//	alert($(a[i]).attr('name').toString() + " compare with " + $(newa[ni]).attr('name').toString());
			if ($(a[i]).attr('name').toString()!= testStr || ni==-1)
			{
				
			
				var t = a.splice(i,1);
				$(t).remove();
				newa.push(t);
				break;
			}

			
			if (i==a.length-1)
			{
				abort=true;
				newa = newa.concat(a);
			}
			

			
			
		}
	}
	var i = 0;
	jQuery.each(newa,function(index,value)
	{
		$('div.tagline[id='+$(value).attr("id")+']').attr("newId",i);
		$(value).attr("id",i);
		$("#viewer").append($(value));
		
		i++;
	})	
	
	jQuery.each($('div.tagline'), function(index,value)
	{
		$(value).attr("id" ,$(value).attr("newId"));
	});
	
}
	
function setUpMeasures()
{


	var i=0;
	//$('body').css('overflow','hidden');

	
	while (i<$('div.imageholder').length) {	
		measures[i]=new Array($('div.imageholder:eq('+i+')').width(),$('div.imageholder:eq('+i+')').height());		
		setUpLink($('div.imageholder:eq('+i+')'));
		i=i+1;

		
	}	
	
	
	
	//$('div.tagline#'+$('div.imageholder:first').attr('id')).css('top','400px');
	
}
function setTagLine()
{
	var i=0;
	var tId = $('div.imageholder:first').attr('id');
	var nId = $(currentTag).attr('id')!=null ? $(currentTag).attr('id') : 0;
	currentTagText = $('div.tagline[id='+ tId +']').text();
	nextTagText =  $('div.tagline[id='+ nId +']').text();
	
	var doAnimate = false;

	
	//tagToAnimate.animate({'display':'block'},1200,'expoout');
	if (currentTagText!=nextTagText)
		$('div.tagline:visible').fadeOut("fast",function()
		{
			$('div.tagline[id='+ tId +']').fadeIn("def");
		}
		);
	else
	{
		$('div.tagline:visible').hide();
		$('div.tagline[id='+ tId +']').show();
	}
	
}
function setUpAnimation(){
	
	$('div.imageholder').pngfix();
		
	page_overlay();
	//$('body').css('overflow','hidden');
	if ($(window).width() < 1024) {
	//	$('body').css('overflow','auto');
	}
	
	
	sortOnName();

	setUpMeasures();
	
	

	$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: 'transparent' });
			
	//$('div.imageholder').hide();
	//$('.loading').hide();
	//pos = myHiddenRightPos($('div.imageholder:first'));

	if (!measures[$('div.imageholder:first').attr('id')][0])
		measures[$('div.imageholder:first').attr('id')][0]=480;

	pos1 = myLeftPos($('div.imageholder:first'));
	$('div.imageholder:first').css({'top':pos1[1],'left':-pos1[0],'position':'absolute'});
	left1 = ($(window).width()-measures[$('div.imageholder:first').attr('id')][0])/2;
	$('div.imageholder:first').animate({'left':left1+'px','display':'block'},900,'expoout', function()
	{
		
		pos2 = myLeftPos($('div.imageholder:eq(1)'));
		$('div.imageholder:eq(1)').css({'top':pos2[1],'left':-pos2[0],'position':'absolute'});
		left2 = 0-(measures[$('div.imageholder:eq(1)').attr('id')][0]*0.75);
		$('div.imageholder:eq(1)').animate({'left':left2+'px', 'display':'block'},400,'expoout');
		/*
		pos = myLeftPos($('div.imageholder:last'));
		$('div.imageholder:last').css({'top':pos[1],'left':$(window).width(),'position':'absolute'});
		left = $(window).width()-(measures[$('div.imageholder:last').attr('id')][0]/4);
		$('div.imageholder:last').animate({'left':left+'px', 'display':'block'},800,'expoout',bindpads);
		*/
		
	}
	
	
	);


	
	$('li.previous').click(previous);
	$('li.next').click(next);
	
	
	// insert timeout here //

	$(document).mousemove(function () {
		li = 0;
	});
	//loop();
	
}
	function setUpLink(o)
	{
			// create and remove link on the image on mousehover
		$(o).hover(function (){
			link = $(this).attr('href');
			
			$(this).bind('click',function (){
				location.href=link;
			});
		},function() {
			$(this).unbind('click');
		});	
	}
	function myCenterPos (o) {
		// returns the centred position of the image
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = ($(window).width()-mymeasures[0])/2;
		var top = ($(window).height()-mymeasures[1])/2;

		if (top < 120) {
			top = 120;
		}
		
		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 = $(window).width();
		var top = 230;//($(window).height()-mymeasures[1])/2;


		
		return new Array(left, top);
	}
	function stopAll()
	{
		var i=0;
	
		while (i<$('div.imageholder').length) {	
			$('div.imageholder:eq('+i+')').unpause();
			$('div.imageholder:eq('+i+')').stop();
			$('div.imageholder:eq('+i+')').stop({clearqueue:true,gotolaststep:true});
			
			
			
			//alert($('div.imageholder:eq('+i+')').width())
			i=i+1;
		}	
	}
	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 = 230;//($(window).height()-mymeasures[1])/2;

		if (top < 120) {
			top = 120;
		}
		
		return new Array(left, top);
	}
	
	function right_in () {
		stopAll();
		//$('body').css('overflow','hidden');

		$('div.imageholder:last').stop({gotolaststep:true});

		var left = $(window).width()-(measures[$('div.imageholder:last').attr('id')][0]/2);
		$('div.imageholder:last').animate({'left':left, 'display':'block'},500,'expoout');
		
	}
	
	function out_right () {
		
		if (animating_bln)
		return;
		stopAll();
		
	 	var pos = $(window).width()-(measures[$('div.imageholder:last').attr('id')][0]/4);
		
		$('div.imageholder:last').animate({'left':pos}, 'fast','expoout');

	}
	function out_left () {
		if (animating_bln)
			return;
		stopAll();
	//	$('div.imageholder:last').stop({gotolaststep:true});
	 	var pos = 0-(measures[$('div.imageholder:eq(1)').attr('id')][0]*0.75);//myLeftPos($('div.imageholder:last'));
		

		$('div.imageholder:eq(1)').animate({'left':pos}, 'fast','expoout');	

	}
	function left_in () {
		stopAll();
		//$('body').css('overflow','hidden');
	//	$('div.imageholder:last').stop({gotolaststep:true});
	//	$('div.imageholder:first').stop({gotolaststep:true});
		var left = 0-measures[$('div.imageholder:eq(1)').attr('id')][0]/2;
	   	$('div.imageholder:eq(1)').animate({'left':left}, 500,'expoout');
		
	}
	
	function previous () {
		li = 0;
		animating_bln= true;
		//	out_right();
		stopAll();
		unbindpads();
		stopAll();
	//	$('body').css('overflow','hidden');
		pos1 = myLeftPos($('div.imageholder:eq(1)'));
		
		$('div.imageholder:first').unpause();
		pos = myLeftPos($('div.imageholder:eq(1)'));
		
		
		$('div.imageholder:eq(1)').animate({'left':'-'+(pos[0])+'px'},400,null);		
		
		$('div.imageholder:first').animate({'left':(0-(pos[0]*0.75))+'px'},800,'expoout');
		var tmpId = parseInt($('div.imageholder:last').attr('id'))-1;
		if (tmpId<0)
			tmpId = measures.length-1;
			
	//	alert(tmpId)

		pos = myHiddenRightPos($('div.imageholder:last'));

		$('div.imageholder:last').show();
		left = ($(window).width()-measures[$('div.imageholder:last').attr('id')][0])/2;
		
		$('div.imageholder:last').animate({'left':left+'px', 'display':'block'},800,'expoout',function() 
		{
			pos2= $(window).width()- (measures[$('#'+tmpId).attr('id')][0]/4);
			$('#'+tmpId).css('left',$(window).width());
			
			$('#'+tmpId).animate({'left':pos2+'px', 'display':'block'},300,'expoout');

			$('div.imageholder:last').insertBefore('div.imageholder:first').show();
			$('body').css({"overflow":"hidden"});
		
			bindpads();
		}
		
		
		);
		currentTag = $('div.imageholder:first');
	//	$('.nav').unblock();
	}

	function next () {
		li = 0;
		
		animating_bln= true;
		stopAll();
		unbindpads();
		stopAll();
	//	$('body').css('overflow','hidden');
		$('div.imageholder:first').unpause();
		pos = $(window).width()- (measures[$('div.imageholder:eq(1)').attr('id')][0]/4);
		$('div.imageholder:first').pause(50);
		$('div.imageholder:first').animate({'left':pos},400,'expoout',function (){
			$('div.imageholder:eq(2)').css('left',-650);
			var tmpPos = (measures[$('div.imageholder:eq(2)').attr('id')][0]*0.75);
			$('div.imageholder:eq(2)').animate({'left':(0-tmpPos)+"px"},400,null);
			$('div.imageholder:first').insertAfter('div.imageholder:last');
				
			//
			$('body').css('overflow','hidden');
			//alert($('body').css('overflow'));

		});
		var i = parseInt($('div.imageholder:first').attr('id'))-1;
		//alert(i - 1);
		$('div.imageholder:last').pause(100);
		$('div.imageholder:last').animate({'left':$(window).width()+"px"},200,null);
		

		
		
		try{
		


		pos = myLeftPos($('div.imageholder:eq(1)'));
		//	$('div.imageholder:eq(1)').hide();
		//$('div.imageholder:eq(1)').css({'top':pos[1],'left':'-'+pos[0]+'px'});
		$('div.imageholder:eq(1)').show();
		left = ($(window).width()-measures[$('div.imageholder:eq(1)').attr('id')][0])/2;
		$('div.imageholder:eq(1)').animate({'left':left, 'display':'block'},800,'expoout',bindpads);
		currentTag = $('div.imageholder:first');
		}
		catch(e)
		{
			alert("Error: \n"+e);
		}
		//$('.nav').unblock();
		
		//$('body').css('overflow','hidden');
	}	

	function bindpads(){
		
		$('div#leftpad').mouseover(left_in);
		$('div#leftpad').mouseout(out_left);
		$('div#rightpad').mouseover(right_in);
		$('div#rightpad').mouseout(out_right);
		$('div#rightpad').click(previous);
		$('div#leftpad').click(next);
	
	//	$('body').css('overflow','hidden');
	//	$('body').css('overflow-x','hidden');
	//	$('html').css('overflow-x','hidden');
		//$('html').css('overflow','hidden');
		resetDivs();
		setUpMeasures();
		setTagLine();
	}
	function resetDivs()
	{
		var list = $('div.imageholder');
		$.each(list,function(index,value)
		{
		
			if (index!=0 && index!=1 && index !=list.length-1)
			{
				$(value).hide();
			//	$(value).css('left','-600px');
			}
		
		})	
	
	}
	function unbindpads() {
		stopAll();
		$('div#leftpad').unbind('mouseover');
		$('div#leftpad').unbind('mouseout');
		$('div#leftpad').unbind('click');
		$('div#rightpad').unbind('mouseover');
		$('div#rightpad').unbind('mouseout');
		$('div#rightpad').unbind('click');
		//$('body').css('overflow','hidden');
		animating_bln= false;
	}


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




	


	
	// handle iframed overlays sizes
	function page_overlay() {
		$('.clippingsWindow').width($(window).width()-5);
		$('.clippingsWindow').height($(window).height()-20);
		$('.clippingsWindow#header').width($(window).width()-20);
		$('.eventsWindow').width($(window).width()-20);
		$('.eventsWindow').height($(window).height()-20);
	}
	
	$(window).resize(function() {
		
		page_overlay();
		
		if (!measures[$('div.imageholder:eq(1)').attr('id')]) return;
		//$('body').css('overflow','hidden');
		left = 0-(measures[$('div.imageholder:eq(1)').attr('id')][0]*0.75);
		middle = ($(window).width()-measures[$('div.imageholder:first').attr('id')][0])/2;
		right = $(window).width()-(measures[$('div.imageholder:last').attr('id')][0]/4);
		$('div.imageholder:eq(1)').css('left',left+'px');
		$('div.imageholder:first').css('left',middle+'px');
		$('div.imageholder:last').css('left',right+'px');
		$('div.imageholder:first').css('top','230px');
		$('div.imageholder:last').css('top','230px');
		$('div.imageholder:eq(1)').css('top','230px');
		//$('body').css('overflow','hidden');
		//$('body').css('overflow-x','hidden');
		//if ($(window).width() < 1024) {
		//	$('body').css('overflow','auto');
	//	}
	});
	

