// JavaScript Document
function include(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
}


include ('_assets/scripts/gf_utils.js');
include ('_assets/scripts/imageloader.js');
include ('_assets/scripts/jquery-1.5.2.min.js');

var _PrevImage = 0;
var _CurrentImage = -1;
var _MaxImage = 72;
var _FinishedCurrent = true;
var _FinishedNext = true;
var _FinishedTextCurrent = true;
var _FinishedTextNext = true;
var _WidthLarge = 0;
var _WidthSmall = 0;
var _ThumbPage = 1;
var _ThumbPageCount = 3;

var _PrevDiv;
var _NextDiv;

function BleedImage ( imgId )
{
	if ( _CurrentImage < 600 ) // these are unscaled images
	{
		var width = f_clientWidth();
		var height = 1536*width/2048;
		
		if ( height < f_clientHeight() )
		{
			height = f_clientHeight();
			width = 2048*height/1536;
		}
		$(imgId).css("width",width);
		$(imgId).css("height",height);
		$(imgId).css("top",(f_clientHeight()-height)/2);
		$(imgId).css("z-index",0);
	}
	else
	{
		$(imgId).css("z-index",20000);
		$(imgId).css("top", (f_clientHeight() - 300 ) / 2 );
		$("#contactImg").css("top", (f_clientHeight() - 300 ) / 2 );
	}
}

function NewWidth ()
{
	var width = f_clientWidth();
	var height = 1536*width/2048;
	
	if ( height < f_clientHeight() )
	{
		height = f_clientHeight();
		width = 2048*height/1536;
	}
	return ( width );
}

function Reposition()
{
	if ( $("#imgId") != null )
	{
		BleedImage ( "#imgId" );
		$("#imgId").css("left",LeftCentrePosn ());
		$("#contactImg").css("left",LeftCentrePosn ());
		$("#currentProjectText").css("left",LeftTextCentrePosn ());
		if ( _CurrentImage > 600 ) _WidthSmall = $("#imgId").attr("width");
		else 					   _WidthLarge = $("#imgId").attr("width");
	}
	if ( $("#thumbs"+_ThumbPage) != null )
		divCentre("#thumbs"+_ThumbPage);
	if ( $("#menuTable") != null )
	{
  	    $("#menuTable").addClass ( "abs" );
		$("#menuTable").css ( "top", f_clientHeight () - 63 );
		$("#menuTable").css ( "left", ( f_clientWidth () - 470 ) / 2 );
	}
}

function Load()
{
	Reposition()
	_CurrentImage = 1;
	var loader = new ImageLoader('_assets/images/large/'+_CurrentImage+'.jpg?v=7');
	loader.loadEvent = function(url, image, index){
		$("#imgId").attr("src",'_assets/images/large/'+_CurrentImage+'.jpg?v=7');
		$("#wait").css("display","none");
		$("#waitImg").attr("src",'_assets/images/blank.png');
	}
	loader.load();
	$("#frontFrame").attr("height",f_clientHeight());
}

window.onresize = Reposition;
window.onload = Reposition;
window.onreset = Reposition;

function LoadImage ()
{
    $("#contactImg").css("display","none");
	$("#contactButton").attr("src",'_assets/images/buttons/contact.png');
	$("#aboutButton").attr("src",'_assets/images/buttons/about.png');
	
	HideHover();
		$("#waitImg").attr("src",'_assets/images/waithotel.gif');
	$("#wait").css("display","block");
	var textLoader = new ImageLoader('_assets/images/text/'+_CurrentImage+'.png?v=7');
	textLoader.load();	
	var loader = new ImageLoader('_assets/images/large/'+_CurrentImage+'.jpg?v=7');
	loader.loadEvent = function(url, image, index){
		SlideImage ();
	}
	loader.load();
}

function NextImage ()
{
	if ( _FinishedNext && _FinishedCurrent && _CurrentImage < _MaxImage && _CurrentImage >= 1 )
	{
		$("#projButton").attr("src",'_assets/images/buttons/thumb.png');
		_PrevImage = _CurrentImage;
		_CurrentImage++;
		LoadImage ();
	}
}

function PrevImage ()
{
	if ( _FinishedNext && _FinishedCurrent && _CurrentImage > 1 && _CurrentImage <= _MaxImage )
	{
		$("#projButton").attr("src",'_assets/images/buttons/thumb.png');
		_PrevImage = _CurrentImage;
		_CurrentImage--;
		LoadImage ();
	}
}

function LeftCentrePosn ()
{
	return ( document.body.clientWidth - $("#imgId").attr("width") ) / 2;	
}

function LeftNextCentrePosn ()
{
	return ( document.body.clientWidth - $("#nextImg").attr("width") ) / 2;	
}

