$(document).ready(function () {

    // Modal content scroll down
    var winH = $(window).height();
    $(".ModalWindow").each(function (i) {
        var top = (winH / 2) - ($(this).parent().height() / 2);
        $(this).css("top", top); // $("#" + i).append(document.createTextNode(" - " + val));
    });

    // Add JavaScript-active class
    $("body").addClass("js");

    $("body.js .FeedbackForm, .SignIn").hide();

    // Toggle feedback
    $("body.js .ModulFeedback .Trigger").click(function () {
        if ($(this).parent().next().hasClass("Visible")) {
            $(this).parent().next().removeClass("Visible").fadeOut();
        } else {
            $(this).parent().next().addClass("Visible").fadeIn();
        }
    });

    $("a[id$=LinkLoginTryAgain]").click(function () {
        $("ul.toolbarMenu li#login div.Login").show();
        $("ul.toolbarMenu li#login div.LoginFail").hide();
    });

    setTimeout(function () { AdjustFilmViewImagePosition($(".FilmView img"), $(".FilmView .InformationHidden")); }, 400);
    setTimeout(function () { AdjustFilmViewImagePosition($(".UploadPreview img"), $(".UploadPreview .InformationHidden")); }, 400);

    $(".UploadFileTerms input").click(function () {
        if ($(this).is(':checked')) {
            $(".Grid_4 .Grid_4_Content .Column_3 .ValidationErrorMessage").hide();
        }
    });

    /*IE 17 fix*/
    if (jQuery.browser.msie && jQuery.browser.version < 8) {

        $('.toolbarMenu > li:not(".language")').each(function () {
            $(this).find('.subMenu').css({ 'margin-left': -$(this).outerWidth() + 11 });
            $('.rightContent').find('.toolbarMenu > li:not(".language")').each(function () {
                $(this).find('.subMenu').css({ 'margin-left': -($(this).find('.subMenu').outerWidth()) - 18 });
            });
        });

    } else {
        /*Moving right allign toolbar submenu to the left
        +60 i padding and margin that we need to add */
        $('.rightContent').find('.toolbarMenu > li:not(".language")').each(function () {

            if (jQuery.browser.mozilla) {
                var scope = $(this).find('.subMenu');
                scope.css({ 'position': 'relative', 'left': '9999px', 'display': 'block' })
                width = scope.outerWidth();
                scope.removeAttr('style');
                scope.css({ 'margin-left': -(width + 59) + $(this).outerWidth() });
            } else {
                var scope = $(this).find('.subMenu');
                scope.css({ 'position': 'relative', 'left': '9999px', 'display': 'block' })
                width = scope.outerWidth();
                scope.removeAttr('style');
                scope.css({ 'margin-left': -(width + 60) + $(this).outerWidth() });
            }
        });
    }



    var timer = setTimeout("");
    var inputFocus = false;
    var mouseOver = false;
    $('.SubMenuTop').hover(
    function () {
        removeSubmenu();
        mouseOver = true;
        clearTimeout(timer);
        $(this).addClass('hover').find('.subMenu');
    }, function () {
        mouseOver = false;
        if (!inputFocus) {
            timer = setTimeout('removeSubmenu()', 500);
        }
    });

    $('.subMenu input,.subMenu button,.subMenu a').hover(
    function () {
        $('.SubMenuTop').unbind('mouseleave');
        clearTimeout(timer);
    }, function () {
        $('.SubMenuTop').bind('mouseleave', function () {
            mouseOver = false;
            if (!inputFocus) {
                timer = setTimeout('removeSubmenu()', 500);
            }
        });
    }).focus(
    function () {
        inputFocus = true;
        clearTimeout(timer);
    }).focusout(
    function () {
        inputFocus = false;
        if (!mouseOver) {
            timer = setTimeout('removeSubmenu()', 500);
        }
    });

    if ($('.selector').length > 0) {
        $('.selector').selectList({ listSize: 5 });
    }

    $("#portal .signUp select[id$=DropDownSite]").change(function () {
        if ($(this).val() != "") {
            $("#portal .signUp #connectselector").slideDown();
        }
        else {
            $("#portal .signUp #connectselector").slideUp();
        }
    });

    $(".Grid_4_Content .signUp select[id$=DropDownSite]").change(function () {
        if ($(this).val() != "") {
            $(".Grid_4_Content .signUp #connectselector").slideDown();
        }
        else {
            $(".Grid_4_Content .signUp #connectselector").slideUp();
        }
    });

    if ($('#portal').length > 0) {
        scope = $('#portal');
        width = scope.find('.intro').find('img').outerWidth();
        scope.find('.floater').css('margin-left', width + 15);
    }

    $(".ModulActionpanel .Content ul li a label").each(function () {
        if ($(this).height() > 20) {
            $(this).parent().addClass("TwoRows");
        }
    });

    $(".ModulForm .Content_2 #GlobalSites input").change(function () {
        InitDropDownGlobalSites();
    });
});

