/*
//
//	Author:				Mark Rowntree 
//	filename:			/js/header.js
//	function:			The core functions that are used to generate the header
//	usage:				<script src="/js/header.js" type="" language="JavaScript"></script>
						renderCalendar();
//	created date:		20/04/2007
//	update history:		[dd/mm/yyyy / developer name: description of update, including referencing project | issue | cr number]		
//
*/

// client rotating object for the homepage
if((typeof o_clientObj == "undefined") || (!o_clientObj.length)){
	var o_clientObj = [
		{
		"clientName":"Bodytalk Lingerie and Beachwear",
		"linkPath":"/clients/bodytalk.shtml",
		"linkTitle":"The Website for Bodytalk Lingerie and Beachwear",
		"img_small":"small",
		"img_med":"med",
		"imgId":"new_willOakley",
		"imgPath":"/images/hp/bodytalk_60_140.jpg",
		"imgPolaroidPath":"bodytalk/bodytalk_hp_med.jpg",
		"imgTitle":"The Bodytalk Lingerie and Beachwear website - click to view the details for this client",
		"teaserContent":"<p class='teaser'>Bodytalk is Harrogate&apos;s exclusive lingerie, swimwear and night wear shop located on Montpellier Mews.</p><p class='teaser'>We provided the client with several design mockups to choose from, then built the site around the chosen design.</p><p class='teaser'>It's a straightforward 'Brochure' site with a page displaying a selection of products from the shop.</p>"
		}
		,
		{
		"clientName":"Will Oakley Photography",
		"linkPath":"/clients/willoakley.shtml",
		"linkTitle":"The new Website for Will Oakley Photography",
		"img_small":"small",
		"img_med":"med",
		"imgId":"new_willOakley",
		"imgPath":"/images/hp/willoakley_60_140.jpg",
		"imgPolaroidPath":"willoakley/willoakley_gall_med.jpg",
		"imgTitle":"New Will Oakley Photography website - click to view the details for this client",
		"teaserContent":"<p class='teaser'>Will Oakley Photography offer original fine art images in various sizes and finishes all produced on specialist materials.</p><p class='teaser'>We built the whole website including a small CMS.</p><p class='teaser'>It has Photo Galleries, a Slideshow and is fully e-commerce enabled</p>"
		}
		,
		{
		"clientName":"Imaginative Traveller",
		"linkPath":"/clients/imTrav.shtml",
		"linkTitle":"The new design for Imaginative Traveller",
		"img_small":"small",
		"img_med":"med",
		"imgId":"new_imTrav",
		"imgPath":"/images/hp/imtrav_60_140.jpg",
		"imgPolaroidPath":"imTrav/imtrav_med.jpg",
		"imgTitle":"New imTrav design - click to view the details for this client",
		"teaserContent":"<p class='teaser'>The Imaginative Traveller are an online travel company.</p><p class='teaser'>See what work we did for them; a site wide rebrand to start with and some help updating the content.</p><br /><p class='teaser'>We'll work with you to identify what work you need doing.</p>"
		}
		,
		{	
		"clientName":"Superbreak",
		"linkPath":"/clients/superbreak.shtml",
		"linkTitle":"Superbreak.com",
		"img_small":"smll",
		"img_med":"med",
		"imgId":"sprbreak",
		"imgPath":"/images/hp/superbreak_60_140.jpg",
		"imgPolaroidPath":"superbreak/superbreak_hp_med.jpg",
		"imgTitle":"New imTrav design - click to view the details for this client",
		"teaserContent":"<p class='teaser'>Superbreak are an online travel company.</p><p class='teaser'>See what work we did for them: we added a couple of new sections, 'friendly' URLs and added 'theatre reviews'.</p><br /><p class='teaser'>Whatever you need, we can help you and ensure you are successful</p>"
		}
	];
}
var a_cacheImages = new Array();
//create an array of images to cache
var a_theImages = ["/images/common/main/no_image.gif","/images/common/main/please_wait.gif","/images/common/header/about_us_over.gif","/images/common/header/clients_over.gif","/images/common/header/home_over.gif","/images/common/header/home_over.gif","/images/common/header/services_over.gif","/images/common/header/top_10_over.gif","/images/hp/main_clients_hp.jpg"];
var s_clientIMGpath = "/images/clients/";

