// JavaScript Document

var xhr = false;

window.addEvent("domready", function() {
	ms.init();
	cl.checkURL();
	ad.init();
});
window.onload = function(){}
window.onunload = function(){};

// initialize page - main setup
ms={
	init:function(){
		if(!document.getElementById) return false;
		if($('navlist')){;
			ms.mainNavList = $('navlist');
			ms.mainNavLI = ms.mainNavList.getElementsByTagName("li");
			ms.mainNavA = ms.mainNavList.getElementsByTagName("a");
			ms.setNavLinks();
		}
		ms.setLogoBT();
	},
	setNavLinks:function(){
		ms.mainNavA = $('navlist').getElements('a');
		for(var i=0;i<ms.mainNavA.length;i++){
			ms.mainNavA[i].onclick = function(){
				// clean href
				var linkhref = this.getAttribute("href");
				var hashindex = linkhref.lastIndexOf("#");
				var sendhash = linkhref.substring(hashindex);
				// click functions
				cl.setCurrent(sendhash);
				cl.loadContent(sendhash);
			}
		}
	},
	setLogoBT:function(){
		/*if($('logo')){
			$('logo').addEvent('click',function(){
				cl.setCurrent("#news");
				cl.loadContent("#home");
				//return false;
			});
		}*/
	}
}

