// Custom JavaScript Functions
var Last;

//var $ = jQuery.noConflict();
// Use jQuery via $(...)
$(document).ready(function(){

$('#TinyLogo').click(
	function(){
		document.location="/";
	}
)
							
$('#pikey_on').click(function(){
//alert("on");

$.cookie('pikey','on',{path:'/'})
$("#pikey").fadeIn("slow");


$(this).hide();
$('#pikey_off').show();
});
$('#pikey_off').click(function(){
//alert("off");
$(this).hide();
$('#pikey_on').show();
$("#pikey").fadeOut("slow");
$.cookie('pikey','off',{path:'/'})
});
//$('#show_user_search').click(function(){$('#search_click_menu li span').removeClass().addClass("test1");$('#user_search').show();$('#site_search').hide();$.cookie('search','user',{path:'/'})});

							

	Init();
	if (CommentsOn){
		LoadComments();
	}
	
	

	
	if (document.getElementById('header').style.display=='block'){
		$("#Toggle").toggle(
			  function () {
				ToggleBanner();
			  	$("#header").slideUp("slow");
				$("#pikey_container").fadeOut("slow");
				$("#Navholder").animate({width:"-=48px"},500);
				$("#TinyLogo").animate({width:"+=48px"},500);
			  },
			  function () {
			  	ToggleBanner();
				$("#header").slideDown("slow");
				$("#pikey_container").fadeIn("slow");
			  	$("#Navholder").animate({width:"+=48px"},500);
				$("#TinyLogo").animate({width:"-=48px"},500);				
			  }
		);
	}
	else{
		$("#Toggle").toggle(
			  function () {
				ToggleBanner();
			 	$("#header").slideDown("slow");
			 	$("#Navholder").animate({width:"+=48px"},500);
				$("#TinyLogo").animate({width:"-=48px"},500);				
			  },
			  function () {
				ToggleBanner();
			  	$("#header").slideUp("slow");
				$("#Navholder").animate({width:"-=48px"},500);
				$("#TinyLogo").animate({width:"+=48px"},500);
			  }		  
		);
	}

	if (AccordionOn){
		$('.'+AccordionName)
		.accordion({
			header : '.'+AccordionHeader,
			alwaysOpen : true,
			fillSpace : false,
			autoHeight: false,
			animated: 'slide',
			event: 'click' //mouseover
		})
		.bind("change.ui-accordion", function (event, ui) {
			ui.newHeader.css('background-image','url(/images/interface/TabHeaderDown.gif)');
			ui.oldHeader.css('background-image','url(/images/interface/TabHeaderRight.gif)');
			ui.newContent;
			ui.oldContent;
		})
		
		if (AccordionID=='News' | AccordionID=='Blogs'){
			$('.'+AccordionName)
			.accordion("activate", '#Year_'+Year)
		}
		else if (AccordionID=='CaseStudy'){
			$('.'+AccordionName)
			.accordion("activate", '#'+ClientName)
		}
		else if (AccordionID=='Staff'){
			$('.'+AccordionName)
			.accordion("activate", '#Role_'+Role)
		}		
		
		fullURL = parent.document.URL
		var parts = fullURL.split("/")
		parts[4]=10; //temp fix to keep it working til it pulls in the right data.
		
	}

	$(".BaseBlock")
	.css( {backgroundPosition: "0 0"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0 97px)"}, 
			{duration:100})
	})

	.mouseout(function(){
	$(this).stop().animate(
	{backgroundPosition:"(0 0)"}, 
	{duration:500})
	})
		
	$("#clients ul li")
	.css( {backgroundPosition: "0 0"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0px 96px)"}, 
			{duration:100})
	})
	.mouseout(function(){
	$(this).stop().animate(
	{backgroundPosition:"(0px 0px)"}, 
	{duration:500})
	})


	$("#stafflist ul li")
	.css( {backgroundPosition: "0 0"} )
	.mouseover(function(){
		$(this).stop().animate(
			{backgroundPosition:"(0px 128px)"}, 
			{duration:100})
	})
	.mouseout(function(){
	$(this).stop().animate(
	{backgroundPosition:"(0px 0px)"}, 
	{duration:500})
	})	

	$("#clients ul a").click(function () {
	$("#clients_popup").fadeIn(300);
	return false;
	}); 

	$("#close_clients_popup").click(function () {
	$("#clients_popup_loading").fadeOut(100, function () {
	$("#clients_popup").fadeOut(500);
	});
	return false;
	}); 
});

