
/* 
Wait keeps multiple effects from running at same time,
Wait = 1 when effect running
Wait = 0 when idle */
window.wait = 0;

var W3CDOM = (document.createElement && document.getElementsByTagName);
var mouseOvers = new Array();
var mouseOuts = new Array();

window.onload = init;

// --------------------------------
//   History & Utility Functions  |
// --------------------------------

// Not used yet...
function historyChange(historyHash){
    if(historyHash==''){

    }else{   
		if(historyHash == 'who') {
			AjaxLoad('container','about.php');
		}
		if(historyHash == 'what') {
			AjaxLoad('container','projects.php');
		}
		if(historyHash == 'what') {
			AjaxLoad('container','contact.php');
		}
	}
}

function isset(varname)
{
  return(typeof(window[varname])!='undefined');
}

function AjaxLoad(div, page)
{
		var myAjax = new Ajax.Updater(div, page);
}

function SendMessage(another, email)
{
	if(another == 'another') {
		AjaxLoad('container','contact.php?r=no&email='+email);
	}
	if( $F('notecontent') != '' && $F('noteemail') != '' )
	{
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($F('noteemail'))) 
		{
			var post = 'notecontent='+$F('notecontent')+'&noteemail='+$F('noteemail');
			new Ajax.Request('submit.php', {method:'post', postBody:post});
			$('message').hide();
			$('contact-left').innerHTML='Your message has been sent!<br><a href="#" onclick="SendMessage(\'another\',\''+$F('noteemail')+'\');"><b>Send another message?</b></a>';
		}
		else {
			$('message').show();
			$('message').innerHTML='Please enter a valid email address';
		}
	}
	else {
		$('message').show();
		$('message').innerHTML='Please fill in all fields!';
	}

}

// ---------------------
//   FX & Transitions
// ---------------------

	/* Window.rapid is used to tell if PushUp and PushDown are called in rapic succession (<10ms), 
	this is caused by a bug when mousing over hidden DIV Edge. */ 

function NeonFade(name)
{
	$(name).morph('color: #646464;background:#FAFAFA;fontWeight:normal;',{duration:0.25});	
}

function NeonGlow(name)
{
	$(name).morph('color: #FFF;background:#FF117A;fontWeight:bold;', {duration:0.25});	
}
	
function PushUp() 
{
	window.rapid = 0;
	setTimeout('PushUpUtil();',30);
}

function PushUpUtil() 
{
	if($('recentprojects-hidden').visible() && window.rapid == 0 ) 
	{
		if($('recentprojects-hidden').visible() )
		Effect.BlindUp('recentprojects-hidden',{duration:0.2, queue: { position:'end', scope: 'toggle', limit:1 } });	
	}
}

function PushDown()
{
	window.rapid = 1;
	if(!$('recentprojects-hidden').visible() ) 
	{
		Effect.BlindDown('recentprojects-hidden',{duration:0.2, queue: { position:'end', scope: 'toggle', limit:1 } });	
	}
}


// Experimental transition (not used)
function ProjectDrop(selection, number)
{
	for (var i=1;i<=number;i++)
	{
		var divname = 'block' + i;
		if(i != selection) 
		{
			Effect.DropOut(divname);
		}
	}
}


// ---------------------
// Navigation FX Functions
// ---------------------


// Home Navigation
function RandomProjectShake(spot,pid)
{	
	if (window.wait == 0)
	{
		// Wait is set to one so function cannot initialize again
		window.wait = 1;
		var divname = 'completed' + spot;
		var loadtext = 'AjaxLoad(\'container\', \'project.php?id='+pid+'\')';
		
		Effect.Shake(divname, {queue: {position:'end', scope: 'menuxscope', limit:1} });
		Effect.BlindUp('container', { duration: 0.75 }, { queue: { position:'end', scope: 'menuxscope', limit:1 } });
		setTimeout(loadtext,750);
		
		// Wait is reset after 1.2 sec (transition = 0.75 sec) + buffer 
		setTimeout('window.wait=0;',1200);
		
	}
}

function RandomProject(pid)
{	
	if (window.wait == 0)
	{
		window.wait = 1;
		var loadtext = 'AjaxLoad(\'container\', \'project.php?id='+pid+'\')';
		Effect.BlindUp('container', { duration: 0.75 }, { queue: { position:'end', scope: 'menuxscope', limit:1 } });
		setTimeout(loadtext,750);	 
		setTimeout('window.wait=0;',1200);
	}
}

function ProjectTag(type)
{
	if (window.wait == 0)
	{
		window.wait = 1;
		Effect.BlindUp('container', { duration: 0.75 }, { queue: { position:'end', scope: 'menuxscope', limit:1 } });
		var loadtext = 'AjaxLoad(\'container\', \'projects.php?type='+type+'&view=all\')' 
		setTimeout(loadtext, 750);
		setTimeout('window.wait=0;',1200);
	}
}

