window.onload=function()
{
	massagelinks(phocus.DOM.getnodes('div#container').node(0));
	setup();
	gallerycontrol.setupgallery();
	updateforms();
	setuptwitter();
	
	// updating the ajax loader
	var throbber=phocus.DOM.getnodes('img#ajaxloader');
	if(throbber.length)
		throbber.set('alpha',0);
}

twitterparams={};
twitterparams.height=90;
function setuptwitter()
{
	var tw = phocus.DOM.getnodes('div.twitter div.ovf ul');
	if(tw.length)
	{
		twitterparams.ul = tw.node(0);
		twitterparams.timeout = setTimeout(scrolltwitter,5000);
	}
}
function scrolltwitter()
{
	// animate the body up
	var an = twitterparams.ul.animate({y: [0,-twitterparams.height,80,0,'inOutCirc']});
	an.onend(scrollcallbacks,'endscroll');
}
scrollcallbacks = {};
scrollcallbacks.endscroll = function()
{
	var li = twitterparams.ul.getnodes('li').node(0);
	var licopy = li.getcopy();
	
	li.deletenode();
	
	twitterparams.ul.addnode(licopy);
	twitterparams.ul.set('y',0);
	
	twitterparams.timeout = setTimeout(scrolltwitter,5000);
}

function updateforms()
{
	var forms=phocus.DOM.getnodes('form.ajax');
	forms.setevent('submit',submitajaxform);
}
function submitajaxform(e)
{
	// currently this functionality only supports inputs, so no selects, radios or check boxes.

	var form=phocus.DOM.getNode(this);
	var url=form.get('action');
	
	var inputs=form.getnodes('input');
	
	vars = {joinajax:'true'};
	for(var i=0;i<inputs.length;i++)
	{
		var op=inputs.node(i);
		
		var name=op.get('name');
		var val=op.get('value');
		var rel=op.get('rel');
		
		if(name == 'email')
		{
			var regex = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
			if(!regex.test(val))
			{
				alert('The email address you entered does not appear to be valid, please try again.');
				phocus.DOM.stopEvent(e);
				return false;
			}
		}
		
		vars[name]=val;
	}
	var ajax=new phocus.AJAX();
	ajax.post(url, vars, cbajaxform);

	phocus.DOM.stopEvent(e);
	return false;
}
function cbajaxform(result)
{
	var formdiv=phocus.DOM.getnodes('div#signupform').node(0);

	if(result == 'signedup')
	{
		formdiv.set('innerHTML','<p>Thanks for signing up to The Trish Nicol Agency mailing list</p>');
	}
}


// gallery objects and methods
gallerycontrol={};
gallerycontrol.setupgallery = function()
{
	var links=phocus.DOM.getnodes('ul.client-gallery-control li a');
	if(links.length > 0)
	{
		links.setevent('click',gallerycontrol.gogalleryimage);
	}
}
gallerycontrol.gogalleryimage = function(e)
{
	var container=phocus.DOM.getnodes('div.client-gallery').node(0);
	
	var obj = container.fadeout(10);
	obj.onend(gallerycontrol,'getgalleryimage',[this]);
	
	phocus.DOM.stopEvent(e);
	
	// updating the ajax loader
	var throbber=phocus.DOM.getnodes('img#ajaxloader');
	if(throbber.length)
		throbber.set('alpha',100);
}
gallerycontrol.getgalleryimage = function(value,button)
{
	var clicked=phocus.DOM.getNode(button);
	
	var url=clicked.get('href');
	var ajax=new phocus.AJAX();
	ajax.post(url, {ajaxget:1}, gallerycontrol.setgalleryimage);
}
gallerycontrol.setgalleryimage = function(content)
{
	var container=phocus.DOM.getnodes('div.client-gallery').node(0);
	container.set('innerHTML',content);
	
	var img=phocus.DOM.getnodes('div.client-gallery img').node(0);
	if(img)
	{
		var img=phocus.DOM.getnodes('div.client-gallery img').node(0);
		img.setevent('load',gallerycontrol.opengallery);
	} else
	{
		gallerycontrol.opengallery();
	}
}
gallerycontrol.opengallery = function()
{
	var container=phocus.DOM.getnodes('div.client-gallery').node(0);
	
	var obj = container.fadein(10);
	fadeupcontent();
}


