﻿/*! Copyright (C) Zebra Homes 2010 */

$(document).ready(
	function()
	{
		$("#hypSavedSearches").click(
			function()
			{
				$("#divRecentSearches").slideToggle("fast");
			});
	});
					
$(document).ready(
	function()
	{
		$("#divRecentSearches").click(
			function()
			{
				$("#divRecentSearches").slideToggle("fast");
			});
	});

$(document).ready(function() {
	$("img.js-rollover").each(function() {
		if(this.src && this.src.length > 0)
		{
			var src = this.src;
			var overImg = new Image();
			overImg.src = src.substr(0, src.lastIndexOf(".")) + "-over" + src.substr(src.lastIndexOf("."));

			$(this).mouseover(function() {
				this.src = overImg.src;
			}).mouseout(function() {
				this.src = src;
			});
		}
	});
});


/* Search Box */

var SearchForPropertiesDefaultText = "Enter Post Code or Area";

function RegisterSearchTextBox(searchTextBoxId)
{
	if(document.getElementById && document.getElementById(searchTextBoxId))
	{
		var txtSearchBox = document.getElementById(searchTextBoxId);
		RestoreDefaultSearchIfBlank(txtSearchBox);

		txtSearchBox.onfocus = function()
		{
			ClearSearchIfDefault(txtSearchBox);
		}

		txtSearchBox.onblur = function()
		{
			RestoreDefaultSearchIfBlank(txtSearchBox);
		}

		if(theForm)
		{
			theForm.onsubmit = function()
			{
				ClearSearchIfDefault(txtSearchBox);
			}
		}
	}
}

function ClearSearchIfDefault(txtSearchBox)
{
	if (txtSearchBox.value == SearchForPropertiesDefaultText)
		txtSearchBox.value = "";
}

function RestoreDefaultSearchIfBlank(txtSearchBox)
{
	if (txtSearchBox.value.replace(/ |\t/g, "") == "")
		txtSearchBox.value = SearchForPropertiesDefaultText;
}

/* RandomQueue Class */
function RandomQueue(items)
{
	this.items = items;
	this.itemsQueue = new Array();
}

RandomQueue.prototype.Pop = function()
{
	if(this.itemsQueue.length == 0) this.PopulateQueue();
	return this.itemsQueue.pop();
}

RandomQueue.prototype.PopulateQueue = function()
{
	for(var i = 0; i < this.items.length; i++)
		this.itemsQueue.push(this.items[i]);

	this.RandomiseArray(this.itemsQueue);
}

RandomQueue.prototype.RandomiseArray = function(array)
{
	for(var i = 0; i < array.length; i++)
	{
		var newIndex = this.GetRandomNumber(i, array.length);
		var temp = array[newIndex];
		array[newIndex] = array[i];
		array[i] = temp;
	}
}

RandomQueue.prototype.GetRandomNumber = function(lower, upper)
{
	return lower + Math.floor((upper - lower) * Math.random());
}

/* SponsoredPropertySlideshow Class */
function SponsoredProperties(imgRootPath, items)
{
	for(var i=0; i<items.length; i++)
	{
		items[i].imgObj = new Image();
		items[i].imgObj.src = imgRootPath + items[i].Image;
	}

	this.items = new RandomQueue(items);
	this.Begin();
}

SponsoredProperties.prototype.Begin = function()
{
	var self = this;
	this.intervalId = setInterval(
		function() { self.ShowRandomProperty(); },
		10000);
}

SponsoredProperties.prototype.ShowRandomProperty = function()
{
	var property = this.items.Pop();
	$("#divSponsoredProperty").html('<a href="PropertyDetails.aspx?ID=' + property.Id + '"><img src="' + property.imgObj.src + '" />' + property.Address + '</a>');
}

/* House Prices */

$(document).ready(  function()
					{
						$("#hypAveragePrices").click(  function ()
														{
															$("#nestoria-widget").slideToggle("slow");
														});
					});

/* Client Side "QuickLogin" Class */
function QuickLogin(quickLoginContainerId, quickLoginArgumentId)
{
	this.Show = function(loginArgument, loginMessage)
	{
		$("#" + quickLoginContainerId).show();
		$("#_QuickLoginMessage").html("<em>" + loginMessage + "</em>");
		$("#" + quickLoginArgumentId).val(loginArgument);
		$("#QuickLogin .Error").hide();
		
		this.HideAdvert();
		
		if(window.location && window.location.href.indexOf("#QuickLogin") == -1)
			window.location = window.location + "#QuickLogin";
	}
	
	this.Hide = function()
	{
		$("#" + quickLoginContainerId).hide();

		this.ShowAdvert();
	}
	
	this.HideAdvert = function()
	{
		$("#ZebraAssistAdWrapper").addClass("hideAd");
	}
	
	this.ShowAdvert = function()
	{
		$("#ZebraAssistAdWrapper").removeClass("hideAd");
	}	
}

/* Cookies, thanks to w3schools.com */
function setCookie(name, value, expireDays)
{
	var expires = new Date();
	expires.setDate(expires.getDate() + expireDays);
	
	document.cookie = name + "=" + escape(value) + ((expireDays == null) ? "" : ";expires=" + expires.toUTCString());
}

function ValidateDate(ControlName) {
	return true;
}
