/* = CLEAR FIELD
-------------------------------------------------
	clears the value of any inpu field with the
	class name of clearDefault
-------------------------------------------------*/

function clickClear() {
	if(!document.getElementsByTagName) return false;
	
	var inputs = document.getElementsByTagName("INPUT");
	
	for ( var i = 0; i < inputs.length; i++)
	{
		if(inputs[i].className.indexOf("clearDefault") != -1)
		{
			inputs[i].onclick = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			}
			inputs[i].onfocus = function() {
				if(this.value == this.defaultValue)
				{
					this.value = "";
				}
				return 0;
			}
			inputs[i].onblur = function() {
				if(this.value == "")
				{
					this.value = this.defaultValue;
				}
				return 0;
			}
		}
	}
	return 0;
}

/* = ISLAND FACTS TEXT UPDATE
-------------------------------------------------
	Pass in the new text to be placed into the
	specified containing element.
-------------------------------------------------*/

function updateFactsText(containerId, newText) {
	$(containerId).update(newText);
	
	return 0;
}

/* = REAL ESTATE PHOTO SLIDESHOW
-------------------------------------------------
	
-------------------------------------------------*/

function prepPhotoSlideShow() {
	
	var slideShow = $('photo-slideshow');
	if(!slideShow) return false;
	
	var slideShowImage = $('photo-slideshow-image');
	if(!slideShowImage) return false;
	
	var slideShowNav = $('photo-slideshow-navigation');
	if(!slideShowNav) return false;
	
	var url = "/real_estate/ajax/slideshow.php";
	
	prepSlideShowNav(slideShowImage, slideShowNav, url);
	
	$('popup-enlarged-photo').onclick = prepEnlargePopUp;
	
	// if no errors
	return 0;
}

function prepSlideShowNav(slideShowImage, slideShowNav, url) {
	if(!slideShowNav) return 1;
	
	var prev = $('psn-prev');
	if(!prev) return 1;
	
	var next = $('psn-next');
	if(!next) return 1;
	
	// set up onclick event for previous link
	prev.onclick = function() {
		
		var newCurrPhotoNum = this.getAttribute("href").split("#")[1];
		var post = "newCurr="+newCurrPhotoNum;
		
		var getPrevPhoto = new Ajax.Request( url, { 
			method: 'post',
			postBody: post,
			onComplete: updateSlideShow(slideShowImage, slideShowNav)
		});
		
		return false;
	}
	
	// set up onclick event for next link
	next.onclick = function() {
		
		var newCurrPhotoNum = this.getAttribute("href").split("#")[1];
		var post = "newCurr="+newCurrPhotoNum;
		
		var getPrevPhoto = new Ajax.Request( url, { 
			method: 'post',
			postBody: post,
			onComplete: updateSlideShow(slideShowImage, slideShowNav)
		});
		
		return false;
	}
	
	// if no errors
	return 0;
}

function updateSlideShow(slideShowImage, slideShowNav, transport) {

	return function(transport) {
		
		var jsonResponse = transport.responseText;
		var data = jsonResponse.evalJSON();

		// update html

		slideShowImage.setAttribute("src", data.photoSrc);
		slideShowImage.setAttribute("alt", data.photoAlt);
		slideShowNav.innerHTML = data.photoNav;
		
		prepPhotoSlideShow();

		return 0;
	}

}

// -----------------------------------------------------------------------------------
//	Only using the overlay resize section
//
//	Lightbox v2.03.3
//	by Lokesh Dhakar - http://www.huddletogether.com
//	5/21/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.com), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/* = PREP ENLARGE POPUP
-------------------------------------------------*/

function prepEnlargePopUp() {
	
	if(!$('photo-popup-wrapper')) {
		var body = document.getElementsByTagName("BODY")[0];

		var photoPopupWrapper = document.createElement("div");
		photoPopupWrapper.setAttribute("id", "photo-popup-wrapper");

		var photoPopup = document.createElement("div");
		photoPopup.setAttribute("id", "photo-popup");

		var arrayPageSize = getPageSize();
		
		photoPopupWrapper.style.width = arrayPageSize[0] + "px";
		photoPopupWrapper.style.height = arrayPageSize[1] + "px";
		
		photoPopupWrapper.appendChild(photoPopup);

		body.appendChild(photoPopupWrapper);
		
		// make first ajax request to get content
		
		var url = "/real_estate/ajax/photoPopup.php";
		var post = "";

		var getContent = new Ajax.Request(url, { 
			method: 'post',
			postBody: post,
			onComplete: function(transport) {
				$('photo-popup').innerHTML = transport.responseText;
				
				var thumbLinks = $('pp-photo-nav').getElementsByTagName("A");
				
				for(var i = 0; i < thumbLinks.length; i++)
				{
					// this function relies on the id of the photo to grab to
					// be in the href after the #.  The ajax will then fetch a JSON
					// objec that has the image url, alt text, and caption text.
					thumbLinks[i].onclick = function() {
						var photoId = this.getAttribute("href").split("#")[1];
						var url = "";
						var post = "photoId="+photoId;
						
						/*var thumbAjax = new Ajax.Request(url,{
							method: post,
							postBody: post,
							onComplete: function() {
								
							}});*/
						return false;
					}
				}
				

				$('photo-popup-wrapper').onclick = $('pp-close-link').onclick = function() {
					$('photo-popup-wrapper').hide();

					return false;
				}

				$('photo-popup').onclick = function(e) {
					if (!e) var e = window.event;
					e.cancelBubble = true;
					if (e.stopPropagation) e.stopPropagation();

					return 0;
				}

				return 0;
			}});
	}
	else {
		
		// if the popup has already been created an is hidden
		$('photo-popup-wrapper').show();
		
	}
	
	return false;
}

