﻿window.CKEDITOR_BASEPATH = "/Scripts/ckeditor";

window.translations = new Object();

function checkRichTextPreference() {
    if ($.cookie('disableRichText') == null) {
        enhanceTextAreas();
        var switcher = $('#richtextSwitcher');
        if (switcher != null) {
            switcher.empty().append("<a href='javascript:toggleRichTextPreference()'>" + window.translations.Switch_to_simple_inputs + "</a>");
        }
    } else {
        var switcher = $('#richtextSwitcher');
        if (switcher != null) {
            switcher.empty().append("<a href='javascript:toggleRichTextPreference()'>" + window.translations.Switch_to_enhanced_inputs + "</a>");
        }
    }
}

function enhanceTextAreas() {
    $(".richtext").each(function(i, elem) {
        CKEDITOR.replace(elem, { toolbar: 'Home' });
    });
}

function toggleRichTextPreference() {
    if (confirm(window.translations.Switch_inputs_are_you_sure)) {
        if ($.cookie('disableRichText') == null) {
            $.cookie('disableRichText', 'true', { expires: 365 });
        } else {
            $.cookie('disableRichText', '', { expires: -1 });
        }
        location.reload();
    }
}


function enhanceDraftListRowClicks() {
    var oddColor, evenColor;
    var highlightColor = rowHighlightingColor != undefined ? rowHighlightingColor : "#A0cdee";
    $("tr.rowOdd").hover(
        function() {
            $(this).css("cursor", "pointer");
            oddColor = $(this).css("background-color");
            $(this).css("background-color", highlightColor);
        },
        function() {
            $(this).css("background-color", oddColor);
        });
    $("tr.rowEven").hover(
        function() {
            $(this).css("cursor", "pointer");
            evenColor = $(this).css("background-color");
            $(this).css("background-color", highlightColor);
        },
        function() {
            $(this).css("background-color", evenColor); 
        });    
    $(".rowOdd").bind("click", function(e) { window.location.href = "/Home/Details/" + this.id; });
    $(".rowEven").bind("click", function(e) { window.location.href = "/Home/Details/" + this.id; });
}

function enhanceDraftListSelectors() {
    $(".draftListSelectorButton").css("display", "none");
    $(".draftListSortAndFilter").bind("change", function(e) { draftListChange(this); });
    $(".draftListSort").bind("change", function(e) { draftListChange(this); });
    if ($.browser.msie) {
        // Fix for ie6+ keyboard accessibility
        // Adapted from: http://www.themaninblue.com/experiment/AccessibleSelect/
        $(".draftListSortAndFilter").each(function() { this.changed = false; });
        $(".draftListSort").each(function() { this.changed = false; });
        $(".draftListSortAndFilter").bind("focus", function(e) { draftListFocus(this); });
        $(".draftListSortAndFilter").bind("keypress", function(e) { draftListKeyed(this, e); });
        $(".draftListSortAndFilter").bind("click", function(e) { draftListClick(this); });
        $(".draftListSort").bind("focus", function(e) { draftListFocus(this); });
        $(".draftListSort").bind("keypress", function(e) { draftListKeyed(this, e); });
        $(".draftListSort").bind("click", function(e) { draftListClick(this); });
    } 
}
function draftListChange(selectList) {
//  This prevents the JQuery select lists from working correctly
//    if (selectList.changed != undefined &&       !selectList.changed) return false;
    if (selectList.value != "") selectList.form.submit();
}
function draftListClick(selectList) { selectList.changed = true; }
function draftListFocus(selectList) { selectList.initValue = selectList.value; }
function draftListKeyed(selectList, e) {
    if (e.keyCode && e.keyCode == 13) { // Rtn key (ignore tab key)
        selectList.changed = true;
        draftListChange(selectList);
    } else if (e.keyCode && e.keyCode == 27) { // Esc key
        selectList.value = selectList.initValue;
        selectList.selectedIndex = 0;
    } else {
        selectList.changed = false;
    }
}