function ShowHideMonths(x,y){			
			if (Last!=x+y){
				$('.post-news-month-c').slideUp('fast');
				$('#Contents_'+x+'_'+y).slideDown('fast');
			}
			Last=x+y;
		}

function client_popup(id){
$("#clients_popup").css("left",id);
}

function fetch_client(page){
$('#clients_popup_container').load('/work/clients/'+ page +'/')
}
			
function HttpRequestObject() {
	if (window.XMLHttpRequest) {return new XMLHttpRequest();}
	else if(window.ActiveXObject) {return new ActiveXObject("Msxml2.XMLHTTP");}
	else if(window.ActiveXObject) {return new ActiveXObject("Microsoft.XMLHTTP");}
	else {alert('Please try firefox, or upgrade your browser!');}
}

function ToggleBanner(){
	objHTTP1.abort(); //Cleares any previous request and starts again!	
	if (ToggleState=='On'){
		ToggleState='Off';}
	else {
		ToggleState='On';}
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/toggleheader/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		objHTTP1.onreadystatechange=HandleToggleChange;
		var MyData='ToggleState='+ToggleState;
		objHTTP1.send(MyData);		
	}
}

function HandleToggleChange(){
	var currTabElem = document.getElementById('Toggle');
	if (ToggleState=='On'){
		currTabElem.setAttribute("class", "starburst-buttonUP");
		currTabElem.setAttribute("className", "starburst-buttonUP");
		$("#Header").slideDown();
	}else{
		currTabElem.setAttribute("class", "starburst-buttonDOWN");
		currTabElem.setAttribute("className", "starburst-buttonDOWN");
		$("#Header").slideUp();
	}
}

function EmailSubmit(){
	//objHTTP1.abort(); //Cleares any previous request and starts again!
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/sendemail/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		var MyData='F0='+encodeURIComponent(document.contactform.Field0.value)+'&F1='+encodeURIComponent(document.contactform.Field1.value)+'&F2='+encodeURIComponent(document.contactform.Field2.value)+'&F3='+encodeURIComponent(document.contactform.Field3.value)+'&F4='+encodeURIComponent(document.contactform.Field4.value)+'&F5='+encodeURIComponent(document.contactform.ContactName.value)+'&F6='+encodeURIComponent(document.contactform.ContactEmail.value)+'&F7='+encodeURIComponent(document.contactform.Field5.value);
		objHTTP1.onreadystatechange=HandleEmailSubmit;
		document.getElementById('SendAjaxData').style.display='block';
		objHTTP1.send(MyData);
	}
}

function HandleEmailSubmit(){
	if (objHTTP1.readyState == 4 && objHTTP1.status==200) {
		try{
			Cdata = eval('(' +objHTTP1.responseText+')');
		}
		catch(er){
			objHTTP1.abort();
		}
		var IncomingLength=Cdata.incoming.length;	
		var MainResponse;
		if (IncomingLength>0) {	
			if (Cdata.incoming[0].message.content=='Fail'){
				document.getElementById('SendErrors').style.display='block';
			}						
			else{			
				document.getElementById('SendOkay').style.display='block';
			}
			document.getElementById('SendAjaxData').style.display='none';				
		}
	}
}

function LoginSubmit(){
	//objHTTP1.abort(); //Cleares any previous request and starts again!
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/dologin/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		var MyData='F0='+encodeURIComponent(document.login.Field0.value)+'&F1='+encodeURIComponent(document.login.Field1.value);
		objHTTP1.onreadystatechange=HandleLoginSubmit;
		document.getElementById('SendAjaxData').style.display='block';
		objHTTP1.send(MyData);
	}
}

function HandleLoginSubmit(){
	if (objHTTP1.readyState == 4 && objHTTP1.status==200) {
		try{
			Cdata = eval('(' +objHTTP1.responseText+')');
		}
		catch(er){
			objHTTP1.abort();
		}
		var IncomingLength=Cdata.incoming.length;	
		var MainResponse;
		if (IncomingLength>0) {	
			if (Cdata.incoming[0].message.content=='Fail'){
				document.getElementById('SendErrors').style.display='block';
			}						
			else{			
				document.location="/myprofile/";
			}
			document.getElementById('SendAjaxData').style.display='none';				
		}
	}
}

