﻿// APLHpropertyresults.js
// Nick Casey 28/05/2007
// Active script for aplacelikehome property results table
// Using hesido.com colour animations

var initialResultsTableString;

var selectedColor = '#c9b873'; //201,184,115
var unselectedColor = '#FFF8F2'; //255, 248, 242

function initResultsTableEvents()
{
    var results = document.getElementById("property");
    
    if(results)
    {
        results.onclick = do_resultsclick;
	    if (results.captureEvents) results.captureEvents(Event.CLICK);
	    results.ondblclick = do_resultsclick;
	    if (results.captureEvents) results.captureEvents(Event.DBLCLICK);

        results.onmouseover = do_resultsmouseover;
	    if (results.captureEvents) results.captureEvents(Event.MOUSEOVER);
	    results.onmouseout = do_resultsmouseout;
	    if (results.captureEvents) results.captureEvents(Event.MOUSEOUT);
    }
}


//fired on reults table mouseover
function do_resultsclick(e) 
{
    //find the row clicked
    var backToList = document.getElementById("property").getAttribute("list");
	var row = selectedRow(e);
	if (row)
	{
	    var property = row.getAttribute("property");
	    window.location.href = "property.aspx?pid=" + property + "&list=" + backToList;
	}
	
}

//fired on reults table mouseover
function do_resultsmouseover(e) 
{
 
    //find the row clicked
	var row = selectedRow(e);
		
	if (row)
	{
        //colour background of current selection
	    if (!row.currentbgRGB) row.currentbgRGB = [255, 248, 242]; //if no mem is set, set it first;
	    doBGFadeMem(row,row.currentbgRGB,[201,184,115],4,20,1);
	    
	    //set the sidebar image
	    var propertyimage = document.getElementById("propertyimage");
	    propertyimage.src = row.getAttribute("image");
	    //hide the menus
	    if(window.hideMenus) hideMenus();
	}
}


//fired on reults table mouseout
function do_resultsmouseout(e) 
{
    //find the row clicked
	var row = selectedRow(e);
	if (row)
	{
	    if (!row.currentbgRGB) return;	//avoid error if mouseout an element occurs before the mosueover
		//(e.g. the pointer already in the object when onload)
		doBGFadeMem(row,row.currentbgRGB,[255, 248, 242],12,30,1);
	}
	
	//clear the sidebar image
	//set the sidebar image
    var propertyimage = document.getElementById("propertyimage");
    propertyimage.src = "image/fixed/defaultproperty.jpg";
}
    
function selectedRow(e)
{

	//var srcElem = window.event.srcElement;
	var srcElem = (window.event) ? window.event.srcElement : e.target;

	//find the table row
	while((srcElem.tagName != "TR")&&(srcElem.tagName != "TABLE"))
	{
		srcElem = srcElem.parentNode;
	}

	if(srcElem.tagName != "TR") return null;

	if(srcElem.rowIndex == 0 ) return null; //its the headings bar
	if(srcElem.childNodes[0].colSpan > 1) return null; //its the page selector - need a better way of identifying it

	return srcElem;
	
}

function doBGFadeMem(elem,startRGB,endRGB,steps,intervals,powr) {
//BG Fader with Memory by www.hesido.com

	if (elem.bgFadeMemInt) window.clearInterval(elem.bgFadeMemInt);
	var actStep = 0;
	elem.bgFadeMemInt = window.setInterval(
		function() { 
			elem.currentbgRGB = [
				easeInOut(startRGB[0],endRGB[0],steps,actStep,powr),
				easeInOut(startRGB[1],endRGB[1],steps,actStep,powr),
				easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)
				];
			elem.style.backgroundColor = "rgb("+
				elem.currentbgRGB[0]+","+
				elem.currentbgRGB[1]+","+
				elem.currentbgRGB[2]+")";
			actStep++;
			if (actStep > steps) window.clearInterval(elem.bgFadeMemInt);
		}
		,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr)
{
    //Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}