//START QUICK SEARCH FUNCTIONS
var RHSSearchPath = 'http://searchwidget.relohomesearch.com/Search.aspx?Search=';
var RHSRHSQuickSearching=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');
    inRHSQuickSearch.className = 'inRHSQuickSearch_Active';
    if (RHSRHSQuickSearching==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) + 310 > RHSBrowserHeight() && RHSGetTop(inRHSQuickSearch) > 310)
                RHSResultPos='&pos=top';
            else
                RHSResultPos='&pos=bottom';
            //DECIDE WHETHER TO JUSTIFY LEFT OR RIGHT
            if (RHSGetLeft(inRHSQuickSearch) + RHSGetWidth(inRHSQuickSearch) - 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;
        }
        delete RHSqslast;
        delete RHSqslastcell;
        RHSShowQuickSearchResults();
    }
}
//SHOW SEARCH RESULTS RETURNED FROM SERVER
var RHSqsCol = 1;
var RHSqsCols = 1;
var RHSqsRow = 1;
function RHSShowQuickSearchResults() {
    RHSEndQuickSearch_cancel();
    RHSSelectQuickSearchResult();
    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; 
    }
}
//SELECT WHICH QUICK-RESULT TO PLACE THE CURSOR ON
var RHSqslast;
var RHSqslastcell;
function RHSSelectQuickSearchResult() {
    if (RHSqslast)
        RHSqslast.className = 'divSearchResult'
    if (RHSqslastcell)
        RHSqslastcell.className = 'tdSearchResult'
    var tcell = document.getElementById('t' + RHSqsCol + '-' + RHSqsRow);
    var acell = document.getElementById('a' + RHSqsCol + '-' + RHSqsRow);
    if (tcell) {
        tcell.className = 'tdSearchResultSelected'
        RHSqslastcell = tcell;
    }
    if (acell) {
        acell.className = 'divSearchResultselected'
        RHSqslast = acell;
    }
}
//REGISTER THE START-QUICK-SEARCH AS THE DEFAULT DOCUMENT KEY-PRESS BEHAVIOR
document.onkeydown = function(e) {
    var inRHSQuickSearch = document.getElementById('inRHSQuickSearch');
    if (inRHSQuickSearch) {
        if (RHSRHSQuickSearching<=0) {
            RHSStartQuickSearch();
        } 
        var keyCode = 0;
        e= (window.event)? event : e;
  	    keyCode = (e.keyCode)? e.keyCode: e.charCode;
        if (keyCode == 13) {
            //ENTER-KEY PRESSED - TRIGGER QUICK-SEARCH ON CURRENTLY SELECTED RESULT
            if (RHSqslast) {
                inRHSQuickSearch.value = '';
                RHSQuickSearch.cancel();
                RHSEndQuickSearch();
                if (window.location.href) {
                    window.location.href=RHSqslast.href;
                } else {
                    window.location=RHSqslast.href;
                }
            }
        }
        else if (keyCode == 39) {
            //RIGHT-ARROW PRESSED
            if( document.getElementById('t' + (RHSqsCol + 1) + '-' + RHSqsRow) ) {
                RHSqsCol=RHSqsCol+1;
                RHSSelectQuickSearchResult();
            }
            else if ( document.getElementById('t' + (RHSqsCol + 2) + '-' + RHSqsRow) ) {
                RHSqsCol=RHSqsCol+2;
                RHSSelectQuickSearchResult();
            }
        }
        else if (keyCode == 40) {
            //DOWN-ARROW PRESSED
            if( document.getElementById('t' + RHSqsCol + '-' + (RHSqsRow + 1)) ) {
                RHSqsRow=RHSqsRow+1;
                RHSSelectQuickSearchResult();
            }
        }
        else if (keyCode == 38) {
            //UP-ARROW PRESSED
            if( document.getElementById('t' + RHSqsCol + '-' + (RHSqsRow - 1)) ) {
                RHSqsRow=RHSqsRow-1;
                RHSSelectQuickSearchResult();
            }
        }
        else if (keyCode == 37) {
            //LEFT-ARROW PRESSED
            if( document.getElementById('t' + (RHSqsCol - 1) + '-' + RHSqsRow) ) {
                RHSqsCol=RHSqsCol-1;
                RHSSelectQuickSearchResult();
            }
            else if ( document.getElementById('t' + (RHSqsCol - 2) + '-' + RHSqsRow) ) {
                RHSqsCol=RHSqsCol-2;
                RHSSelectQuickSearchResult();
            }
        }
        else if (keyCode == 27) {
            //ESCAPE-KEY PRESSED, SO CLOSE THE SEARCH RESULTS
            inRHSQuickSearch.value = '';
            RHSEndQuickSearch();
        }
    }
};

//GLOBAL SCRIPT VARIABLES
var minClientHeight = 400;
var minClientWidth = 600;

// 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;}
    if (w < minClientWidth) w=minClientWidth;
	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;}
    if (h < minClientHeight) h=minClientHeight;
	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;
}
