$(document).ready(function () {

    String.prototype.endsWith = function (str, ignoreCase) {
        var regex = ignoreCase ? new RegExp(str + "$", "i") : new RegExp(str + "$", "i");
        return this.match(regex) != null;
    }

    $(".lightbox:not('.iFramed')").click(function (e) {
        if (!$(this).hasClass("iFramed")) {
            e.preventDefault();
            var autoScale, autoDimensions, width, height, title, titleShow, titlePosition, id;
            id = $(this).attr('id').split("_");

            height = id[1].split("height");
            height = parseInt(height[1]) + 40;
            width = id[2].split("width");
            width = parseInt(width[1]) + 40;
            background = id[0].split("background");
            background = background[1];
            title = $(this).attr('title');
            autoScale = false;
            autoDimensions = false;
            titleShow = false;
            titlePosition = 'inside';
            if (height == 'NaN' || height == 0) {
                height = '95%';
            }
            if (title != 'NaN') {
                titleShow = true;

            }
            $(this).fancybox({
                'titleShow': titleShow,
                'titlePosition': titlePosition,
                'width': width,
                'height': height,
                'closeWord': ' ',
                'autoScale': autoScale,
                'autoDimensions': autoDimensions,
                'type': 'iframe',
                'overlayColor': background

            }).trigger('click.fb');
            $("#fancybox-wrap").find("iframe").bind("load", function () {
                $("iframe").contents().find(".lightbox").each(function () {
                    $(this).addClass("iFramed").attr("href", this.href + "?LB=1");
                });
                $("iframe").contents().find("a:not('.lightbox')").each(function () {
                    if (!this.target) this.target = "_parent";
                    if (window.location.href.endsWith(this.href, true)) $(this).addClass("lightboxClose");
                });
                $("iframe").contents().find("a.lightboxClose").click(function (e) {
                    e.preventDefault();
                    $("#fancybox-close").click();
                });
                $("iframe").contents().find("#sub").hide();
                $("iframe").contents().find("#page, #main, #s4-bodyContainer, #content .full").css({ "width": "auto", "float": "none", "minWidth": "1%", "minHeight": "1%" })
                $("iframe").contents().find(".sitePath").css({ "margin": "0 0 10px 0" })
                $("iframe").contents().find("#content").css({ "float": "none", "width": "auto", "padding": "0", "minHeight": "1%" })
            });
        }
    });

});

var temp = true;

function linkInLightBox() {
    //alert("linkInLightBox");
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxLink']");
    var eventTriggerHeight = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxHeight']");
    var eventTriggerWidth = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxWidth']");

    eventTriggerWidth.keydown(function (e) { return isNumberKey(e) });
    eventTriggerHeight.keydown(function (e) { return isNumberKey(e) });

    //alert("id=" + $(eventHandler).attr("id") + "; trigger.checked=" + eventTrigger[0].checked);
    if (eventTrigger[0].checked) {
        $(eventHandler).addClass("lightbox");
        if (!$(eventHandler).attr("id") || $(eventHandler).attr("id") == null || $(eventHandler).attr("id") == NaN || $(eventHandler).attr("id") == '' || $(eventHandler).attr("id") == 'undefined') {
            $(eventHandler).attr("id", "backgroundgray_height500_width500");
            eventTriggerHeight.val(500).removeAttr("disabled");
            eventTriggerWidth.val(500).removeAttr("disabled");
        }
    }
    else {
        $(eventHandler).removeClass("lightbox");
        $(eventHandler).attr("id", "");
        eventTriggerHeight.val("").attr("disabled", "disabled");
        eventTriggerWidth.val("").attr("disabled", "disabled");
    }
}

function queryLinkInLightBox() {
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxLink']");
    var eventTriggerHeight = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxHeight']");
    var eventTriggerWidth = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxWidth']");

    if ($(eventHandler).hasClass("lightbox") && temp) {
        eventTrigger.focus().click();
    }
    else if (temp) {
        eventTriggerHeight.val("").attr("disabled", "disabled");
        eventTriggerWidth.val("").attr("disabled", "disabled");
    }
    temp = false;
}

function linkLightBoxWidth() {
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxWidth']");
    var lightBoxId = $(eventHandler).attr("id");
    if (lightBoxId != '' && lightBoxId != null) {
        lightBoxId = lightBoxId.split("_");
        for (var i = 0; i < lightBoxId.length; i++) {
            if (lightBoxId[i].search("width") != -1) {
                lightBoxId[i] = "width" + eventTrigger.val();
            }
        }
        $(eventHandler).attr("id", lightBoxId.join("_"));
    }
}

function queryLinkLightBoxWidth() {
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxWidth']");
    var lightBoxId = $(eventHandler).attr("id");
    if (lightBoxId != '' && lightBoxId != null) {
        lightBoxId = lightBoxId.split("_");
        for (var i = 0; i < lightBoxId.length; i++) {
            if (lightBoxId[i].search("width") != -1) {
                lightBoxId[i] = lightBoxId[i].replace("width", "");
                eventTrigger.val(lightBoxId[i]);
            }
        }
    }
}

function linkLightBoxHeight() {
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxHeight']");
    var lightBoxId = $(eventHandler).attr("id");
    if (lightBoxId != '' && lightBoxId != null) {
        lightBoxId = lightBoxId.split("_");
        for (var i = 0; i < lightBoxId.length; i++) {
            if (lightBoxId[i].search("height") != -1) {
                lightBoxId[i] = "height" + eventTrigger.val();
            }
        }
        $(eventHandler).attr("id", lightBoxId.join("_"));
    }
}

function queryLinkLightBoxHeight() {
    var eventHandler = RTE.CanvasEvents.getNodeFromEvent('Link');
    var eventTrigger = $("input[id*='Ribbon.Link.Link.Behavior.LightBoxHeight']");
    var lightBoxId = $(eventHandler).attr("id");
    if (lightBoxId != '' && lightBoxId != null) {
        lightBoxId = lightBoxId.split("_");
        for (var i = 0; i < lightBoxId.length; i++) {
            if (lightBoxId[i].search("height") != -1) {
                lightBoxId[i] = lightBoxId[i].replace("height", "");
                eventTrigger.val(lightBoxId[i]);
            }
        }
    }
}

/* rte functions */
