// Fixed size controll
function setBackground(_width,_height) 
{		
	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;
	}
	
	// Set main menu
	document.getElementById('mainmenu').style.top = ((height-5-document.getElementById('mainmenu').offsetHeight)/2)+"px";
	
	
	// Set left
	if(width > 400+722+60+40)
	{
		document.getElementById('mainmenu').style.left  = (((width-400-722)/2)+722+60)+"px";
		document.getElementById('mainmenuScrollHack').style.left  = (((width-400-722)/2)+722+60)+"px";
		document.getElementById('main').style.left  = ((width-400-722)/2)+"px";	
	}
	
	else 
	{
		document.getElementById('mainmenu').style.left  = (722+60)+"px";
		document.getElementById('mainmenuScrollHack').style.left  = (722+60)+"px";
		document.getElementById('main').style.left  = "20px";		
	}	
}

// Set background image
function setBackgroundImage() 
{
	var randomId = Math.floor(Math.random()*backgroundImages.length);
	var image = new Image ();
	image.onload = function()
	{		
		var newObject = document.createElement('img');	
		newObject.id = 'backgroundImage';
		newObject.style.display = "none";		
		var oldObject = document.getElementById('backgroundImage');
		document.getElementById('backgroundImage').id = 'backgroundImage_old';		
		var parentObject=oldObject.parentNode
		parentObject.insertBefore(newObject,oldObject.nextSibling);		
		setBackground(backgroundImages[randomId].width,backgroundImages[randomId].height);	
		document.getElementById('backgroundImage').src = backgroundImages[randomId].file;		
		$('#backgroundImage').fadeIn('normal' , function() 
			{		
				var removeObject=document.getElementById('backgroundImage_old')
				removeObject.parentNode.removeChild(removeObject);
			}
		);
					
	}
	image.src = backgroundImages[randomId].file;	
}


// Set content
var contentCurrent = null;
function setContent(_navObject,_contentId)
{	

	if(videoCurrentHolder != null && _contentId != 'work')
	{
		videoplayer.stop();
		$('#'+videoCurrentHolder).html('');		
		$('#'+videoCurrentHolder).hide();
		$('#'+videoCurrentHolder+'_image').show();
		$('#'+videoCurrentHolder+'_copy').show();
		$('#'+videoCurrentHolder+'_close').hide();
		videoCurrentHolder = null;
	}
	
	if(_contentId != '' && contentCurrent != null)
		setBackgroundImage();

	if(_contentId == '')
	{
		$('#mainmenu .selected').removeClass('selected');
		$('#'+contentCurrent).fadeOut('normal');	
		contentCurrent = null;
	}
	
	else if(contentCurrent != _contentId)
	{
	
		// Set class
		$('#mainmenu .selected').removeClass('selected');
		$(_navObject).addClass('selected');
		
		if(contentCurrent != null)
		{
			$('#'+contentCurrent).fadeOut('normal', function() {
				
				if(contentCurrent == 'work')
				{
					$('#'+currentWork).hide();	
					currentWork = 'latestprojects';
					$('#workMenu .selected').removeClass('selected');
					$('#menu_'+currentWork).addClass('selected');
					$('#'+currentWork).show();					
				}
				
				$('#'+_contentId).fadeIn('normal');	
				if(_contentId != 'work')
					document.getElementById(_contentId).style.top = ((document.getElementById('mainBody').offsetHeight-document.getElementById(_contentId).offsetHeight+150)/2)+"px";
				contentCurrent = _contentId;
			});	
		}
		
		else 
		{
			$('#'+_contentId).fadeIn('normal');	
			if(_contentId != 'work')	
				document.getElementById(_contentId).style.top = ((document.getElementById('mainBody').offsetHeight-document.getElementById(_contentId).offsetHeight+150)/2)+"px";	
			contentCurrent = _contentId;	
		}
	
	}
	
	// Set path
	window.location.hash = _contentId;
}

// Set work
var currentWork = 'latestprojects';
function setWork(_id)
{	
	if(videoCurrentHolder != null)
	{
		videoplayer.stop();
		$('#'+videoCurrentHolder).html('');		
		$('#'+videoCurrentHolder).hide();
		$('#'+videoCurrentHolder+'_image').show();
		$('#'+videoCurrentHolder+'_copy').show();
		$('#'+videoCurrentHolder+'_close').hide();
		videoCurrentHolder = null;
	}
	
	$('#workMenu .selected').removeClass('selected');
	$('#menu_'+_id).addClass('selected');
	$('#'+currentWork).fadeOut('normal', function() {
			currentWork = _id;
			$('#'+_id).fadeIn('normal');		
	});	
	
	// Set path
	window.location.hash = 'work/'+_id;	
}


// Set spot
var videoCurrentHolder = null;
function loadVideo(_objectId,_filename,_url)
{		
	if(_objectId == 'close' || videoCurrentHolder != null)
	{
		videoplayer.stop();
		$('#'+videoCurrentHolder).html('');		
		$('#'+videoCurrentHolder).hide();
		$('#'+videoCurrentHolder+'_image').show();
		$('#'+videoCurrentHolder+'_copy').show();
		$('#'+videoCurrentHolder+'_close').hide();
	}
	
	if(_objectId != 'close')	
	{
		$('#'+_objectId).show();
		$('#'+_objectId+'_image').hide();
		$('#'+_objectId+'_copy').hide();
		$('#'+_objectId+'_close').show();
		videoplayer.videoHolderId = _objectId;
		videoplayer.autoSetPlayerType();
		videoplayer.play(_filename);
		
		videoCurrentHolder = _objectId;
		
		
		// Set path
		window.location.hash = 'work/'+currentWork+'/'+_url;	
	}
	
	else 
		videoCurrentHolder = null;
}

// Set video object
var videoplayer = new videoControlls('videoplayer','latest_1');
videoplayer.onEnded = function() {
						alert('test');
						videoplayer.stop();
						$('#'+videoCurrentHolder).html('');		
						$('#'+videoCurrentHolder).hide();
						$('#'+videoCurrentHolder+'_image').show();
						$('#'+videoCurrentHolder+'_copy').show();
						$('#'+videoCurrentHolder+'_hide').hide();
						videoCurrentHolder = null;
					  }
							 
// Onload video
var hash = window.location.hash.substring(1).split('/');
function onLoadHash()
{	
	if(document.getElementById(hash[0]))
		setContent(document.getElementById('mainmenu_'+hash[0]),hash[0]);
	
	if(document.getElementById(hash[1]))
	{
		$('#workMenu .selected').removeClass('selected');
		$('#'+currentWork).hide();	
		$('#menu_'+hash[1]).addClass('selected');
		currentWork = hash[1];
		$('#'+hash[1]).show();	
		window.location.hash = 'work/'+hash[1];			
	}
	
	if(hash[2])	
	{
		window.location.hash = 'work/'+currentWork+'/_'+hash[2];
		window.location.hash = 'work/'+currentWork+'/'+hash[2];			
	}
}


// Client login
function clientLogin() 
{ 	
	$.ajax({
		type: 'POST',
		dataType: 'text',
		data: {
			'username': $('#username').val(),
			'password': $('#password').val()
		},
		url: '/designedit/api_login.php',
		success: function (data) {
					if(data == 'Success') 
					{ 
						window.location.href = '/designedit'; 
					} 
					else 
					{
						$('#username').val(data);
						$('#password').val('');
					}
				}
		});
	return false;
}

