var g_url_prefix = "";

var Globals = {
    search              : null,
    tooltipSearch       : null
}

function onLoad()
{
    Globals.search = new Search({
        textboxId           : "search",
        divId               : "result",
        textboxTextDefault  : "search bed & breakfast...",
        url                 : g_url_prefix + "/search.php?s=",
        htmlHeader          : "<div style=\"clear:both; padding:0px 0px 15px 0px;\"><div style=\"padding:0px 0px 2px 24px; background-image: url(" + g_url_prefix + "/images/general/sign.gif); background-repeat:no-repeat; background-position:center left;\">Search</div><div style=\"height:1px; background-color:#969696; background-image: url(" +  g_url_prefix + "/images/general/header.gif); background-repeat:no-repeat; background-position:bottom right;\"><!-- --></div></div>",
        htmlFooter          : "",
        htmlLoading         : "<div style=\"clear:both; height:90px;\"><img src=\"" + g_url_prefix + "/images/general/loading.gif\" style=\"margin-top:28px;\" /></div>",
        htmlNotFound        : "<div class=\"fA11b\" style=\"clear:both; height:90px;\"><div style=\"padding-top:37px;\">No results found. Please, try another search.</div></div>"
    });

    Globals.tooltipSearch = new Tooltip("tooltipSearch");
    Globals.tooltipSearch.edgeType = Tooltip.EdgeType.topRight;
    Globals.tooltipSearch.edgePosition = 80;
}


//
// Search
//

