function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	return false;
    }
}



// CHECKBOX CHANGER: function to control checkboxes
// The JavaScript Source!! http://javascript.internet.com -->
// Use checkMethod: 0 1 2, for uncheck all, check all, switch all, respectively
function checkAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
if (box.checked == false) box.checked = true;
   }
}

function uncheckAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
if (box.checked == true) box.checked = false;
   }
}

function switchAll() {
for (var j = 1; j <= 14; j++) {
box = eval("document.checkboxform.C" + j); 
box.checked = !box.checked;
   }
}

function checkControl(checkMethod, itemCount, formName) {
	for (var j = 1; j <= itemCount; j++) {
	box = eval("document."+formName+".C" + j); 
	if (checkMethod == 0) box.checked = false;
	//   else
	//   if (checkMethod == 1) box.checked = true;
	//  else
	//   if (checkMethod == 2) box.checked = !box-checked;
   }
}

// The windowWidth routine based on code at:
//    http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
// We currently don't need the height, but the code is left for possible future needs.

function windowWidth() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
  }
  return myWidth;
}

// The left nav bar width is 20% of the window width.
function iFrameLeftCoordinate() {
  var leftCoordinate = 0;
  leftCoordinate = Math.max(Math.round((windowWidth() * 0.2)) + 15, 215);
  return leftCoordinate;
}

function iFrameWidth() {
  var frameWidth = 0;
  frameWidth = Math.min(windowWidth() - iFrameLeftCoordinate() - 30, 700);
  return frameWidth;
}


// The functions between the CDATA comments are for showing maps using Google
//<![CDATA[

var geocoder = null;
var propertyMap = null;
var baseIcon = null;
var mapAddress = "";
var mapAddressArray = new Array();
var siteBase = "http://www.barnhartassociates.com/";
var imageBase = siteBase + "images/";
var propDetailURL = null;

// Define some constants for code readability
var mlsNum = 0; 
var propType = 1;
var propAddress1 = 2;
var propAddress2 = 3;
var photoURL = 4;
var propPrice = 5;
var propVal1 = 6;
var propVal2 = 7;
var propAgent = 8;
var propLatitude = 9;
var propLongitude = 10;


// The next two functions are used on the property detail pages.
function showAddress() {
  if (geocoder) {
	  geocoder.getLatLng(
		mapAddress,
		function(point) {
		  if (!point) {
			alert("Map address not found.");
		  } else {
			propertyMap.setCenter(point, 13);
			var marker = new GMarker(point);
			propertyMap.addOverlay(marker);
			// GEvent.addListener(marker, "click", function() {
			// 	marker.openInfoWindowHtml(mapAddress);
			// 	}
			// );
		  }
		}
	  );
	}
}

function loadMap() {
  //DEBUG alert("mapAddress = " + mapAddress);
  if (GBrowserIsCompatible()) {
	propertyMap = new GMap2(document.getElementById("map"));
	geocoder = new GClientGeocoder();
	propertyMap.addControl(new GSmallMapControl());  
	showAddress();
  }
}



// This functions in this section are used by the multi-pin maps.

// The functions below are necessary to make the Google maps operate properly with the concept
// of "closures". More info at these links (or Google for "Javascript function closures".
// http://www.javascriptkit.com/javatutors/closures2.shtml
// http://joust.kano.net/weblog/archive/2005/08/08/a-huge-gotcha-with-javascript-closures/

// Because this file is loaded before the Google javascript source file, we need to wrap the baseIcon
// declaration in this routine so it can be called after the Google source is loaded. If this is
// not done, then the GIcon class will not yet be defined.
function defineBaseIcon() {
	baseIcon = new GIcon();
	baseIcon.shadow = imageBase + "markerShadow.png";
	baseIcon.iconSize = new GSize(32, 32);
	baseIcon.shadowSize = new GSize(59, 32);
	baseIcon.iconAnchor = new GPoint(16, 32);
	baseIcon.infoWindowAnchor = new GPoint(16, 2);
	baseIcon.infoShadowAnchor = new GPoint(25, 25);
}

function defineBaseIconSmall() {
	baseIconSmall = new GIcon();
	baseIconSmall.shadow = imageBase + "markerShadowSmall.png";
	baseIconSmall.iconSize = new GSize(16, 16);
	baseIconSmall.shadowSize = new GSize(30, 16);
	baseIconSmall.iconAnchor = new GPoint(8, 16);
	baseIconSmall.infoWindowAnchor = new GPoint(8, 1);
	baseIconSmall.infoShadowAnchor = new GPoint(12, 12);
}

function noPhotoAvailable(theImage) {
	theImage.src = imageBase + "no_photo.jpg";
}

function createMarker(point, pinArray) {
	var htmlStr = "<img src=\"" + pinArray[photoURL] 
			+ "\" style=\"float: left; margin-right: 5px;\" width=\"80px\" height=\"60px\" onerror=\"noPhotoAvailable(this)\" />" 
			+ pinArray[propPrice] + pinArray[propVal1] + pinArray[propVal2] 
			+ "<a href=\"" + propDetailURL + "?ln="
			+ pinArray[mlsNum] + "&pg=1\">Click for details</a>";

	if (pinArray[propAgent]) {
		var icon = new GIcon(baseIcon);
		icon.image =  imageBase + "markerBlueDot.png";
	} else {
		var icon = new GIcon(baseIconSmall);
		icon.image =  imageBase + "markerBurgundyDotSmall.png";
	}

	var marker = new GMarker(point, icon);
	// GEvent.addListener(marker, "mouseover", function() {
	//	marker.openInfoWindowHtml(htmlStr);
	//	}
	//);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(htmlStr);
		}
	);
	return marker;
}

function createPin(pinArray) {
	var point = new GLatLng(pinArray[propLatitude], pinArray[propLongitude]);
	if (point) {
		// alert("createPin mapAddress = " + pinAddress);
		propertyMap.addOverlay(createMarker(point, pinArray));
	}
}

function showAddressArray() {
  	var funcArray = [];
  	for (var arrayIndex in mapAddressArray) {
  	  var pinArray = mapAddressArray[arrayIndex];
	  // alert("showAddressArray mapAddress = " + pinAddress);
	  
	  // Use "Function" keyword (with upper case "F"!) to type cast so browsers don't complain
	  var funcPtr = Function(createPin(pinArray));
	  funcArray.push(funcPtr);
	}
	for (var j in funcArray) {
		funcArray[j]();
	}
}

function loadAgentMap() {
  if (GBrowserIsCompatible()) {
  	
  	// The map object on agent pages must have an id of "agentmap". The map object on the 
  	//    Barnhart home page must have an id of "homepagemap".
  	var mapdiv = document.getElementById("agentmap");
  	if (mapdiv) {
  		propDetailURL = "mls_detail_pnwrp_agent_site.php";
  	} else {
  		mapdiv = document.getElementById("homepagemap");
  		propDetailURL =  siteBase + "mls/mls_detail_pnwrp.php";
  	}
  	propertyMap = new GMap2(mapdiv);
  		
	defineBaseIcon();
	defineBaseIconSmall();
	propertyMap.addControl(new GSmallMapControl());  
	propertyMap.setCenter(new GLatLng(44.0590, -123.0675), 9);
	showAddressArray();
  }
}

//]]>
