$(document).ready(function() {

    fixTrasparency();
    
    $("#message").html($("#message").val() + "<br/> " + window.location);

    $('.changeLang').click(function() {
        PortalController.changeLanguage($(this).attr('id').split('.')[1], {
            callback: function() {
                location.reload(true);
            },
            errorHandler: function(message) {
                alert("Error[changelanguage]: " + message);
            }
        });
    });

    $('#sendMail').click(function() {
        openMailDialog();
    });

    $('#closeMailPanel').click(function() {
        closeDialog();
    });

    $('#closeContentPanel').click(function() {
        closeDialog();
    });

    $('#printArea').click(function() {
        /*{mode: "popup", popClose: true}*/
        $("div.pageContent").printArea();
    });

    $("#submit").click(function() {
        $(".error").hide();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailToVal = $("#emailTo").val();
        if (emailToVal == '') {
            $("#emailTo").after('<span class="error" style="padding-left: 25px;">' + getContent['general.email.required'] + '</span>');
            hasError = true;
        } else if (!emailReg.test(emailToVal)) {
            $("#emailTo").after('<span class="error" style="padding-left: 25px;">' + getContent['general.email.error'] + '</span>');
            hasError = true;
        }

        /*var emailFromVal = $("#emailFrom").val();
            if(emailFromVal == '') {
                    $("#emailFrom").after('<span class="error">You forgot to enter the email address to send from.</span>');
                    hasError = true;
            } else if(!emailReg.test(emailFromVal)) {
                    $("#emailFrom").after('<span class="error">Enter a valid email address to send from.</span>');
                    hasError = true;
            }*/

        var subjectVal = $("#subject").val();
        if (subjectVal == '') {
            $("#subject").after('<span class="error" style="padding-left: 25px;">' + getContent['general.subject.required'] + '</span>');
            hasError = true;
        }

        var messageVal = $("#message").val();
        /*            if(messageVal == '') {
                    $("#message").after('<span class="error" style="padding-left: 25px;">You forgot to enter the message.</span>');
                    hasError = true;
            }
*/
        var noteVal = $("#note").val();
        if (hasError == false) {
            PortalController.sendMail(emailToVal, subjectVal, messageVal, noteVal, {
                callback: function(error) {
                    if (error != "") {
                        $('#submit').hide();
                        $("#sendEmail").hide('normal', function() {
                            $("#sendEmail").before('<span class="error">' + getContent['general.email.sent'] + '</span>');
                        });

                    }
                },
                errorHandler: function(message) {
                    alert("Error[sendmail]: " + message);
                }
            });
        }

        return false;
    });

    initMenu();
    $('#addFavorites').jFav();
});

function fixTrasparency() {
    $(".trasparency-50").css("opacity", "0.2");
    $(".trasparency-50").css("-moz-opacity", "0.2");
    $(".trasparency-50").css("filter", "alpha(opacity=20)");

    $(".trasparency-90").css("opacity", "0.9");
    $(".trasparency-90").css("-moz-opacity", "0.9");
    $(".trasparency-90").css("filter", "alpha(opacity=90)");

    $(".trasparency-15").css("opacity", "0.05");
    $(".trasparency-15").css("-moz-opacity", "0.05");
    $(".trasparency-15").css("filter", "alpha(opacity=5)");
}

function search() {
    if (document.searchForm.phrase.value == "") return;
    location.href = context + '/search/' + document.searchForm.phrase.value;
}

function openLanguageDialog() {
    box = new Boxy($('#languagePanel'), {modal: true});
}

function openContentDialog(key) {

    box = new Boxy($('#contentPanel'), {modal: true});
    $('#contentPanel .titleText').html(getContent['popup.' + key + '.title']);
    $('#contentPanel .contentText').html(getContent['popup.' + key + '.description']);
}

function openMailDialog() {

    resetForm();
    /*slideDown containerMail */
    box = new Boxy($('#containerMail'), {modal: true});
}

function closeDialog() {
    box.hide();
}

function resetForm() {
    $('#submit').show();
    $("#sendEmail").show();
    $(".error").hide();
    $(':input', '#sendEmail').not(':button, :submit, :reset, :hidden, #message').val('');
}

function initMenu() {
    $('#menu li ul').hide();
    var hUlBefore = $('.submenuContainer').height();
    var incHeight = 0;
    if (secondMenu != null && secondMenu != "l") {
        $('#' + secondMenu + " ul:first").show();
        $('#' + secondMenu + " .submenu .itemMenu").css('color', '#faa74a');
        if ($.browser.msie && parseInt(jQuery.browser.version) < 8) {
            incHeight = $('#' + secondMenu + " ul").height();
            $('.submenuContainerFill').height($('#' + secondMenu).height() + hUlBefore + 8);
            $('.submenuContainerBorder').height($('#' + secondMenu).height() + hUlBefore + 7);
        }
    }
    if (thirdMenu != null && thirdMenu != "l") $('#' + thirdMenu + " a").css('color', '#1599d3');
    $('#menu li .submenu').click(function() {
        var checkElement = $(this).next();
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) return false;
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            var nowHeight = checkElement.height() - incHeight;
            incHeight = checkElement.height();
            if ($.browser.msie && parseInt(jQuery.browser.version) < 8) {
                $('.submenuContainerFill').animate({'height' : "+=" + (nowHeight)}, 'fast');
                $('.submenuContainerBorder').animate({'height' : "+=" + (nowHeight)}, 'fast');
            }
            $('#menu ul:visible').slideUp('fast');
            checkElement.slideDown('fast');
            //            if ($('#menu ul').is(':visible')){
            //                $('#menu ul:visible').slideUp('normal', function () {
            //                checkElement.slideDown('normal');
            //                });
            //            } else {
            //                checkElement.slideDown('normal');
            //            }
            return false;
        }
        return true;
    });
}

var secondMenu;
var thirdMenu;

function viewMenu(second, third) {
    secondMenu = 'l' + second;
    thirdMenu = 'l' + third;
}