// Fixed size controll
function fixedWindow(_verifyId,_setId,_width,_height) 
{
	if (document.getElementById(_verifyId).offsetWidth > _width)
		document.getElementById(_setId).style.width = "100%";
	else 
		document.getElementById(_setId).style.width = _width+"px";
	
	if (document.getElementById(_verifyId).offsetHeight > _height)
		document.getElementById(_setId).style.height = "100%";
	else
		document.getElementById(_setId).style.height = _height+"px";		
}

// Window resize
function resizeWindow (_width,_height) 
{	
	var intWidth = screen.availWidth-10;
	var intHeight = screen.availHeight-10;
	
	if(intWidth > _width)
		intWidth = _width;
		
	if(intHeight > _height)
		intHeight = _height;	
	
	var intLeft = (screen.availWidth / 2) - (intWidth / 2 );
	var intTop = (screen.availHeight / 2) - (intHeight / 2 );
	
	window.moveTo(intLeft,intTop); 
	window.resizeTo(intWidth,intHeight);
}

// Set content
var contentCurrent = 'home';
var currentEditorsListing = null;
function setContent(_navObject,_contentId)
{	
	// Content switch
	$('#mainmenu .selected').removeClass('selected');
	
	if(_navObject != null)
		$(_navObject).addClass('selected');
	
	if(_contentId == 'home')
	{
		$('#logoBottom').fadeOut('normal', function() {
				$('#linksBottom').fadeIn('normal');
		});		
		
	}
	
	else 
	{
		$('#linksBottom').fadeOut('normal', function() {
					$('#logoBottom').fadeIn('normal');
			});	
	}
		
	// Set new
	function setNew()
	{		
		if(_contentId == 'vfx' || _contentId == 'colour')
		{
			$('#playerTitle').addClass('center');
			$('#playerInformation').addClass('center');
			playSpot(reelData[_contentId].video,reelData[_contentId].title,reelData[_contentId].overview,reelData[_contentId].width,reelData[_contentId].height);
			
		}
		
		else 
		{
			if(_contentId == 'editors') 
				$('#'+_contentId).html(currentEditorsListing);
			if(_contentId == 'home') 
				$('#newwork').fadeIn('normal');	
			$('#'+_contentId).fadeIn('normal');			
		}
		
		contentCurrent = _contentId;	
	}
	
	// Open player
	if(spotCurrent != null)
	{
		videoplayer.stop();
		spotCurrent = null;
		$('#player').fadeOut('normal', function() {
				if(contentCurrent == 'home' && _contentId != 'home')
					$('#newwork').animate({width:33}, 400).fadeOut('normal');	
				else if(contentCurrent == 'home')
					$('#newwork').animate({width:33}, 400);
					
				if(contentCurrent == 'editors')
				{
					$('#'+contentCurrent).fadeOut('normal', function() {
							setNew();
					});		
				}
				
				else 
					setNew();
		});	
	}
	
	else {	
		if(contentCurrent == 'home')
			$('#newwork').fadeOut('normal');
		$('#'+contentCurrent).fadeOut('normal', function() {
				setNew();
		});		
	}
	
	// Set path
	window.location.hash = _contentId;
	
	// Editor fadeout
	if(contentCurrent == 'editors' && editorCurrent != null) 
	{
		// Stop video
		if(spotCurrent != null) 
		{
			videoplayer.stop();
			spotCurrent = null;
		}
		
		// Fadeout/reset
		$('#player').fadeOut('normal');
		$('#reel').fadeOut('normal', function() {
			$('#editors .selected').removeClass('selected');
			$('#reels .selected').removeClass('selected');
			$('#reel #thumbnails').show();
			editorCurrent == null;	
			reelCurrent = null;
			spotCurrent = null;
			editorData = null;
		});			
	}
}

