
//START QUICK SEARCH FUNCTIONS
var RHSSearchPath = 'http://www.relohomesearch.com/Search.aspx?Search=';
var RHSQuickSearching=0;
var RHSQuickSearch = {
   search: function(qSearch) {RHSQuickSearchRequest(qSearch);delete this.timeoutID;}
  ,init: function(qSearch) { this.cancel();var self = this; if (qSearch) { this.timeoutID = window.setTimeout(function() {self.search(qSearch);}, 500);} else { this.timeoutID = window.setTimeout(function() {self.search('');}, 500);} }
  ,cancel: function() {if(typeof this.timeoutID == "number") {window.clearTimeout(this.timeoutID);delete this.timeoutID;}}};
function RHSStartQuickSearch() {
    RHSEndQuickSearch_cancel();
    var inRHSQuickSearch = document.getElementById('inRHSQuickSearch');
    if (inRHSQuickSearch) {
        inRHSQuickSearch.className = 'inRHSQuickSearch_Active';
        if (RHSQuickSearching==0) {
            //inRHSQuickSearch.focus();
        }
        if (inRHSQuickSearch.value == 'Search') {inRHSQuickSearch.value = ''; RHSEndQuickSearch(); } else {RHSQuickSearch.init(inRHSQuickSearch.value); }
    }
}
//REQUEST QUICK SEARCH RESULTS FROM SERVER
var RHSLastSearch;
var RHSResultPos='&pos=bottom';
var RHSResultAlign='&align=right';
function RHSQuickSearchRequest(Search) {
    //CANCEL ANY ATTEMPTS TO CLOSE THE QUICK-SEARCH-BOX
    RHSQuickSearch.cancel();
    RHSEndQuickSearch_cancel();
    if (Search == '') {
        RHSEndQuickSearch_init();
    }
    else if (Search == RHSLastSearch) {
        RHSShowQuickSearchResults();
    } else {
        var inRHSQuickSearch = document.getElementById('inRHSQuickSearch'); 
        var iRHSWidget = document.getElementById('iRHSWidget'); 
        if (inRHSQuickSearch && iRHSWidget){
            //DECIDE WHETHER TO DISPLAY RESULTS AT THE TOP OR BOTTOM
            if (RHSGetTop(inRHSQuickSearch) + RHSGetHeight(inRHSQuickSearch) - RHSScrollTop() + 310 > RHSBrowserHeight() && RHSGetTop(inRHSQuickSearch) > 310)
                RHSResultPos='&pos=top';
            else
                RHSResultPos='&pos=bottom';
            //DECIDE WHETHER TO JUSTIFY LEFT OR RIGHT
            if (RHSGetLeft(inRHSQuickSearch) + RHSGetWidth(inRHSQuickSearch) - RHSScrollLeft() - 500 > 0)
                RHSResultAlign='&align=right';
            else
                RHSResultAlign='&align=left';
        }

        RHSLastSearch = Search;
        //POINT I-FRAME TO THE NEW SEARCH PATH
        var iRHSWidget = document.getElementById('iRHSWidget');
        if (iRHSWidget){
            iRHSWidget.src = RHSSearchPath + Search + RHSResultPos + RHSResultAlign;
        }
        RHSShowQuickSearchResults();
    }
}
//SHOW SEARCH RESULTS RETURNED FROM SERVER
var RHSqsCol = 1;
var RHSqsCols = 1;
var RHSqsRow = 1;
function RHSShowQuickSearchResults() {
    RHSEndQuickSearch_cancel();
    var inRHSQuickSearch = document.getElementById('inRHSQuickSearch'); 
    var iRHSWidget = document.getElementById('iRHSWidget'); 
    if (inRHSQuickSearch && iRHSWidget){
        iRHSWidget.style.display='block';
        iRHSWidget.style.fontSize='12px';

        //DECIDE WHETHER TO DISPLAY RESULTS AT THE TOP OR BOTTOM
        if (RHSResultPos=='&pos=top'){
            iRHSWidget.style.top = RHSGetTop(inRHSQuickSearch) - RHSGetHeight(iRHSWidget) + 'px';
        } else {
            iRHSWidget.style.top = RHSGetTop(inRHSQuickSearch) + RHSGetHeight(inRHSQuickSearch) + 'px';
        }
        
        //DECIDE WHETHER TO JUSTIFY LEFT OR RIGHT
        if (RHSResultAlign=='&align=right') {
            iRHSWidget.style.left = RHSGetLeft(inRHSQuickSearch) + RHSGetWidth(inRHSQuickSearch) - RHSGetWidth(iRHSWidget) + 'px';
        } else {
            iRHSWidget.style.left = RHSGetLeft(inRHSQuickSearch) + 'px';
        }
    }
}

