// JavaScript Document
//this file contains all the scripts that make all the pages on the 3Vista site run - except the catalogue!  Those are kept separate for my sanity.


//this function set runs the main nav menu
	var currentimage=null;
	var currentMenu="subdate";
	var inprocessMenu=false;

	function montre(id,image) {
		if (currentimage == image){
		}else{
			if (image == null){ <!--if the menu is being reset
				document.getElementById(currentimage).innerHTML = "<img src='images/nav_" + currentimage + ".gif' />";
				document.getElementById(currentimage).style.paddingTop = "12px";
			}else{ <!--or, if you're mousing over one of the menu items, swap images
				document.getElementById(image).innerHTML = "<img src='images/nav_" + image + "_o.gif' />";
				document.getElementById(image).style.paddingTop = "5px";
			}
			if (currentimage == null){
			}else{ <!--if a menu is already open, close it by swaping images back
				document.getElementById(currentimage).innerHTML = "<img src='images/nav_" + currentimage + ".gif' />";
				document.getElementById(currentimage).style.paddingTop = "12px";
			}
			currentimage = image;
		}
		if (inprocessMenu == true){
		}else{
			if (currentMenu == id){
			}else{ <!--open one menu and close the other
				inprocessMenu = true;
				document.getElementById(id).style.zIndex = 3;
				document.getElementById(currentMenu).style.visibility = "hidden";
				document.getElementById(id).style.visibility = "visible";
				document.getElementById(currentMenu).style.zIndex = 0;				
				currentMenu = id;
				inprocessMenu = false;
			}
		}
	}