// Set editor
var editorCurrent = null;
var editorData = null;
function setEditor(_navObject,_editorId)
{	
	// Remove selected
	$('#editors .selected').removeClass('selected');
	
	// New div
	var newDiv = document.createElement("div");
	newDiv.innerHTML = _navObject.innerHTML;
	newDiv.className = 'selected';
	newDiv.onclick = new Function ("setEditor(this,'"+_editorId+"')"); 	
	var nodeList = document.getElementById('editors');
	nodeList.insertBefore(newDiv,nodeList.getElementsByTagName("div")[0]);
		
	// Remove
	_navObject.parentNode.removeChild(_navObject);	
	$(_navObject).addClass('selected');	
	
	// Set path
	window.location.hash = _editorId;
	
	function setReels(_fadeIn) 
	{
		// Load
		$.ajax({
			type: 'GET',
			dataType: 'json',
			url: '/designedit_inc/editors.php?editor='+_editorId,
			success: function (data) 
				{
					editorData = data;
					var reelMenu = '<span class="menu" onclick="setPlay(0)">PLAY REEL</span>';
					var initialReelId = null;
					for(var i in data)
					{
						reelMenu += '<span class="break">/</span>';
						reelMenu += '<span class="menu" onclick="setReel(this,\''+i+'\')" id="reel_'+i+'" >'+data[i].title+'</span>';
						
						if(initialReelId == null)
							initialReelId = i;
						else if(onLoadHash[1] && i == onLoadHash[1])
						{
							initialReelId = i;
							editorCurrent = _editorId;
							onLoadHash[0] = null;
							onLoadHash[1] = null;
							window.location.hash = _editorId+'/'+initialReelId;							
						}
					}
					$('#reels').html(reelMenu);	
					$('#reels #reel_'+initialReelId).addClass('selected');
					$('#reel #thumbnails').show();
					reelCurrent = null;
					setReel(document.getElementById('reel_'+initialReelId),initialReelId);					
					if(_fadeIn)
						$('#reel').fadeIn('normal');
				}
		});	
	}
	
	if(editorCurrent != null)
	{ 
		if(spotCurrent != null)
		{
			videoplayer.stop();
			$('#player').fadeOut('normal');
			spotCurrent = null;
		}			
		
		$('#reel').fadeOut('normal', function() {
				setReels(true);		
		});
	}
	
	else 
		setReels(true); 
	
	editorCurrent = _editorId;
}


// Set reel
var reelCurrent = null;
function setReel(_navObject,_reelId)
{	
	if(reelCurrent != _reelId) 
	{		
		// Set thumbnails
		function setThumbnails(_fadeIn) 
		{
			var onLoadVideoId = null;
			var thumbnails = '';
			for(var i in editorData[_reelId].videos)
			{
				thumbnails += '<div class="thumb" >';
				thumbnails += '		<img src="'+editorData[_reelId].videos[i].thumb+'" width="160" height="117"  onclick="setPlay('+i+')"  />';
				thumbnails += '     <div class="hover" onclick="setPlay('+i+')" id="thumb_'+i+'"></div> '; 
				thumbnails += '     <div class="title">'+editorData[_reelId].videos[i].title+'</div>';
				thumbnails += '     <div class="buttons">';
				thumbnails += '        <div onclick="setPlay('+i+')">PLAY</div>';
				thumbnails += '         <a href="/download/'+editorCurrent+'/'+_reelId+'/'+i+'">DOWNLOAD</a>';
				thumbnails += '     </div>';                         
				thumbnails += '</div> ';
				
				if(onLoadVideoId == null && onLoadHash[2] && editorData[_reelId].videos[i].url == onLoadHash[2])
					onLoadVideoId = i;
				
			}
			thumbnails += '<div class="clear"></div>';
			$('#thumbnails').html(thumbnails);
			
			if(onLoadVideoId != null)
			{
				$('#reel #thumbnails').hide();
				reelCurrent = _reelId;
				window.location.hash = editorCurrent+'/'+_reelId+'/'+onLoadHash[2];					
				onLoadHash[2] = null;
				setPlay(onLoadVideoId);
			}
			
			if(_fadeIn)			
				$('#reel #thumbnails').fadeIn('normal');
		}		
		
		// Set nav
		$('#reels .selected').removeClass('selected');
		$(_navObject).addClass('selected');
		
		// Close player
		if(spotCurrent != null)
		{ 
			videoplayer.stop();
			spotCurrent = null;
			$('#player').fadeOut('normal', function() 
										{
											setThumbnails(true);
											spotCurrent = null;
										});
		}
		
		// Swich categories
		else if(reelCurrent != null)
		{ 			
			// Set path
			window.location.hash = editorCurrent+'/'+_reelId;
			
			$('#reel #thumbnails').fadeOut('normal', function() 
											{
												setThumbnails(true);
											});
		}
		
		// New editor
		else
			setThumbnails(false);		
		
		reelCurrent = _reelId;
	}
}

