// Vars.
var currentContent = null;
var hash = window.location.hash.substring(1).split('/');
var clientsIds = new Object();
var ourstoryLineText = new Object();
var currentClient = null;
var initialLoad = true;
var initialMenuLoad = true;

// Fixed size controll
function fixedWindow() 
{
	// Area
	if(currentContent != 'client')
	{
		if (document.getElementById('mainBody').offsetWidth > 992)
		{
			document.getElementById('main').style.width = (document.getElementById('mainBody').offsetWidth-100)+"px";	
			
			//document.getElementById('menu').style.width = (document.getElementById('mainBody').offsetWidth-50)+"px";			
			document.getElementById('home').style.width = (document.getElementById('mainBody').offsetWidth-50-12)+"px";	
		}
		else 
		{			
			document.getElementById('main').style.width = "968px";
			//document.getElementById('menu').style.width = "968px";		
			document.getElementById('home').style.width = "968px";	
		}
		
	}
	
	// Menu
	var left = (((Math.floor((document.getElementById('mainBody').offsetWidth-50-12)/155)*155)-12)-400);
	if(initialMenuLoad)
		document.getElementById('menu').style.left = left+"px";	
	else
	{
		$('#menu').stop();
		$('#menu').animate({left:left}, 250);
	}
	initialMenuLoad = false;
	
	
	//if(Math.floor((document.getElementById('mainBody').offsetWidth-50+12)/155) < 10) {		
	//	document.getElementById('menu').style.right = "60px";	
	//}

	//else
	//{
	//	var row = (Math.floor((document.getElementById('mainBody').offsetWidth-50+12)/155)*155)-10;
	//	document.getElementById('menu').style.right = (document.getElementById('mainBody').offsetWidth-row)+"px";
	//}
}


// Adjust header
function adjustHeader() 
{
	var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body
	var top=document.all? iebody.scrollTop : pageYOffset;
	document.getElementById('header').style.top = (20-top)+"px";
}
window.onscroll = adjustHeader;


// Thumbnais rollover
function thumbnail(_action,_id)
{
	if(!homeRollOverHold)
	{
		if(_action == 'over')
			$('#home_'+_id).fadeOut(50);
		else 
			$('#home_'+_id).fadeIn('slow');	
	}
}


// Press
function pressRollover(_action,_id)
{
	if(_action == 'over')
		$('#press_'+_id).animate({opacity:1}, 400);
	else
		$('#press_'+_id).animate({opacity:0}, 500);				
}


// Exhibits roll over
var exhibitsOut = true;
function exhibitsRollover(_action, _boxId)
{
	if(_action == 'click' && exhibitsOut)
		exhibitsOut = false;
	else if(_action == 'click')
		exhibitsOut = true;	
	
	if(_action == 'over' || (_action == 'click' && $('#'+_boxId).css('height') == '143px'))
	{
		$('#'+_boxId).animate({height:$('#'+_boxId+'copy').height()+150}, 400);
		$('#press').css('height',$('#'+_boxId+'copy').height()+150+25);
	}
	else if($('#'+_boxId).css('height') != '143px' && exhibitsOut)
		$('#'+_boxId).animate({height:143}, 500);					
}


// Random squares
function randomSquares()
{
	var _total = homeCount;	
	if (document.getElementById('mainBody').offsetWidth >= 992)
		var width = (document.getElementById('mainBody').offsetWidth-50);
	else 
		var width = 992;
	
	// Vars	
	var row = Math.floor(width/155);
	var rows = Math.ceil(_total/row);
	var total = Math.floor((_total*0.30)/rows); 
	
	// Random
	var blue = new Object();
	for(i=1; i <= rows; i++) 
	{ 
		var to = (i*row)-row;
		var from =  row*i;
		for(r=1; r <= total; r++) 
		{
			var randomNum = Math.floor(Math.random() * (to - from + 1) + from); 
			blue[randomNum] = true;
		}
	}
		
	// Set	
	var i=1;	
	while(document.getElementById('home_highlight_'+i))
	{
		if(blue[i])
		{
			if(document.getElementById('home_latest_'+i))
				document.getElementById('home_latest_'+i).className = "latest blue";
			document.getElementById('home_highlight_'+i).src = document.getElementById('home_highlight_'+i).src.replace('green.jpg','blue.jpg');
		}
		else
		{
			if(document.getElementById('home_latest_'+i))
				document.getElementById('home_latest_'+i).className = "latest gree";
			document.getElementById('home_highlight_'+i).src = document.getElementById('home_highlight_'+i).src.replace('blue.jpg','green.jpg');
		}
		i++;
	}	
}




