$(document).ready(function() {

    $("#mainMenu li.activePage").addClass("currentPage");
    $("#mainMenu > li").hover(
		function() {
		    if ($(this).children().is("ul")) {
		        $("#mainMenu li.activePage").removeClass("activePage").addClass("hideSubmenu");
		        $(this).addClass("activePage");
		    }
		}, function() {
		    if ($(this).children().is("ul")) {
		        $(this).removeClass("activePage");
		        $("#mainMenu li.hideSubmenu").addClass("activePage").removeClass("hideSubmenu");
		        $("#mainMenu li.currentPage").addClass("activePage");
		    }
		})



    $('.newsletterSubscription input.newsletterSubscriptionEmail, #ankiro_input_right').focus(function() {
        $(this).animate({ width: "185px" }, 500);

    });

    $('.newsletterSubscription input.newsletterSubscriptionEmail, #ankiro_input_right').blur(function() {
        $(this).animate({ width: "105px" }, 500);

    });



    $("#priorityNav > li").hover(
		function() {
		    $(this).addClass("hover");
		}, function() {
		    $(this).removeClass("hover");
		})


    var priorityNr = 0;
    $(".prioritySlide:gt(0)").hide();

    $("ul#priorityNav li").click(function() {

        priorityNr = $("ul#priorityNav li").index(this);
        $("#priorityNav li").removeClass("currentItem").eq(priorityNr).addClass("currentItem");
        $("#prioritySlides .prioritySlide").fadeOut("slow").eq(priorityNr).fadeIn("slow");

    })



    $("table tr:odd").addClass("lightRow");


    $("#priorityNav > li").hover(
		function() {
		    $(this).addClass("hover");
		}, function() {
		    $(this).removeClass("hover");
		})





    /* BRANCH - EQUAL HIGHT */

    var branchesHeight = 50;
    var branchesNr = $(".branches").length;


    if (branchesHeight < $(this).height()) {
        $(".branches").each(function(i) {
            if (branchesHeight < $(this).height()) {
                branchesHeight = $(this).height()
            }
        });

        $(".branches").each(function(i) {
            $(this).children(".branchesBody").css({ height: branchesHeight + "px" })

        });
    }


    /* THEME EQUAL HEIGHT */



    var themeHeight = 50;
    var themeNr = $(".branches").length;


    if (branchesHeight < $(this).height()) {
        $("#themePage .brancheOverview").each(function(i) {
            if (branchesHeight < $(this).height()) {
                branchesHeight = $(this).height()
            }
        });

        $("#themePage .brancheOverview").each(function(i) {
            $(this).children(".brancheOverviewContent").css({ height: branchesHeight - 70 + "px" })

        });
    }




    /* STYLE SWITCHER */

    $('.styleswitch').click(function() {
        switchStylestyle(this.getAttribute("rel"));
        return false;
    });

    var c = readCookie('style');
    if (c) switchStylestyle(c);





    function switchStylestyle(styleName) {
        $('link[@rel*=style][@title]').each(function(i) {
            this.disabled = true;
            if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
    }

    // cookie functions http://www.quirksmode.org/js/cookies.html
    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    function eraseCookie(name) {
        createCookie(name, "", -1);
    }


    /* FrontPage alert cookie */

    $('#closeAlert').click(function() {
        /*Get cookie*/
        var alertCookie = $.cookie('alertCookie');

        /*Get hidden value */
        var alertResetHidden = $("#alertResetHidden");

        if (alertResetHidden != null) {
            $.cookie('alertCookie', '2-' + alertResetHidden.val(), { expires: 365 });
            $("#alert").hide();
        }

        return false;
    });


    $("#alert").each(function(i) {

        /*Get cookie*/
        var alertCookie = $.cookie('alertCookie');

        /*Get hidden value */
        var alertResetHidden = $("#alertResetHidden");

        if (alertResetHidden != null) {

            /*If cookie exists*/
            if (alertCookie != null) {

                /*Cookie value format: "number of appearences"-"reset iso date" */
                var a = alertCookie.split('-');
                if (a.length == 2) {

                    /*If cookie reset value is older then current reset date, we'll reset cookie and show alert*/
                    if (a[1] < alertResetHidden.val()) {
                        $.cookie('alertCookie', '1-' + alertResetHidden.val(), { expires: 365 });
                        $(this).show();
                    }
                    else {
                        /*If alert was shown only once will show it again and increese number of apearences*/
                        if (a[0] == '1') {
                            $.cookie('alertCookie', '2-' + alertResetHidden.val(), { expires: 365 });
                            $(this).show();
                        }
                    }
                }
            }
            /*If cookie doesnt exists we create it and show alert*/
            else {
                $.cookie('alertCookie', '1-' + alertResetHidden.val(), { expires: 365 });
                $(this).show();
            }
        }
    });


// Fix forms that have absolute position
var maxTop = 0;
var $fel = null;
var $target = $('[id$="form_current"]');

if( $target.length > 0 ) {
     // Find the 'lowest' element and adjust height
     var bla = $target.find('div > *');

     $target.find('div > *').each(function () {
          var p = $(this).position();

          if (p.top > maxTop) {
               maxTop = p.top;
               $fel = $(this);
          }
     }).end().height(maxTop + $fel.height() + 10);
}

});