// Set spot
var spotCurrent = null;
function setPlay(_spotId)
{		
	// Close
	if(_spotId == -1) 
	{
		// Set path
		window.location.hash = editorCurrent+'/'+reelCurrent;
			
		videoplayer.stop();
		$('#player').fadeOut('normal', function() {
				$('#reel #thumbnails').fadeIn('normal');
				spotCurrent = null;
		});		
	}
	
	// Play
	else
	{ 
		// Set player
		function setPlayer(_fadeIn) 
		{	
			// Set path
			window.location.hash = editorCurrent+'/'+reelCurrent+'/'+editorData[reelCurrent].videos[_spotId].url;
			
			// Set close
			document.getElementById('playerCloseButton').onclick = function(){ setPlay(-1,null,null,null,null); };
						
			// Set end
			videoplayer.onEnded = function() {
									if(spotCurrent+1 < editorData[reelCurrent].videos.length)
										setPlay(spotCurrent+1);
									else 
										setPlay(-1);
									}
									
			videoplayer.onDownload = function(filename) {
								location.href = '/download/'+editorCurrent+'/'+reelCurrent+'/'+spotCurrent;
							 }	
							 											
			// Set position	
			$('#playerTitle').removeClass('center');
			$('#playerInformation').removeClass('center');				
			$('#player').css('margin-top',100);
			$('#player').css('left',460);
			$('#player').css('margin-left',0);
				
		
			// Set width/height
			if(editorData[reelCurrent].videos[_spotId].width > 0)
			{
				$('#playerHolder').css('width',editorData[reelCurrent].videos[_spotId].width);
				$('#playerHolder').css('height',editorData[reelCurrent].videos[_spotId].height);
				videoplayer.setSeekBar('scroller',editorData[reelCurrent].videos[_spotId].width-145);
				$('#scrollHolder').css('width',editorData[reelCurrent].videos[_spotId].width-145);
				$('#scroller').css('width',editorData[reelCurrent].videos[_spotId].width-145);
			}
			else 
			{
				$('#playerHolder').css('width',702);
				$('#playerHolder').css('height',360);
				videoplayer.setSeekBar('scroller',702-145);
				$('#scrollHolder').css('width',702-145);
				$('#scroller').css('width',702-145);					
			}
					
			$('#playerTitle').html(editorData[reelCurrent].videos[_spotId].title);
			
			var information = '';
					
			if(editorData[reelCurrent].videos[_spotId].agency !='')
				information += 'AGENCY: '+editorData[reelCurrent].videos[_spotId].agency+'<br />';
			if(editorData[reelCurrent].videos[_spotId].producer !='')	
				information += 'AGENCY PRODUCER: '+editorData[reelCurrent].videos[_spotId].producer+'<br />';
			if(editorData[reelCurrent].videos[_spotId].director !='')
				information += 'DIRECTOR: '+editorData[reelCurrent].videos[_spotId].director+'<br />';
			if(editorData[reelCurrent].videos[_spotId].company !='')
				information += ' PRODUCTION COMPANY: '+editorData[reelCurrent].videos[_spotId].company+'<br />';
				
			$('#playerInformation').html(information);	
			
			if(_fadeIn)
			{
				$('#player').fadeIn('normal', function() {
							videoplayer.play(editorData[reelCurrent].videos[_spotId].video);
						});
			}
					
			else 
				videoplayer.play(editorData[reelCurrent].videos[_spotId].video);	
		}
		
		if(spotCurrent == null)
		{ 
			$('#reel #thumbnails').fadeOut('normal', function() {
								setPlayer(true);
							});
		}	
		
		else 
			setPlayer(false);		
		
		spotCurrent = parseInt(_spotId);
	}
}