function printPage() {
    if (window.print) {
        var header = document.getElementById("header");
        if (header) {
            var url = document.createElement("p");
            url.id = "printpage_url";
            url.innerHTML = top.location.href;
            url.className = "clear";
            header.parentNode.insertBefore(url, header);
        }
        window.print();
        setTimeout("printPage_cleanUp()", 100);
    }
}

function printPage_cleanUp() {
    var url = document.getElementById('printpage_url');
    if (url)
        url.parentNode.removeChild(url);
}

function preventDoubleSubmit() {
    $('form').submit(function () {
        if (!$(this).hasClass("noDisable"))
            $('input[type=submit]', this).attr('disabled', 'disabled');
    });
}

function setupSubmitSaveButtons() {
    $('.commentSave,.docCommentSave').each(function () {
        $('input[type=submit]', this).click(function () {
            $('input[name=submitOrSave]').val('save');
        });
    });
    $('.commentSubmit,.docCommentSubmit').each(function () {
        $('input[type=submit]', this).click(function () {
            $('input[name=submitOrSave]').val('submit');
        });
    });
}

function setupOtherAffiliation() {
    $(document).ready( function() {
        if ($("input#affiliationSelectDefault:checked").val()) {
            $('input#affiliationOther').attr('disabled', 'disabled');
        }
    });
    
    $('input#affiliationSelectOtherRadio').click(function () {
        $('input#affiliationOther').removeAttr('disabled', '');
    });

    $('input#affiliationSelectDefault').click(function () {
        $('input#affiliationOther').attr('disabled', 'disabled');
    });
}

function commentUnsavedWarning() {
    window.onbeforeunload = function () {
        if ((CKEDITOR.instances.issue != null && CKEDITOR.instances.issue.checkDirty()) ||
            (CKEDITOR.instances.remedy != null && CKEDITOR.instances.remedy.checkDirty())) {
            return "You have not saved or submitted your comment.";
        }
    };
    $('textarea#issue,textarea#remedy').change(function () {
        window.onbeforeunload = function () {
            return "You have not saved or submitted your comment.";
        };
    });
    $('textarea#issue,textarea#remedy').closest('form').submit(function () {
        window.onbeforeunload = null
    });
}

function setupPopupWindowButtons() {
    window.popups = {};
    $('a.shouldPopupWindow').bind('click', function () {
        var link = $(this);
        var windowWidth = link.attr("width");
        var windowHeight = link.attr("height");
        if (!windowWidth) windowWidth = 400;
        if (!windowHeight) windowHeight = 300;
        var left = (screen.width / 2) - (windowWidth / 2);
        var top = (screen.height / 2) - (windowHeight / 2);
        closePopupWindow();
        window.popups.popup = window.open(link.attr('href'), 'thePopup', "width=" + windowWidth + ",height=" + windowHeight + ",top=" + top + ",left=" + left, true);
        window.popups.popup.focus();
        $(window).bind('unload', closePopupWindow);
        return false;
    });

    $('.shouldCloseWindow').bind('click', function () {
        window.close();
        return false;
    });
}

function closePopupWindow() {
    if (window.popups.popup != null && window.popups.popup.close) {
        window.popups.popup.close();
        window.popups.popup = null;
    }
}

function prettifySubmitButtons() {
    $('input.searchSubmit').each(function (index, element) {
        var el = $(element);
        el.hide();
        var newEl = $('<a href="#" class="searchSubmit">' + el.val() + '</a>');
        newEl.bind('click', (function (button) {
            return function () {
                var input = button.parent().find('input[type="text"]');
                if (input.val() != '') {
                    button.click();
                }
                return false;
            }
        })(el));
        el.before(newEl);
    });
}

function focusOnErrorSummary() {
    $('span.validation-summary-errors').attr("id", "errors");
    $('span.validation-summary-errors').attr("tabindex", "0");
    $("#errors").focus();
}

$(document).ready(function () {

    enhanceDraftListSelectors();

    checkRichTextPreference();

    enhanceDraftListRowClicks();

    preventDoubleSubmit();

    setupSubmitSaveButtons();

    setupOtherAffiliation();

    commentUnsavedWarning();

    setupPopupWindowButtons();

    prettifySubmitButtons();

    focusOnErrorSummary();
});