function RHSAdjustWidgetSize(iframeWindow) {
    if (iframeWindow.document.height) {
        var iframeElement = document.getElementById ("iRHSWidget");
        iframeElement.style.height = iframeWindow.document.height + 'px';
        iframeElement.style.width = iframeWindow.document.width + 'px';
    } else if (document.all) {
        var iframeElement = document.all["iRHSWidget"];
        if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat') {
            iframeElement.style.height =
            iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
            iframeElement.style.width =
            iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
        } else {
            iframeElement.style.height =
            iframeWindow.document.body.scrollHeight + 5 + 'px';
            iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
        }
    }
}

//END QUICK SEARCH FUNCTIONS
var eqsID;
function RHSEndQuickSearch() {
  if (document.getElementById('iRHSWidget')) {
    var iRHSWidget = document.getElementById('iRHSWidget'); 
    iRHSWidget.style.display='none';
    iRHSWidget.style.fontSize='0px';
  }
}
function RHSEndQuickSearch_init() {
    RHSEndQuickSearch_cancel();
    eqsID = window.setTimeout('RHSEndQuickSearch()', 1000);
}
function RHSEndQuickSearch_cancel() {
    if(typeof eqsID == "number") {
        window.clearTimeout(eqsID);
        delete eqsID; 
    }
}

// GET BROWSER'S INNER-WIDTH
function RHSBrowserWidth() {
	var w = 0;
	if(!window.innerWidth)
	{	if(!(document.documentElement.clientWidth == 0))
		    {w = document.documentElement.clientWidth-0;}
		else 
		    {w = document.body.clientWidth-0; }
	}
	else {w = window.innerWidth-0;}
	return w;
}
// GET BROWSER'S INNER-HEIGHT
function RHSBrowserHeight() {
	var h = 0;
	if(!window.innerHeight)
	{	if(!(document.documentElement.clientHeight == 0))
		    {h = document.documentElement.clientHeight;}
		else 
		    {h = document.body.clientHeight; }
	}
	else {h = window.innerHeight;}
	return h;
}
// GET ELEMENT'S LEFT
function RHSGetLeft(elm)
{
    var x = 0;
    x = elm.offsetLeft;
    elm = elm.offsetParent;
    while(elm != null)
     {
        x = parseInt(x) + parseInt(elm.offsetLeft);
        elm = elm.offsetParent;
     }
    return x;
}
// GET ELEMENT'S TOP
function RHSGetTop(elm)
{
    var y = 0;
    y = elm.offsetTop;
    elm = elm.offsetParent;
    while(elm != null)
     {
        y = parseInt(y) + parseInt(elm.offsetTop);
        elm = elm.offsetParent;
     }
    return y;
}
// GET ELEMENT'S HEIGHT
function RHSGetHeight(elm)
{
    var y = 0;
    if (elm) { if (elm.offsetHeight) { y = elm.offsetHeight; } }
    return y;
}
// GET ELEMENT'S WIDTH
function RHSGetWidth(elm)
{
    var x = 0;
    if (elm) { if (elm.offsetHeight) { x = elm.offsetWidth; } }
    return x;
}
//GET BROWSER SCROLL-TOP
function RHSScrollTop(){
    var y = 0;
    if (self.pageYOffset)
	    y = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
	    y = document.documentElement.scrollTop;
    else if (document.body)
	    y = document.body.scrollTop;
    return y;
}
//GET BROWSER SCROLL-LEFT
function RHSScrollLeft(){
    var x = 0;
    if (self.pageXOffset)
	    x = self.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
	    x = document.documentElement.scrollLeft;
    else if (document.body)
	    x = document.body.scrollLeft;
    return x;
}
