/*
* JTip
* By Cody Lindley (http://www.codylindley.com)
* Under an Attribution, Share Alike License
* JTip is built on top of the very light weight jquery library.
*
* Enhanced by deimos.ch.
* (überarbeitet 7.4.2010/uvb. Interpretiertes HTML möglich. Siehe GetJtipTextUrl())
*
* a) Load window content per http:
*   <a href="somefile.html?title=some_window_title&width=250" class="jTip" id="must_be_unique">?</a>
*
* a) Static text as window content:
*   <a href="text?title=some_window_title&text=some_window_body&width=250" class="jTip" id="must_be_unique">?</a>
*
* a) Layer as window content:
*   <a href="layer?title=some_window_title&id=id_of_layer&width=250" class="jTip" id="must_be_unique">?</a>
*
* Additional query parameters for href argument: ?link=http://www.google.ch
*/

//on page load (as soon as its ready) call JT_init
var pathToImages = "../pics/"

$(document).ready(JT_init);

function JT_init() {
    $("a.jTip")
    .hover(function() { JT_show(this.href, this.id) }, function() { $('#JT').remove() })
    .click(function() { return false });
}

function JT_show(url, linkId) {
    var de = document.documentElement;
    var w = self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    var hasArea = w - getAbsoluteLeft(linkId);
    var clickElementy = getAbsoluteTop(linkId) - 3; //set y position
    var clickElementx = getAbsoluteLeft(linkId); // will be corrected below 

    // parse url:
    var a = url.split(/\?/); 
    url = a[0];
    var params;
    if (a.length > 1) {
        params = parseQuery(a[1]);
    } else {
        params = parseQuery("");
    }

    var mode;
    if ((/\/text$/).test(url)) {
        mode = "text";
    } else if ((/\/layer$/).test(url)) {
        mode = "layer";
    } else {
        mode = "http";
    }

    var title;
    title = params["title"];
    if (params['width'] === undefined) { params['width'] = 250 };

    if (params['link'] !== undefined) {
        $('#' + linkId).bind('click', function() { window.location = params['link'] });
        $('#' + linkId).css('cursor', 'pointer');
    }

    var body;
    body = "<div id=\"JT\" style=\"width:" + params['width'] * 1 + "px\">";
    if (hasArea > ((params['width'] * 1) + 75)) {
        body += "<div id=\"JT_arrow_left\"></div>";
    } else {
        body += "<div id=\"JT_arrow_right\" style=\"left:" + (params['width']) + "px\"></div>";
    }
    if (title) {
        if (hasArea > ((params['width'] * 1) + 75)) {
            body += "<div id=\"JT_close_left\">" + title + "</div>";
        } else {
            body += "<div id=\"JT_close_right\">" + title + "</div>";
        }
    }
    body += "<div id=\"JT_copy\">";
    if (mode == "http") {
        body += "<div class=\"JT_loader\"></div>";
    }
    body += "</div>";
    body += "</div>";

    $("body").append(body);
    if (hasArea > ((params['width'] * 1) + 75)) {
        var arrowOffset = getElementWidth(linkId) + 11;
        clickElementx = getAbsoluteLeft(linkId) + arrowOffset; //set x position
    } else {
        clickElementx = getAbsoluteLeft(linkId) - ((params['width'] * 1) + 15); //set x position
    }

    $('#JT').css({ left: clickElementx + "px", top: clickElementy + "px" });
    $('#JT').show();

    if (mode == "text") {
        var text = params["text"];
        if (params["image"] != null) {
            text += "<br />";
            text += "<img src=\"" + pathToImages + params["image"] + "\" />";
        }

        $('#JT_copy').attr("innerHTML", text);
    } else if (mode == "layer") {
        $('#JT_copy').html($("#" + params["id"]).html());
    } else {
        $('#JT_copy').load(url);
    }

}

function getElementWidth(objectId) {
    x = document.getElementById(objectId);
    return x.offsetWidth;
}

function getAbsoluteLeft(objectId) {
    // Get an object left position from the upper left viewport corner
    o = document.getElementById(objectId)
    oLeft = o.offsetLeft            // Get left position from the parent object
    while (o.offsetParent != null) {   // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent    // Get parent object reference
        oLeft += oParent.offsetLeft // Add parent left position
        o = oParent
    }
    return oLeft
}

function getAbsoluteTop(objectId) {
    // Get an object top position from the upper left viewport corner
    o = document.getElementById(objectId)
    oTop = o.offsetTop            // Get top position from the parent object
    while (o.offsetParent != null) { // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent  // Get parent object reference
        oTop += oParent.offsetTop // Add parent top position
        o = oParent
    }
    return oTop
}

function parseQuery(query) {
    var Params = new Object();
    if (!query) return Params; // return empty object
    var Pairs = query.split(/[;&]/);
    for (var i = 0; i < Pairs.length; i++) {
        var KeyVal = Pairs[i].split('=');
        if (!KeyVal || KeyVal.length != 2) continue;
        var key = KeyVal[0]
        key = decodeURIComponent(key);  
        var val = KeyVal[1]
        val = val.replace(/\+/g, ' ');
        val = decodeURIComponent(val);
        Params[key] = val;
    }
    return Params;
}

function blockEvents(evt) {
    if (evt.target) {
        evt.preventDefault();
    } else {
        evt.returnValue = false;
    }
}