// the hashing object
var hashobj={};
hashobj.currenthash='';
hashobj.rooturl='http://trishnicol.maxco.com.au';
hashobj.sanitiseurl=function(url)
{
	var url = url.replace(/^\s+|\s+$/g, '');
	url = url.replace(/[a-zA-Z]*:\/\/[^\/]+\//g, '');
	
	return url;
}

function setup()
{
	// set up the polling engine
	hashinterval=window.setInterval("gohash()", 500);
}
function gohash()
{
	// get the hash
	var hash=window.location.hash;
	hash=hash.replace(/#/,'');
	if(hash == hashobj.currenthash) return;
	
	gosectionbyurl(hash);
}
function sethash(hash)
{
	var hash = hashobj.sanitiseurl(hash);
	if(hash.charAt(hash.length-1)!='/')hash+='/';
	window.location.hash=hashobj.currenthash=hash;
	
	updatemenu();
}

function massagelinks(node)
{
	// navigation effects
	var nodes=node.getnodes('a');
	nodes.set('title','');
	var in_ani={colour: [0x55bcFF,0x424242,10,0,'linear'] };
	var out_ani={colour: [null,0xbcbcbc,10,0,'linear'] };
	var click_ani={colour: [0x000000,0xbcbcbc,5,0,'linear'] };
	
	for(var i=0;i<nodes.length;i++)
		if(nodes.node(i).get('className').indexOf('sel')!=-1 || nodes.node(i).get('className').indexOf('inactive')!=-1 || nodes.node(i).get('className').indexOf('nlinks') != -1)
			nodes.node(i).lock();
			
	
	nodes.setuprollinout(in_ani,out_ani);
	nodes.setupclick(click_ani);
	
	// lists
	var listitems=node.getnodes('ul.projects li');
	if(listitems.length)
	{
		listitems.set('cursor','pointer');
		listitems.setevent('click',
			function(e)
			{
				var me=phocus.DOM.getnodes(this);
				window.location = me.getnodes('a').get('href')[0];
				phocus.DOM.stopEvent(e);
			}
		);
		listitems.setevent('mouseover',
			function(e)
			{
				var me=phocus.DOM.getnodes(this);
				me.getnodes('a').set('className','hover');
			}
		);
		listitems.setevent('mouseout',
			function(e)
			{
				var me=phocus.DOM.getnodes(this);
				me.getnodes('a').set('className','');
			}
		);
	}
	
	// anchors
	nodes.setevent('focus',
		function()
		{
			this.blur();
		}
	)
	
	// ajax anchors
	var ajaxas=node.getnodes('a.ajax');
	ajaxas.setevent('click',
		function(e)
		{
			var me=phocus.DOM.getnodes(this);
			gosection(me.get('href'),me.get('id'),me.get('rev'));
			phocus.DOM.stopEvent(e);
		}
	)
}

contentobj={};
contentobj.ready=false;
contentobj.listener=null;
contentobj.url=null;
contentobj.listenermethod=function(t,rt)
{
	if(this.ready == true)
	{
		// get the body element
		var bod=phocus.DOM.getnodes('div#body div.bodyb').node(0);
		
		bod.set('innerHTML',this.content);
		bod.cleanupcache();
		
		// massage the new links
		massagelinks(phocus.DOM.getnodes('div#body').node(0));
		
		// setup any galleries
		gallerycontrol.setupgallery();
		
		// fade the content up
		fadeupcontent();
		
		return -1;
	} else
		return 100;
}

function gosection(url,id, type)
{
	contentobj.ready=false;

	fadeoutcontent(type);
	getcontent(hashobj.sanitiseurl(url));
	
	// set the window hash
	sethash(url.split(hashobj.rooturl).join(''));
//	sethash(id.split('_').join('/'));
}
function gosectionbyurl(url)
{
	contentobj.ready=false;

	fadeoutcontent('2');
	getcontent(hashobj.sanitiseurl(url));
	
	// set the window hash
	sethash(url.split(hashobj.rooturl).join(''));
//	sethash(url);
}
function fadeupcontent()
{
	// get the body element
	var bod=phocus.DOM.getnodes('div#body').node(0);
	
	// get the internal element height
	var bodchild=phocus.DOM.getnodes('div#body div.bodyb').node(0);
	var dim=bodchild.getdims();
	
	// animate the body up
	bod.animate({h: [null,dim.y,20,0,'inOutCirc']});
//	bod.set('h',dim.y-50);
	bod.animate({alpha: [null,100,20] });
	
	// updating the ajax loader
	var throbber=phocus.DOM.getnodes('img#ajaxloader');
	if(throbber.length)
		throbber.set('alpha',0);
}
function fadeoutcontent(type)
{
	var type = type ? type : 1;

	// get the body element
	var bod=phocus.DOM.getnodes('div#body').node(0);
	var dim=bod.getdims();
	
	// set the body height
	bod.set('h',dim.y);
	
	// animate the body down
	if(type == 1)
		bod.animate({h: [dim.y,1,20,0,'outCirc'], alpha: [100,0] });
	else if(type == 2)
		bod.animate({alpha: [100,0,5,0,'linear']});
	
	// the listener
	contentobj.listener=phocus.Runtime.addrun(contentobj,'listenermethod',null,(type==1 ? 20 : 5) ,100);
	
	// updating the ajax loader
	var throbber=phocus.DOM.getnodes('img#ajaxloader');
	if(throbber.length)
		throbber.set('alpha',100);
}
function getcontent(url)
{
	var url = '/'+url;
//	if(url == '') var url='/';
	contentobj.url=url;
	
	var ajax=new phocus.AJAX();
	ajax.post(url, {ajaxget:1}, contentready);
}
function contentready(c)
{
	contentobj.content=c;
	contentobj.ready=true;
	pageTracker._trackPageview(contentobj.url);
}
function updatemenu()
{
	var lis=phocus.DOM.getnodes('ul#menu li a');
	lis.unlock();
	lis.set('colour',0xbcbcbc);
	lis.delclass('sel');
	
	var prim=window.location.hash;
	prim=prim.split('/')[0];
	prim=prim.split('#')[1];
	
	lis = phocus.DOM.getnodes('ul#menu li a.'+prim);
	lis.addclass('sel');
	lis.set('colour',0x424242);
	lis.lock();
}