function InitDropDownGlobalSites() {
    var dropdownstartpage = $(".ModulForm .Content_2 .GlobalSitesStartPage [id$=DropDownStartPage]");
    dropdownstartpage.find("option").hide();

    var ischecked = false;
    $(".ModulForm .Content_2 #GlobalSites input").each(function () {
        if ($(this).is(":checked")) {
            ischecked = true;
            var id = $(this).attr("id").split(':')[1];
            dropdownstartpage.find("option[value='" + id + "']").show();
        }
    });

    if (ischecked) {
        $($(".ModulForm .Content_2 .GlobalSitesStartPage [id$=DropDownStartPage] option")[0]).show();
        if (!dropdownstartpage.find("option[value='" + dropdownstartpage.val() + "']").is(":visible")) {
            dropdownstartpage.val("");
        }
        $(".ModulForm .Content_2 .GlobalSitesStartPage").show();
    }
    else {
        $(".ModulForm .Content_2 .GlobalSitesStartPage").hide();
    }
}

function removeSubmenu() {
    $('.toolbarMenu').find('.hover').removeClass("hover");
}

function Login(language) {
    $("ul.toolbarMenu li#login div.Login").hide();
    $("ul.toolbarMenu li#login div.AjaxLoader").show();

    var username = $("ul.toolbarMenu li#login div.Login input[id$=TextBoxUsername]").val();
    var password = $("ul.toolbarMenu li#login div.Login input[id$=TextBoxPassword]").val();
    var rememberMe = $("ul.toolbarMenu li#login div.Login input[id$=CheckBoxRememberMe]").is(':checked');

    Dvoted.CMS.Services.AjaxScriptService.AuthenticateUser(username, password, rememberMe, language, Login_onSuccess, Login_onFail);
}

function Login_onSuccess(response) {
    if (response) {
        if (response.IsAuthenticated) {
            window.location.reload();
        }
        else {
            $("ul.toolbarMenu li#login div.LoginFail label").html(response.ErrorMessage);
            $("ul.toolbarMenu li#login div.LoginFail").show();
            $("ul.toolbarMenu li#login div.AjaxLoader").hide();
        }
    }
}

function Login_onFail() {
}

// Forum ToggleTopicOptions
function ToggleTopicOptions(control) {
    if ($(control).hasClass("Visible")) {
        $(control).removeClass("Visible").slideUp(600);
    } else {
        $(control).addClass("Visible").slideDown(400);
    }
}

// Forum Search ToggleForumSearchOptions
function ToggleForumSearchOptions(control, link) {
    if ($(control).hasClass("Visible")) {
        $(control).removeClass("Visible").slideUp(600);
        $(link).text('More search options');
    } else {
        $(control).addClass("Visible").slideDown(400);
        $(link).text('Less search options');
    }
}

// Wall
function ToggleWallComment(control) {
    if ($(control).hasClass("Visible")) {
        $(control).removeClass("Visible").slideUp(600);

    } else {
        $(control).addClass("Visible").slideDown(400);

    }
}

function WallCommentPost(link, div) {
    if ($("#" + div).hasClass("Visible")) {
        $("#" + div).removeClass("Visible");
        $("#" + div).slideUp();
        $("#" + link).css("color", "#F87428");
    } else {
        $("#" + div).addClass("Visible");
        $("#" + div).slideDown();
        $("#" + link).css("color", "#999999");
    }
}

// Userform
function ToggleChangePassword() {
    if ($("#DivChangePassword").hasClass("Visible")) {
        $("#DivChangePassword").removeClass("Visible").slideUp();
    } else {
        $("#DivChangePassword").addClass("Visible").slideDown();
    }
}

function AdjustFilmViewImagePosition(img, info) {
    if (img.length > 0) {
        $(img).each(function (index) {
            var wrapperHeight = 0;
            if ($(this).parent().is('li') || $(this).parent().parent().parent().parent().parent().is('li')) {
                if ($(this).parent().is('li')) {
                    wrapperHeight = $(this).parent().parent().parent().parent().height();
                }
                else {
                    wrapperHeight = $(this).parent().parent().height();
                }
            }
            else {
                wrapperHeight = $(this).parent().height();
            }
            var objHeight = $(this).height();
            if (objHeight > 0) {
                var diff = wrapperHeight - objHeight;
                $(this).css("margin-top", diff / 2).fadeIn(250, function () { $(info[index]).fadeIn(200); });
            }
        });
    }
}

function HandleFormLabel(obj, isOnFocus) {
    var label = $(obj).parent().find("label").html();
    if (isOnFocus) {
        if ($(obj).val() == label) {
            $(obj).val("");
        }
    }
    else {
        if ($(obj).val() == "") {
            $(obj).val(label);
        }
    }
}
