var xmlhttp = createXmlHttpRequestObject();
var xmlhttpsilent = createXmlHttpRequestObject();
var currentdisplay = '';
var showerrors = true;
var appendthis = '';
var usersubsection = '';
var browsertype='';
var picupdate1='';

if (typeof window.innerWidth != 'undefined'){
		browsertype='ff';
	}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		browsertype='ie';
    }else{
		browsertype='misc';
  	};

function loaddefaults(){
	if(browsertype=='ff'){
			document.getElementById('mainframe').style.height = window.innerHeight+"px";
		}else if(browsertype=='ie'){
			document.getElementById('mainframe').style.height = document.documentElement.clientHeight+"px";
		}else{
			document.getElementById('mainframe').style.height = document.getElementsByTagName('body')[0].clientHeight+"px";
		};
	};
  		
function createXmlHttpRequestObject(){
	var xmlhttp;
	try{
		xmlhttp = new XMLHttpRequest();
		}catch(e){
			var xmlhttpversions = new Array("MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0",
											"MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
			for(i=0; i < xmlhttpversions.length && !xmlhttp; i++){
				try{
					xmlhttp = new ActiveXObject(xmlhttpversions[i]);
					}catch(e){}
				};
		};
	if(!xmlhttp){
			alert("problem creating xmlhttp request");
		}else{
			return xmlhttp;
		};
	};

function displayerror(message){
	if(showerrors){
		showerrors = false;
		alert("error encountered: \n"+message);
		setTimeout("setstuff();", 1000);
		};
	};

function resetstuff(a){
	if(a == 'reset'){
		document.getElementById('mainpage').innerHTML = '';
		document.getElementById('housepage').innerHTML = '';
		document.getElementById('greenhousepage').innerHTML = '';
		document.getElementById('solarpage').innerHTML = '';
		document.getElementById('aboutuspage').innerHTML = '';
		document.getElementById('gallerypage').innerHTML = '';
		if(document.getElementById('bubblepos1')){
			document.getElementById('bubblepos1').innerHTML = '';
			};
		};
	};

function setstuff(a,b,c){
	document.getElementById('locallocation').value = a;
	
	if(browsertype == 'ie'){
		//need to reset height of element for next data
		document.getElementById("mainbackground").style.height = document.documentElement.offsetHeight+"px";
	}else{
		//need to reset height of element for next data
		document.getElementById("mainbackground").style.height = (window.outerHeight - 130) + "px";
	};

	if(c){
		resetstuff(c);
		};

	if(xmlhttp){

	try{
		if((xmlhttp.readyState == 4 || xmlhttp.readyState == 0)){
			xmlhttp.open("GET", "mainajax.php?"+b, true);
			xmlhttp.onreadystatechange = handlerequeststatechange;
			xmlhttp.send(null);
			};
		}catch(e){
			displayerror(e.toString());
		};
	};

	};

function handlerequeststatechange(){
	if(xmlhttp.readyState == 4){
		if(xmlhttp.status == 200){
			try{
				readresponse();
				}catch(e){
					displayerror(e.toString());
				};
			}else{
				displayerror(xmlhttp.statusText);
			};
		};
	};

function readresponse(){
	//only do something with return data if needed
	if(document.getElementById('localwait').value == ''){

	var locallocation = document.getElementById('locallocation').value;					//display straight up value
	var mydiv = document.getElementById(locallocation);

	//write response text from above
	var response = xmlhttp.responseText;
//alert(response);

	if(appendthis == ''){
			sethtml(locallocation,response,'no');
		}else{
			//append data to section instead of overwriting and reset pointer
			sethtml(locallocation,response,'yes');
			appendthis = '';
		};

	//reset this value back to nothing for next use
	var currentlocation = '';
		};

	//reset the waiting image back to none
	if(document.getElementById('localwait').value != ''){
	//	alert(xmlhttp.responseText);
		var waiting = document.getElementById('localwait').value;
		var parent = document.getElementById(waiting);
        var child = document.getElementById('somethingwaiting');
        parent.removeChild(child);

		document.getElementById('localwait').value = '';
		};
	};

//-----------------------------------------------//
//use this function to process any javascript passed back from ajax
function sethtml(div,content,appendthis){
    var search = content;
    var script;

	if(appendthis == 'yes'){
			document.getElementById(div).innerHTML += content;
		}else{
    		document.getElementById(div).innerHTML = content;
    	};

    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)){
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);

      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;
      	block = search.substr(0, search.indexOf(RegExp.$1));
      	search = search.substring(block.length + RegExp.$1.length);
      	var oScript = document.createElement('script');
      	oScript.text = block;
      	document.getElementsByTagName("head").item(0).appendChild(oScript);
    	};

    screenshuffle();
   	appendthis = '';
	};   
//----------------------------------------------//

function menureset(){
	document.getElementById('homebutton').src = 'images/homebutton.gif';
	document.getElementById('housebutton').src = 'images/housebutton.gif';
	document.getElementById('greenhousebutton').src = 'images/greenhousebutton.gif';
	document.getElementById('solarbutton').src = 'images/solarbutton.gif';
	document.getElementById('aboutusbutton').src = 'images/aboutusbutton.gif';
	document.getElementById('gallerybutton').src = 'images/gallerybutton.gif';
	};

function menuchange(location,locallocation,extras){
	menureset();
	if(extras){extras = "&"+extras}else{extras='';};
	document.getElementById(location).src = 'images/'+location+'1.gif';
	setstuff(locallocation,locallocation+'=1'+extras,'reset');
	};
	
function housemenu(location,action){
	if(action == 'showhousemenu'){
		appendthis = 'yes';
		setstuff(location,action+'=1');
		};
	};

function hidemenu(a){
	if(document.getElementById('housemenu')){
		document.getElementById('housepage').removeChild(document.getElementById('housemenu'));
		};
	};
	
function loaddefault(){
	buildhiddendivs();
	menuchange('homebutton','mainpage');
   	var htmlheight = document.documentElement.scrollHeight;  
   	var windowheight = document.documentElement.offsetHeight;
   	//loaddefaults();
	};
	
function buildhiddendivs(){
	var bubblediv = document.createElement("div");
	bubblediv.id = "bubblepos1";
	bubblediv.className = "bubbleposinit";
	document.getElementById("mainframe").appendChild(bubblediv);
	};
	
function picupdate(picdir,picname,comment){
	var pic = document.getElementById('bubbleimage');
	var com = document.getElementById('bubblecomment');
	pic.src='pics/'+picdir+'/'+picname;
	com.innerHTML = comment;
	
	picupdate1 = (Number(pic.height) + Number(com.clientHeight)) + 100;
	//alert(picupdate1)
	document.getElementById("bubblepos1").style.height = picupdate1+"px";
	screenshuffle();
	};
	
function screenshuffle(){
    //grab size of returning data and resize background div so it extends whole height
  	var htmlheight = document.documentElement.scrollHeight;  
   	var windowheight = document.documentElement.offsetHeight;
   	
   	if ( htmlheight < windowheight ){ 
			document.getElementById("mainbackground").style.height = windowheight + "px";
		}else{ 
			document.getElementById("mainbackground").style.height = htmlheight + "px";
		};
	};