function SetNextTopPosn ()
{
	$("#nextImg").css("top", (f_clientHeight() - 300 )/ 2 );	
}//screen.availHeight

function SetTopPosn ()
{
	$("#imgId").css("top", (f_clientHeight() - $("#imgId").attr("height") ) / 2 );
}

function NewLeftTextCentrePosn ()
{
	return ( document.body.clientWidth - $("#nextProjectText").attr("width") ) / 2;	
}

function LeftTextCentrePosn ()
{
	return ( document.body.clientWidth - $("#currentProjectText").attr("width") ) / 2;	
}

function SlideImage()
{
	var newLeft,newWidth;
	var newTextLeft = document.body.clientWidth/2 -300;
	var nextStartScroll, currentEndScroll, textNextStart, textCurrentEnd;
	
	_FinishedCurrent = _FinishedNext = false;
	$("#nextImg").attr("src","_assets/images/large/"+_CurrentImage+".jpg?v=7");
	$("#nextProjectText").attr("src","_assets/images/text/"+_CurrentImage+".png?v=7");
	$("#wait").css("display","none");
		$("#waitImg").attr("src",'_assets/images/blank.png');

	if ( _CurrentImage > 600 )
	{
		SetNextTopPosn ();
		newWidth = 600;
		$("#nextImg").css("width",600);
		$("#nextImg").css("height",300);
	}
	else
	{
		BleedImage ( "#nextImg" );
		//var h,w;
//		h = 2048 * f_clientHeight() / 1536;
	//	w = document.body.clientWidth;
		//_WidthLarge = h < w ? h : w ;
		newWidth = NewWidth(); //_WidthLarge;// = $("#nextImg").attr("width" );
	}
	newLeft = ( document.body.clientWidth - newWidth ) /2

	if ( _PrevImage < _CurrentImage )
	{
		currentEndScroll = -$("#imgId").attr("width");
		textNextStart = document.body.clientWidth;
		textCurrentEnd = -600;
		nextStartScroll = document.body.clientWidth;
	}
	else
	{
		currentEndScroll = document.body.clientWidth;
		textNextStart = -600;
		textCurrentEnd = document.body.clientWidth;
		nextStartScroll = -newWidth;
	}
	$("#nextImg").css("left",nextStartScroll);
	$("#nextImgDiv").css("display","block");
	$("#imgId").animate ( { left:currentEndScroll }, 2000, function () { _FinishedCurrent = true; FinishImage ();} );
	$("#movieHolder").animate ( { left:-f_clientWidth() }, 2000, function () {} );
	$("#thumbs"+_ThumbPage).animate ( { left:-780 }, 2000, function () {} );
	$("#nextImg").animate ( { left:newLeft }, 2000, function () { _FinishedNext = true; FinishImage (); } );

	$("#nextProjectText").css("left",textNextStart);
	$("#nextProjectText").css("display","block");
	$("#currentProjectText").animate ( { left:textCurrentEnd }, 2000, function () { _FinishedTextCurrent = true; FinishTextImage ();} );
	$("#nextProjectText").animate ( { left:newTextLeft }, 2000, function () { _FinishedTextNext = true; FinishTextImage (); } );
}

function FinishImage ()
{
	if ( _FinishedCurrent && _FinishedNext )
	{
		var newLeft;
		$("#imgDivId").css("display","none");

		if ( _CurrentImage > 600 )
		{
			SetTopPosn ();
			newLeft = document.body.clientWidth/2 - 300;
			$("#projButton").attr("src",'_assets/images/buttons/work.png');
		}
		else
		{			
			newLeft = LeftNextCentrePosn ();
			$("#projButton").attr("src",'_assets/images/buttons/thumb.png');
			ShowHover();
		}
		$("#imgId").css("width",$("#nextImg").css("width"));
		$("#imgId").css("height",$("#nextImg").css("height"));		
		$("#imgId").css("top",$("#nextImg").css("top"));
		$("#imgId").css("left",newLeft);
		$("#imgId").css("z-index",$("#nextImg").css("z-index"));

		if ( _CurrentImage == 667 )
		{
           $("#contactImg").css("display","block");
		   $("#contactImg").css("top",$("#nextImg").css("top"));
		   $("#contactImg").css("left",newLeft);
		}
		// ensure reset if already loaded (30/10/11)
		var newIm = '_assets/images/large/'+_CurrentImage+'.jpg?v=7';
		if ( $("#imgId").attr ( "src" ) == newIm )
		{
			resetImage ();
		}
		else
		{
			$("#imgId").load ( resetImage );
			$("#imgId").attr("src",newIm);
		}
	}
}

