// Header panels
var headerPanelsCurrentId = 0;
function headerPanels(_action,_id) {
	
	// Autoscroll
	headerPanelsLoop('stop'); 	
	
	// De-Select
	if(_id != null)
		$('#panels_nav_'+headerPanelsCurrentId).removeClass('selected');
	
	// Select
	if(_id != null)
		$('#panels_nav_'+_id).addClass('selected');
		
	
	// Fadein Info
	if((_action == 'info' && (headerPanelsCurrentId != _id || $('#panels_info').height() == 0)) || ((_action == '' || _action == 'onView' || _action == 'onClose') && headerPanelsCurrentId != _id && $('#panels_info').height() > 0))  
	{
		$('#info').fadeOut('fast', function(){
					
			// Load
			$.ajax({
					type: 'GET',
					dataType: 'json',
					url: '/designedit_inc/whatsnew/'+work.data[_id].id+'/spot.json',
					success: function (data) {
						var info = '<h2>'+data.title+'</h2>';
							if(data.info != '') 
								info += '<p>'+data.info+'</p>';
						$('#info').html(info);	
						$('#panels_info').animate({height:$('#info').height()+42},'normal');
						$('#clostButton').animate({bottom:0},'fast');
						$('#info').fadeIn('fast');	
					}
				});	
		});	
	}
	
	// Fadeout Info
	else if(_action == 'close' || (_action == 'info' && headerPanelsCurrentId == _id)) 
	{
		$('#panels_info').animate({height:0},'normal');
		$('#clostButton').animate({bottom:-18},'normal');		
	}
	
	// Swich panels
	if(_action == '' || _action == 'onView' || _action == 'onClose')
	{
	
		// Clear work
		if(_action == '') 
		{
			work.close(null);
			$('#panels_player').hide();
		}
		
		if(_action == 'onView' && work.mediaCurrentType == 'video') 
		{
			$('#panels_'+headerPanelsCurrentId).hide();	
			$('#panels_'+_id).hide();
		}
			
		else if(headerPanelsCurrentId != _id || document.getElementById('panels_'+_id).style.display == 'none') 
		{	
			if($('#panels .active').length > 0) 
			{
				$('#panels .active').fadeOut('fast', function() {
						$('#panels .active').removeClass('active');
						$('#panels_'+_id).fadeIn('fast').addClass('active');		
				});	
			}
				
			else 
			{
				$('#panels_'+_id).fadeIn('fast').addClass('active');				
			}
		}
		
	}
	
	// Set current
	if(_id != null)
		headerPanelsCurrentId = parseInt(_id);
		
}

// Header onView
function headerPanelsOnView(_currentId,_id) 
{	
	if(_id != null)
	{
		if(!document.getElementById('panels_player_'+_id)) 
			$('#panels_player').show();				
		
		headerPanels('onView',_id);
	}
}	

// Header onClose
function headerPanelsOnClose(_id) 
{		
	$('#panels_player').hide();
	headerPanels('onClose',_id);
}	

// Header Panels Loop
var headerPanelsTimeOut = null;
function headerPanelsLoop(_action) 
{
	if(_action == 'stop') 
	{
		clearTimeout(headerPanelsTimeOut);
	}
	
	else 
	{
		var id = headerPanelsCurrentId+1;
		if(id == work.length)
			id = 0;
		headerPanels('',id);
		headerPanelsTimeOut = setTimeout('headerPanelsLoop(null)',5000);	
	}
}

// Client Login
function clientLogin(action) 
{ 
	
	// Toggle
	if(action == 'toggle') 
	{
		
		// Clear Input
		$('#username').val('');
		$('#password').val('');
		
		// Toggle
		if($('#login').height() == 0) 
			$('#login').animate({height:84},'fast');		
		else
			$('#login').animate({height:0},'fast');
	}
	
	// Submit	
	else if(action == 'submit') 
	{
		$.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('');
						}
					}
			});	
	}
	
}

