﻿jQuery(function($) {
    $("input[value='disabled']").each(function() {
        $(this).parent().parent().hide();
    });

    $("[class*='name.HideThisSection'] :checkbox:not(:checked)").map(function() {
        $(this).parent().parent().nextAll("div").each(function() {
            $(this).find("input[id$='_ecstate']").each(function() {
                $(this).parent().parent().hide();
                $(this).val("disabled");
            });
        });
    });



    $("[class*='name.HideThisSection'] input").click(function() {
        var checked = $(this).attr("checked");
        $(this).parent().parent().nextAll("div").each(function() {
            $(this).find("input[id$='_ecstate']").each(function() {
                if (!checked) {
                    $(this).parent().parent().hide();
                    $(this).val("disabled");
                } else {
                    $(this).parent().parent().show();
                    $(this).val("");
                }
            });
        });
    });


    $("input").keypress(function(e) {
        var isFirstmatch = true;
        if (e.keyCode == 13) {
            $(this).parents().each(function() {
                if (isFirstmatch) {
                    $(this).find("input[type='submit'],input[type='image']").map(function() {
                        isFirstmatch = false;
                        $(this).trigger('click');
                        return false;
                    });
                }
            });
            return false;
        }
    });

    $(".modalinfo").click(function() {
        var maxWidth = 800;
        var maxHeight = 800;

        if ($("#modalinfo-overlay").html() == null) {
            $(this).after("<div id='modalinfo-overlay'></div><div id='modalinfo-content'></div><div id='modalinfo-close' ><a href='#' style='float:right;'><img src='/SYSTEM/button_close.png' alt='luk' /></a></div>");
        }

        var w = ($(window).width() < maxWidth) ? $(window).width() * 0.7 : maxWidth;
        var h = ($(window).height() < maxHeight) ? $(window).height() * 0.7 : maxHeight;
        var lPos = (($(document).width() - w) / 2); // +$(document).scrollLeft();
        var tPos = (($(window).height() - h) / 2) + $(document).scrollTop();


        $("#modalinfo-overlay").css({ 'position': 'absolute', 'top': '0px', 'left': '0px', 'z-index': '1000', 'background': '#000' });
        $("#modalinfo-overlay").css({ width: $(document).width() + 'px', height: $(document).height() + 'px', opacity: 0.7 }).show(); //.fadeIn();

        $("#modalinfo-content").css({ position: 'absolute','margin-top':'15px', top: '200px', left: '200px', display: 'none', width: '600px', 'z-index': '1001', background: '#fff', padding: '10px', overflow: 'auto' });
        $("#modalinfo-content").css({ 'left': lPos + 'px', 'width': w + 'px', 'top': tPos + 'px', 'height': h + 'px', 'background': '#fff' }).fadeIn();


        $("#modalinfo-close").css({ position: 'absolute', top: '180px', left: '200px', display: 'none', width: '600px', 'z-index': '1002','border-bottom': '1px solid #ccc', background: '#fff', padding: '10px', overflow: 'auto' });
        $("#modalinfo-close").css({ 'left': lPos + 'px', 'width': w + 'px', 'top': tPos - 20 + 'px', 'height': '15px', 'background': '#fff' }).fadeIn();



        var href = $(this).attr("href").replace(/\s/g, '%20');
        $("#modalinfo-content").load(href);

        $("#modalinfo-overlay").click(function() {
            $(this).fadeOut();
            $("#modalinfo-content").fadeOut();
        });
        $("#modalinfo-close").click(function() {
            $(this).fadeOut();
            $("#modalinfo-overlay").fadeOut();
            $("#modalinfo-content").fadeOut();
        });


        $(window).resize(function() {
            $("#modalinfo-overlay:visible").map(function() {
                $(".modalinfo").trigger("click");
            });
        });
        return false;
    });


});