var s_browser = navigator.appName;
var s_browserVersion = navigator.appVersion;

function renderClientTeaser(i_Index){
	//remove any polaroids visible
	if(document.getElementById('clientPolaroid')){
		var o_div = document.getElementById('clientPolaroid');
		var o_parent = document.getElementById('contentInner800')
		o_parent.removeChild(o_div);
	}
	//how many options are there?
	var i_options = o_clientObj.length;
	//select an index
	var i_newIndex = null;
	while((i_newIndex == null) || (i_newIndex == i_Index)){
		i_newIndex = Math.floor(Math.random()*i_options);
	}
	//insert the relevant data into the teaser
	var o_this = o_clientObj[i_newIndex];
	//cache the larger image
	cacheImages(o_this.imgPolaroidPath);	
	var s_teaserImg = "<a href=\"" + o_this.linkPath + "\" onmouseover=\"clientPolaroid('" + o_this.imgId  + "','over','" + o_this.linkTitle + "','" + o_this.img_small + "','" + o_this.img_med + "','/images/clients/" + o_this.imgPolaroidPath + "');\" onmouseout=\"clientPolaroid('" + o_this.imgId  + "','out');\"><img src=\"" + o_this.imgPath  + "\" id=\"" + o_this.imgId  + "\" height=\"140\" width=\"60\" alt=\"" + o_this.imgTitle  + "\" title=\"" + o_this.imgTitle  + "\" class=\"hp_teaserIMG\" border=\"0\" /></a>";
	document.getElementById('hp_cont3_main').innerHTML = s_teaserImg + o_this.teaserContent;
	setTimeout("renderClientTeaser(" + i_newIndex + ");",30000);
}

function displayClientTeaser(){
	if(document.getElementById('hp_cont3_main')){
		setTimeout("renderClientTeaser(0);",30000);
	}
}

//cache images
function cacheImages(s_IMGpath){
	var s_IMGpath = (s_IMGpath.indexOf('/images') !=-1) ? s_IMGpath : s_clientIMGpath + s_IMGpath ;
	var i_cacheImages = a_cacheImages.length;
	a_cacheImages[i_cacheImages] = new Image();
	a_cacheImages[i_cacheImages].src = s_IMGpath;
}
//now the cacheImages function has loaded, cache the base images
for(var i_IMG = 0; i_IMG < a_theImages.length; i_IMG++){
	cacheImages(a_theImages[i_IMG]);
}

function IMGswap(s_ID,s_status){
	if(s_ID && s_status && document.getElementById(s_ID + "NavImg")){
		var s_filename = (s_status == 'over') ? s_ID + "_over.gif" : s_ID + ".gif";
		var s_repfname = (s_status == 'over') ? s_ID + ".gif" : s_ID + "_over.gif";
		//retrieve the current location
		var s_host = location.protocol + "//" + location.host;
		var s_path = document.getElementById(s_ID + "NavImg").src.replace(s_repfname,"");
		s_path = s_path.replace(s_host,"");
		document.getElementById(s_ID + "NavImg").src = s_path + s_filename;
	}
}