// Work thumbnail roll overs
function workThumb(action,id) 
{
	
	$('#thumb_'+id).stop();
	
	if(action == 'over' && work.currentId != id) 
		$('#thumb_'+id).animate({marginTop:0}, 150, 'linear').addClass('open');
	else 
		$('#thumb_'+id).animate({marginTop:104}, 'normal', 'linear', function() { $('#thumb_'+id).removeClass('open');});
}

// Work control
function workControl(application) 
{
	
	// Elements
	this.overlay = 'overlay';
	this.lightbox = 'lightbox';
	this.navSlider = 'slider';
	this.nextButton = 'next';
	this.previousButton = 'previous';
	this.holder = 'holder';
	
	// Parameters
	var thisObject = this;
	this.application = application;
	this.type = 'video'
	this.data = null;
	this.currentId = null;
	this.length = 0;
	this.onload = null;
	this.mobile = false;
	this.onViewClear = false;
	this.device = null;
	
	// Lightbox
	this.islightbox = true;
	lightbox = false;
	autoscroll = true;
	iFrameId = null;
	
	// Navigation
	this.isNavigation = true;
	this.navPosition = 0;	
	this.navItems = 6;	
	this.navWidth = 96;
	this.navMargin = 6;
	
	// HTML 5
	this.html5Position = true;
	
	// Media
	mediaVideoSWF = '/_flash/video.swf?layout=hide';
	mediaVideoWidth = 880;
	mediaVideoHeight = 580;
	
	mediaAudioSWF = '/_flash/audio_small.swf?';
	mediaAudioWidth = 184;
	mediaAudioHeight = 134;
	
	this.mediaCurrentType = null;
	
	// Set video media 
	this.setVideoMedia = function(_swf,_width,_height) {
		mediaVideoSWF = _swf;
		mediaVideoWidth = _width;
		mediaVideoHeight = _height;
		
		if(!mediaVideoSWF.match(/\?/))
			mediaVideoSWF += '?';
	}
	
	// Set audio media 
	this.setAudioMedia = function(_swf,_width,_height) {
		mediaAudioSWF = _swf;
		mediaAudioWidth = _width;
		mediaAudioHeight = _height;
		
		if(!mediaAudioSWF.match(/\?/))
			mediaAudioSWF += '?';	
	}
	
	// Onload data
	$.ajax({
		type: 'GET',
		dataType: 'json',
		url: '/designedit_inc/'+this.application+'/list.json',
		success: function (data) 
				 {
					 
					// Set data
					thisObject.data = data;
					thisObject.length = data.length;
					
					// Set width
					if(thisObject.isNavigation) 
					{
						// Set Width
						$('#'+thisObject.navSlider).width((thisObject.navWidth+thisObject.navMargin)*data.length);									
						
						// Set navigation
						if(data.length <= thisObject.navItems) {
							if(data.length == '1') 
								$('#'+thisObject.navSlider).hide();
								
							$('#'+thisObject.nextButton).hide();
							$('#'+thisObject.previousButton).hide();	
						} 
						else {
							$('#'+thisObject.navSlider).show();
							$('#'+thisObject.nextButton).show();
							$('#'+thisObject.previousButton).show();
						}
					}
					
					// Onload
					if(thisObject.onload != null) {
						for(i in thisObject.data) 
							if(thisObject.data[i].url == thisObject.onload) 
									thisObject.view(i);									
					}
					
					// Hash
					else 
					{
						var hashUrl = window.location.hash.substring(1);
						if(hashUrl !='') {
							for(i in thisObject.data)  {
								if(thisObject.data[i].url == hashUrl) 
										thisObject.view(i);
													
							}
						}	
					}
					
				 }
		});	
		
	// Get height
	function getWindowHeight() 
	{
		var d = document;
		return Math.max(
			Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
			Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
			Math.max(d.body.clientHeight, d.documentElement.clientHeight)
		);
	}	
	
	// Get object x position
	function getX(obj)
	{
		var left = 0;
		if(obj.offsetParent)
			while(1) 
			{
			  left += obj.offsetLeft;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.x)
			left += obj.x;
		return left;
	}
	
	// Get objecy y position
	function getY(obj)
	{
		var top = 0;
		if(obj.offsetParent)
			while(1)
			{
			  top += obj.offsetTop;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.y)
			top += obj.y;
		return top;
	}
	
	// Load media
	function loadMedia(type,id,initial,filename,width,height) 
	{		
		
		// Mobile
		if(thisObject.mobile) 
		{
			// Audio
			if(type == 'audio' || filename.match(/\.mp3$/)) 
			{	
				
				// Object				
				var audioObject = document.getElementById('html5audio');
				
				// Inline
				if(audioObject.canPlayType('audio/mpeg')) 
				{
					
					// Set
					document.getElementById('html5audioHolder').style.display = "block"; 
				
					// Position
					if(thisObject.html5Position) 
					{
						$('#'+thisObject.holder).html('<div id="holder_audio"></div>');	
						document.getElementById('html5audioHolder').style.top = getY(document.getElementById('holder_audio'))+"px";
						document.getElementById('html5audioHolder').style.left = getX(document.getElementById('holder_audio'))+"px";	
					}
					
					// Play
					audioObject.src = '/'+filename;
					audioObject.play();
							
				} 
				
				// Url
				else 
				{
						location.href = '/'+filename;	
				}							
			}
			
			// Video
			else if(type == 'video') 
			{
				
				// Set media type
				thisObject.mediaCurrentType = 'video';
				
				var videoObject = document.getElementById('html5video');
								
				// Default
				if(!videoObject.canPlayType('video/quicktime')) {
					location.href = '/'+filename;						
				}
				
				// Tablet
				else if(thisObject.device == 'tablet') 
				{		
				
					$('#'+thisObject.holder).html('<div id="holder_video"></div>');	
					document.getElementById('html5videoHolder').style.top = getY(document.getElementById('holder_video'))+"px";
					document.getElementById('html5videoHolder').style.left = getX(document.getElementById('holder_video'))+"px";					
					
					videoObject.src = '/'+filename;
					videoObject.width = width;
					videoObject.height = height
					videoObject.play();		
				}
				
				// Phone
				else 
				{					
					var videoObject = document.getElementById('html5video');
					videoObject.src = '/'+filename;
					videoObject.width = width;
					videoObject.height = height
					videoObject.play();					
				}
			
			}				
		}
		
		// Flash Audio
		else if(type == 'audio' || filename.match(/\.mp3$/)) 
		{
			
			// Set by id or general
			if(document.getElementById(thisObject.holder+'_'+id)) 
				$('#'+thisObject.holder+'_'+id).html('<div id="holder_audio"><div id="flashholder"></div></div>');				
			else 
				$('#'+thisObject.holder).html('<div id="holder_audio"><div id="flashholder"></div></div>');
			
			// Load flash
			var flashvars = {};
			var params = {};				
				params.wmode = "transparent";
			var attributes = {};		
				attributes.name = "flashAudioPlayer";
				attributes.id = "flashAudioPlayer";	
			swfobject.embedSWF(mediaAudioSWF+"&file="+escape('/'+filename)+"&next="+escape('work.next()')+"&previous="+escape('work.previous()'), "flashholder", mediaAudioWidth, mediaAudioHeight, "10.0.0", false, flashvars, params, attributes);
		
		}
		
		// Flash Video
		else if(type == 'video') 
		{
			
			// Function call		
			if(document.getElementById('flashVideoPlayer')) {				
				getFlashMovie("flashVideoPlayer").loadMedia('/'+filename);			
			} 
			
			// Setup
			else 
			{				
				$('#'+thisObject.holder).html('<div id="holder_video"><div id="flashholder"></div></div>');
				var flashvars = {};
				var params = {};				
					params.wmode = "transparent";
					params.allowFullScreen = true;	
				var attributes = {};		
					attributes.name = "flashVideoPlayer";
					attributes.id = "flashVideoPlayer";	
					attributes.bgcolor = "#000000";	
				swfobject.embedSWF(mediaVideoSWF+"&file="+escape('/'+filename)+"&next="+escape('work.next()')+"&previous="+escape('work.previous()'), "flashholder", width, height, "10.0.0", false, flashvars, params, attributes);
			}
		}	
		
		// QuickTime
		else if(type == 'qt') 
		{ 
			// SET IFRAME
			if(iFrameId == null) 
			{
				iFrameId = 'frame_'+new Date().getTime()+Math.floor(Math.random()*11);
				var iFrame=document.createElement("IFRAME");
				iFrame.setAttribute("id",iFrameId);
				iFrame.setAttribute("name",iFrameId);
				iFrame.setAttribute("width",0);
				iFrame.setAttribute("height",0);
				iFrame.setAttribute("frameBorder",0);
				iFrame.setAttribute("scrolling",0);
				iFrame.setAttribute("allowtransparency",1);
				iFrame.style.visibility="hidden";
				document.getElementsByTagName('body')[0].appendChild(iFrame);
			}
			$('#'+thisObject.holder).html('<div id="embedQuickTime"></div>');
			QT_WriteOBJECT('/'+filename, width, height, '','AUTOPLAY','true','CONTROLLER','true','scale','ASPECT','QTSRCCHOKESPEED','movierate', 'bgcolor','000000','QTNEXT1','</_js/quicktimeloop.html> T<'+iFrameId+'>'); 
		}
							
	} 
	
	// Next
	this.next = function() 
	{
		if(this.currentId + 1 == this.length) 
			this.view(0);	
		else	
			this.view(this.currentId+1);		
	}
	
	// Previous
	this.previous = function() 
	{
		if(this.currentId == 0) 
			this.view(this.length-1);
		else	
			this.view(this.currentId-1);		
	}		
	
	// View	
	this.view = function (id) 
	{
		// Verify media
		if(!thisObject.data[id].file.match(/\.mp3$|\.flv$|\.mov$|\.mp4$/)) 
		{
			// On view function
			if(typeof this.onView != "undefined") 
				this.onView(this.currentId, null);	
			
			// Close
			this.close(id);
			
			// Break
			return;
		}
		
		// Set type		
		if(thisObject.data[id].file.match(/\.mp3$/)) 
		{
			if(this.mediaCurrentType != 'audio')
			{
				$('#'+this.holder).html('');
				if(document.getElementById(this.holder+'_'+this.currentId)) 	
					$('#'+this.holder+'_'+this.currentId).html('');
			}				
			this.mediaCurrentType = 'audio';			
		} else {
			if(this.mediaCurrentType != 'video')
			{
				$('#'+this.holder).html('');
				if(document.getElementById(this.holder+'_'+this.currentId)) 	
					$('#'+this.holder+'_'+this.currentId).html('');
			}
			this.mediaCurrentType = 'video';
		}
		
		// On view function
		if(typeof this.onView != "undefined") 
			this.onView(this.currentId, id);			
		
		// Clear Individual holder
		if(document.getElementById(this.holder+'_'+this.currentId)) 	
			$('#'+this.holder+'_'+this.currentId).html('');
			
		// Set current id
		this.currentId = parseInt(id);	
		
		// Clear
		if(this.mobile || this.onViewClear) 
			this.clear();
		
		// Mobile
		if(this.mobile && this.device != 'tablet') { 
			loadMedia(this.type,id,false,thisObject.data[id].file,mediaVideoWidth,mediaVideoHeight);					
		}
		
		// Browser
		else {
		
			// Set url;
			if(typeof thisObject.data[id].url != "undefined") 
				window.location.hash = thisObject.data[id].url;
			
			// Set type
			this.type = 'video';		
			
			// Scroll Position
			if(autoscroll)
				this.navPosition = Math.floor((parseInt(id))/this.navItems);
			
			// Load lightbox
			if(!lightbox && this.islightbox) 
			{
				if(autoscroll)
					this.setNavigation(false);	
				
				lightbox = true;
				$('#'+this.overlay).height(getWindowHeight());
				$('#'+this.lightbox).height(getWindowHeight());	
				$('#'+this.lightbox).fadeIn('normal',function(){
						loadMedia(thisObject.type,id,true,thisObject.data[id].file,mediaVideoWidth,mediaVideoHeight);			
					});	
			}
			
			// Load media
			else 
			{
				if(autoscroll)
					this.setNavigation(true);	
		
				loadMedia(this.type,id,false,thisObject.data[id].file,mediaVideoWidth,mediaVideoHeight);			
				
			}		
		}
	}
	
	// Close
	this.close = function (_id) 
	{	
		// Clear url
		window.location.hash = ' ';
		
		$('#'+this.holder).html('');
		if(document.getElementById(this.holder+'_'+this.currentId)) 	
			$('#'+this.holder+'_'+this.currentId).html('');	
		
		// Defaults
		autoscroll = true;
		lightbox = false;
		this.currentId = null;
		this.mediaCurrentType = null;		
			
		// On view function
		if(typeof this.onClose != "undefined" && _id != null) 
			this.onClose(_id);				
		
		// Clear
		this.clear();
		
		// Light box
		if(this.islightbox) 
			$('#'+this.lightbox).fadeOut();
				
	}
	
	// Clear
	this.clear = function() {
		
		// Tablet
		if(this.mobile) {
			
			// Video
			var videoObject = document.getElementById('html5video');
			videoObject.pause();
			videoObject.width = 0;
			videoObject.height = 0;				
			document.getElementById('html5videoHolder').style.top = "-2000px";     
			
			// Audio
			var audioObject = document.getElementById('html5audio');
			audioObject.pause();		
			document.getElementById('html5audioHolder').style.top = "-2000px";         				
		}
			
		// Holder
		$('#'+this.holder).html('');
	}
	
	// Nav
	this.setNavigation = function (animate) {
		if(this.isNavigation) 
		{ 		
			$('#'+this.nextButton).removeClass('off');
			$('#'+this.previousButton).removeClass('off');	
			
			var pos = this.navPosition*(this.navItems*(this.navWidth+this.navMargin))+1;
			
			if(animate) 			
				$('#'+this.navSlider).animate({left:pos*-1},'normal');
			else
				$('#'+this.navSlider).css({left:pos*-1});
			
			
			if(this.length <= (this.navPosition+1)*this.navItems) 
				$('#'+this.nextButton).addClass('off');
				
			if(this.navPosition == 0) 
					$('#'+this.previousButton).addClass('off');					
		}
	}
	
	// Nav next
	this.navNext = function()
	{
		if(this.length > (this.navPosition+1)*this.navItems) { 
			autoscroll = false;
			this.navPosition++;
			this.setNavigation(true);
		}
		
	}
	
	// Nav previous
	this.navPrevious = function()
	{
		if(this.navPosition > 0) { 
			autoscroll = false;
			this.navPosition--;
			this.setNavigation(true);		
		}
		
	}
	
}