function prepTabs(tabId) {
	
	var tabs = $(tabId);
	var tabLink = tabs.getElementsByTagName("A");
	for ( var i = 0; i < tabLink.length; i++) {
		
		tabLink[i].onclick = function() {
			var showBlockId = this.getAttribute("href").split("#")[1];

			var parentToSel = this.parentNode.parentNode;
			// to see if we are already at that state
			if(parentToSel.className.match("sel")) {
				return false;
			}

			for(var j = 0; j < tabLink.length; j++) {
				var hideBlockId = tabLink[j].getAttribute("href").split("#")[1];
				if(hideBlockId == showBlockId) {
					continue;
				}
				else {
					var parent = tabLink[j].parentNode.parentNode;
					$(parent).removeClassName('sel');
				}
				var blockToHide = $(hideBlockId);
				if(blockToHide) {
					$(blockToHide).hide();
				}
			}
			
			var blockToShow = $(showBlockId);
			if(blockToShow) {
				$(blockToShow).show();
			}
			
			$(parentToSel).addClassName('sel');
			
			return false;
		}
	}
}

function prepPopUpLink() {
	if(!document.getElementsByTagName) return true;
	
	// default height and width for popup window
	var width = "580";
	var height = "580";
	
	var links = document.getElementsByTagName("A");
	for ( var i = 0; i < links.length; i++)
	{
		if(links[i].className.indexOf("popup-link") != -1) {
			var classes = links[i].className.split(" ");
			for( var j = 0; j < classes.length; j++)
			{
				if(classes[j].indexOf("popup-link") == -1) continue;
				var splitClass = classes[j].split("-");
				if(splitClass.length <= 2) continue;
				width = splitClass[2];
				if(splitClass.length <=3) continue;
				height = splitClass[3];
			}
			
			links[i].onclick = function() {
				var href = this.getAttribute("href");
				var title = this.innerHTML;
				newWin = window.open(href,"poppage2", "resizable=yes,scrollbars=yes,status=no,toolbar=no,location=no,width="+width+",height="+height+",left=200,top=20");
				return false;
			}
			
		}
		else if (links[i].className.indexOf("new-window") != -1) {
			links[i].onclick = function() {
				var href = this.getAttribute("href");
				var title = this.innerHTML;
				newWin = window.open(href,"poppage2", "resizable=yes,scrollbars=yes,location=yes,toolbar=yes");
				return false;
			}
		}
		else {
			continue;
		}
	}

	return 0;
}

function newSearch() {
	
	// create link because init has to have a link passed in
	var link = document.createElement("A");
	// set the href of the link
	link.setAttribute("href", "/smallsearch.html");
	
	// initialize new lightbox
	var lb = new lightbox(link);
	
	// activate lightbox
	lb.activate();
	
	
	return false;
}

/* = AJAX INCLUDE FOR TEXTIMONIALS
-------------------------------------------------*/

function display_testimonials(test_type){
	new Ajax.Updater('testimonials', "/php/testimonials.php?type="+test_type)
}


/* = INIT FUNCTIONS
-------------------------------------------------
	These are functions that are loaded after page
	load but need varibles passed to them and tests
	the browser to make sure that those functions
	can be run..
-------------------------------------------------*/

function init() {
	
	if(!document.getElementById || !document.getElementsByTagName) return false;
	
	
	
	if(!document.createElement) return false;
	//prepPhotoSlideShow();
	prepPopUpLink();
	clickClear();
	
	return 0;
}

addLoadEvent(init);

/* = ON LOAD
---------------------------------------------------------------
	add the on load events
---------------------------------------------------------------*/
function addLoadEvent(func) {
	var oldOnLoad = window.onload
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}

function loadFavoritesPage() {
	window.parent.location = "index.php?mode=f&Property=search";
}
