﻿if (typeof je != "object") je = new Object();

je.centerLightBoxVertically = true;
je.ignoreHideOverlay = false;

je.center = function (elm, bHorizontally, bVertically) {
    if (elm === null) {
        return;
    }
    if (bHorizontally) {
        $(elm).css({
            "position": "absolute",
            "left": ($(window).width() - $(elm).outerWidth()) / 2 + $(window).scrollLeft()
        });
    }
    if (bVertically) {
        $(elm).css({
            "position": "absolute",
            "top": ($(window).height() - $(elm).height()) / 2 + $(window).scrollTop() - 15
        });
    }
}

je.showOverlay = function (oOnClickFunction, sOverlayHtml, iOverlayWidth, iOverlayHeight, bDisableBorders) {
    $("html").bind("keyup", je.keyboard);

    if (oOnClickFunction == null)
        oOnClickFunction = je.hideOverlay;
    var oOverlay = $("#overlay");
    if (oOverlay.size() == 0) {
        oOverlay = $("<div id=\"overlay\" />").appendTo(document.body);
        oOverlay.css({ "opacity": "0", "position": (document.all) ? "absolute" : "fixed" });
        oOverlay.click(function () { oOnClickFunction() });
    }

    je.resizeOverlay();
    oOverlay.fadeTo(0, 0.7);
    oOverlay.show();

    if (sOverlayHtml == null)
        return;

    var oOverlayContainer = $("#overlay-container");
    if (oOverlayContainer.size() == 0) {
        oOverlayContainer = $("<div id=\"overlay-container\" />").appendTo(document.body);
    }
    oOverlayContainer.width((iOverlayWidth) ? iOverlayWidth : "auto");
    oOverlayContainer.height((iOverlayHeight) ? iOverlayHeight : "auto");
    oOverlayContainer.html(sOverlayHtml + "<a href=\"#\" id=\"overlay-closebutton\"></a>");

    $("#overlay-closebutton").click(function () {
        oOnClickFunction();
        return false;
    });
    if (bDisableBorders) {
        oOverlayContainer.css("padding", 0);
    } else {
        oOverlayContainer.css("padding", "8px");
    }
    oOverlayContainer.show();
    je.center(oOverlayContainer, true, je.centerLightBoxVertically);

    $(window).bind("resize", je.handleOverlayWhenResized);
}

je.handleOverlayWhenResized = function () {
    je.resizeOverlay();
    je.center($("#overlay-container"), true, je.centerLightBoxVertically);
}

// Updates the light box dimensions
je.updateOverlaySize = function (iOverlayWidth, iOverlayHeight) {
    var oOverlayContainer = $("#overlay-container");
    if (oOverlayContainer) {
        oOverlayContainer.width(iOverlayWidth + "px");
        oOverlayContainer.height(iOverlayHeight + "px");
        je.center(oOverlayContainer, true, je.centerLightBoxVertically);
    }
}

je.resizeOverlay = function () {
    var oOverlay = $("#overlay");
    if (oOverlay == null)
        return;
    var arrayPageSize = je.getPageSize();
    $(oOverlay).css({
        "width": arrayPageSize[0],
        "height": arrayPageSize[1]
    });
}

je.toggleClose = function (showOrHide) {
    je.ignoreHideOverlay = !showOrHide;
    $("#overlay-closebutton").toggle(showOrHide);
}

je.hideOverlay = function () {
    if (je.ignoreHideOverlay)
        return;

    $("html").unbind("keyup", je.keyboard);
    $(window).unbind("resize", je.handleOverlayWhenResized);
    $("#overlay").fadeOut(500);
    $("#overlay-container").replaceWith("<div id=\"overlay-container\"></div>");
    $("#overlay-container").hide();
    //je.centerLightBoxVertically = false;
}

je.keyboard = function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 27)
        je.hideOverlay();
}

je.getPageSize = function () {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;

    if (self.innerHeight) {	// All except Explorer
        windowWidth = (document.documentElement.clientWidth) ? document.documentElement.clientWidth : self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
    pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

$.fn.getAttributes = function (includeEvents) {
    var attributes = this[0].attributes;
    var events = ["abort", "blur", "change", "click", "dblclick", "error", "focus", "keydown", "keypress", "keyup", "load", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup", "reset", "resize", "select", "submit", "unload"];
    var array = {};
    for (var i = 0; i < attributes.length; i++) {
        if (includeEvents || !includeEvents && jQuery.inArray(attributes[i].nodeName.replace(/^on/, ""), events) == -1) {
            array[attributes[i].nodeName] = attributes[i].nodeValue
        }
    }

    return array;
}

$(document).ready(function () {
    $("input").each(function (i, element) {
        var input = $(element);
        var atttributes = input.getAttributes(true);

        if (atttributes["type"] != "submit" && atttributes["type"] != "reset" && atttributes["type"] != "button")
            return;

        if (!atttributes["value"])
            return;

        if (atttributes["class"])
            atttributes["class"] = "btn " + atttributes["class"];
        else
            atttributes["class"] = "btn";

        var html = "<button";
        $.each(atttributes, function (key, value) { html += " " + key + "=\"" + value + "\""; });
        html += "><span><span>" + input.val() + "</span></span></button>";

        input.replaceWith(html);
    });

    $("a#print").click(function () {
        window.print();
    });

    $("a#pdf").click(function () {
        var sUrl = location.href;
        if (sUrl.indexOf("?") >= 0) sUrl += "&print=true";
        else sUrl += "?print=true";

        location.href = "http://www.pdfspot.com/html-to-pdf-converter.aspx?url=" + escape(sUrl) + "&pdf_name=" + escape('') + "&pdf_orientation=portrait&pdf_page_size=A4&scripts_enabled=true";
    });

    $("a#sendtofriend").click(function () {
        var sHtml = "<iframe src=\"/SendToFriend.aspx\" width=\"407\" height=\"300\" frameborder=\"0\" scrolling=\"no\"></iframe>";
        je.showOverlay(null, sHtml, 407, 300);
    });
});