// Open Intro
var introTimeOut = null;
function loadIntro(_width, _height)
{
	//document.getElementById('introimage').style.left = ((document.getElementById('mainBody').offsetWidth-_width)/2)+"px";	
	//document.getElementById('introimage').style.top = ((document.getElementById('mainBody').offsetHeight-_height)/2)+"px";
	
	document.getElementById('backgroundImage').style.width = document.getElementById('mainBody').offsetWidth+"px";
	document.getElementById('backgroundImage').style.height = "968px";	

	// Set
	var width = document.getElementById('mainBody').offsetWidth;
	var height = document.getElementById('mainBody').offsetHeight;
	var imageWidth = Math.ceil(_width*(height/_height));
	var imageHeight = Math.ceil(_height*(width/_width));
		
	// Size
	if (width <= height || imageHeight <= height) 
	{
		document.getElementById('backgroundImage').style.height = height+"px";
		document.getElementById('backgroundImage').style.width = imageWidth+"px";	
	} else {
		document.getElementById('backgroundImage').style.width = width+"px";
		document.getElementById('backgroundImage').style.height = imageHeight+"px";
	}
		
	// Position
	if (width < imageWidth) 
		document.getElementById('backgroundImage').style.left = ((imageWidth-width)/-2)+"px";
	else 
		document.getElementById('backgroundImage').style.left = 0;
		
	if (height < imageHeight) {
		document.getElementById('backgroundImage').style.top = ((imageHeight-height)/-2)+"px";
	} else {
		document.getElementById('backgroundImage').style.top = 0;
	}
	
	$('#introimage').css('visibility','visible');
	introTimeOut = setTimeout('closeIntro()',3000);	
}


// Clost Intro
function closeIntro()
{	
	clearTimeout(introTimeOut);
	
	// Set Home
	$('#header').show();
	$('#home').show();
	$('#cover').css('height','auto'); 	
	var i = 1;
	randomSquares();
	while(document.getElementById('home_block_'+i))
	{
		$('#home_block_'+i).show();
		$('#home_block_'+i).css('opacity',1);											
		i++;
	}	
	homeRollOverHold = false;
	initialLoad = true;
	currentContent = 'home';
	$('#cover').css('overflow','visible');
	$('#cover').hide();	 
	$('#cover').fadeIn(2000);
	
	
	$('#introimage').animate({left:50,top:150,opacity:0.0}, 1500,function(){$('#introimage').hide();});
	$('#introimage img').animate({width:143,height:143}, 1500);
}

// Home
var homeRollOverHold = true;
function loadHome() 
{	
	// Animation
	function homeAnimation() 
	{
		currentContent = 'home';
		function loadThumbnails() 
		{
			var countId = 1;
			while(document.getElementById('home_block_'+countId))
			{
				if($('#home_block_'+countId)) 
				{
					$('#home_block_'+countId).css('top',Math.floor(Math.random() * (600 - -500 + 1) + -600));
					$('#home_block_'+countId).css('left',Math.floor(Math.random() * (200 - -600 + 1) + -200));
					$('#home_block_'+countId).show();			
					
					if(!document.getElementById('home_block_'+(countId+2)))
					{
					$('#home_block_'+countId).delay((countId*50)-(countId*50)).animate({left:0,top:0,opacity:1}, 1000,'easeOutSine',function() {
								homeRollOverHold = false;						
								fixedWindow(); 
								$('#cover').css('height','auto'); 
								$('#cover').css('overflow','visible');					
							})
					}
					
					else 
						$('#home_block_'+countId).delay((countId*50)-(countId*50)).animate({left:0,top:0,opacity:1}, 1000,'easeOutSine',function() {});
				}
				countId++;
			}
			
			// Hash
			window.location.hash = '';
		}	
			
		$('#header').fadeIn(2000);
		homeRollOverHold = true;	
		loadThumbnails();
		
	}
	
	// Hash redirect
	if((document.getElementById(hash[0]) || hash[1]) && initialLoad)
	{
		initialLoad = false;
		currentContent = 'home';
		$('#header').fadeIn();	
		fixedWindow(); 
		$('#cover').css('height','auto'); 
		$('#cover').css('overflow','visible');		
	}
	
	// Start
	else if(currentContent == null) 
	{
		randomSquares();
		homeAnimation();
	}
	
	// content
	else 
	{
		$('#menu .selected').removeClass('selected');
		if(currentContent == 'contact' || currentContent == 'ourstory')
			$('#contactbackground').fadeOut('normal');
		if(currentContent != 'home')
		{
			$('#'+currentContent).fadeOut('normal', function() {
					currentContent = 'home';
					fixedWindow();
					randomSquares();
					
					// Animation
					$('#cover').css('height','100%'); 
					$('#main').css('overflow','visible'); 
					$('#cover').css('overflow','hidden');
					var i = 1;
					while(document.getElementById('home_block_'+i))
					{
						$('#home_block_'+i).css('opacity',0);											
						i++;
					}
					$('#home').show(); 
					homeAnimation();
			});	
		}
	}	 
}