function LoadComments(){	
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/LoadComments/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var MyData='PageNumber='+PageNo+'&ItemsPerPage='+ItemsPerPage+'&Mode='+Mode+'&ItemID='+ItemID+'&TotalItems='+TotalItems;
		objHTTP1.onreadystatechange=HandleLoadComments;
		objHTTP1.send(MyData);
	}
}

function HandleLoadComments(){
	if (objHTTP1.readyState == 4 && objHTTP1.status==200) {
		try{
			Cdata = eval('(' +objHTTP1.responseText+')');
		}
		catch(er){
			objHTTP1.abort();alert(er);
		}
		var IncomingLength=Cdata.incoming.length;	
		var MainResponse;
		if (IncomingLength>0) {	
			document.getElementById('CommentsListingBox').innerHTML=Cdata.incoming[0].Content;
		}
	}
}

function CommentSubmit(){
	//objHTTP1.abort(); //Cleares any previous request and starts again!
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/comment/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		var MyData='F0='+encodeURIComponent(document.commentform.Field0.value)+'&F1='+encodeURIComponent(document.commentform.Field1.value)+'&F2='+encodeURIComponent(document.commentform.Field2.value)+'&F3='+encodeURIComponent(document.commentform.Field4.value)+'&F4='+encodeURIComponent(document.commentform.Field5.value)+'&F5='+encodeURIComponent(document.commentform.Field6.value);
		objHTTP1.onreadystatechange=HandleCommentSubmit;
		document.getElementById('SendAjaxData').style.display='block';
		objHTTP1.send(MyData);
	}
}

function HandleCommentSubmit(){
	if (objHTTP1.readyState == 4 && objHTTP1.status==200) {
		try{
			Cdata = eval('(' +objHTTP1.responseText+')');
		}
		catch(er){
			objHTTP1.abort();
		}
		var IncomingLength=Cdata.incoming.length;	
		var MainResponse;
		if (IncomingLength>0) {	
			if (Cdata.incoming[0].message.content=='Fail'){
				document.getElementById('SendErrors').style.display='block';
				alert(Cdata.incoming[1].message.content);
			}						
			else{	
				document.getElementById('SendErrors').style.display='none';
				document.getElementById('SendOkay').style.display='block';
				LoadComments();
			}
			document.getElementById('SendAjaxData').style.display='none';				
		}
	}
}


function CallStaffBlogs(){
	$('#StaffBlogsContents')
		.fadeOut('slow',function(){CallStaffBlogsAjax()});
}
function CallStaffBlogsAjax(){	
	if (objHTTP1.readyState == 4 || objHTTP1.readyState == 0) {
		objHTTP1.open('POST','/callblogs/',true);
		objHTTP1.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		var MyData='PID='+Pid+'&Sid='+Sid;
		objHTTP1.onreadystatechange=HandleCallStaffBlogs;
		//document.getElementById('CallAjaxData').style.display='block';
		objHTTP1.send(MyData);
	}
}

function HandleCallStaffBlogs(){
	if (objHTTP1.readyState == 4 && objHTTP1.status==200) {
		//alert(objHTTP1.responseText);
		try{
			Cdata = eval('(' +objHTTP1.responseText+')');			
		}
		catch(er){
			objHTTP1.abort();alert('Data error.');
		}
		var IncomingLength=Cdata.incoming.length;
		if (IncomingLength>0) {
			var Items=Cdata.incoming[0].message.total;
			var Content=Cdata.incoming[0].message.content;	
		}
		document.getElementById('StaffBlogsContents').innerHTML=Content;
		var NextPrev;
		if (Pid==1){
			NextPrev='<div id="StaffBlogsPrev" Class="PrevOff"><a href="#" onclick="Pid--;CallStaffBlogs();return false;">< Prev</a></div>';
		}
		else {
			NextPrev='<div id="StaffBlogsPrev" Class="PrevOn"><a href="#" onclick="Pid--;CallStaffBlogs();return false;">< Prev</a></div>';
		}
		if (Items>5){
			NextPrev+='<div id="StaffBlogsNext" Class="NextOn"><a href="#" onclick="Pid++;CallStaffBlogs();return false;">Next ></a></div>';
		}
		else{
			NextPrev+='<div id="StaffBlogsNext" Class="NextOff"><a href="#" onclick="Pid++;CallStaffBlogs();return false;">Next ></a></div>';
		}
		$('#StaffBlogsContents')
			.fadeIn('slow',function(){document.getElementById('StaffBlogsNextPrev').innerHTML=NextPrev;});   
	}
}