﻿/// <reference path="SMSCentral.Utils.js" />

function getPaginationLinks() {
    var commonPaginationLinks = {
        prev_link: "<span class='actionButton icon'><a href='#' title='" + Lang.Common.PreviousPage + "'><span>" + Lang.Common.PreviousPage + "</span></a></span>",
        next_link: "<span class='actionButton icon'><a href='#' title='" + Lang.Common.NextPage + "'><span>" + Lang.Common.NextPage + "</span></a></span>",

        prev_link_disabled: "<span class='actionButton disabled icon'><a title='" + Lang.Common.PreviousPage + "'><span>" + Lang.Common.PreviousPage + "</span></a></span>",
        next_link_disabled: "<span class='actionButton disabled icon'><a title='" + Lang.Common.NextPage + "'><span>" + Lang.Common.NextPage + "</span></a></span>"
    };
    return commonPaginationLinks;
}



// Site logic
var activeMenuElement = null;
var timers = {};
var alertDialogButtons = {
    Ok: function () {
        $(this).dialog('close');
    }
};

function showMessage(title, message, isError) {
    if (!$("#dialog").dialog('isOpen')) {
        if (isError) {
            message = "<span class='ui-icon ui-icon-alert' style='margin: 10px; margin-top: 0px; margin-left: 0px; float: left;' />" + message;
        }

        $("#dialog").html(message);
        $("#ui-dialog-title-dialog").html(title);

        $('#dialog').dialog('option', 'buttons', alertDialogButtons);
        $("#dialog").dialog('open');
    }
}

var antiForgeryToken = null;

function overMainElement(e, _this) {
    // Show sub menu and add hover class
    if ($(_this).attr("ref") !== undefined) {
        var position = $(_this).position();
        var subMenu = $("div.subMenuButton[rel=" + $(_this).attr("ref") + "]");
        var subMenuContent = $("div.subMenu[rel=" + $(_this).attr("ref") + "]");

        if (activeMenuElement && activeMenuElement !== $(_this).attr("ref")) {
            $("div[rel=" + activeMenuElement + "]").fadeOut(100);
        }

        subMenu.css({ left: position.left, top: position.top });
        subMenuContent.css({ left: position.left, top: position.top + 36 });

        subMenu.fadeIn(100);
        subMenuContent.fadeIn(100);
        activeMenuElement = $(_this).attr("ref");

    }
    else {
        $(_this).addClass("hover");
    }
}

function outMainElement(e, _this) {
    // Hide sub menu and remove hover class
    if ($(_this).attr("ref") !== undefined) {
        $(_this).removeClass("menuHover");
    }
    else {
        $(_this).removeClass("hover");
    }
}

