// JavaScript Document

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 addToLastViewed(development) {
	if (development == "") return;
	
	var lastViewedPosOne = "";
	lastViewedPosOne = readCookie("lastViewedPosOne");
	if (development != lastViewedPosOne && (lastViewedPosOne != "" || lastViewedPosOne != null)) {
		createCookie("lastViewedPosTwo", lastViewedPosOne, 1);
	}
	createCookie("lastViewedPosOne", development, 1);		
}

function displayLastViewed(pos) {
  // Opens & Reads XML file
  if (window.XMLHttpRequest) {
	xhttp=new XMLHttpRequest()
  } else {
	xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhttp.open("GET","/menu.xml",false);
  xhttp.send("");
  xmlDoc=xhttp.responseXML;


	var lastViewedDev = "";
	if (pos == 1) {
		lastViewedDev = readCookie("lastViewedPosOne");
	} else {
		lastViewedDev = readCookie("lastViewedPosTwo");
	}
	
	var thisLink = "";
	var thisTitle = "";
	var thisDescription = "";
	var thisDev = "";
	var x = xmlDoc.getElementsByTagName("development");
	for (i=0;i<x.length;i++) {
		thisDev = x[i].getAttribute("name");
		if (thisDev == lastViewedDev) {
			thisTitle = x[i].getAttribute("title");
			thisLink = x[i].getAttribute("link");
			thisDescription = x[i].getAttribute("description");
			break;
		}
	}
	
	document.write("<div class=" + "\"" + "inner" + "\"" + "><p class=" + "\"" + "tbold" + "\"" + "><a href=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</a></p>" + "<p>" + thisDescription + "</p></div>");
}



function loadNews(maxItems) {
  // Opens & Reads XML file
  if (window.XMLHttpRequest) {
	xhttp=new XMLHttpRequest()
  } else {
	xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhttp.open("GET","/menu.xml",false);
  xhttp.send("");
  xmlDoc=xhttp.responseXML;
  
	var x = xmlDoc.getElementsByTagName("news")[0].getElementsByTagName("article");
	for (i=0;i<x.length && i < maxItems;i++) {
	  var thisTitle = x[i].getAttribute("title");
	  var thisDate = x[i].getAttribute("date");
	  var thisLink = x[i].getAttribute("link");
  
	  document.write("<div class=" + "\"" + "news-list" + "\"" + ">");
	  document.write("<p class=" + "\"" + "news-title" + "\"" + "><a href=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</a></p>");
	  document.write("<p class=" + "\"" + "news-date" + "\"" + ">" + thisDate + "</p>");
	  document.write("</div>");
	}
}

function displayCurrentDate() {
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var monthStr = "";
	var weekDay = currentTime.getDay() + 1;
	var weekDayStr = "";
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	
	switch (weekDay) {
		case 1:
			weekDayStr = "Sunday";
			break;
		case 2:
			weekDayStr = "Monday";
			break;
		case 3:
			weekDayStr = "Tuesday";
			break;
		case 4:
			weekDayStr = "Wednesday";
			break;
		case 5:
			weekDayStr = "Thursday";
			break;
		case 6:
			weekDayStr = "Friday";
			break;
		case 7:
			weekDayStr = "Saturday";
			break;
	}
	
	switch (month) {
		case 1:
			monthStr = "January";
			break;
		case 2:
			monthStr = "February";
			break;
		case 3:
			monthStr = "March";
			break;
		case 4:
			monthStr = "April";
			break;
		case 5:
			monthStr = "May";
			break;
		case 6:
			monthStr = "June";
			break;
		case 7:
			monthStr = "July";
			break;
		case 8:
			monthStr = "August";
			break;
		case 9:
			monthStr = "September";
			break;
		case 10:
			monthStr = "October";
			break;
		case 11:
			monthStr = "November";
			break;
		case 12:
			monthStr = "December";
			break;
	}
	
	document.write("<div id=" + "\"" + "pdate" + "\"" + ">" + weekDayStr + " " + day + " " + monthStr + " " + year + "</div>");

/*	
<div id="pdate">Friday 06 August 2010</div>
*/
}


var isEven = function(someNumber){
	return (someNumber%2 == 0) ? true : false;
};

function loadHomesForSale(isSold) {
  // Opens & Reads XML file
  if (window.XMLHttpRequest) {
	xhttp=new XMLHttpRequest()
  } else {
	xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhttp.open("GET","/menu.xml",false);
  xhttp.send("");
  xmlDoc=xhttp.responseXML;
  
  var devType;
  var markerLink;
  if (isSold) {
	  devType = "developments_sold";
	  markerLink = "/images/framework/bullet-blue.gif";
  } else {
	  devType = "developments_for_sale";
	  markerLink = "/images/framework/bullet-red.gif";
  }
  
  var x = xmlDoc.getElementsByTagName(devType)[0].getElementsByTagName("development");
  for (i=0;i<x.length;i++) {
	  var thisDev = x[i].getAttribute("name");
	  var thisType = x[i].getAttribute("type");
	  var thisTitle = x[i].getAttribute("title");
	  var thisLocation = x[i].getAttribute("location");
	  var thisLink = x[i].getAttribute("link");
	  
	  var colourScheme = "";
	  if (isEven(i)) {
		  colourScheme = " bggrey";
      }

	  document.write("<tr>");
	  document.write("<td class=" + "\"" + "property" + colourScheme + "\"" + "><a href=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</a></td>");
	  document.write("<td class=" + "\"" + "location" + colourScheme + "\"" + ">" + thisLocation + "</td>");
	  document.write("<td class=" + "\"" + "type" + colourScheme + "\"" + ">" + thisType + "</td>");
	  document.write("<td class=" + "\"" + "availability" + colourScheme + "\"" + "><a href=" + "\"" + thisLink + "\"" + "><img src=" + "\"" + markerLink + "\"" + " alt" + "\"" + "\"" + " border=" + "\"" + "0" + "\"" + "/></a></td>");
	  document.write("</tr>");
  }
}



function loadMenus(currentDev, currentDetail, currentSubDetail) {
  // Opens & Reads XML file
  if (window.XMLHttpRequest) {
	xhttp=new XMLHttpRequest()
  } else {
	xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhttp.open("GET","/menu.xml",false);
  xhttp.send("");
  xmlDoc=xhttp.responseXML;
  
  // Display menus

  displayDevelopments("developments_for_sale", currentDev, currentDetail, currentSubDetail);
  
document.write("<div class=" + "\"" + "disappear" + "\"" + " style=" + "\"" + "background-color: #404040; padding: 1px 0px 5px 0px;" + "\"" + "></div>");
document.write("<div class=" + "\"" + "disappear" + "\"" + " style=" + "\"" + "background-color: #404040; padding: 1px 0px 5px 0px;" + "\"" + "></div>");
document.write("<div class=" + "\"" + "disappear" + "\"" + " style=" + "\"" + "background-color: #404040; padding: 1px 0px 5px 0px;" + "\"" + "></div>");
document.write("<div>&nbsp;</div>");


document.write("<img src=" + "\"" + "/images/framework/bullet-blue-grey.gif" + "\"" + " class=" + "\"" + "lstitle" + "\"" + " alt=" + "\"" + "\"" + " border=" + "\"" + "0" + "\"" + "/>");
document.write("<span style=" + "\"" + "position: relative; top: -5px; color: #ffffff; font-weight: normal; font-size: 14px;" + "\"" + ">Sold</span>");
document.write("<div class=" + "\"" + "disappear" + "\"" + " style=" + "\"" + "background-color: #404040; padding: 1px 0px 5px 0px;" + "\"" + ">");
document.write("<div class=" + "\"" + "disappear" + "\"" + "></div>");
document.write("</div>");

  displayDevelopments("developments_sold", currentDev, currentDetail, currentSubDetail);	

  addToLastViewed(currentDev);
}


// Function used to display menus
function displayDevelopments(devType, currentDev, currentDetail, currentSubDetail) {
	var x = xmlDoc.getElementsByTagName(devType)[0].getElementsByTagName("development");
	for (i=0;i<x.length;i++) {
	  var thisDev = x[i].getAttribute("name");
	  var thisTitle = x[i].getAttribute("title");
	  var thisLink = x[i].getAttribute("link");
	  var myStr = "";
	  if (thisDev == currentDev) {
		  myStr = "<div class=" + "\"" + "lmenu-item-on" + "\"" + "><a href=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</a></div>";
		  document.write(myStr);
		  document.write("<div class=" + "\"" + "appear" + "\"" + " style=" + "\"" + " background-color: #404040; padding: 1px 0px 5px 0px;" + "\"" +">");
		  // list details
		  var y = x[i].getElementsByTagName("detail");
		  for (j=0;j<y.length;j++) {
			  var thisDetail = y[j].getAttribute("name");
			  var thisDetailTitle = y[j].getAttribute("title");
			  var thisDetailLink = y[j].getAttribute("link");
			  if (thisDetail == currentDetail) {
				  myStr = "<div class=" + "\"" + "lmenu-sub-item-on" + "\"" + ">&nbsp;&nbsp;&nbsp;&nbsp;<a href=" + "\"" + thisDetailLink + "\"" + ">" + thisDetailTitle + "</a></div>";
			      document.write(myStr);
				  // list sub details
				  var z = y[j].getElementsByTagName("sub-detail");
				  document.write("<div id=" + "\"" + "smi142" + "\"" + " class=" + "\"" + "appear" + "\"" + ">");
				  for (k=0;k<z.length;k++) {
					  var thisSubDetail = z[k].getAttribute("name");
					  var thisSubDetailTitle = z[k].getAttribute("title");
					  var thisSubDetailLink = z[k].getAttribute("link");
					  if (thisSubDetail == currentSubDetail || (currentSubDetail == "" && k == 0) ) {
						  myStr = "<div class=" + "\"" + "lmenu-sub-item-on" + "\"" + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=" + "\"" + thisSubDetailLink + "\"" + ">" + thisSubDetailTitle + "</a></div>";
					      document.write(myStr);
					  } // end if condition
					  else {
						  myStr = "<div class=" + "\"" + "lmenu-item" + "\"" + ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=" + "\"" + thisSubDetailLink + "\"" + ">" + thisSubDetailTitle + "</a></div>";
					      document.write(myStr);
				  	  } // end else condition
				  } // end bottom level loop
			      document.write("</div>");
			  } // end if condition 
			  else {
				  myStr = "<div class=" + "\"" + "lmenu-item" + "\"" + ">&nbsp;&nbsp;&nbsp;&nbsp;<a href=" + "\"" + thisDetailLink + "\"" + ">" + thisDetailTitle + "</a></div>";
			      document.write(myStr);
			  } // end else condition
		  } // end middle level loop
		  document.write("</div>");
	  } // end if within top level
	  else {
		  myStr = "<div class=" + "\"" + "lmenu-top-item" + "\"" + "><a href=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</a></div>";
	      document.write(myStr);
	  } // end else within top level
	} // end top level loop
} // end function


function displayQuickLinks() {
  // Opens & Reads XML file
  if (window.XMLHttpRequest) {
	xhttp=new XMLHttpRequest()
  } else {
	xhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  xhttp.open("GET","/menu.xml",false);
  xhttp.send("");
  xmlDoc=xhttp.responseXML;

	var x=xmlDoc.getElementsByTagName("developments_for_sale")[0].getElementsByTagName("development");
	for (i=0;i<x.length;i++) {
	  var thisTitle = x[i].getAttribute("title");
	  var thisLink = x[i].getAttribute("link");
	  var myStr = "<option value=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</option>";
	  document.write(myStr);	  				
	}

	x=xmlDoc.getElementsByTagName("developments_sold")[0].getElementsByTagName("development");
	for (i=0;i<x.length;i++) {
	  var thisTitle = x[i].getAttribute("title");
	  var thisLink = x[i].getAttribute("link");
	  var myStr = "<option value=" + "\"" + thisLink + "\"" + ">" + thisTitle + "</option>";
	  document.write(myStr);	  				
	}
}


