// JavaScript Document
function addEvents() {
	if(document.getElementById('entegraRate') != null) {
		if(document.getElementById('entegraRate').checked == true) {
			document.getElementById('interestRate').onkeypress = function() {return false;};
		}
		document.getElementById('entegraRate').onchange = function() { toggleRateBox(this.value); };
		document.getElementById('idcustomRate').onchange = function() { toggleRateBox(this.value); };
	}
}

function toggleRateBox(ratetype) {
	if(ratetype == 'entegraRate') {
		document.getElementById('interestRate').value = document.getElementById('entegraRateAmount').value;
		document.getElementById('interestRate').className = "disabled";
		document.getElementById('interestRate').onkeypress = function() {return false;};
	} else if(ratetype == 'customRate') {
		document.getElementById('interestRate').onkeypress = '';
		document.getElementById('interestRate').className = "normal";
	}
}


function prepareSlideShow() {
	if(document.getElementById("slideshowimg") != null) {
		gSlideShowHTML = document.getElementById('photos_top_img').innerHTML;
	}
}

////////////////////////////// Make the Top Tabs Link //////////////////////////////

// Prepate the TABS for Linking
function prepareTab() {	
	if (!d.getElementById("tabs")) return false;
	var tabs = d.getElementById("tabs").getElementsByTagName("LI");
	for (var i=0; i<tabs.length; i++) {
		// I like cheese :)
		if (tabs[i].className != "cheese") {
			tabs[i].onmousedown = function() {
				whichTab(this);
			} 
		}
	}
}

// Switch the TABS to Display Selected
function whichTab(whichtab) {
	var tabs = d.getElementById("tabs").getElementsByTagName("LI");
	for (var i=0; i<tabs.length; i++) {
		if (tabs[i].className == "on") {
			tabs[i].className = "off";
		}
		if (tabs[i].className == "off") {
			document.getElementById(tabs[i].id+"_content").style.display = "none";
		}
	}
	
	var item = whichtab;
	item.className = "on";
	
	var element=item.id; 
	
	document.getElementById(element+"_content").style.display = "block";
}

////////////////////////////// Integrate Previous and Next Slider //////////////////////////////

var i1 = 0;
var i2 = 0;
var item_width = 80;

// Prepare the THUMBNAILS for Linking Next
function prepareThumbsNext() {
	if (!document.getElementById("thumbs_next")) return false;

	var nav_link = document.getElementById("thumbs_next");	
	nav_link.onmousedown = function() {
		movement = setTimeout("scrollThumbsNext()", 10);
		movement;
	}
}

// Move the THUMBNAILS to Scroll Next
function scrollThumbsNext() {
	if (!document.getElementById("photos_thumbs")) return false;
	var nav_images = document.getElementById("photos_thumbs"); //images container
	var list_items = nav_images.getElementsByTagName("li").length; //images
	var total_distance = ((list_items - 7) * (item_width + 1)) * -1; //total distance for the images.  this does not include the original two images and should be a negative number for moving the pictures left
	
	var current_left = parseInt (nav_images.style.left); //get the current position of the images
	if (current_left <= total_distance) { //if the images are further than the total distance, exit
		i1 = 0;
		return true;
	} else {
		if (i1 < -2) { //call this function 3 times recursively to emulate a scrolling effect.
			i1 = 0;
			nav_images.style.left = (parseInt(nav_images.style.left) - 1) + "px"; //move the image
			return true;
		} else {		
			for (var j1 = 0; j1 < (item_width / 3); j1++) {
				current_left = current_left - 1;
				nav_images.style.left = current_left + "px"; //move the image
			}
		
			move = setTimeout("scrollThumbsNext()", 10);
			i1--;
		}
	}
	/*
	if (current_left == total_distance) {
		//disable the right slider
		document.getElementById("thumbs_next").src = "images/scroll_right_grey.gif";
	} else if (current_left == 0) {
		//disable the left slider
		document.getElementById("thumbs_back").src = "images/scroll_left_grey.gif";
	} else {
		document.getElementById("thumbs_next").src = "images/scroll_right.gif";
		document.getElementById("thumbs_back").src = "images/scroll_left.gif";
	}
	*/
}

// Prepare the THUMBNAILS for Linking Back
function prepareThumbsBack() {
	if (!document.getElementById("thumbs_back")) return false;

	var nav_link = document.getElementById("thumbs_back");	
	nav_link.onmousedown = function() {
		movement = setTimeout("scrollThumbsBack()", 10);
		movement;
	}
}