// Image
function viewImage(_filename) {
	
	if(_filename == 'close') {
		$('#lightbox').fadeOut('normal');	
	}
	
	else {
		function getWindowHeight() 
		{
			var d = document;
			return Math.max(
				Math.max(d.body.scrollHeight, d.documentElement.scrollHeight),
				Math.max(d.body.offsetHeight, d.documentElement.offsetHeight),
				Math.max(d.body.clientHeight, d.documentElement.clientHeight)
			);
		}		
		$('#holder').html('<img src="'+_filename+'" width="880" height="580" />');
		$('#overlay').height(getWindowHeight());
		$('#lightbox').height(getWindowHeight());	
		$('#lightbox').fadeIn('normal');	
	}	
}

// Tool tip
function toolTip(title,FunctionObject,evt) {		
	
	// Object
	if(!document.getElementById('toolTip')) {
		var ObjectDIV = document.createElement('div'); 
		ObjectDIV.className = 'tool_tip';
		ObjectDIV.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(ObjectDIV);				  
	}
	
	// Set mouse out
	FunctionObject.onmouseout = new Function(' var Remove = document.getElementById("toolTip"); Remove.parentNode.removeChild(Remove); this.onmouseout=null; document.onmousemove = null');	
	
	// Scroll position
	function ScrollPosition() {
		var position = [0, 0];
		if (typeof window.pageYOffset != 'undefined')
		{
		position = [
		window.pageXOffset,
		window.pageYOffset
		];
		}
		else if (typeof document.documentElement.scrollTop
		!= 'undefined' && document.documentElement.scrollTop > 0)
		{
		position = [
		document.documentElement.scrollLeft,
		document.documentElement.scrollTop
		];
		}
		else if (typeof document.body.scrollTop != 'undefined')
		{
		position = [
		document.body.scrollLeft,
		document.body.scrollTop
		];
		}
		return position;
	}	
	
	// Set 
	if(document.getElementById('toolTip')) {
		document.getElementById('toolTip').innerHTML = title+'<div class="arrow"></div>';					
		document.getElementById('toolTip').style.display = "block";						
		var Scroll = ScrollPosition();	
		document.getElementById('toolTip').style.left=(evt.clientX-(document.getElementById('toolTip').offsetWidth/2)+Scroll[0])+"px";
		if (evt.clientX-15-document.getElementById('toolTip').offsetWidth < 10) {
			document.getElementById('toolTip').style.left=(evt.clientX+15)+"px";
		}							
		document.getElementById('toolTip').style.top=(evt.clientY-12-document.getElementById('toolTip').offsetHeight+Scroll[1])+"px";
		if (evt.clientY-document.getElementById('toolTip').offsetHeight < 0) {
			document.getElementById('toolTip').style.top = "10px";
		}
	}	
	
	// Mouse move
	document.onmousemove = toolTipMove;		
	function toolTipMove(evt) {
		evt=(evt)?evt:((window.event)?event:null);		
		var Scroll = ScrollPosition();	
		document.getElementById('toolTip').style.left=(evt.clientX-(document.getElementById('toolTip').offsetWidth/2)+Scroll[0])+"px";
		if (evt.clientX-15-document.getElementById('toolTip').offsetWidth < 10) {
			document.getElementById('toolTip').style.left=(evt.clientX+15)+"px";
		}	
		document.getElementById('toolTip').style.top=(evt.clientY-12-document.getElementById('toolTip').offsetHeight+Scroll[1])+"px";
		if (evt.clientY-document.getElementById('toolTip').offsetHeight < 0) {
			document.getElementById('toolTip').style.top = "10px";
		}
	}	
	
}

// Flash connect
function getFlashMovie(movieName) 
{
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

// Clock
function insertTimeDate(currentDate,_daylightsaving)
{
	var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	
	// time
	var thetime = new Date();
	
	// time/date
	var tHour=thetime.getUTCHours()-((_daylightsaving==1)?5:4);
	var tMin=thetime.getUTCMinutes();
	var tSec=thetime.getUTCSeconds();
	var day = thetime.getUTCDate();
  	var month = thetime.getUTCMonth();
	
	var tAmPm=" ";
	
	if (tHour < 0)
		tHour += 24;
	
	if (tHour>=12)
		tAmPm="pm";
	else
		tAmPm="am";
	
	if (tHour>=13)
		tHour-=12;
	
	if (tHour==0)
		tHour=12;
	
	if (tSec<10)
	 tSec="0"+tSec;
	
	if (tMin<10)
	 tMin="0"+tMin;
	
	if(document.getElementById('time'))
		document.getElementById('time').innerHTML  =   tHour+":"+tMin+" "+tAmPm+" (EST) on "+ months[month]+". "+day;
		
	setTimeout('insertTimeDate(null,'+_daylightsaving+')',1000);	
}

	
