// JavaScript Document

var gMenuObj = [];
var gCurrentSelectedMenu = null;
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position
var insidetarget = false;



var menulist = [
				
				{displayText: "Services", url: "index", title:  "J&H Fire Protection", swf: "slideshow_1.swf"},
				{displayText: "About Us", url: "about", title: "About Us", swf: "slideshow_3.swf"},
				{displayText: "Contact", url: "contact", title: "Contact", swf: "contact.swf"}
				];

function setupAJAX() {
	if (window.XMLHttpRequest) {
	  // If IE7/8, Mozilla, Safari, etc: Uses native XMLHttpRequest
	var ajaxRequest = new XMLHttpRequest();
	return ajaxRequest;
	} else {
	if (window.ActiveXObject) {
	  // ...otherwise, use the ActiveX control for IE5.x and IE6
	 var ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
	 return ajaxRequest;
	  } else {
	alert("Sorry, this page won't work with this browser!\nYou need to upgrade to a recent version of\nFirefox, Safari, Opera or Internet Explorer.");
	
	  }
	 }
	}
	

window.onload = function(){
	create_menu();
	// embed swf by matching page title against menulist to obtain flash file name. Note: Some pages have locally embedded swf movies
	if(document.title == "J&H Fire Protection"){
	setTimeout(function(){swfobject.embedSWF("swf/pulsing_per_second.swf" , "home_bottom_strap", "945", "26", "9.0.0", false)}, 50);
	//test for when page was last viewed
	var pageLastViewed = readCookie("JH");
	var timenow = new Date().getTime()
	if(timenow - pageLastViewed > 600000){
	swfobject.embedSWF("swf/slideshows/services.swf" , "main", "980", "300", "9.0.0", false);	
	}else{
	swfobject.embedSWF("swf/slideshows/services_revisit.swf" , "main", "980", "300", "9.0.0", false);	
	}
	createCookie("JH", timenow)
	}
	
	display_footer();
	logo_bindings();
	var pageLastViewed = readCookie("JH");
	
	
	//document.onmousemove = follow;
}

function create_menu(){
var navcontainerDiv = document.getElementById("menu_bar");
	var navlist = document.createElement("ul");// create unordered list element
	navlist.id = "navlist";
	var menuslength = menulist.length;
	for (var  i= 0; i < menuslength; i++){
	var this_menu_obj = new menu_constructor(i); // create an li object
	gMenuObj.push(this_menu_obj); // push onto gMenuObj
		// add menu sep graphic
		if(i< menuslength-1){
		var menuSep = document.createElement("img");
		menuSep.src = "images/menu_separator_pink.gif";
		this_menu_obj.li.appendChild(menuSep);
		}
	navlist.appendChild(this_menu_obj.li);
	}
	navcontainerDiv.appendChild(navlist);
	latch_colour();	
	
	
}