$(document).ready(function() {
    // Replacing flags
    // replaceAll(str, searchTerm, replaceWith, ignoreCase)
    $("#eaddress").each(function(indexitem, item) {
        item = $(item);
        //infosmscentralpl
        item.html(replaceAll(item.html(), "infosms", "info@sms", true));
        item.html(replaceAll(item.html(), "centralpl", "central.pl", true));
        item.attr("href", replaceAll(item.attr("href"), "infosms", "info@sms", true));
        item.attr("href", replaceAll(item.attr("href"), "centralpl", "central.pl", true));
    });

    // Tooltip for credit value
    $("a.creditAndCost[title]").tooltip({ tip: '#creditTooltip', effect: 'slide', onBeforeShow: function() {
        return $("a.creditAndCost").attr("ref") === "showToolTip";
    }
    });

    // Preloading main menu images...
    $.preloadImages(
        '/Skins/Base/Images/sub_menu_container.gif',
        '/Skins/Base/Images/sub_menu_footer.gif',
        '/Skins/Base/Images/sub_menu_footer_left.gif',
        '/Skins/Base/Images/sub_menu_footer_right.gif',
        '/Skins/Base/Images/sub_menu_gradient_top.gif',
        '/Skins/Base/Images/sub_menu_left_edge.gif',
        '/Skins/Base/Images/sub_menu_left_element.gif',
        '/Skins/Base/Images/sub_menu_middle_element.gif',
        '/Skins/Base/Images/sub_menu_right_edge.gif',
        '/Skins/Base/Images/sub_menu_right_element.gif',
        '/Skins/Base/Images/sub_menu_right_top.gif',
        '/Skins/Base/Images/menu_arrow.gif');

    // Applying pngFix
    $(".logo").pngFix();

    // Styling checkboxes
    $('input:checkbox:visible').checkbox({ cls: 'jquery-safari-checkbox', empty: '/Skins/Base/jQueryCheckBox/empty.png' });

    // Initializing menu...
    $(".mainMenu .outer").each(function(index, item) {
        if ($(item).attr("ref") !== undefined) {

            // Mouse over sub element
            $("div[rel=" + $(item).attr("ref") + "]").mouseover(function(e) {
                activeMenuElement = $(item).attr("ref");

                if (timers[activeMenuElement]) {
                    clearTimeout(timers[activeMenuElement]);
                }
                $("div[rel=" + $(item).attr("ref") + "]").fadeIn();
            });

            // Mouse out sub element
            $("div[rel=" + $(item).attr("ref") + "]").mouseout(function(e) {
                if (timers[activeMenuElement]) {
                    clearTimeout(timers[activeMenuElement]);
                }
                timers[$(item).attr("ref")] = setTimeout(function() {
                    $("div[rel=" + $(item).attr("ref") + "]").fadeOut(100);
                }, 100);
            });
        }
    });
    var blockMenu = true;

    $(".mainMenu .outer").hoverIntent({
        sensitivity: 10,
        interval: 3,
        over: function(e) {
            overMainElement(e, this);
        },
        timeout: 0,
        out: function(e) {
            outMainElement(e, this);
        }
    });

    // Creating dialog windows
    if ($("#dialog").length === 0) {
        $("BODY").append("<div id='dialog'>Welcome</div>");
    }

    $("#dialog").dialog({
        bgiframe: true,
        modal: true,
        title: "SMSCentral",
        autoOpen: false,
        resizable: true,
        buttons: {
            Ok: function() {
                $(this).dialog('close');
            }
        }
    });

    $("#tutorial").dialog({
        bgiframe: true,
        modal: true,
        title: "SMSCentral - Tutoriale",
        autoOpen: false,
        resizable: false,
        width: 650,
        height: 500,
        buttons: {
            "Zamknij tutoriale": function() {
                $(this).dialog('close');
            }
        },
        show: {
            effect: 'fade',
            speed: 600
        },
        open: function(event, ui) {
            $("#tutorialButton").effect('transfer', { to: "#tutorial", className: 'ui-effects-transfer' }, 500);

            $(".ui-widget-overlay").click(function() {
                $("#tutorial").dialog('close');
            });
        },
        beforeclose: function(event, ui) {
            $("#tutorial").effect('transfer', { to: "#tutorialButton", className: 'ui-effects-transfer' }, 500);
        }
    });

    if ($("#hideTutorials").length === 0) {
        $("#tutorialButton").show();
    }

    // Initializing selectboxes
    $("select[rel=selectBox]").selectbox();
    $("select[rel=smallSelectBox]").selectbox({ className: "jquery-selectbox" });
    $("input[rel=dateBox]").datepicker({ dateFormat: 'yy-mm-dd', buttonImage: '/images/datepicker.gif' });

    var lang = currentLang.substring(0, 2);
    $('input[rel=dateBox]').datepicker('option', $.datepicker.regional[lang]);

    // Setting default AJAX options for every AJAX request
    var unloadingStarted = false;
    $.ajaxSetup({
        cache: false,
        error: function(XMLHttpRequest, textStatus, thrownError) {
            if (!unloadingStarted) {
                showMessage(Lang.AjaxMessages.AjaxErrorTitle, Lang.AjaxMessages.AjaxError, true);
            }
            if (typeof (this.context) != "undefined" && typeof (this.context.blocker) != "undefined") {
                this.context.blocker.unblock();
            }
        }
    });
    $(window).bind("beforeunload", function() {
        unloadingStarted = true;
    });


    $("#langPL").click(function() {
        $.cookie("lang", "pl-PL", { path: "/" });
        window.location.reload();
    });

    $("#langEN").click(function() {
        $.cookie("lang", "en-US", { path: "/" });
        window.location.reload();
    });

    $("#langDE").click(function() {
        $.cookie("lang", "de-DE", { path: "/" });
        window.location.reload();
    });

    antiForgeryToken = $("input[name=__RequestVerificationToken]").val();
});

function ShowTutorial() {
    if (!$("#tutorial").dialog('isOpen')) {
        $("#tutorial").dialog('open');
    }
}

function confirm(title, message, action, noAction) {
    var alertPromptButtons = {};
    alertPromptButtons[Lang.Common.No] = function () {
        if (noAction) {
            noAction();
        }
        $(this).dialog('close');
    };
    alertPromptButtons[Lang.Common.Yes] = function () {
        if (action) {
            action();
        }
        $(this).dialog('close');
    };

    $("#dialog").html(message);
    $("#ui-dialog-title-dialog").html(title);

    $('#dialog').dialog('option', 'resizable', false);
    $('#dialog').dialog('option', 'buttons', alertPromptButtons);
    $("#dialog").dialog('open');
}

function ToggleCommunication() {
    var message = $(".communication").hasClass("stopped") ? Lang.SMSCentral.UnblockCommunicationPrompt : Lang.SMSCentral.BlockCommunicationPrompt;
    confirm("SMSCentral", message, function () {
        window.location = "/Account/ToggleCommunication";
    });
}