// Load client
function loadClient(_type,_id,_buttons) 
{	
	videoCurrent = null;	
	$('#'+currentContent).fadeOut('normal', function() {
		
		if(_buttons)
		{
			$('#clientArrowRight').show();
			$('#clientArrowLeft').show();
		}
		
		else 
		{
			$('#clientArrowRight').hide();
			$('#clientArrowLeft').hide();			
		}
		
		currentContent = 'client'
		$('#cover').css('overflow','visible');
		window.scrollTo(0,0);
		currentClient = _id;
		
		var id = null;
		if(_type == 'clients')
			id = _id;
		else 
			id = clientsIds[_id].id;
						
		// Load
		$.ajax({
			type: 'GET',
			dataType: 'json',
			url: '/designedit_inc/json.php',
			data: "type=home&id="+id,
			success: function (data) 
					{					
						window.location.hash = 'client/'+data.url;	
							
						document.getElementById('clientImages').style.width = (data.width+(11*data.images.length)+95)+"px";
						document.getElementById('main').style.width = (data.width+(11*data.images.length)+95)+"px";
						
						$('#clientName').html(data.title);
						$('#clientCopyInsert').html(data.description);		
						
						var html = '';
						for(i in data.images)
						{
							if(data.images[i].type == 'video')
							{
							html += '<div class="button">';
								html += '	<div class="video" id="video_'+i+'" style="width:'+data.images[i].width+'px; height:'+data.images[i].height+'px"></div>';
								html += '	<img src="'+data.images[i].image+'" width="'+data.images[i].width+'" height="'+data.images[i].height+'" onclick="loadVideo(\'video_'+i+'\',\''+data.images[i].video+'\')"   />';
								html += '</div>';
							}								
							else
								html += '<div><img src="'+data.images[i].image+'" width="'+data.images[i].width+'" height="'+data.images[i].height+'"  /></div>';				
							
						}
						html += '<span class="clear"></span>'; 
						$('#clientImages').html(html);	
						
						$('#client').fadeIn('normal');
					}
			});
	});	
}


// Previous client
function previousClient()
{
	currentClient--;
	if(currentClient < 0)
		currentClient = clientsIds.length-1;
	
	loadClient('home',currentClient,true); 
}


// Next
function nextClient()
{
	currentClient++;
	if(currentClient == clientsIds.length)
		currentClient = 0;
	loadClient('home',currentClient,true);
}


// Load content
function loadContent(_object,_id) 
{	
	if(currentContent != _id)
	{
		$('#cover').css('overflow','hidden');
		$('#menu .selected').removeClass('selected');		
		$(_object).addClass('selected');
		if(currentContent == 'contact' || currentContent == 'ourstory')
			$('#contactbackground').fadeOut('normal');
		if(_id == 'press')	
		{
			exhibitsOut = true;
			exhibitsRollover('','exhibits');
			exhibitsRollover('','awards');
		}		
		$('#'+currentContent).fadeOut('normal', function() {
				window.location.hash = _id;				
				if(currentContent == 'client')
				{
					loadVideo(null,null);
					window.scrollTo(0,0);
					currentContent = _id;
					fixedWindow();
				}
				currentContent = _id;				
				$('#'+currentContent).fadeIn('normal');					
				if(_id == 'contact')
				{
					$('#contactbackground').css('backgroundColor','#75D0F6');
					$('#contactbackground').fadeIn('normal');
				}				
				else if(_id == 'ourstory')
				{
					$('#ourstoryLine').html(ourstoryLineText[Math.floor(Math.random()*ourstoryLineText.length)]);
					$('#ourstoryHeight').css('height',Math.max(document.getElementById('ourstoryLine').offsetHeight+100,document.getElementById('ourstoryTextHeight').offsetHeight+100));
					$('#contactbackground').css('backgroundColor','#c7e5d3');	
					$('#contactbackground').fadeIn('normal');
				}
				else if(_id == 'clients')
				{
					var i=0;
					var maxHeight = 0;
					while(document.getElementById('clientsColumn_'+i))
					{
						maxHeight = Math.max(document.getElementById('clientsColumn_'+i).offsetHeight,maxHeight);
						i++;
						
					}
					var i=0;
					while(document.getElementById('clientsColumn_'+i))
					{
						$('#clientsColumn_'+i).css('height',maxHeight);
						i++;
					}
				}
		});
	}
}


// Video
var videoCurrent = null;
var videoplayer = new videoControlls('videoplayer','latest_1');
function loadVideo(_objectId,_filename)
{		
	// Close
	if(videoCurrent != null)
	{
		videoplayer.stop();
		$('#'+videoCurrent).html('');		
		$('#'+videoCurrent).hide();
	}
		
	// Play
	if(_filename != null)
	{
		$('#'+_objectId).show();
		videoplayer.videoHolderId = _objectId;
		videoplayer.autoSetPlayerType();
		videoplayer.play(_filename);		
		videoCurrent = _objectId;
	}
}


// Onload hash
function onLoadHash()
{	
	// Load
	$.ajax({
		type: 'GET',
		dataType: 'json',
		url: '/designedit_inc/json.php',
		data: "type=settings",
		success: function (data) 
		{		
			// Data
			clientsIds = data.clientsids;
			ourstoryLineText = data.ourstory
			
			// Content
			if(document.getElementById(hash[0]) && hash[0] != 'client')
				loadContent(document.getElementById('menu_'+hash[0]),hash[0]);
			
			// Client
			else if(hash[1])	
			{
				for(i in clientsIds)
				{
					if(hash[1] == clientsIds[i].url)
					{
						loadClient('clients',clientsIds[i].id,false);
						break;	
					}					
				}				
			}			
		}		
	});			
}
