
var Globals = {
    search              : null,
    tooltip             : null
}

var Preferences = {
    search : {
        textboxId           : "search",
        divId               : "result",
        textboxTextDefault  : "search bed & breakfast...",
        url                 : "/search.php?s=",
        htmlHeader          : "<div style=\"clear:both; padding:0px 0px 15px 0px;\"><div class=\"fA11n cTx\" style=\"padding:0px 0px 2px 24px; background-image: url(/images/general/sign.gif); background-repeat:no-repeat; background-position:center left;\">Search</div><div style=\"height:1px; background-color:#969696; background-image: url(/images/general/header.gif); background-repeat:no-repeat; background-position:bottom right;\"><!-- --></div></div>",
        htmlFooter          : "",
        htmlLoading         : "<div style=\"clear:both; height:90px;\"><img src=\"/images/general/loading.gif\" style=\"margin-top:28px;\" /></div>",
        htmlNotFound        : "<div class=\"cTx fA11b\" style=\"clear:both; height:90px;\"><div style=\"padding-top:37px;\">No results found. Please, try another search.</div></div>"
    },
    
    tooltip : {
        containerId         : "tooltip",
        htmlHeader          : "",
        htmlFooter          : "",
        htmlLoading         : ""
    }    
}

function onLoad()
{
    Globals.search = new Search(Preferences.search);
    Globals.search.onLoad();

    Globals.tooltip = new Tooltip(Preferences.tooltip);
    Globals.tooltip.div.className = "fA11n cTx";
}


//////////////////// Search ////////////////////


function Search(o)
{
    this.request;
    this.textbox = document.getElementById(o.textboxId);
    this.div = document.getElementById(o.divId);
    this.textboxTextDefault = o.textboxTextDefault;
    this.url = o.url;
    this.htmlHeader = o.htmlHeader;
    this.htmlFooter = o.htmlFooter;
    this.htmlLoading = o.htmlLoading;
    this.htmlNotFound = o.htmlNotFound;
}

Search.prototype.onLoad = function()
{
    this.textbox.removeAttribute("readOnly");
    this.textbox.value = this.textboxTextDefault;
}

Search.prototype.onTextboxFocus = function()
{
    if (this.textbox.value == this.textboxTextDefault)
        this.textbox.value = "";
}

Search.prototype.onTextboxBlur = function()
{
    if (this.isTextboxEmpty())
        this.textbox.value = this.textboxTextDefault;
}

Search.prototype.onTextboxKeyDown = function(e)
{
    var k = e.keyCode || key.which;
    
    switch (k)
    {
        case 27:
            if (this.isTextboxEmpty())
                this.textbox.blur();
            else
                this.reset();
            return false;

        case 13:
            if (this.isTextboxEmpty())
                this.reset();
            else
                this.search();                  
            return false;
    }

    return true;
}

Search.prototype.onButtonPress = function()
{
    if (this.textbox.value == this.textboxTextDefault)
    {
        var _this = this;
        this.textbox.value = "";
        setTimeout(function() { _this.textbox.focus(); }, 100);
    }
    else if (!this.isTextboxEmpty())
        this.search();
}

Search.prototype.isTextboxEmpty = function()
{
    return this.textbox.value.replace(/^\s*|\s*$/g, "").length == 0;
}

Search.prototype.reset = function()
{
    if (this.div.offsetHeight > 0)
        this.div.innerHTML = "<!-- -->";

    var _this = this;
    setTimeout(function() { _this.textbox.value = ""; }, 50);
}

Search.prototype.search = function()
{
    if (typeof(this.request) == "undefined")
        this.request = Ajax.createRequest();
        
    var _this = this;        

    this.request.open("GET", this.url + encodeURIComponent(this.textbox.value.replace(/^\s*|\s*$/g, "")), true);
    this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.request.setRequestHeader("Cache-Control", "no-cache");
    this.request.onreadystatechange = function() 
    {
        switch (_this.request.readyState)
        {
            case 0:
            case 1:
            case 2:
            case 3:
                if (_this.htmlLoading.length > 0)
                    _this.div.innerHTML = _this.htmlHeader + _this.htmlLoading + _this.htmlFooter;
                break;

            case 4:
                if (_this.request.status == 200)
                {
                    if (_this.request.responseText.length == 0 && _this.htmlNotFound.length > 0)
                        _this.div.innerHTML = _this.htmlHeader + _this.htmlNotFound + _this.htmlFooter;
                    else
                        _this.div.innerHTML = _this.htmlHeader + _this.request.responseText + _this.htmlFooter;
                }
                break;
        }
    }
    
    this.request.send(null);
}