function renderCalendar(){

	var thisdate = new Date();
	var DaysArray = new Array("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
	var MonthsArray = new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec");
	var DateNum = thisdate.getDate();
	var LastNum = 0;
	var SuperScript = "";

	if (DateNum>10){
		var NumTens = DateNum/10;
		var NumTensString = NumTens.toString();
		var TenTimes = ((parseInt(NumTensString,10))* 10);
		LastNum = DateNum - TenTimes;
	}else{
		LastNum = DateNum;
	}

	if (LastNum == 0){
		SuperScript = "th";	
	}else if (LastNum == 1){
		SuperScript = "st";		
	}else if (LastNum == 2){
		SuperScript = "nd";	
	}else if (LastNum == 3){
		SuperScript = "rd";	
	}
	if ((DateNum == 11) || (DateNum == 12) || (DateNum == 13)){
		SuperScript = "th";
	}
	
	
	var thisDay = thisdate.getDay();
	var DisplayDay = DaysArray[thisDay];
	
	var thisMonth = thisdate.getMonth();
	var DisplayMonth = MonthsArray[thisMonth];
	
	var DisplayYear = thisdate.getFullYear();
	
	var DisplayDate = DisplayDay + " " + DateNum + SuperScript + " " + DisplayMonth;
	
	document.write(DisplayDate + " " + DisplayYear);
}



function clientPolaroid(s_id,s_status,s_caption,s_orig,s_replace,s_replaceIMGpath){
	if((s_id && s_id != "") && (s_status && s_status != "")){
		if(s_status == "over"){
			var o_img = document.getElementById(s_id);
			if(!s_replaceIMGpath){
				var s_src = o_img.src;
				s_src = s_src.replace(location.protocol + "//" + location.host,"");
				s_src = s_src.replace(s_orig,s_replace);
			}else{
				var s_src = s_replaceIMGpath;
			}
			//get the width of this image
			o_IMG = new Image();
			o_IMG.src = s_src;
			s_imgWidth = o_IMG.width;
			//create new div
			//find the position of the image and set the position to display the new div
			var s_app = navigator.appName;
			var posX,posY,s_X_minus,s_Y_minus;
			//calculate the position in the page by bubbling up as long as there is a 'offsetParent' element
			posX = o_img.offsetTop;
			posY = o_img.offsetLeft;
			o_El = o_img;
			while(o_El = o_El.offsetParent){
				posX += o_El.offsetTop;
				posY += o_El.offsetLeft;
			}
			i_X_minus = 100;
			i_Y_minus = 450;
			//if there is one, use posY to position the image, otherwise just place the image in the middle of the page
			if(posX){
				var s_posX = posX - i_X_minus;
			}else{
				var s_posX = 50;
			}
			if(posY){
				var s_posY = posY - i_Y_minus;
			}else{
				var s_posY = 350;
			}
			//create the main DIV to contain the image
			s_captionDiv = "<div id='clientPolaroidText'>" + s_caption + "</div>";
			o_div = document.createElement('div');
			o_div.setAttribute('id',"clientPolaroid");
			o_div.className="clientPolaroid"; 
			o_div.style.position = "absolute";       
			o_div.style.left = s_posY + "px";
			o_div.style.top = s_posX + "px";
			o_div.style.textAlign = "center";
			o_div.style.zIndex = "100";
			o_div.style.width = s_imgWidth + "px";
			o_div.style.height = "auto";
			o_div.style.backgroundColor = "#C5CCFF";
			o_div.style.border = "1px solid #339";
			o_div.innerHTML = '<img src="' + s_src + '" alt="' + s_caption + '" title="' + s_caption + '" id="clientPolaroidImage" border=o />' + s_captionDiv;
			document.getElementById('contentInner800').appendChild(o_div);
		}else{
			//destroy the polaroid DIV
			var o_div = document.getElementById('clientPolaroid');
			o_parent = document.getElementById('contentInner800')
			o_parent.removeChild(o_div);
		}
	}
}

function displayLargeImg(s_id,s_filename){
	if(s_id && s_filename){
		//get the filename & path
		s_filename = s_filename.substring(s_filename.indexOf('/clients/') + 9, s_filename.length);
		//amend to refer to the large version of the image
		s_filename = s_filename.replace('med','large');
		//create modal popup
		//add a new div to the parent document, make it the same size as the entire page, insert another div at this point in the page, grey out and disable the background
		o_topDiv = document.createElement('div');
		o_topDiv.setAttribute('id',"modalParentDiv");
		i_pageHeight = getPageHeight();
		i_pageWidth = getClientWidth();
		o_topDiv.style.position="absolute";       
		o_topDiv.style.left= "0px";
		o_topDiv.style.top="0px";
		o_topDiv.style.textAlign = "center";
		o_topDiv.style.zIndex = "100";
		o_topDiv.style.width= i_pageWidth + "px";
		o_topDiv.style.height= i_pageHeight + "px";
		o_topDiv.style.backgroundColor="#929292";
		i_opacity = 60;
		o_topDiv.style.opacity = i_opacity/100;
		o_topDiv.style.MozOpacity = i_opacity/100;
		o_topDiv.style.filter = "alpha(opacity=" + i_opacity + ")";
		o_topDiv.style.KhtmlOpacity = "'" + i_opacity/100 + "'";
		document.getElementById('pageDiv').appendChild(o_topDiv);
		//add the image div
		o_IMG = new Image();
		o_IMG.onerror = function(){
			document.getElementById('modalImgDiv').innerHTML = '<img src="/images/common/main/no_image.gif" width="400" height="400" alt="No Image" title="No Image" border="0" />';
		}		
		o_IMG.src = s_clientIMGpath + s_filename;
		
		i_modalWidth = 410;
		i_modalHeight = o_IMG.height;
		
		o_modalDiv = document.createElement('div');
		o_modalDiv.setAttribute('id',"modalDiv");
		o_modalDiv.style.position = "absolute";       
		o_modalDiv.style.left = ((i_pageWidth - i_modalWidth)/2) + "px";
		o_modalDiv.style.top = getScrollTop() + "px";
		o_modalDiv.style.textAlign = "center";
		o_modalDiv.style.margin = "0";	
		o_modalDiv.style.padding = "0";	
		o_modalDiv.style.zIndex = "100";
		o_modalDiv.style.width = i_modalWidth + "px";
		o_modalDiv.style.height = "auto";
		o_modalDiv.style.backgroundColor = "#ffffff";

		o_modalImgDiv = document.createElement('div');
		o_modalImgDiv.setAttribute('id',"modalImgDiv");
		o_modalImgDiv.style.position = "relative";
		//this is causing problems in damn IE!!!!
		if(s_browser.indexOf("Microsoft") != -1){     
			o_modalImgDiv.style.left = "0";
		}else{
			o_modalImgDiv.style.left = "5px";
		}
		o_modalImgDiv.style.top = "5px";
		o_modalImgDiv.style.textAlign = "center";
		o_modalImgDiv.style.margin = "0";
		o_modalImgDiv.style.padding = "0";
		o_modalImgDiv.style.width= "400px";
		o_modalImgDiv.style.height= "auto";
		o_modalImgDiv.setAttribute('class',"border1");
		o_modalImgDiv.style.zIndex = "1";
		o_modalImgDiv.innerHTML = '<img src="' + o_IMG.src + '" id="modalImg" alt="" title="" border="1" />';
		o_modalDiv.appendChild(o_modalImgDiv);		

		o_modalCloseDiv = document.createElement('div');
		o_modalCloseDiv.setAttribute('id',"modalCloseDiv");
		o_modalCloseDiv.style.position = "relative";       
		o_modalCloseDiv.style.textAlign = "center";
		o_modalCloseDiv.style.margin = "0";
		o_modalCloseDiv.style.padding = "5px 0 0 0";
		o_modalCloseDiv.style.top = "5px";
		o_modalCloseDiv.style.width = i_modalWidth + "px";
		o_modalCloseDiv.style.height = "30px";
		o_modalCloseDiv.innerHTML = 'Close <img onclick="removeLargeImg();" src="/images/common/main/close_button.gif" align="absmiddle" width="20" height="20" alt="Click to close" title="Click to close" border="0" class="showHand" />';
		o_modalDiv.appendChild(o_modalCloseDiv);
		document.getElementById('pageDiv').appendChild(o_modalDiv);
		showPleaseWait('modalDiv');		
		
	}
}

function removeLargeImg(){
	if(document.getElementById('modalDiv')){
		document.getElementById('pageDiv').removeChild(document.getElementById('modalParentDiv'));
		document.getElementById('pageDiv').removeChild(document.getElementById('modalDiv'));
	}
}

function pausecomp(millis){
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < millis);
}

