﻿
function $get(itemId) {
    return document.getElementById(itemId);
}

function getElementPosition(theElement) {

    var posX = 0;
    var posY = 0;

    while (theElement != null) {
        posX += theElement.offsetLeft;
        posY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    return { x: posX, y: posY };
}

function display(id) {
    var object = $get(id);

    if (object == null)
        return;

    if (object.style.display == "none")
        object.style.display = "";
    else
        object.style.display = "none";
}

/* SEARCH BOX CLASS */

var SearchBox = new Object;
SearchBox.WaterMarks = new Object();

SearchBox.configure = function(textBoxId, watermarkText, buttonId) {

    if ($get(textBoxId) != null) {
        $get(textBoxId).onfocus = function() { SearchBox.focus(textBoxId); }
        $get(textBoxId).onblur = function() { SearchBox.blur(textBoxId); }
        if (buttonId)
            $get(textBoxId).onkeypress = function(e) { return SearchBox.pressEnterToGo(buttonId, e); }

        SearchBox.setWatermarkText(textBoxId, watermarkText)
        SearchBox.blur(textBoxId);
    }
}

SearchBox.setWatermarkText = function(textBoxId, watermarkText) {

    SearchBox.WaterMarks[textBoxId] = watermarkText;
}

SearchBox.blur = function(textBoxId) {
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    var inputDefText = SearchBox.WaterMarks[textBoxId];

    if (t.value == '' || t.value == inputDefText) {
        t.style.color = '#aaa';
        t.value = inputDefText;
    }
}

SearchBox.focus = function(textBoxId) {
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    var inputDefText = SearchBox.WaterMarks[textBoxId];

    if (t.value == inputDefText)
        t.value = '';

    t.style.color = 'black';
}

SearchBox.value = function(textBoxId) {
    if (!textBoxId)
        textBoxId = 'q';
    var t = $get(textBoxId);
    return t.value;
}

SearchBox.pressEnterToGo = function(buttonId, event) {
    event = event || window.event;
    var keyCode = null;

    if (event.which)
        keyCode = event.which;
    else if (event.keyCode)
        keyCode = event.keyCode;

    if (keyCode == 13) {
        $get(buttonId).click();
        return false;
    }

    return true;
}