// content load
cl={
	checkURL:function(){
		if(location.hash.substring(1)){
			cl.setCurrent(location.hash);
			cl.loadContent(location.hash);
		}else if(!$('adminlist')){
			cl.setCurrent("#news");
			cl.loadContent("#home");
		}
	},
	setCurrent:function(senthash){
		if(senthash != "http://clients.mainframe.co.uk"){
		for(var i=0;i<ms.mainNavA.length;i++){
			// remove existing cur class
			if(DOMhelp.cssjs("check",ms.mainNavA[i],"cur")){
				DOMhelp.cssjs("remove",ms.mainNavA[i],"cur");
			}
			// clean href
			var linkhref = ms.mainNavA[i].getAttribute("href");
			var hashindex = linkhref.lastIndexOf("#");
			var sendhash = linkhref.substring(hashindex);
			// set new cur on sent link
			if(sendhash == senthash){
				DOMhelp.cssjs("add",ms.mainNavA[i],"cur");
			}
		}
		} // end if
	},
	loadContent:function(senthash){
		if(senthash.substring(0,1) == "#"){
			if(senthash.substring(1,8) == "project" && senthash != "#projects"){
				cl.url = "project.php?load=1&id=" + senthash.substring(8);
			}else if(senthash.substring(1,9) == "newspost"){
				cl.url = "newspost.php?load=1&id=" + senthash.substring(9);
			}else if(senthash.substring(1,10) == "presspost"){
				cl.url = "presspost.php?load=1&id=" + senthash.substring(10);
			}else if(senthash == "#home"){
				cl.url = "news.php?load=1";
			}else{
				cl.url = senthash.substring(1) + ".php?load=1";
			}
		}else{
			// other requests
			//alert("loadin something else");
			//cl.url = senthash;
		}
		
		if(window.XMLHttpRequest){
			xhr = new XMLHttpRequest();
		}else{
			if(window.ActiveXObject){
				try{
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e){}
			}
		}
		
		
		if(xhr){
			xhr.onreadystatechange = cl.showContents;
			xhr.open("GET",cl.url,true);
			xhr.send(null);
		}else{
			alert("couldn't create XMLHttpRequest");
		}
			
		return false;
	},
	showContents:function(){
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				if(window.location.hash){
					//checkMe(window.location.hash.substring(1));
					//pageTracker._trackPageview(window.location.hash.substring(1));
				}
				var loaderDiv = document.getElementById("contentarea");
				var outMsg = xhr.responseText;
				loaderDiv.innerHTML = outMsg;
				cl.initContent();
			}else{
				alert("There was a problem with the request: " + xhr.status + "\n\nThat page doesn't exist\nPlease double check the URL");
			}
		}
	},
	initContent:function(){
		if($('contentarea').getElement('.acctog')){
			cl.removeToggleLinks();
		}
		if($('contentarea').getElement('.acchide')){ // check hide object exists
			cl.commentsAcc = new Accordion('li.acctog', 'div.acchide', {
			start:'all-closed',
			opacity: false,
			duration: 300, /* default 500 */
			transition: Fx.Transitions.Quint.easeOut,
			alwaysHide: true,
			onActive: function(togglers, stretchers){
			},
			onComplete: function(){
			},
			onBackground: function(togglers, stretchers){
			}
			},$('contentArea'));
			cl.revealTempHidden();
		}
		if($('contentarea').getElement('.emailaddr')){ // there are email addresses
			cl.initemails();	
		}
		if($('contentarea').getElement('.leavecomm')){ // add a comment links
			cl.initcomments();
		}
		if($('contentarea').getElement('.viewmov')){ // movie links
			cl.initmovies();
		}
		if($('contentarea').getElement('.reelpics')){ // office pics exist
			cl.initreelpics();
			cl.reelGal = cl.rotateHouseReelPics.periodical(4000); // start first periodical
		}
		if($('contentarea').getElement('.officepics')){ // office pics exist
			cl.initofficepics();
			cl.londonGal = cl.rotateLondonOfficePics.periodical(4000); // start first periodical
			//offset 2nd set of images
			cl.countdownNorth = cl.startNorthOfficePics.periodical(2000);
		}
	},
	removeToggleLinks:function(){ // stops togglers changing the url
		cl.togglers = $$('.acctog');
		for(var i=0;i<cl.togglers.length;i++){
			cl.togglers[i].getElement('a').onclick = function(){
				return false;											  
			}
		}
	},
	revealTempHidden:function(){ // unhide accordion hidden objects (stops flashing at load time)
		cl.togreveals = $$('.acchide');
		for(var i=0;i<cl.togreveals.length;i++){
			DOMhelp.cssjs("remove",cl.togreveals[i],"temphide");
		}
	},
	initemails:function(){ // completes mainframe email addresses
		cl.emailaddrs = $$('.emailaddr');
		for(var i=0;i<cl.emailaddrs.length;i++){
			var emailname = cl.emailaddrs[i].get('text');
			var emailaddr = emailname + "@mainframe.co.uk";
			cl.emailaddrs[i].set('text', emailaddr);
			cl.emailaddrs[i].set('href', "mailto:"+emailaddr);
		}
	},
	initcomments:function(){ // sets comment link to open form
		cl.leavecomms = $$('.leavecomm');
		for(var i=0;i<cl.leavecomms.length;i++){
			cl.leavecomms[i].addEvent('click',function(){
				// clean href
				var linkhref = this.getAttribute("href");
				cl.openwindow(linkhref, "comments", 568, 350);
				return false;
			});
		}
	},
	initmovies:function(){
		cl.viewmovs = $$('.viewmov');
		for(var i=0;i<cl.viewmovs.length;i++){
			cl.viewmovs[i].addEvent('click',function(){
				// clean href
				var linkhref = this.getAttribute("href");
				cl.openwindow(linkhref, "movies", 760, 653);
				return false;
			});
		}
	},
	openwindow:function(url, name, width, height){
		config = 'width=' + width + ',height=' + height;
		newWindow = window.open(url, name, config);
		var x = (screen.width - width) / 2;
		var y = (screen.height - height) / 2;
		newWindow.moveTo(x, y);
		newWindow.focus();
		return false;	
	},
	initofficepics:function(){
		if(cl.londonGal){ $clear(cl.londonGal); }
		if(cl.northGal){ $clear(cl.northGal); }
		cl.londonpics = $('londonoffice').getElements('.officepic');
		for(var i=0;i<cl.londonpics.length;i++){
			if(i != 0){
				cl.londonpics[i].setOpacity(0);
			}else{
				DOMhelp.cssjs("add",cl.londonpics[i],"curpic");
				cl.londonpics[i].setOpacity(1);
			}
		}
		cl.northpics = $('northoffice').getElements('.officepic');
		for(var i=0;i<cl.northpics.length;i++){
			if(i != 0){
				cl.northpics[i].setOpacity(0);
			}else{
				DOMhelp.cssjs("add",cl.northpics[i],"curpic");
				cl.northpics[i].setOpacity(1);
			}
		}
	},
	rotateLondonOfficePics:function(){
		cl.londonpics = $('londonoffice').getElements('.officepic'); // array li elements to fade
		for(var i=0;i<cl.londonpics.length;i++){
			if(DOMhelp.cssjs("check",cl.londonpics[i],"curpic")){ // look for for curpic
				DOMhelp.cssjs("remove",cl.londonpics[i],"curpic"); // remove curpic and fade out
				var nextpic = i+1;
				cl.londonpics[i].fade('0');
			}
			if(nextpic == i){ // if next pic then make curpic and fade in
				DOMhelp.cssjs("add",cl.londonpics[i],"curpic");
				cl.londonpics[i].fade('1');
			}
			if(nextpic == cl.londonpics.length){ // if last image is curpic then fade in first element again
				DOMhelp.cssjs("add",cl.londonpics[0],"curpic");
				cl.londonpics[0].fade('1');
			}
		}
	},
	initreelpics:function(){
		if(cl.reelGal){ $clear(cl.reelGal); }
		cl.reelpics = $$('.reelpic');
		for(var i=0;i<cl.reelpics.length;i++){
			if(i != 0){
				cl.reelpics[i].setOpacity(0);
			}else{
				DOMhelp.cssjs("add",cl.reelpics[i],"curpic");
				cl.reelpics[i].setOpacity(1);
			}
		}
	},
	rotateHouseReelPics:function(){
		cl.reelpics = $$('.reelpic'); // array li elements to fade
		for(var i=0;i<cl.reelpics.length;i++){
			if(DOMhelp.cssjs("check",cl.reelpics[i],"curpic")){ // look for for curpic
				DOMhelp.cssjs("remove",cl.reelpics[i],"curpic"); // remove curpic and fade out
				var nextpic = i+1;
				cl.reelpics[i].fade('0');
			}
			if(nextpic == i){ // if next pic then make curpic and fade in
				DOMhelp.cssjs("add",cl.reelpics[i],"curpic");
				cl.reelpics[i].fade('1');
			}
			if(nextpic == cl.reelpics.length){ // if last image is curpic then fade in first element again
				DOMhelp.cssjs("add",cl.reelpics[0],"curpic");
				cl.reelpics[0].fade('1');
			}
		}
	},
	startNorthOfficePics:function(){
		$clear(cl.countdownNorth); // clear count down start periodical
		cl.northGal = cl.rotateNorthOfficePics.periodical(4000); // start second periodical
	},
	rotateNorthOfficePics:function(){
		cl.northpics = $('northoffice').getElements('.officepic');
		for(var i=0;i<cl.northpics.length;i++){
			if(DOMhelp.cssjs("check",cl.northpics[i],"curpic")){
				DOMhelp.cssjs("remove",cl.northpics[i],"curpic");
				var nextpic = i+1;
				cl.northpics[i].fade('0');
			}
			if(nextpic == i){
				DOMhelp.cssjs("add",cl.northpics[i],"curpic");
				cl.northpics[i].fade('1');
			}
			if(nextpic == cl.northpics.length){
				DOMhelp.cssjs("add",cl.northpics[0],"curpic");
				cl.northpics[0].fade('1');
			}
		}
	}
}