// Set spot
function playSpot(_filename,_title,_description,_width,_height)
{		
	// Close
	if(_filename == -1) 
	{
		// Set path
		window.location.hash = '';
			
		videoplayer.stop();
		spotCurrent = null;
		$('#player').fadeOut('normal', function() {
				$('#mainmenu .selected').removeClass('selected');
				contentCurrent = 'home';
				$('#home').fadeIn('normal');	
		});		
	}
	
	// Play
	else
	{ 
		function setPlayer(_fadeIn) 
		{		
			videoplayer.onEnded = null;
			
			videoplayer.onDownload = function(filename) {
				location.href = '/download/content/'+contentCurrent;
			}	
			
			document.getElementById('playerCloseButton').onclick = function(){ playSpot(-1,null,null,null,null); };
		
			$('#player').css('margin-top',80);
			$('#player').css('left','50%');
			$('#player').css('margin-left',Math.ceil(_width/-2));
		
			$('#playerHolder').css('width',_width);
			$('#playerHolder').css('height',_height);
			videoplayer.setSeekBar('scroller',_width-145);
			$('#scrollHolder').css('width',_width-145);
			$('#scroller').css('width',_width-145);					
			
			
			$('#playerTitle').html(_title);
			$('#playerInformation').html(_description);	
			
			if(_fadeIn)
			{
				$('#player').fadeIn('normal', function() {
							videoplayer.play(_filename);
						});
			}
			
			else 
				videoplayer.play(_filename);
		}
	
	
		if(spotCurrent == null)
			setPlayer(true);	
		
		else 
			setPlayer(false);
		
		spotCurrent = parseInt(_filename);
	}
}

// New work
function newWork(_action)
{
	if(_action == 'open')
	{
		if($('#newwork').css('width') == '278px')
			$('#newwork').animate({width:33}, 400);	
		else
			$('#newwork').animate({width:278}, 400);	
	}
		
	// Close
	else if(_action == 'close' || _action == -1)
	{
		$('#newwork').animate({width:33}, 400);	
		videoplayer.stop();
		spotCurrent = null;
		$('#logoBottom').fadeOut('normal', function() {
				$('#linksBottom').fadeIn('normal');
		});
		$('#player').fadeOut('normal', function() {				
				$('#home').fadeIn('normal');
				contentCurrent = 'home';	
		});
		
	}
		
		
	// Play
	else
	{ 
		// Set player
		function setPlayer(_fadeIn) 
		{	
			// Set path
			window.location.hash = 'newwork/'+reelData.newwork[_action].url;
			
			// Set close
			document.getElementById('playerCloseButton').onclick = function(){ newWork('close'); };
						
			// Set end
			videoplayer.onEnded = function() {
									if(spotCurrent+1 < reelData.newwork.length)
										newWork(spotCurrent+1);
									else 
										newWork(-1);
									}	
			
			// Download						
			videoplayer.onDownload = function(filename) {
				location.href = '/download/newwork/'+_action;
			}
													
			// Set position	
			$('#playerTitle').removeClass('center');
			$('#playerInformation').removeClass('center');	
			
			$('#player').css('margin-top',120);
			$('#player').css('left','47%');
			$('#player').css('margin-left',Math.ceil(reelData.newwork[_action].width/-2));
				
		
			// Set width/height
			if(reelData.newwork[_action].width > 0)
			{
				$('#playerHolder').css('width',reelData.newwork[_action].width);
				$('#playerHolder').css('height',reelData.newwork[_action].height);
				videoplayer.setSeekBar('scroller',reelData.newwork[_action].width-145);
				$('#scrollHolder').css('width',reelData.newwork[_action].width-145);
				$('#scroller').css('width',reelData.newwork[_action].width-145);
			}
			else 
			{
				$('#playerHolder').css('width',702);
				$('#playerHolder').css('height',360);
				videoplayer.setSeekBar('scroller',702-145);
				$('#scrollHolder').css('width',702-145);
				$('#scroller').css('width',702-145);					
			}
					
			$('#playerTitle').html(reelData.newwork[_action].title);
			
			var information = '';
					
			if(reelData.newwork[_action].agency !='')
				information += 'AGENCY: '+reelData.newwork[_action].agency+'<br />';
			if(reelData.newwork[_action].producer !='')	
				information += 'AGENCY PRODUCER: '+reelData.newwork[_action].producer+'<br />';
			if(reelData.newwork[_action].director !='')
				information += 'DIRECTOR: '+reelData.newwork[_action].director+'<br />';
			if(reelData.newwork[_action].company !='')
				information += ' PRODUCTION COMPANY: '+reelData.newwork[_action].company+'<br />';
				
			$('#playerInformation').html(information);	
			
			if(_fadeIn)
			{
				$('#player').fadeIn('normal', function() {
							videoplayer.play(reelData.newwork[_action].video);
						});
			}
					
			else 
				videoplayer.play(reelData.newwork[_action].video);	
		}
		
		if(spotCurrent == null)
		{ 
			$('#home').fadeOut('normal', function() {
								$('#linksBottom').fadeOut('normal', function() {
										$('#logoBottom').fadeIn('normal');
								});
								setPlayer(true);
							});
		}	
		
		else 
			setPlayer(false);		
		
		spotCurrent = parseInt(_action);
	}
}