function getPageHeight(){
	//get the full page height
	i_pageHeight = 0;
	if(document.body.offsetHeight && window.scrollMaxY){
		i_pageHeight = window.innerHeight + window.scrollMaxY;
	// all but Explorer Mac  
	}else if(document.body.scrollHeight > document.body.offsetHeight){
		i_pageHeight = document.body.scrollHeight;
	// works in Explorer 6 Strict, Mozilla (not FF) and Safari  
	}else if(document.documentElement && document.documentElement.scrollHeight){
		i_pageHeight = document.documentElement.scrollHeight;
	}else{
		i_pageHeight = document.body.offsetHeight + document.body.offsetTop;
	}
	return i_pageHeight;
}

function getObjVertPos(o_obj){
		i_pos = 0;
  		if(o_obj.offsetParent){
        	while(1)
        	{
				i_pos += o_obj.offsetTop;
				if(!o_obj.offsetParent){
        	    	break;
				}
				o_obj = o_obj.offsetParent;
    	    }
		}else if(obj.y){
    	    i_pos += o_obj.y;	
		}
	return i_pos;
}

function getClientWidth() {
	return getFilterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function getClientHeight() {
	return getFilterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function getScrollLeft() {
	return getFilterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function getScrollTop() {
	return getFilterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function getFilterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function showPleaseWait(s_divId){
	var o_obj;
	//if no div ID is provided, attach it to the 'pageDiv' div element
	if(typeof s_divId == "undefined"){
		//add parent div
		o_parentDiv = document.createElement('div');
		o_parentDiv.setAttribute('id',"parentDiv");
		o_parentDiv.style.position = "absolute";
		//o_parentDiv.style.backgroundColor = "red";
		o_parentDiv.style.top = "0";
		o_parentDiv.style.left = "0";
		o_parentDiv.style.width = "100%";
		o_parentDiv.style.height = "100%";
		o_parentDiv.style.textAlign = "center";

		o_pleaseWaitOuterDiv = document.createElement('div');
		o_pleaseWaitOuterDiv.setAttribute('id',"pleaseWaitOuterDiv");
		o_pleaseWaitOuterDiv.style.position = "relative";
		o_pleaseWaitOuterDiv.style.top = "200px";
		o_pleaseWaitOuterDiv.style.left = "0";
		o_pleaseWaitOuterDiv.style.width = "200px";
		o_pleaseWaitOuterDiv.style.height = "auto";
		o_pleaseWaitOuterDiv.style.margin = "0 auto";
		o_pleaseWaitOuterDiv.style.padding = "0";
		o_pleaseWaitOuterDiv.style.border = "1px solid black";										
		o_parentDiv.appendChild(o_pleaseWaitOuterDiv);
		
		document.getElementById('pageDiv').appendChild(o_parentDiv);
		o_obj = document.getElementById('pleaseWaitOuterDiv');
	}else{
		o_parentObj = document.getElementById(s_divId);
		o_pleaseWaitOuterDiv = document.createElement('div');
		o_pleaseWaitOuterDiv.setAttribute('id',"pWOuterImageDiv");
		o_pleaseWaitOuterDiv.style.position = "absolute";
		o_pleaseWaitOuterDiv.style.top = "0";
		o_pleaseWaitOuterDiv.style.left = "0";
		o_pleaseWaitOuterDiv.style.height = "auto";
		o_pleaseWaitOuterDiv.style.width = "100%";
		o_pleaseWaitOuterDiv.style.textAlign = "center";
		o_pleaseWaitOuterDiv.style.padding = "0";
		o_pleaseWaitOuterDiv.style.margin = "0 auto";
		o_parentObj.appendChild(o_pleaseWaitOuterDiv);
		o_obj = document.getElementById('pWOuterImageDiv');
	}
		//add the div
		o_pleaseWaitDiv = document.createElement('div');
		o_pleaseWaitDiv.setAttribute('id',"pleaseWaitDiv");
		o_pleaseWaitDiv.style.position = "relative";
		o_pleaseWaitDiv.style.width = "auto";
		o_pleaseWaitDiv.style.height = "auto";
		o_pleaseWaitDiv.style.textAlign = "center";
		o_pleaseWaitDiv.style.margin = "0 auto";
		o_pleaseWaitDiv.style.padding = "5px";
		o_pleaseWaitDiv.style.backgroundColor = "white";
		
		o_pleaseWaitDiv.innerHTML = 'Please Wait<br /><img src="/images/common/main/please_wait.gif" id="pleaseWaitImg" alt="" title="" width="45" height="45" border="0" />';
		o_obj.appendChild(o_pleaseWaitDiv);
}