function TopMenuNav(section)
{
	/* Uses innerhtml to renders faster */

	if (window.wait == 0)
	{
		window.wait = 1;
		if(section == 'who') 
		{
			var newTitle= "The Porfolio of Jack Lukic - About Me";
			Effect.BlindUp('container', { duration:0.75, queue: { position:'end', scope: 'menuxscope', limit:1} });
			setTimeout('AjaxLoad(\'container\' , \'about.php\')', 750);
			$('who').innerHTML = '<img src="images/navbar_who_down.gif" width="102"/>';
			$('what').innerHTML = '<a href="#" onclick="TopMenuNav(\'what\');" ><img src="images/navbar_what.gif" alt="What\'s He Do?" id="what_mouseover" width="117" /></a>';
			$('how').innerHTML = '<a href="#" onclick="TopMenuNav(\'how\');" ><img src="images/navbar_how.gif" alt="How Can We Get Him?" id="how_mouseover" width="173" /></a>';
			init();
			
		}
		if(section == 'what') 
		{
			var newTitle= "The Porfolio of Jack Lukic - Portfolio";
			Effect.BlindUp('container', { duration:0.75, queue: { position:'end', scope: 'menuxscope', limit:1} });
			setTimeout('AjaxLoad(\'container\' , \'projects.php\')', 750);
			$('what').innerHTML = '<img src="images/navbar_what_down.gif" width="117"/>';
			$('who').innerHTML = '<a href="#" onclick="TopMenuNav(\'who\');" ><img src="images/navbar_who.gif" alt="Who Is He?" id="who_mouseover" width="102" /></a>';
			$('how').innerHTML = '<a href="#" onclick="TopMenuNav(\'how\');" ><img src="images/navbar_how.gif" alt="How Can We Get Him?" id="how_mouseover" width="173" /></a>';
			init();
		}
		if(section == 'how') 
		{
			var newTitle= "The Porfolio of Jack Lukic - Contact";
			Effect.BlindUp('container', { duration:0.75, queue: { position:'end', scope: 'menuxscope', limit:1} });
			setTimeout('AjaxLoad(\'container\' , \'contact.php\')', 750);
			$('how').innerHTML = '<img src="images/navbar_how_down.gif" width="173"/>';
			$('who').innerHTML = '<a href="#" onclick="TopMenuNav(\'who\');" ><img src="images/navbar_who.gif" alt="Who Is He?" id="who_mouseover" width="102" /></a>';
			$('what').innerHTML = '<a href="#" onclick="TopMenuNav(\'what\');" ><img src="images/navbar_what.gif" alt="What\'s He Do?" id="what_mouseover" width="117" /></a>';
			init();
		}
		setTimeout('window.wait=0;',2050);
		
		//Add event to hash & rewrite title
		//unFocus.History.addHistory(section);
		//console.log('History Hash:' + section);

	}
}

// Project Navigation

function ProjectSelect(pid)
{
	if (wait == 0)
	{
		window.wait = 1;
		Effect.Fade('container', {duration: 0.5 }, {queue: {position:'end', scope: 'portxscope', limit:1} });
		setTimeout('AjaxLoad(\'container\', \'project.php?id='+pid+'\')', 500);
		setTimeout('window.wait=0;',1200);
	}
}


// --------------------
//   Mouseover Script
// --------------------

function init()
{
	
	// Initialize History Listener
	// this.historyListener=historyChange;
	// unFocus.History.addEventListener('historyChange', this.historyListener);
	// this.historyListener(unFocus.History.getCurrent());
	
	// Preload mousedown images
	image1 = new Image();
	image1.src = "images/navbar_what_down.gif";
	image2 = new Image();
	image2.src = "images/navbar_how_down.gif";
	image3 = new Image();
	image3.src = "images/navbar_who_down.gif";

	if (!W3CDOM) return;

	var imgs = document.getElementsByTagName('img');
	var img_cnt = imgs.length;
	for (var i=0;i<imgs.length;i++)
	{
		if(imgs[i].id.indexOf('mouseover') != -1) {
		imgs[i].onmouseover = mouseGoesOver;
		imgs[i].onmouseout = mouseGoesOut;
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		mouseOuts[i] = new Image();
		mouseOuts[i].src = imgs[i].src;
		mouseOvers[i] = new Image();
		mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_over" + suffix;
		imgs[i].number = i;
		}
	}

	var imgs = document.getElementsByTagName('input');
	for (var i=0;i<imgs.length;i++)
	{
		if(imgs[i].id.indexOf('mouseover') != -1) {
		imgs[i].onmouseover = mouseGoesOver;
		imgs[i].onmouseout = mouseGoesOut;
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		mouseOuts[i + img_cnt] = new Image();
		mouseOuts[i + img_cnt].src = imgs[i].src;
		mouseOvers[i + img_cnt] = new Image();
		mouseOvers[i + img_cnt].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_over" + suffix;
		imgs[i].number = i + img_cnt;
		}
	}
}

function mouseGoesOver()
{
	this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	this.src = mouseOuts[this.number].src;
}