// Set video object
var videoplayer = new videoControlls('videoplayer','playerHolder');
	videoplayer.defaultControlls = false;
	videoplayer.setSeekBar('scroller',425);
	videoplayer.onTogglePlay = function(_isPaused) {
								if(_isPaused)
									document.getElementById('playstop').innerHTML = 'PLAY';
								else 
									document.getElementById('playstop').innerHTML = 'PAUSE';
								}
	videoplayer.onEnded = function() {
									if(spotCurrent+1 < editorData[reelCurrent].videos.length)
										setPlay(spotCurrent+1);
									else 
										setPlay(-1);
								}
	videoplayer.onDownload = function(filename) {
								location.href = '/download/'+editorCurrent+'/'+reelCurrent+'/'+spotCurrent;
							 }	
							 
// Onload video
var onLoadHash = window.location.hash.substring(1).split('/');
var reelData = null;
function onLoadEditor()
{	
	// Load new work
	$.ajax({
	type: 'GET',
	dataType: 'json',
	url: '/designedit_inc/json.php',
	success: function (data) 
		{
			reelData = data;	
			currentEditorsListing = $('#editors').html();
			if(document.getElementById('editor_'+onLoadHash[0]))
			{
				$('#home').fadeOut('normal', function() 
					{	
						setContent(document.getElementById('mainmenu_editors'),'editors');
						setEditor(document.getElementById('editor_'+onLoadHash[0]),onLoadHash[0]);	
					});
			}	
			
			else if(document.getElementById('mainmenu_'+onLoadHash[0]))
			{
				setContent(document.getElementById('mainmenu_'+onLoadHash[0]),onLoadHash[0]);		
			}
			
			else if(onLoadHash[0] == 'newwork')
			{
				newWork('open');
				if(onLoadHash[1])
				for(var i in reelData.newwork)
				{
					if(reelData.newwork[i].url == onLoadHash[1])
						newWork(i);		
				}	
			}
			
			else 
				$('#newwork').delay(400).animate({width:278}, 400,function(){ $('#newwork').delay(400).animate({width:33}, 400); });
		}
	});
}

// Ini Load
function iniLoad()
{
	$('#home').fadeIn('slow');
	$('#mainmenu').fadeIn('slow');
	$('#newwork').fadeIn('slow');
	$('#linksBottom').fadeIn('slow');
	ScrollBarSet(true,'newwork',false);	
}

// Client login
function sendProjectLogin() 
{
	var user = document.getElementById('log_user').value;
	var pass = document.getElementById('log_pass').value;
	location.href='http://clientlogin.schoolediting.com/go/projects/logging/'+user+'/'+pass;	
	return false;	
}