//this function set controls the fading of both the main content section of the page and the image in the top bar.  it also starts the loader image
	var currentMain = "mhome";
	var currentNum = "m001";
	var nextNum = "m002";
	var inprocess = false;
	var divT = "";
	var currentImg = "himg001";
	var nextImg = "himg002";
	var currentRand = 1;

	function changediv(divID){
		if (inprocess == true){
		}else{
			if (currentMain == divID || divID == ""){
			}else{
				document.getElementById("loader").style.height = 75;  //loader div height defined! (compensating for IE)
				document.getElementById("loader").style.zIndex = 100; //loader div up!
				if (currentNum == "m001"){
					nextNum = "m002";
				}else if (currentNum == "m002"){
					nextNum = "m001";
				}
				if (currentImg == "himg001"){
					nextImg = "himg002";
				}else if (currentImg == "himg002"){
					nextImg = "himg001";
				}
				inprocess = true;
				new Effect.Fade(nextNum, {duration:.00001});
				new Effect.Fade(nextImg, {duration:.00001});
				divT = divID
				var tt=setTimeout("divrequest(divT);", 150);
			}
		}
	}
				
				
	function divrequest(ddd){			
		var url = "includes.php";
		request.open("POST", url, true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.onreadystatechange = changedivfade;
		request.send("inc=" + ddd);
		currentMain = ddd;
	}
	
	function changedivfade(){
		if (request.readyState == 4){
			if (request.status == 200){
				var response = request.responseText;
				document.getElementById(nextNum).innerHTML = response;
				document.getElementById(currentNum).style.zIndex = 2;
				document.getElementById(nextNum).style.zIndex = 1;
				document.getElementById(nextNum).style.height = 'auto';
				new Effect.Appear(nextNum);
				new Effect.Fade(currentNum);
				currentNum = nextNum;
				document.getElementById('mcontent').style.height = 'auto';
				imageswap();
				}else{
				alert("Internal error code " + request.status + ". Please contact site adminstrator")
			}
		}
	}

	function imageswap(){
		var rand = Math.floor(Math.random()*5) + 1;
		//to make sure that the same image isn't shown twice in a row
		if (rand == currentRand){
			if (currentRand < 4){
				rand = rand + 1;
			}else{
				rand = rand - 1;
			}
		}
		if (rand < 10){
			rand = "0" + rand;
		}
		var newimage = "images/headshot" + rand + ".gif";
		document.getElementById(currentImg).style.zIndex = 2;
		document.getElementById(nextImg).style.zIndex = 1;
		document.getElementById(nextImg).innerHTML = "<img src='" + newimage + "' />";
		new Effect.Appear(nextImg);
		new Effect.Fade(currentImg);
		currentImg = nextImg;
		currentRand = rand;

		if (inprocess == true){
			var t=setTimeout("document.getElementById('loader').style.zIndex = -1; document.getElementById('loader').style.height = 0; inprocess = false;", 1200);
		}
	}
	
//----------------------------------------------------------------------------------------

//the following scripts are for sub-pages loaded into the main page

//this function controls the management page - it makes the divs appear and disappear
	function appear(div_id){
		if (document.getElementById(div_id).style.height == "auto"){
			document.getElementById(div_id).style.height = "30px";
		}else{
			document.getElementById(div_id).style.height = "auto";	
		}
		document.getElementById('mcontent').style.height = "100%";
	}

//----------------------------------------------------------------------------------------

//this function set is for the contact us page - it makes the form work and calls the validation script.
	function fvalidate(){
		var fname = document.getElementById("name").value;
		var fcomp = document.getElementById("company").value;
		var femail = document.getElementById("email").value;
		var fphone = document.getElementById("phone").value;
		var fcomt = document.getElementById("comment").value;
		var url = "validate.php?name=" + fname + "&comp=" + fcomp + "&email=" + escape(femail) + "&phone=" + escape(fphone) + "&comment=" + escape(fcomt);
		request.open("GET", url, true);
		request.onreadystatechange = updatePage;
		request.send(null);
	}
	
	function updatePage(){
		if (request.readyState == 4) {
			var newText = request.responseText;
			document.getElementById("echo").innerHTML = "<span class='textRedBold'>" + newText; + "</span>";
		}
	}

//----------------------------------------------------------------------------------------

//this function set is for the stills page - it makes the image slider work.
var picmove = false;
var picpos = 1;

	function imgjumper(container,picbar,jumploc,xw){
		/*
		container = the picbar's main container div
		picbar = the picbar's div id
		jumploc = the location to jump to
		xw = the pics x witdh
		*/
		if (picmove == true){
		}else{
			picmove = true;
			var leftloc = (-(jumploc - 1) * xw);
			document.getElementById(picbar).style.left = leftloc + "px";
			new Effect.Move (picbar,{x:0, y:0, mode:'relative', duration:0.00001, afterFinish:function(){picmove=false;} });
			picpos = jumploc * 1;
			if (document.getElementById(container).style.height == "auto"){
			}else{
				document.getElementById(container).style.height = "auto";
			}
		}
	}

	function imgslider(direction,picbar,xw,numpics){
		/*
		direction = backwards or forwards
		picbar = the div to move
		xw = how many pixels 'x' to move it
		numpics = number of pictures
		*/
		if (picmove == true){
		}else{
			var tempa = document.getElementById(picbar).style.left;
			var tempb = (tempa.substring(0,tempa.indexOf('p'))) * (-1);
			picpos = (tempb / xw) + 1;
			if (direction == 'forward' && picpos < numpics){
				picmove = true;
				xw = -(xw);
				new Effect.Move (picbar,{ x:xw, y:0, mode:'relative', afterFinish:function(){picmove=false;} });
			}else if (direction == 'backward' && picpos > 1){
				picmove = true;
				new Effect.Move (picbar,{ x:xw, y:0, mode:'relative', afterFinish:function(){picmove=false;} });
			}
		}
	}
	
	function imgcloser(container){
		document.getElementById(container).style.height = "14px";		
	}
	
//----------------------------------------------------------------------------------------
	
//this function controls the careers page - it makes the divs appear and disappear
	function carAppear(div_id){
		if (document.getElementById(div_id).style.height == "auto"){
			document.getElementById(div_id).style.height = "50px";
		}else{
			document.getElementById(div_id).style.height = "auto";	
		}
		document.getElementById('mcareers').style.height = "100%";
	}