function resetImage ()
{	
	$("#imgDivId").css("display","block");
	$("#nextImgDiv").css("display","none");
	$("#movieHolder").css("display","none");
	$("#thumbs"+_ThumbPage).css("display","none");
	$("#imgId").unbind ();
}

function FinishTextImage ()
{
	if ( _FinishedTextNext && _FinishedTextCurrent )
	{
		$("#currentProjectText").css("display","none");
		$("#currentProjectText").css("left",$("#nextProjectText").css("left"));	
		
		// ensure reset if already loaded (30/10/11)
		var newTxt = '_assets/images/text/'+_CurrentImage+'.png?v=7';
		if ( $("#currentProjectText").attr ( "src" ) == newTxt )
		{
			resetText ();
		}
		else
		{
			$("#currentProjectText").load ( resetText );
			$("#currentProjectText").attr("src",newTxt);
		}
	}
}

function resetText ()
{
	$("#currentProjectText").css("display","block");
	$("#nextProjectText").css("display","none");
}

//---------------------------------------------------------------------------------------------
// Button Actions
//---------------------------------------------------------------------------------------------
function cmdContact_Click ()
{
	if ( _FinishedNext && _FinishedCurrent && _CurrentImage != 667 )
	{
		_PrevImage = _CurrentImage;
		_CurrentImage = 667;
		LoadImage ();
		$("#contactButton").attr("src",'_assets/images/buttons/contactUnderline.png');
	}
}


function cmdAbout_Click ()
{
	if ( _FinishedNext && _FinishedCurrent && _CurrentImage != 666 )
	{
		_PrevImage = _CurrentImage;
		_CurrentImage = 666;
		LoadImage ();
		$("#aboutButton").attr("src",'_assets/images/buttons/aboutUnderline.png');
	}
}

function cmdProj_Click ()
{
	if ( _FinishedNext && _FinishedCurrent )
	{
		_PrevImage = _CurrentImage;
		if ( _CurrentImage > 600 || _CurrentImage < 0)
		{
			_CurrentImage = 1;
			$("#projButton").attr("src",'_assets/images/buttons/thumb.png');
			LoadImage ();
		}
		else loadThumbs ();
	}
}



function divCentre(objName)
{
	//alert ( arguments.callee.caller );
	if ( $(objName) != null )
	{
		$(objName).css("left",( f_clientWidth() - $(objName).attr("width") ) / 2);
		$(objName).css("top",( f_clientHeight() - $(objName).attr("height") ) / 2);
	}
}


function loadImageNumber ( imageNumber )
{
	if ( _FinishedNext && _FinishedCurrent && imageNumber <= _MaxImage && imageNumber >= 1 )
	{
		$("#projButton").attr("src",'_assets/images/buttons/thumb.png');
		_PrevImage = _CurrentImage;
		_CurrentImage = imageNumber;
		LoadImage ();
	}	
}

function loadThumbs ()
{
	$("#projButton").attr("src",'_assets/images/buttons/work.png');

    HideHover();
	$("#thumbs1").css("top", f_clientHeight() / 2  - 215 );
	var newLeft,newWidth;
	var nextStartScroll, currentEndScroll, textNextStart, textCurrentEnd;
	
	_FinishedCurrent = _FinishedNext = false;

	//FinishTextImage ();
	//FinishImage ();

	newLeft = ( f_clientWidth() - 780 ) /2;
	currentEndScroll = -$("#imgId").attr("width");
	textCurrentEnd = -$("#currentProjectText").attr("width");
	nextStartScroll = document.body.clientWidth;
	
	//alert( $("#imgId").css("left") + " , " + currentEndScroll + " = " + $("#imgId").css("display") );
	//alert( $("#nextImg").css("left") + " , " + currentEndScroll + " = " + $("#nextImg").css("display") );
	 
	$("#thumbs1").css("left",nextStartScroll);
	$("#thumbs1").css("display","block");
	$("#imgId").animate ( { left:currentEndScroll }, 2000, function () { _FinishedCurrent = true; FinishThumbs();} );
	$("#thumbs1").animate ( { left:newLeft }, 2000, function () { _FinishedNext = true; FinishThumbs (); } );
	$("#movieHolder").animate ( { left:-f_clientWidth() }, 2000, function () {} );
	//$("#thumbs1").animate ( { left:-600 }, 2000, function () {} );
	$("#currentProjectText").animate ( { left:textCurrentEnd }, 2000, function () { _FinishedTextCurrent = true; /*FinishTextImage ();*/} );

	// also end off next just in case
	//$("#nextImg").animate ( { left:-$("#nextImg").attr("width") }, 2000, function () { _FinishedNext 	 = true; FinishThumbs();} );
	//$("#nextProjectText").animate ( { left:textCurrentEnd }, 2000, function () { _FinishedTextCurrent = true; /*FinishTextImage ();*/} );

	var count = 1;

	// clear current thumbs
	for ( count = 1 ; count <= 24 ; count++ )
	{
		var objName = "#thumb" + count;
		$(objName).css("display","none");
		$(objName).attr("src",'_assets/images/thumb/e.png');
	}
	for ( count = 1 ; count <= 24 ; count++ )
	{		
		var objName = "#thumb" + count;
		$(objName).attr("src",'_assets/images/waithotel.gif');
		$(objName).css("display","block");

		var loader = new ImageLoader('_assets/images/thumb/'+count+'.jpg?v=7');
		loader.index = count;
		loader.loadEvent = function(url, image, index){ ShowThumb (index, url); }
		loader.load();
	}
	_ThumbPage = 1;
	_PrevImage = -2;
	_CurrentImage = -1;
}