//////////////////// Tootltip ////////////////////


function Tooltip(o)
{
    this.request;

    this.div = document.createElement(o.containerId);
    this.div.style.visibility = "hidden";
    this.div.id = o.containerId;
    this.div.style.position = "absolute";
    this.div.style.padding = "8px";
    this.div.style.border = "1px solid #969696";
    this.div.style.backgroundColor = "#ffffff";
    this.div.style.overflow = "auto";
    this.div.style.textAlign = "justify";
    document.body.appendChild(this.div);
    
    this.htmlHeader = o.htmlHeader;
    this.htmlFooter = o.htmlFooter;
    this.htmlLoading = o.htmlLoading;
}

Tooltip.prototype.show = function(element, offsetX, offsetY, width, height, flipY, source)
{
    if (typeof(element) != "object")
        throw new Error("Ivalid element");

    var p = Tooltip.findPos(element);
    var left = p[0] + offsetX;
    var top = p[1] + offsetY;
    this.showAbsolute(left, top, width, height, flipY, source);
}

Tooltip.prototype.showAbsolute = function(left, top, width, height, flipY, source)
{
    if (left > -1) this.div.style.left = left + "px";
    if (top > -1) this.div.style.top = top + "px";
    if (width > -1) this.div.style.width = width + "px";
    if (height > -1) this.div.style.height = height + "px";

    if (/^http/.test(source))
    {
        if (typeof(this.request) == "undefined")
            this.request = Ajax.createRequest();

        var _this = this;        

        this.request.open("GET", source, true);
        this.request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        this.request.setRequestHeader("Cache-Control", "no-cache");
        this.request.onreadystatechange = function() 
        {
            switch (_this.request.readyState)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                    if (_this.htmlLoading.length > 0)
                        _this.div.innerHTML = _this.htmlHeader + _this.htmlLoading + _this.htmlFooter;
                    break;

                case 4:
                    if (_this.request.status == 200)
                        _this.div.innerHTML = _this.htmlHeader + _this.request.responseText + _this.htmlFooter;
                    break;
            }
        }
        
        this.request.send(null);                    
    }
    else
        this.div.innerHTML = this.htmlHeader + source + this.htmlFooter;
    
    this.div.style.left = (left + this.div.offsetWidth > document.body.clientWidth + document.body.scrollLeft ? document.body.clientWidth + document.body.scrollLeft - this.div.offsetWidth : left) + "px";
    this.div.style.top = (top + this.div.offsetHeight > document.body.clientHeight + document.body.scrollTop ? top - this.div.offsetHeight + flipY : top) + "px";
    this.div.style.visibility = "visible";
}

Tooltip.prototype.hide = function()
{
    this.div.style.visibility = "hidden";
    this.div.innerHTML = "<!-- -->";
    this.div.style.height = "";
    this.div.style.width = "";
}

Tooltip.findPos = function(o)
{
    var l = t = 0;

    if (o.offsetParent)
    {
        l = o.offsetLeft;
        t = o.offsetTop;

        while (o = o.offsetParent)
        {
            l += o.offsetLeft;
            t += o.offsetTop;
        }
    }

    return [l, t];
}


//////////////////// Ajax ////////////////////


function Ajax()
{
}

Ajax.createRequest = function()
{
    if (typeof(XMLHttpRequest) != "undefined")
    {
        return new XMLHttpRequest();
    }
    else if (typeof(ActiveXObject) != "undefined")
    {
        var versions = new Array(
              "Msxml2.XMLHTTP.7.0",
              "Msxml2.XMLHTTP.6.0",
              "Msxml2.XMLHTTP.5.0",
              "Msxml2.XMLHTTP.4.0",
              "MSXML2.XMLHTTP.3.0",
              "MSXML2.XMLHTTP",
              "Microsoft.XMLHTTP"
              );
           
        for (var i = 0; i < versions.length; i++)
        {
            try 
            {
                var request = new ActiveXObject(versions[i]);
                return request;
            }
            catch (e)
            {
            }
        }
    }

    throw new Error("Unable to create request object");
}