// Move the THUMBNAILS to Scroll Back
function scrollThumbsBack() {
	if (!document.getElementById("photos_thumbs")) return false;
	var nav_images = document.getElementById("photos_thumbs"); //images container
	var list_items = document.getElementById("photos_thumbs").getElementsByTagName("li").length; //images
	var total_distance = ((list_items - 7) * (item_width + 1)) * -1; //total distance for the images.  this does not include the original two images and should be a negative number for moving the pictures left
	
	var current_left = parseInt (nav_images.style.left); //current location of the image
	if (current_left >= 0) { //if the images are further than the total distance, exit
		i2 = 0;
		return true;
	} else {
		if (i2 > 2) { //call this function 3 times recursively to emulate a scrolling effect.
			i2 = 0;
			nav_images.style.left = (parseInt(nav_images.style.left) + 1) + "px"; //move the image
			return true;
		} else {
			for (var j2 = 0; j2 < (item_width / 3); j2++) {
				current_left = current_left + 1;
				nav_images.style.left = current_left + "px";
			}
		
			move = setTimeout("scrollThumbsBack()", 10);
			i2++;
		}
	}
	/*
	if (current_left == total_distance) {
		//disable the right slider
		document.getElementById("thumbs_next").src = "images/scroll_right_grey.gif";
	} else if (current_left == 0) {
		//disable the left slider
		document.getElementById("thumbs_back").src = "images/scroll_left_grey.gif";
	} else {
		document.getElementById("thumbs_next").src = "images/scroll_right.gif";
		document.getElementById("thumbs_back").src = "images/scroll_left.gif";
	}
	*/
}

//disable the slider for the opera browser as it does not support the overflow:hidden style.
function disableSlider() {
	//disable the overflow: hidden style for the div and change it to overflow: auto
	document.getElementById("slide").style.overflow = "auto";
	//disable the arrow bars
	document.getElementById("scroll_left").style.display = "none";
	document.getElementById("scroll_right").style.display = "none";
	//determine the size required for the overflow div
	var olsize = document.getElementById("photos_thumbs").getElementsByName("li").length * (82);
	//reduce the size of the overflow OL for the scroll bar.
	document.getElementById("photos_thumbs").style.width = olsize;
	//document.getElementById("photos_thumbs").style.height = 135;
}

////////////////////////////// Make the Photos Thumbnails Clickable //////////////////////////////

// Display the Appropriate PHOTO for Display
function showPic(whichpic) {
	if (!document.getElementById("photos_top_img")) return true;
	var photos_placeholder = document.getElementById("photos_top_img");
	var photos_lightbox = document.getElementById("photos_lightbox");
	var source_title = whichpic.getAttribute("title");
	if(source_title != "Video") {
		var source = whichpic.getAttribute("source");
		var source_link = whichpic.getAttribute("source_link");
		//photos_placeholder.setAttribute("src",source);
		photos_placeholder.innerHTML = '<a href="' + source + '" rel="lightbox[photoset]" id="photos_lightbox" title="' + source_title + '"><img src="' + source + '" id="photos_placeholder" alt="" title="" /></a>';
		photos_lightbox.setAttribute("href",source_link);
		photos_lightbox.setAttribute("title",source_title);
		return false;
	} else {
		photos_placeholder.innerHTML = gSlideShowHTML;
	}
}

// Prepare the PHOTO GALLERY for Linking
function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("photos_thumbs")) return false;
  var gallery = document.getElementById("photos_thumbs");
  var links = gallery.getElementsByTagName("img");
  for ( var i=0; i < links.length; i++) {
    links[i].onclick = function() {
      return showPic(this);
	}
    links[i].onkeypress = links[i].onclick;
  }
}

//////////// Functions that hide/display calculator and email friend feature/////////////////

function toggleLeftNav(objA, objDiv) {
	if(objDiv.style.display == "none") {
		//get a list of the divs
		var hideList = document.getElementById('homes_extras').getElementsByTagName('div');
		//loop through the divs
		//alert(hideList.length);
		for(var i=0; i<hideList.length; i++) {
			//if the div isn't hidden, hide it
			if(!(hideList[i].style.display == "none")) {
				Effect.SlideUp(hideList[i].id); //hide the div
				document.getElementById('homes_extras').getElementsByTagName('a')[i].className = ''; //assign class to atag
			} else {
				hideList[i].style.height = '';
			}
		}
		//slide the clicked div down
		Effect.SlideDown(objDiv.id);
		//assign class
		objA.className = 'on';
	}
}

function prepareLeftNav() {
	if(document.getElementById("homes_extras") != null) {
		var navList = document.getElementById("homes_extras").getElementsByTagName("a");
		for(var i=0; i<navList.length; i++) {
			navList[i].onclick = function() { 
				toggleLeftNav(this, document.getElementById(this.getAttribute('idDiv')));
				setTimeout("setRecaptcha()", 1000);
			};
		}
	}
	if(document.getElementById('mapHome') != null) {
		document.getElementById('mapHome').style.display = 'none';
	}
}