function nextThumbs ()
{
	var nextPage = _ThumbPage + 1;
    HideHover();
	$("#thumbs"+nextPage ).css("top", f_clientHeight() / 2  - 215 );
	var newLeft,newWidth;
	var nextStartScroll, currentEndScroll, textNextStart, textCurrentEnd;
	
	_FinishedCurrent = _FinishedNext = false;
	
	newLeft = ( f_clientWidth() - 780 ) /2;
	nextStartScroll = document.body.clientWidth;

	$("#thumbs"+nextPage).css("left",nextStartScroll);
	$("#thumbs"+nextPage).css("display","block");
	$("#thumbs"+_ThumbPage).animate ( { left:-780 }, 2000, function () { _FinishedCurrent = true; FinishThumbs();} );
	$("#thumbs"+nextPage).animate ( { left:newLeft }, 2000, function () { _FinishedNext = true; FinishThumbs (); } );

	var count = 1;

	// clear current thumbs
	for ( count = (nextPage-1)*24 + 1 ; count <= nextPage*24 ; count++ )
	{
		var objName = "#thumb" + count;
		//$(objName).css("display","none");
		$(objName).attr("border", '0');
		$(objName).attr("src",'_assets/images/blank.png');
	}
	for ( count = (nextPage-1)*24 + 1 ; count <= Math.min(nextPage*24,_MaxImage) ; count++ )
	{		
		var objName = "#thumb" + count;
		$(objName).attr("src",'_assets/images/waithotel.gif');
		$(objName).css("display","block");
		$(objName).attr("border", '1');
		var loader = new ImageLoader('_assets/images/thumb/'+count+'.jpg?v=7');
		loader.index = count;
		loader.loadEvent = function(url, image, index){ ShowThumb (index, url); }
		loader.load();
	}
	_ThumbPage = nextPage;
}


function prevThumbs ()
{
 	var nextPage = _ThumbPage - 1;

	HideHover();
	$("#thumbs"+nextPage).css("top", f_clientHeight() / 2  - 215 );
	var newLeft,newWidth;
	var nextStartScroll, currentEndScroll, textNextStart, textCurrentEnd;
	
	_FinishedCurrent = _FinishedNext = false;
	
	newLeft = ( f_clientWidth() - 780 ) /2;
	nextStartScroll = -780;

	$("#thumbs"+nextPage).css("left",nextStartScroll);
	$("#thumbs"+nextPage).css("display","block");
	$("#thumbs"+_ThumbPage).animate ( { left:f_clientWidth() }, 2000, function () { _FinishedCurrent = true; FinishThumbs();} );
	$("#thumbs"+nextPage).animate ( { left:newLeft }, 2000, function () { _FinishedNext = true; FinishThumbs (); } );
	_ThumbPage = nextPage;
}

function FinishThumbs ()
{
	_CurrentImage = -1;
	$("#projButton").attr("src",'_assets/images/buttons/work.png');
	$("#imgDivId").css("display","none");	
	$("#currentProjectText").css("display","none");	
	for ( var count=1;count<=_ThumbPageCount;count++)
	{
		if ( count != _ThumbPage )
			$("#thumbs"+count).css("display","none");
	}
}

function ShowThumb (count,url)
{
	var objName = "#thumb" + count;
	$(objName).attr("src",url);	
}

function HideHover ()
{
	$("#leftHover").css("display","none");
	$("#rightHover").css("display","none");
}
function ShowHover ()
{
	$("#leftHover").css("display","block");
	$("#rightHover").css("display","block");
	$("#leftHover").css("height",f_clientHeight()-180);
	$("#leftHover").css("width",f_clientWidth()/2);
	$("#rightHover").css("height",f_clientHeight()-180);
	$("#rightHover").css("width",f_clientWidth()/2);
}
