﻿// Set up editors
$(function() {
    $(".editor").ckeditor();
});

function UpdateEditors() {
    for (instance in CKEDITOR.instances)
        CKEDITOR.instances[instance].updateElement();
}

// Delete Confirmation
$(function() {
    $("a:contains('Delete')").click(function() {
        if (!confirm("Delete this item?"))
            return false;
    });
});

// Datepicker
$(function() {
    $(".datepicker").each(function() {
        var calIcon = $("<img src='images/COMM_DateIconSize16x16.gif' alt='Calendar' align='middle' style='margin-left:5px' />").click(function() {
            $(this).prev().datepicker({ onSelect: function() { $(this).prev().datepicker('destroy'); } }, { onClose: function(date) { $(this).prev().datepicker('destroy'); } }).datepicker('show'); return false;
        }).css("cursor", "pointer");
        $(this).after(calIcon);
    });
});

// Change img align's
$(function() {
    $("img[align='middle']").attr("align", "absmiddle");
});

// Module Manager
$(function() {
    $(".modulemgr-showbutton").click(function() {
        $(".modulemgr").animate({ height: "600px", width: "450px" }, 300, function() {
            $(".modulemgr-contentwrapper").show();
            var contentht = $(".modulemgr").height() - $(".modulemgr-title").outerHeight();
            $(".modulemgr-content").css("height", contentht - 10 + "px");
            var modht = $(".modulemgr-content").height() - $(".modulemgr-updatebutton").outerHeight();
            $(".modulemgr-modulediv").css("height", modht + "px");
        });
    });

    $(".modulemgr-closebutton").click(function() {
        $(".modulemgr-contentwrapper").hide();
        $(".modulemgr").animate({ height: "0", width: "0" }, 300);
    });

    $(".modulemgr-updatebutton-button").click(function() {
        if (!confirm("Are you sure you want to make these changes? If you have ticked any 'Delete' boxes, those modules will be removed permanently."))
            return false;
    });
});

// Returns true if the current browser is IE and is less than version 8
function IsIEBelow8(){
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) < 8){
        return true;
    }else{
        return false;
    }
}

// jQuery function to center an object on the screen
jQuery.fn.center = function() {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}

//////////////// Browser specific adjustments
$(function() {
    if (IsIEBelow8()) {
        $(".searchpane-searchbox input").css("margin-bottom", "2px");
        $(".photopane").css("margin-bottom", "-3px");
        $(".middle").css("margin-bottom", "-20px");
        $(".pagemenu li").css({ "margin-top": "0", "margin-bottom": "0" });
        $(".pagemenu li:odd").css("margin-top", "-3px");
        $(".homemodule").closest("body").find(".menupane").css("margin-bottom", "75px");
    }
});

//////////////// Other specific adjustments

// Make pagename in "Website Changes" report a link to the page
$(function() {
    $("h3:contains('Website Changes')").closest(".contentpanecontainer").find(".reports-reporttable tr").each(function() {
        var pagename = $(this).find("td:first").text();
        $(this).find("td:first").html("<a href='Default.aspx?pn=" + pagename + "'>" + pagename + "</a>");
    });
});

