﻿/// <reference path="~/js/jQuery/jquery-1.6.1-vsdoc.js" />
/// <reference path="~/js/Browse.js" />

$(document).ready(function () {
    $('a:contains("INSTALL PLUG-IN")').click(
        function () {
            window.startInstall();
            return false;
        });

    $('a:contains("ENABLE PAGERAGE")').click(
        function () {
            checkInstallId(20, "enablePageRage", "enablePageRage");
        });

    if ($.browser.msie && parseInt($.browser.version) <= 7) {
        $(function () {
            var zIndexNumber = 1000;
            $('div').each(function () {
                $(this).css('zIndex', zIndexNumber);
                zIndexNumber -= 10;
            });
        });
    }

    prepareMenu();

    $('ul#FooterMenu li:last-child, #PageRageMenu li:last-child, #loginWrapper li:last-child').addClass('lastListItem');
    
    $('a.noLink').click(function (e) {
        e.preventDefault();
    });

    $('input[title!=""]').hint();
    $('textarea[title!=""]').hint();

    $('#loginTrigger').click(function () {
        $('#loginPanel').show();
        $('form input').keypress(function (e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $('#LoginButton').click();
                return true;
            }
        });
    });

    if ( $('input.passwordField').length > 0 && 
    !($.browser.msie && parseInt($.browser.version) <= 8)) {
        setInitialPasswordInputType();

        $('#loginTrigger').click(setInitialPasswordInputType);

        $('input.passwordField').focus(function (event) {
            event.preventDefault();
            $(this).removeAttr('type');
            $(this).prop('type', 'password');
        });

        $('input.passwordField').blur(function (event) {
            event.preventDefault();
            setPasswordInputType($(this));
        });
    }

    $('#loginPanel').mouseleave(function () {
        if ($('#UserName:focus,#Password:focus,#RememberMe:focus').length == 0) {
            $(this).fadeOut('slow');
            $('form input').keypress(function (e) {
                if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                    $('.SearchBarPageSearchBtn input').click();
                    return true;
                }
            });
        }
    });

    $('#loginPanel input[type="text"], #loginPanel input[type="password"]').mouseover(function () {
        $(this).stop().attr('title', '');
    });

});


$(function () {
    $('ul#FooterMenu').find('> li').hover(function () {
        $(this).find('ul')
        .stop(true, true).slideToggle('fast');
    });
});

function setInitialPasswordInputType() {
    $('input.passwordField').removeAttr('type');
    if ($('input.passwordField').val() == '' || $('input.passwordField').val().indexOf('PASSWORD') > -1) {
        $('input.passwordField').prop('type', 'text');
    }
    else {
        $('input.passwordField').prop('type', 'password');
    }
}

function setPasswordInputType(passwordField) {
    $(passwordField).removeAttr('type');
    if ($(passwordField).val() == '' || $(passwordField).val().indexOf('PASSWORD') > -1) {
        $(passwordField).prop('type', 'text');
    }
    else {
        $(passwordField).prop('type', 'password');
    }
}


function prepareMenu() {
    var menuWidth = $('#PageRageMenu').outerWidth(true);
    var menuCount = $('#PageRageMenu li:visible').length;
    var menuWidthPixels = Math.floor(((menuWidth - (menuCount - 1)) / menuCount));
    var menuExtraPixels = ((menuWidth - (menuCount - 1)) % menuCount);

    $('#PageRageMenu li').css('width', menuWidthPixels + 'px');
    $('#PageRageMenu li:last-child').css('width', (menuWidthPixels + menuExtraPixels) + 'px');

    $('#PageRageMenu a.selected').parent().addClass('selected');

    var footerMenuWidth = $('#FooterMenu').outerWidth(true);
    var footerMenuMargin = ((980 - footerMenuWidth) / 2);
    $('#FooterMenu').css('margin-left', footerMenuMargin + 'px');
}

function hideMenuItem(menuTitle) {
    var menuItem = $('a:contains("' + menuTitle + '")').parent();

    if (menuItem.length == 0) {
        return;
    }

    menuItem.hide();
    prepareMenu();
}

function showMenuItem(menuTitle) {
    var menuItem = $('a:contains("' + menuTitle + '")').parent();

    if (menuItem.length == 0) {
        return;
    }

    menuItem.show();
    prepareMenu();
}

function activateOptions(clientInstallId) {
    if (!clientInstallId || (clientInstallId + "").length !== 36) {
        $("#SiteUI-PrimaryNav").find("a").css("margin", "0 17px");

        $("#InstallLeftNav").show();

        if ($("#InstallButton")) {
            $('#CreateButton').fadeOut('slow');
            $("#InstallButton").fadeIn('slow');
        }

        hideMenuItem("ENABLE PAGERAGE");
        hideMenuItem("CREATE YOUR OWN");
        showMenuItem("INSTALL PLUG-IN");
    }
    else {
        hideMenuItem("INSTALL PLUG-IN");
        $.getJSON('//www.pagerage.com/EnableHandler.ashx',
            { id: clientInstallId, rnd: Math.floor(Math.random() * 10000) },
            function (results) {
                if (results) {
                    $("#SiteUI-PrimaryNav").find("a").css("margin", "0 26px");
                    if ($("#CreateButton")) {
                        $("#CreateButton").fadeIn('slow');
                    }
                    hideMenuItem("ENABLE PAGERAGE");
                    showMenuItem("CREATE YOUR OWN");
                }
                else {
                    $("#SiteUI-PrimaryNav").find("a").css("margin", "0 16px");

                    $("#EnableLeftNav").bind("click", clientInstallId, function (event) {
                        enablePageRage(event.data);
                    });
                    $("#EnableLeftNav").show();

                    if ($("#EnableButton")) {
                        $("#EnableButton").bind("click", clientInstallId, function (event) {
                            enablePageRage(event.data);
                        });
                        $('#CreateButton').fadeOut('slow');
                        $("#EnableButton").fadeIn('slow');
                    }

                    $('a:contains("ENABLE PAGERAGE")')
                        .unbind("click")
                        .bind(
                        "click",
                        clientInstallId,
                        function (event) {
                            enablePageRage(event.data);
                        }
                    );
                }
            }
        );
    }
}

function enablePageRage(id) {
    var data = {};
    data.id = id;
    data.enable = "true";
    data.rnd = Math.floor(Math.random() * 10000);
    $.getJSON(
        '//www.pagerage.com/EnableHandler.ashx',
        data,
        function (results) {
            if (results) {
                $("#EnableButtton").fadeOut(400);
                hideMenuItem("ENABLE PAGERAGE");
                $("#EnableLeftNav").hide();
                $("#CreateButton").fadeIn(400);
                showMenuItem("CREATE YOUR OWN");
            }
            else {
                alert("Enable failed. Please try again.");
            }
        }
    );

}

function validateRequiredField(callingObject) {
    if ($(callingObject).val() == '' ||
        $(callingObject).val() == $(callingObject).attr('title')) {
        $(callingObject).addClass('failedValidation');
        return false;
    } else {
        $(callingObject).removeClass('failedValidation');
        return true;
    }
}

function isLoginCompleted() {
    var isUserNameEntered = validateRequiredField($('#UserName')[0]);
    var isPasswordEntered = validateRequiredField($('#Password')[0]);
    return isUserNameEntered && isPasswordEntered;
}


