var base_url='/';
var showHideContent;
//var showHideMenu;
var showHideLogin;
var ploading=false;
var mloading=false;
var mout=false;
var a;
var sa=false;
var t;
var l;

/////////////////////////////////////////////////////
window.onload=function(e){
	showHideContent = new fx.Combo('content', {height: true, opacity: true, duration: 200, transition:fx.circOut});
	//showHideMenu = new fx.Combo('subMenuHolder', {moveleft: true, height:false, opacity: true, duration: 250});
	showHideLoading = new fx.Combo('contentLoading', {height: false, opacity: true, duration: 50});
	showHideLogin = new fx.Height('login', {duration: 200, transition:fx.circOut});
	showHideLoading.toggle();
	showHideLogin.hide();
	new ajax(base_url+'page-content/login',{method: 'get', update: $('login')});
	t = new Array();
}

////////////////////////////////////////////////////
//content handling
function changeContent(location,e,main){
	if (ploading) return;
	if (e==a) return;
	if (e){
		$(e).style.color='#c1ee66';
		if (a) $(a).style.color='#444';
		if (main) if (sa){ $(sa).style.color='#444'; sa=null; emptyMenu();}
		a=e;
	} 
	ploading=true;
	showHideLoading.toggle();
	showHideContent.hide();
	setTimeout("new ajax('"+location+"', { method: 'get', update: 'content', onComplete: showContent, onFailure:errorContent});",300);
}

function changeMenu(menu,e){
	showHideLoading.toggle();
	if (mloading){
		$(e).style.color='#c1ee66';
		if (a) $(a).style.color='#444';
		setTimeout("changeMenu('"+menu+"','"+e+"');",50);
		return;
	}
	if (e==sa) return;
	$(e).style.color='#c1ee66';
	if (a) $(a).style.color='#444';
	sa=e;
	a=null;
	mout=true;
	setTimeout("new ajax('"+menu+"', { method: 'get', update: 'subMenu', onComplete: showMenu});",200);
}

/////////////////////////////////////////////////////
//helper functions

function showContent(){
	setTimeout("showHideLoading.hide();",350);
	setTimeout("showHideContent.toggle();",150);
	setTimeout("ploading=false;",350);
}

function errorContent(t){
	setTimeout("showHideLoading.toggle();",350);
	$('content').innerHTML='An error('+t.status+') occurred while attempting to load the page. Please try again later.';
	$('content').innerHTML +='<br /><br />\nthese are the transport children available for use in the error handler. (they are available in the completion handler too)<br />';
	for (i in t){
		$('content').innerHTML += '<br />\n '+i+' : '+t[i];
	}
	setTimeout("showHideContent.toggle();",150);
	setTimeout("ploading=false;",350);
}


function showMenu(){
	if (mloading){
		setTimeout("showMenu()",50);
		return;
	}
	c=0;
	//for (x in $('subMenu')) $('content').innerHTML += x+'<br />';
	l=$('subMenu').children || $('subMenu').childNodes;
	
	if ((l.length)&&(!mloading)){
		mloading=true;
		for (i=0;i<l.length;i++){
			sl = l[i].children || l[i].childNodes;
			for (j=0;j<sl.length;j++){
				if (sl[j].id){
					c++;
					t[c] = new fx.Combo(sl[j], {mover: true, height:false, opacity: true, duration: 250});
					t[c].hide();
					setTimeout("t["+c+"].toggle();",150*c);
				}
			}
		}
		setTimeout("mloading=false;showHideLoading.hide();",150*t.length);
	}else{
		setTimeout("showMenu()",200);
	}
}

function emptyMenu(){
	if (!mout) return;
	if (mloading){
		setTimeout("emptyMenu()",50);
		return;	
	}
	if (t.length){
		mloading=true;
		for (j=0;j<t.length;j++){
			if(t[j+1]) setTimeout("t["+(j+1)+"].toggle();",(150*j)+600);
		}
	}
	setTimeout("$('subMenu').innerHTML='';showHideLoading.hide();",(t.length*150)+950);
	setTimeout("mloading=false;",(150*t.length)+950);
}

function showLogin(){
	showHideLogin.toggle();
	if (!showHideLogin.now)
	 	setTimeout("$('login').getElementsByTagName('input')[0].focus();",200);
}

function contact(){
	if (!(/Safari/.test(navigator.userAgent))){ new ajax(base_url+'contact', { method: 'post', postBody: Form.serialize('contactForm'), update: 'contact_form', onFailure:errorForm}); return; }
	l=$('contactForm').children || $('contactForm').childNodes;
	for (i=0;i<l.length;i++){
		if (i==0) postString ='?';
		else postString +='&';
		postString += l[i].getAttribute('name')+'='+l[i].value;
	}
	new ajax(base_url+'contact', { method: 'post', postBody: postString, update: 'contact_form', onFailure:errorForm});
}

function errorForm(t){
	if (t.status==406) $('formWarning').innerHTML=t.responseText;
	else $('formWarning').innerHTML='An unknown error occurred, please try again in a moment.';
}