function Search(o)
{
    this.request = null;
    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;

    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)
{
    switch (Utilities.getKeyCode(e)) {
        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 (!this.request) {
        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("Cache-Control", "no-cache");
    this.request.onreadystatechange = function() {
        switch (_this.request.readyState) {
            case 0:
            case 1:
            case 2:
                break;
            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);
}


//
// Tooltip
//

function Tooltip(id)
{
    this.image = {
        src                     : g_url_prefix + "/images/general/tooltip.png",
        width                   : 892,
        height                  : 752,
        borderTop               : {left:13, top:45, width:0, height:13},
        borderLeft              : {left:0, top:58, width:13, height:0},
        borderRight             : {left:879, top:58, width:13, height:0},
        borderBottom            : {left:13, top:739, width:0, height:13},
        cornerWidth             : 13,
        cornerHeight            : 13,
        cornerTopLeft           : {left:0, top:45, width:13, height:13},
        cornerTopRight          : {left:879, top:45, width:13, height:13},
        cornerBottomLeft        : {left:0, top:739, width:13, height:13},
        cornerBottomRight       : {left:879, top:739, width:13, height:13},
        edgeTopBottomWidth      : 51,
        edgeTopBottomHeight     : 32,
        edgeLeftRightWidth      : 32,
        edgeLeftRightHeight     : 31,
        edgeOffset              : 13,
        edgeTop                 : {left:204, top:0, width:31, height:32}, //not implemented
        edgeBottom              : {left:235, top:0, width:31, height:32}, //not implemented
        edgeLeft                : {left:266, top:0, width:32, height:31},
        edgeRight               : {left:298, top:0, width:32, height:31},
        edgeTopLeft             : {left:102, top:0, width:51, height:32},
        edgeTopRight            : {left:153, top:0, width:51, height:32},
        edgeBottomLeft          : {left:0, top:0, width:51, height:32},
        edgeBottomRight         : {left:51, top:0, width:51, height:32}
    };

    this.flipVerticalOffset = 0;
    this.flipVerticalEnabled = false;
    this.edgeType = Tooltip.EdgeType.none;
    this.edgePosition = 0;
    this.hideDelay = 1000;

    this.timeout = null;
    this.width = 0;
    this.height = 0;
    this.idDivFrame = id + "_frame";
    this.idDivClient = id + "_client";

    this.divFrame = document.createElement("div");
    this.divFrame.id = this.idDivFrame;
    this.divFrame.style.visibility = "hidden";
    this.divFrame.style.position = "absolute";
    document.body.appendChild(this.divFrame);

    this.divClient = document.createElement("div");
    this.divClient.id = this.idDivClient;
    this.divClient.style.position = "absolute";
    this.divClient.style.backgroundColor = "white";
    this.divFrame.appendChild(this.divClient);

    var _preload = new Image(this.image.width, this.image.height);
    _preload.src = this.image.src;
}

Tooltip.EdgeType = {
    none                    : 0,
    left                    : 1,
    right                   : 2,
    topLeft                 : 3,
    topRight                : 4,
    bottomLeft              : 5,
    bottomRight             : 6
}

Tooltip.BorderType = {
    left                    : 0,
    right                   : 1,
    top                     : 2,
    bottom                  : 3
}

Tooltip.CornerType = {
    topLeft                 : 0,
    topRight                : 1,
    bottomLeft              : 2,
    bottomRight             : 3
}

Tooltip.EdgePosition = {
    min                     : 0,
    max                     : 0x7FFFFFFF
}

Tooltip.prototype.openRelative = function(element, offsetX, offsetY, width, height, html)
{
    var p = Utilities.getElementPosition(element);
    this.openAbsolute(p[0] + offsetX - document.body.scrollLeft, p[1] + offsetY - document.body.scrollTop, width, height, html);
}

Tooltip.prototype.openAbsolute = function(left, top, width, height, html)
{
    if (!this.isCreated()) {
        this.createTooltip(width, height, html);
    } else {
        this.setText(html);
    }
    if (this.flipVerticalEnabled) {
        this.divFrame.style.top = Math.max(0, (top + this.height > document.body.clientHeight ? top - this.height + this.flipVerticalOffset : top))  + document.body.scrollTop + "px";
    } else {
        this.divFrame.style.top = Math.max(0, (top + this.height > document.body.clientHeight ? document.body.clientHeight - this.height : top)) + document.body.scrollTop + "px";
    }
    this.divFrame.style.left = Math.max(0, (left + this.width > document.body.clientWidth ? document.body.clientWidth - this.width : left)) + document.body.scrollLeft + "px";
    this.show();
}

Tooltip.prototype.setText = function(s)
{
    this.divClient.innerHTML = s;
}

Tooltip.prototype.show = function()
{
    if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = null;
    }
    this.divFrame.style.visibility = "visible";
}

Tooltip.prototype.hide = function()
{
    var delay = this.hideDelay;
    if (this.divFrame.style.visibility == "visible") {
        if (arguments.length > 0) {
            var d = parseInt(arguments[0], 10);
            if (!isNaN(d)) {
                delay = d;
            }
        }
        var _this = this;
        this.timeout = setTimeout(function() {if (_this.timeout) {clearTimeout(_this.timeout);_this.timeout = null;}_this.divFrame.style.visibility = "hidden";}, delay);
    }
}

Tooltip.prototype.close = function()
{
    this.divFrame.style.visibility = "hidden";

    var o = this.divFrame.firstChild;
    var t;

    while (o) {
        if (o.id != this.idDivClient) {
            t = o;
            o = o.nextSibling;
            this.divFrame.removeChild(t);
        } else {
            o = o.nextSibling;
        }
    }
    this.divClient.innerHTML = "";
    this.divClient.style.height = "";
    this.divClient.style.width = "";
}

Tooltip.prototype.createTooltip = function(width, height, html)
{
    if (!this.isValidDimension(width)) {
        throw new Error("Invalid width");
    }

    var x = 0;
    var y = 0;
    var isEdgeTop = (this.edgeType == Tooltip.EdgeType.topLeft || this.edgeType == Tooltip.EdgeType.topRight || this.edgeType == Tooltip.EdgeType.top);
    var isEdgeBottom = (this.edgeType == Tooltip.EdgeType.bottomLeft || this.edgeType == Tooltip.EdgeType.bottomRight || this.edgeType == Tooltip.EdgeType.bottom);
    var isEdgeLeft = (this.edgeType == Tooltip.EdgeType.left);
    var isEdgeRight = (this.edgeType == Tooltip.EdgeType.right);

    this.divClient.style.left = (isEdgeLeft ? this.image.edgeLeftRightWidth : this.image.cornerWidth) + "px";
    this.divClient.style.top = (isEdgeTop ? this.image.edgeTopBottomHeight : this.image.cornerHeight) + "px";

    var w = width - 2 * this.image.cornerWidth;

    if (isEdgeLeft || isEdgeRight) {
        w -= this.image.edgeLeftRightWidth - this.image.edgeOffset;
    }

    if (w >= 0) {
        this.divClient.style.width = w + "px";
    }

    if (this.isValidDimension(height)) {
        var h = height - 2 * this.image.cornerHeight;
        if (isEdgeTop || isEdgeBottom) {
            h -= this.image.edgeTopBottomHeight - this.image.edgeOffset;
        }
        if (h >= 0) {
            this.divClient.style.height = h + "px";
        }
    }

    this.divClient.innerHTML = html;

    var clientWidth = this.divClient.offsetWidth;
    var clientHeight = this.divClient.offsetHeight;
    var edgePositionMax = (isEdgeLeft || isEdgeRight) ? clientHeight - this.image.edgeLeftRightHeight : clientWidth - this.image.edgeTopBottomWidth;

    if (isEdgeLeft) {
        x += this.image.edgeLeftRightWidth - this.image.edgeOffset;
    }

    this.edgePosition = this.edgePosition ? Math.max(Math.min(this.edgePosition, edgePositionMax), 0) : 0;

    if (isEdgeTop) {
        y = this.image.edgeTopBottomHeight - this.image.edgeOffset;
        if (this.edgePosition > 0) {
            this.addBorder(x + this.image.cornerWidth, y, this.edgePosition, Tooltip.BorderType.top)
        }
        this.addEdge(x + this.image.cornerWidth + this.edgePosition, 0, this.edgeType);
        if (this.edgePosition < edgePositionMax) {
            this.addBorder(x + this.image.cornerWidth + this.edgePosition + this.image.edgeTopBottomWidth, y, clientWidth - this.edgePosition - this.image.edgeTopBottomWidth, Tooltip.BorderType.top);
        }
    } else {
        this.addBorder(x + this.image.cornerWidth, y, clientWidth, Tooltip.BorderType.top);
    }

    this.addCorner(x, y, Tooltip.CornerType.topLeft);
    this.addCorner(x + this.image.cornerWidth + clientWidth, y, Tooltip.CornerType.topRight);

    y += this.image.cornerHeight;

    if (isEdgeLeft) {
        if (this.edgePosition > 0) {
           this.addBorder(x, y, this.edgePosition, Tooltip.BorderType.left);
        }
        this.addEdge(0, this.image.cornerHeight + this.edgePosition, this.edgeType);
        if (this.edgePosition < edgePositionMax) {
            this.addBorder(x, this.image.cornerHeight + this.edgePosition + this.image.edgeLeftRightHeight, clientHeight - this.edgePosition - this.image.edgeLeftRightHeight, Tooltip.BorderType.left);
        }
    } else {
        this.addBorder(x, y, clientHeight, Tooltip.BorderType.left);
    }

    if (isEdgeRight) {
        if (this.edgePosition > 0) {
           this.addBorder(x + this.image.cornerWidth + clientWidth, y, this.edgePosition, Tooltip.BorderType.right);
        }
        this.addEdge(x + this.image.cornerWidth + clientWidth, this.image.cornerHeight + this.edgePosition, this.edgeType);
        if (this.edgePosition < edgePositionMax) {
            this.addBorder(x + this.image.cornerWidth + clientWidth, this.image.cornerHeight + this.edgePosition + this.image.edgeLeftRightHeight, clientHeight - this.edgePosition - this.image.edgeLeftRightHeight, Tooltip.BorderType.right);
        }
    } else {
        this.addBorder(x + this.image.cornerWidth + clientWidth, y, clientHeight, Tooltip.BorderType.right);
    }

    y += clientHeight;
    this.addCorner(x, y, Tooltip.CornerType.bottomLeft);
    this.addCorner(x + this.image.cornerWidth + clientWidth, y, Tooltip.CornerType.bottomRight);

    if (isEdgeBottom) {
        if (this.edgePosition > 0) {
            this.addBorder(x + this.image.cornerWidth, y, this.edgePosition, Tooltip.BorderType.bottom);
        }
        this.addEdge(x + this.image.cornerWidth + this.edgePosition, y, this.edgeType);
        if (this.edgePosition < edgePositionMax) {
            this.addBorder(x + this.image.cornerWidth + this.edgePosition + this.image.edgeTopBottomWidth, y, clientWidth - this.edgePosition - this.image.edgeTopBottomWidth, Tooltip.BorderType.bottom);
        }
        y += this.image.edgeTopBottomHeight;
    } else {
        this.addBorder(x + this.image.cornerWidth, y, clientWidth, Tooltip.BorderType.bottom);
        y += this.image.cornerHeight;
    }

    this.width = width;
    this.height = (this.isValidDimension(height)) ? height : y;
}

Tooltip.prototype.addCorner = function(left, top, cornerType)
{
    switch (cornerType) {
        case Tooltip.CornerType.topLeft:
            this.addImageSlice(left, top, this.image.cornerTopLeft.left, this.image.cornerTopLeft.top, this.image.cornerTopLeft.width, this.image.cornerTopLeft.height);
            break;
        case Tooltip.CornerType.topRight:
            this.addImageSlice(left, top, this.image.cornerTopRight.left, this.image.cornerTopRight.top, this.image.cornerTopRight.width, this.image.cornerTopRight.height);
            break;
        case Tooltip.CornerType.bottomLeft:
            this.addImageSlice(left, top, this.image.cornerBottomLeft.left, this.image.cornerBottomLeft.top, this.image.cornerBottomLeft.width, this.image.cornerBottomLeft.height);
            break;
        case Tooltip.CornerType.bottomRight:
            this.addImageSlice(left, top, this.image.cornerBottomRight.left, this.image.cornerBottomRight.top, this.image.cornerBottomRight.width, this.image.cornerBottomRight.height);
            break;
    }
}

Tooltip.prototype.addBorder = function(left, top, size, borderType)
{
    switch (borderType) {
        case Tooltip.BorderType.left:
            this.addImageSlice(left, top, this.image.borderLeft.left, this.image.borderLeft.top, this.image.borderLeft.width, size);
            break;
        case Tooltip.BorderType.right:
            this.addImageSlice(left, top, this.image.borderRight.left, this.image.borderRight.top, this.image.borderRight.width, size);
            break;
        case Tooltip.BorderType.top:
            this.addImageSlice(left, top, this.image.borderTop.left, this.image.borderTop.top, size, this.image.borderTop.height);
            break;
        case Tooltip.BorderType.bottom:
            this.addImageSlice(left, top, this.image.borderBottom.left, this.image.borderBottom.top, size, this.image.borderBottom.height);
            break;
    }
}

Tooltip.prototype.addEdge = function(left, top, edgeType)
{
    switch (edgeType) {
        case Tooltip.EdgeType.left:
            this.addImageSlice(left, top, this.image.edgeLeft.left, this.image.edgeLeft.top, this.image.edgeLeft.width, this.image.edgeLeft.height);
            break;
        case Tooltip.EdgeType.right:
            this.addImageSlice(left, top, this.image.edgeRight.left, this.image.edgeRight.top, this.image.edgeRight.width, this.image.edgeRight.height);
            break;
        case Tooltip.EdgeType.topLeft:
            this.addImageSlice(left, top, this.image.edgeTopLeft.left, this.image.edgeTopLeft.top, this.image.edgeTopLeft.width, this.image.edgeTopLeft.height);
            break;
        case Tooltip.EdgeType.topRight:
            this.addImageSlice(left, top, this.image.edgeTopRight.left, this.image.edgeTopRight.top, this.image.edgeTopRight.width, this.image.edgeTopRight.height);
            break;
        case Tooltip.EdgeType.bottomLeft:
            this.addImageSlice(left, top, this.image.edgeBottomLeft.left, this.image.edgeBottomLeft.top, this.image.edgeBottomLeft.width, this.image.edgeBottomLeft.height);
            break;
        case Tooltip.EdgeType.bottomRight:
            this.addImageSlice(left, top, this.image.edgeBottomRight.left, this.image.edgeBottomRight.top, this.image.edgeBottomRight.width, this.image.edgeBottomRight.height);
            break;
    }
}

Tooltip.prototype.addImageSlice = function(left, top, sliceLeft, sliceTop, sliceWidth, sliceHeight)
{
    var divClip = document.createElement("div");
    divClip.style.position = "absolute";
    divClip.style.left = left + "px";
    divClip.style.top = top + "px";
    divClip.style.width = sliceWidth + "px";
    divClip.style.height = sliceHeight + "px";
    divClip.style.overflow = "hidden";

    var divPNG = document.createElement("div");
    divPNG.style.position = "absolute";
    divPNG.style.left = -sliceLeft + "px";
    divPNG.style.top = -sliceTop + "px";
    divPNG.style.width = this.image.width + "px";
    divPNG.style.height = this.image.height + "px";
    divPNG.style.backgroundImage = "url(" + this.image.src + ")";
    divPNG.style.backgroundPosition = "bottom left";
    divPNG.style.backgroundRepeat = "no-repeat";

    divClip.appendChild(divPNG);
    this.divFrame.appendChild(divClip);
}

Tooltip.prototype.isCreated = function()
{
    return this.divFrame.childNodes.length > 1;
}

Tooltip.prototype.isValidDimension = function(px)
{
    var r = parseInt(px, 10);
    return (!isNaN(r) && r >= 0);
}


//
// Utilities
//

function Utilities()
{
}

Utilities.createEventHandlerMethod = function(o, m)
{
    return function() {
        o[m].apply(o, arguments);
    }
}

Utilities.addEvent = function(o, e, h)
{
    if (o.addEventListener) {
        o.addEventListener(e, h, false);
    } else if (o.attachEvent) {
        o.attachEvent("on" + e, h);
    }
}

Utilities.removeEvent = function(o, e, h)
{
    if (o.remoteEventListener) {
        o.removeEventListener(e, h, false);
    } else if (o.detachEvent) {
        o.detachEvent("on" + e, h);
    }
}

Utilities.stopEvent = function(e)
{
    if (e) {
        if (window.event) {
            e.cancelBubble = true;
            e.returnValue = false;
        } else {
            e.preventDefault();
            e.stopPropagation();
        }
    }
    return false;
}

Utilities.getEvent = function(e)
{
    return (window.event) ? window.event : e;
}

Utilities.getEventTarget = function(e)
{
    return (window.event) ? window.event.srcElement : e.target;
}

Utilities.getKeyCode = function(e)
{
    return (e.keyCode) ? e.keyCode : e.which ? e.which : e.charCode;
}

Utilities.getElementPosition = function(o)
{
    var l = 0, 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 = ["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");
}
