var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


var so;
var soMenu;
var lid;
var setlid;
var map = null;
var geocoder = null;
var pubMapDiv = null;
var pubMapLayer = null;
//var trafficInfo = new GTrafficOverlay();
var initContent = 'images';
var toggleState = 1;
	
	//Gets variables from the url that have been posted using GET method
	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
	}
	
    function loadMap(address) {
      if (GBrowserIsCompatible()) {
	  	pubMapDiv = xGetElementById("overlayTour_tourContent");
        map = new GMap2(pubMapDiv);
       // map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();
		if(address)showAddress(address);
		map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setMapType(G_HYBRID_MAP);
      }
    }
	
	function toggleTraffic() {
      if (toggleState == 1) {
        map.removeOverlay(trafficInfo);
        toggleState = 0;
      } else {
        map.addOverlay(trafficInfo);
        toggleState = 1;
      }
	  }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
      }});}}
		
//Newer object AJAX
//MAPPING
function getMapDataRequest(LID) {
	var mXMLHttpRequest	= new XMLHttpRequest;
	
	mXMLHttpRequest.open("GET", 'PH_modules/Classes/PHP/pub_reqData.php?action=' +
								Url.encode("http://www.publishhomes.com/domains/api/pub_getMapData.php") +
								'&method=get&lid='+LID, true );
	mXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {
			var res = mXMLHttpRequest.responseText;
			loadMap(res);
			setMenuHighlight('map');
			xGetElementById('overlayTour_tourContent').style.backgroundColor = "white";
			
		}
	}
	mXMLHttpRequest.send(null);
}

//TOUR START
function getClickDataRequest(lid, resolution) {
	var tXMLHttpRequest	= new XMLHttpRequest;
	tXMLHttpRequest.open("GET", "http://www.publishhomes.com/domains/api/track_listings.php?l="+lid+"&r="+resolution, true);
	tXMLHttpRequest.send(null);
}

//HOME DATA
function getHomeDataRequest(LID) {
	var hXMLHttpRequest	= new XMLHttpRequest;
	
	hXMLHttpRequest.open("GET", 'PH_modules/Classes/PHP/pub_reqData.php?action=' + 
					Url.encode("http://www.publishhomes.com/domains/api/pub_getHomeData_phase2.php") +
					'&method=get&lid='+LID, true );
	hXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {
			//alert("homedata done");
			var res = hXMLHttpRequest.responseText;
			xGetElementById('overlayTour_tourContent').style.backgroundColor = "white";
			xGetElementById('overlayTour_tourContent').innerHTML = res;
			setMenuHighlight('details');
		}
	}
	hXMLHttpRequest.send(null);
}

//Video DATA - Currently Jade Mills Only
function getVideoDataRequest(LID) {
	var hXMLHttpRequest	= new XMLHttpRequest;
	
	hXMLHttpRequest.open("GET", 'PH_modules/Classes/PHP/pub_reqData.php?action=' + 
					Url.encode("http://www.publishhomes.com/domains/api/pub_getVideoData.php") +
					'&method=get&lid='+LID, true );
	hXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {
			//alert("homedata done");
			var res = hXMLHttpRequest.responseText;
			xGetElementById('overlayTour_tourContent').style.backgroundColor = "white";
			xGetElementById('overlayTour_tourContent').innerHTML = res;
			setMenuHighlight('video');
		}
	}
	hXMLHttpRequest.send(null);
}

//AGENT DATA
function getAgentDataRequest(LID) {
	var aXMLHttpRequest	= new XMLHttpRequest;
	aXMLHttpRequest.open("GET", 'PH_modules/Classes/PHP/pub_reqData.php?action=' + 
					Url.encode('www.publishhomes.com/domains/api/pub_getAgentData_phase2.php') +
					'&method=get&lid='+LID+'&dm=1&agent=181', true );
	aXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {
			var res = aXMLHttpRequest.responseText;
			xGetElementById('overlayTour_tourContent').style.backgroundColor = "white";
			xGetElementById('overlayTour_tourContent').innerHTML = res;	
			setMenuHighlight('contact');
		}
	}
	aXMLHttpRequest.send(null);
}

function setMenuHighlight(section){
	if(section == 'images') xGetElementById('overlayTour_menu_button_images').className  = 'overlayTour_menu_button_selected'; else xGetElementById('overlayTour_menu_button_images').className  = 'overlayTour_menu_button';
	if(section == 'video') xGetElementById('overlayTour_menu_button_video').className  = 'overlayTour_menu_button_selected'; else xGetElementById('overlayTour_menu_button_video').className  = 'overlayTour_menu_button';
	if(section == 'details') xGetElementById('overlayTour_menu_button_details').className  = 'overlayTour_menu_button_selected'; else xGetElementById('overlayTour_menu_button_details').className  = 'overlayTour_menu_button';
	if(section == 'map') xGetElementById('overlayTour_menu_button_map').className  = 'overlayTour_menu_button_selected'; else xGetElementById('overlayTour_menu_button_map').className  = 'overlayTour_menu_button';
	if(section == 'contact') xGetElementById('overlayTour_menu_button_contact').className  = 'overlayTour_menu_button_selected'; else xGetElementById('overlayTour_menu_button_contact').className  = 'overlayTour_menu_button';
}