// admin scripts
ad={
	init:function(){
		ad.setDeleteLinks();
		ad.initForms();
		if($('sortlist')){
			ad.initSortables();
		}
	},
	initForms:function(){
		if($('imageoption')){ ad.initImageCheckers(); }
		
	},
	setDeleteLinks:function(){
		if($('contentarea').getElement('.deleteitem')){
			ms.deletelinks = $$('.deleteitem');
			for(var i=0;i<ms.deletelinks.length;i++){
				ms.deletelinks[i].onclick = function(){
					return confirm("DELETE - Are you sure?");	
				}
			}
		}	
	},
	initImageCheckers:function(){
		if($('imgcheck')){
			if($('imageoption').options[$('imageoption').selectedIndex].index != 0){
				ad.updateImageChecker();
			}
			$('imageoption').onchange = function(){ // updates current image checker
				ad.updateImageChecker();
			}
		}
	},
	updateImageChecker:function(){
		var dir = $('imgdir').get('text');
		var imageoption = $('imageoption').options[$('imageoption').selectedIndex].value;
		if($('imageoption').options[$('imageoption').selectedIndex].index != 0){
			$('imgholder').innerHTML = "<img src=\""+dir+imageoption+"\" />";
			if(DOMhelp.cssjs("check",$('imgcheck'),"hideMe")){
				DOMhelp.cssjs("remove",$('imgcheck'),"hideMe");
			}
		}else{
			if(!DOMhelp.cssjs("check",$('imgcheck'),"hideMe")){
				DOMhelp.cssjs("add",$('imgcheck'),"hideMe");
			}
		}
	},
	initSortables:function(){
		var sortableList = new Sortables('sortlist',{
			clone:true,
			revert:{
					duration:300,
					transition: Fx.Transitions.Quint.easeOut
				},
			opacity:.5,
			onStart: function(el){
				
			},
			onSort: function(el){
				
			},
			onComplete: function(el){
				var newOrder = sortableList.serialize(0);
				//$('trackorder').set('text',newOrder);
				$('itemorder').set('value',newOrder);
			}
		});
		var submitlinks = $$('.submitlink');
		for(var i=0;i<submitlinks.length;i++){
			submitlinks[i].onclick = function(){
				$('itemorderedit').submit();
				return false;
			}
		}
		
	}
}



function checkMe(param){
	if(param){
		alert(param);
	}else{
		alert("got here");
	}
}