function setRecaptcha() {
	if(document.getElementById("homes_extras") != null) {
		var divList = document.getElementById("homes_extras").getElementsByTagName("div");
		var recaptchaID = '';
		for(var i=0; i<divList.length; i++) {
			if(divList[i].style.display != 'none') {
				var output = '';
				output = output + '	<li id="recaptcha_image" class="recaptcha_image"></li>';
				output = output + '	<li>';
				output = output + '		<label for="recaptcha_response_field">Enter the words you see*</label>';
				output = output + '		<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" class="recaptcha_response_field" />';
				output = output + '	</li>';
				output = output + '	<li id="recaptcha_reload" class="recaptcha_reload"><a id="aReloadRecaptcha" href="">Or get another CAPTCHA</a></li>';
				divList[i].getElementsByTagName('ul')[0].innerHTML = output;
				recaptchaID = divList[i].getElementsByTagName('ul')[0].id;
			} else {
				if(divList[i].getElementsByTagName('ul').length > 0) {
					divList[i].getElementsByTagName('ul')[0].innerHTML = '';
				}
			}
		}
	}
	
	Recaptcha.create("6Lf04wMAAAAAABxFJVpfiNb1R53Q5JxHTKuMStFP", recaptchaID, {
		theme: 'custom', 
		lang: 'en', 
		custom_theme_widget: 'recaptcha_widget'
	});
	setTimeout("setRecaptchaWidth()", 200);
	if(document.getElementById('aReloadRecaptcha') != null) {
		document.getElementById('aReloadRecaptcha').setAttribute('href', 'javascript:Recaptcha.reload()');
	}
}

function setRecaptchaWidth() {

	var imgLI = document.getElementById('recaptcha_image');
	imgLI.style.height = '55px';
	imgLI.style.width = '290px';
	imgLI.getElementsByTagName('img')[0].style.height = '55px';
	imgLI.getElementsByTagName('img')[0].style.width = '290px';

}

//These are the functions for demographic charts


function prepareLinks(){
	
	if (!document.getElementById || !document.getElementsByTagName) {
		return;
	}
	if (!document.getElementById("demographics_nav")) {
		return;
	}
	
	var list = document.getElementById("demographics_nav");
	var links = list.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		links[i].onclick = function() {
			var query = this.getAttribute("href").split("?")[1];
			var url = "/search/details/includes/demographic_charts.php?"+query;
			return !grabFile(url);
		};
	}
}

function prepareLinks2(){
	if (!document.getElementById || !document.getElementsByTagName) {
		return;
	}
	if (!document.getElementById("labour_nav")) {
		return;
	}
	var list2 = document.getElementById("labour_nav");
	var links2 = list2.getElementsByTagName("a");
	for (var i=0; i<links2.length; i++) {
		links2[i].onclick = function() {
			var query = this.getAttribute("href").split("?")[1];
			var url = "/search/details/includes/demographic_charts.php?"+query;
			return !grabFile2(url);
		};
	}
}

function grabFile(file) {
	http = getHTTPObject();
	if (http) {
		http.onreadystatechange = parseResponse;
		http.open("GET", file, true);
		http.send(null);
		return true;
	}else{
		return false;
	}
}

function grabFile2(file) {
	http = getHTTPObject();
	if (http) {
		http.onreadystatechange = parseResponse;
		http.open("GET", file, true);
		http.send(null);
		return true;
	}else{
		return false;
	}
}


function parseResponse() {
	if (http.readyState == 4) {

		if (http.status == 200 || http.status == 304) {
			var details = document.getElementById("demographics_charts");
			details.innerHTML = http.responseText;
			prepareLinks();
			prepareLinks2();
		}
	}
}

// Map home on home details page
	function initGMap(){	
		// Function to create marker
		function createMarker(point,html) {
			var marker = new GMarker(point);
			GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
			return marker;
		}
		
		// General map variables
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		
		// Set center of map and zoom level
		var latitude = parseFloat(document.getElementById('home_latitude').value);
		var longitude = parseFloat(document.getElementById('home_longitude').value);
		map.setCenter(new GLatLng(latitude, longitude),14);
		
		// Map point with info
		var point = new GLatLng(latitude,longitude); 
		var marker = createMarker(point,'<div style="width: 200px;"></div>')
		map.addOverlay(marker);
	}

////////////////////////////// Execute the Necessary Functions //////////////////////////////

addLoadEvent(prepareLeftNav);
addLoadEvent(prepareTab);
addLoadEvent(prepareGallery);
addLoadEvent(prepareSlideShow);
addLoadEvent(addEvents);
addLoadEvent(prepareLinks);
addLoadEvent(initGMap);
addLoadEvent(setRecaptcha);
if (navigator.appName == "Opera") {
	addLoadEvent(disableSlider);
} else {
	addLoadEvent(prepareThumbsNext);
	addLoadEvent(prepareThumbsBack);
}
//addLoadEvent(map_house);