function menu_constructor(i){
			this.anchor = document.createElement("a");// create anchor
			this.anchor.href= "#";
			this.anchor.url = menulist[i].url;
			this.anchor.displayText = menulist[i].displayText;
			this.anchor.onclick = function() {select_menu(this); return false};
			this.anchor.onmouseout = function() {latch_colour();};
			this.anchor.onmouseover=function(){kill_menu_highlight(this);}; 
			this.li = document.createElement("li");
			var mytextnode = document.createTextNode(menulist[i].displayText);
			this.anchor.appendChild(mytextnode);
			this.li.appendChild(this.anchor);
	}
	
	function select_menu(obj){
	window.location = obj.url+".html";
	}
	
	function latch_colour(){
	if(document.title == "J&H Fire Protection"){
	var menu = "Services";	
	}else{
	var menu = document.title;
	}
	var menuslength = menulist.length;
	for(var i = 0; i < menuslength; i++) {
	if (menu == gMenuObj[i].anchor.displayText) {
		gMenuObj[i].anchor.style.color = "#0056a6";// change to lighter colour
		gCurrentSelectedMenu = gMenuObj[i].anchor;
		}
	}	
	}
	
		
	// kill highlight when mouse is over another item
	
	function kill_menu_highlight(){
		try{
	gCurrentSelectedMenu.style.color = "";// kill to let CSS resume
		}catch(e){
			//alert(e);
		}
	}
	
	function logo_bindings(){
	var logosDiv = document.getElementById("logosDiv");
	logosDiv.style.cursor = "pointer";
	var thelogos = logosDiv.getElementsByTagName("img");
	for(var i = 0; i < thelogos.length; i++) {
		switch(i){
		case 0:
		thelogos[i].onclick = function(){ window.open("http://www.britsafe.org/") }; // British Safety Council
		thelogos[i].onmouseover = function(){popup("Go to Britsh Safety Council");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 1:
		thelogos[i].onclick = function(){ window.open("http://www.britishfireconsortium.org.uk/") }; // British Safety Consortium
		thelogos[i].onmouseover = function(){popup("Go to British Fire Consortium");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 2:
		thelogos[i].onclick = function(){ window.open("http://www.safecontractor.com/") }; // safecontractor
		thelogos[i].onmouseover = function(){popup("Go to Safe Contractor");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 3:
		thelogos[i].onclick = function(){ window.open("http://www.uk-fa.org/") }; // UK Fire Association
		thelogos[i].onmouseover = function(){popup("Go to UK Fire Association");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 4:
		thelogos[i].onclick = function(){ window.open("http://www.uk-fa.org/") }; // XACT
		thelogos[i].onmouseover = function(){popup("Go to Xact");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 5:
		thelogos[i].onclick = function(){ window.open("http://www.ccnsg.com/") }; // CCNSG
		thelogos[i].onmouseover = function(){popup("Go to CCNSG site");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 6:
		thelogos[i].onclick = function(){ window.open("http://www.firesprinklers.org.uk/") }; // firesprinklers
		thelogos[i].onmouseover = function(){popup("Go to Fire Sprinklers Org");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		case 7:
		thelogos[i].onclick = function(){ window.open("http://www.ecitb.org.uk/") }; // ECITB
		thelogos[i].onmouseover = function(){popup("Go to ECITB site");};
		thelogos[i].onmouseout = function(){supress_popup();};
		break;
		}
		
		thelogos[i].onmousedown = function(){return false;}; // stop browser's ability to drag images
		
	}
	}
	
	
	function send_query(){
	var contact_name = document.getElementById("contact_name").value;
	var contact_phone = document.getElementById("contact_phone").value;
	var contact_email = document.getElementById("contact_email").value;
	var query_type = document.getElementById("query_type").value;
	var query_text = document.getElementById("query_box").value;
	if(contact_name=="" || contact_phone=="" || contact_email=="" || query_text==""){
	document.getElementById("messageDiv").innerHTML = "Please complete fields";
	return;
	}
	
	var poststring = "contact_name="+contact_name+"&contact_phone="+contact_phone+"&contact_email="+contact_email+"&query_type="+query_type+"&query_text="+query_text;
	
	var ajaxRequest = new XMLHttpRequest();
	ajaxRequest.open("POST", "php/send_contact_form.php", true);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.onreadystatechange = function() {
	if(ajaxRequest.readyState == 4){
		if (ajaxRequest.status == 200) {
			var returned_message = ajaxRequest.responseText;
			document.getElementById("messageDiv").innerHTML = returned_message;
			}else{
            document.getElementById("messageDiv").innerHTML = "Error - please try later";			
            }
	      }
	    }
	ajaxRequest.send(poststring);
		
	}
	
	
	function display_footer(){
	var today = new Date();
	var thisYear = today.getUTCFullYear();	
	document.getElementById("footer").innerHTML = "&copy; "+thisYear+" J&amp;H Fire Protection UK Ltd. &#8226; Design &amp; Programming &copy; "+thisYear+" Kaleidoscopic Creation &#8226; All Rights Reserved";
	}
	
	function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
		}
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
	
	
	
	function popup(theName){
		return; // not used at this time
	insidetarget = true;
	var popup=document.getElementById("popupDiv");
	if(popup.firstChild)
		{
		popup.removeChild(popup.firstChild);
		}
	mytextnode = document.createTextNode(theName);
	popup.appendChild(mytextnode);
	}
	
	function supress_popup(){
	insidetarget = false;
	}
	
	function activate_popup(){
	insidetarget = true;
	}
	
	// Follow the mousey
	
	function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
	function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}
	function follow(evt) {
	if (insidetarget) {
	var obj = document.getElementById("popupDiv").style; 
	obj.visibility = 'visible';
	obj.left = (parseInt(mouseX(evt))+offX) + 'px';
	obj.top = (parseInt(mouseY(evt))+offY) + 'px';
	}
	else if(!insidetarget){
	var obj = document.getElementById("popupDiv").style; 
	obj.visibility = 'hidden';
	}
	}