//AJAX controls
function bootStrap_overlayTour_start(lid, type, initData){
	//alert("tourstart"+lid);
	var bXMLHttpRequest	= new XMLHttpRequest;
	setlid = lid;
    strapType = type;
    initContent = initData;
	bXMLHttpRequest.open("GET", "PH_modules/Classes/PHP/overlayTour_bootstrap.php?s=start&l="+lid+"&t="+type+"&i="+initData, true );
	bXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {			
			rescode = bXMLHttpRequest.responseText;  // These following lines get the response and update the page
			//alert(rescode);
			xGetElementById('overlayContent').innerHTML = rescode;	
			
			xOpacity("overlayTour_background", .7);
			xOpacity("overlayTour_outerBorder", .4);
				
			var el = xGetElementById('overlayTour_tourContent');
				
			so = new SWFObject("http://www.publishhomes.com/domains/api/tourapi_domains_phase2.swf", "tourflash", "800", "540", "9", "#000");
			//so.addParam("wmode", "opaque");
			so.addParam("swliveconnect", "true");
			so.addVariable("lid", setlid);
			so.addVariable("audioID", 4); //not used but kept for future purposes
			so.write(el);
			xWinScrollTo(window, 0, 0, 500);
			setMenuHighlight('images');
		}
	}
	bXMLHttpRequest.send(null);
}

function bootStrap_overlayTour_section(lid, initData){
  http.open("GET", "PH_modules/Classes/PHP/overlayTour_bootstrap.php?s=section&l="+lid+"&t="+type+"&i="+initData, true);
  http.onreadystatechange = getBootStrapResponseSection;
  http.send(null);	
}

function bootStrap_overlayTour_remove(){
	var bXMLHttpRequest	= new XMLHttpRequest;
	bXMLHttpRequest.open("GET", "PH_modules/Classes/PHP/overlayTour_bootstrap.php?s=remove", true );
	bXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {			
			rescode = bXMLHttpRequest.responseText;  // These following lines get the response and update the page
			 xGetElementById('overlayContent').innerHTML = rescode;	
		}
	}
	bXMLHttpRequest.send(null);
	
 
}

//Tour DATA
function getTourDataRequest(LID) {
	var bXMLHttpRequest	= new XMLHttpRequest;
	setlid = lid;
    strapType = type;
    initContent = initData;
	bXMLHttpRequest.open("GET", "PH_modules/Classes/PHP/overlayTour_bootstrap.php?s=start&l="+lid+"&t="+type+"&i="+initData, true );
	bXMLHttpRequest.onreadystatechange	= function() {
		if (this.readyState == XMLHttpRequest.DONE) {			
			rescode = bXMLHttpRequest.responseText;  // These following lines get the response and update the page
			//alert(rescode);
			xGetElementById('overlayContent').innerHTML = rescode;	
			
			xOpacity("overlayTour_background", .7);
			xOpacity("overlayTour_outerBorder", .4);
				
			var el = xGetElementById('overlayTour_tourContent');
				
			so = new SWFObject("http://www.publishhomes.com/domains/api/tourapi_domains_phase2.swf", "tourflash", "800", "540", "9", "#000");
			//so.addParam("wmode", "opaque");
			so.addParam("swliveconnect", "true");
			so.addVariable("lid", setlid);
			so.addVariable("audioID", 4); //not used but kept for future purposes
			so.write(el);
			xWinScrollTo(window, 0, 0, 500);
		}
	}
	bXMLHttpRequest.send(null);
}



function getBootStrapResponseSection() {
  if (http.readyState == 4) { 
    rescode = http.responseText;  // These following lines get the response and update the page
	//alert(rescode);
    xGetElementById('overlayContent').innerHTML = rescode;	

	
  }
}

//Flash recognition controls
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName];
    }
}

function callExternalInterface(bool) {


//if(bool == "off"){   document.getElementById('tourflash').flashDisabled();}
//if(bool == "on"){   document.getElementById('tourflash').flashEnabled();}
}

function removeinput(x) {

if (x == 'Type email address here') {
x = '';
}
}

function showFormElements(ElementId){
 var oElement = document.getElementById(ElementId);

    oElement.style.visibility = "visible";
}

function hideFormElements(ElementId){
 var oElement = document.getElementById(ElementId);
  
    oElement.style.visibility = "hidden";
  
}


function openFlyer(photoid, audioID) {
				window.open("alpha_flyer.php?lid="+photoid+"&aid="+audioID, "_blank");
}

function openEmail(lid, address){
	var urllink = Url.encode("http://www.publishhomes.com/pub_tour.php?lid="+lid+"&aid=181");
	var urlAddress = Url.encode(address);
	var mailto_link = 'mailto: ?subject='+urlAddress+'&body='+urllink;
	window.open(mailto_link,'emailWindow');	
}

function hideTour(){
	document.getElementById('tourLayer').style.visibility = "hidden";
	document.getElementById('pubShading').style.visibility = "hidden";
	
	
}

