var EUROFARM = {

    url: "www.eurofarm.cz/",
    
    init: function () {
        EUROFARM.print();
        EUROFARM.emulateHover($("#navlist li"));
        EUROFARM.navigation();
        EUROFARM.selectAutoSubmit();
        EUROFARM.dictionary();
        EUROFARM.links();
    },
    
    emulateHover: function (items) {
        items.bind("mouseover", function () {
            $(this).addClass('hover');
        });
        
        items.bind("mouseout", function () {
            $(this).removeClass('hover');
        });
    },
    
    navigation: function () {
        $("#navlist li:first").attr("id", "first-tab");
        $("#navlist li:last").attr("id", "last-tab");
        $("#navlist li a.active").parent().addClass('current');
        $("#navlist a").each(function (i) {
            if ($(this).attr('class').indexOf('active') !== -1 ) {
                $(this).parent().addClass('current');    
            }
        });
        $("#subnavlist a").each(function (i) {
            if ($(this).attr('class').indexOf('active') !== -1 ) {
                $(this).addClass('active');    
            }
        });
    },

    print: function () {
        $("#print").bind("click", function () {
            window.print();
            return false;
        });
    },
    
    selectAutoSubmit: function () {
        $("#right-top select").bind("change", function () {
            $("#right-top form")[0].submit();
        });
    },
    
    dictionary: function () {
        $(".definition").each(function (i) {
            var pos = Drupal.absolutePosition(this);
            var left = pos.x - 71;
            var top = pos.y - 84;
            var bubble = $('<div class="bubble"><a href="' + jQuery.trim($(this).attr("href")) + '">' + jQuery.trim($(this).attr("title")) + '<br /><span>Podrobné vysvětlení &raquo;</span></a></div>');

            bubble.css("left", left + 'px');
            bubble.css("top", top + 'px');
            
            $('body').append(bubble);
            
            $(this).bind("mouseover", function () {
                bubble.css("display", "block");
            });
            
            bubble.hover (
                function () {},
                function () {
                    bubble.css("display", "none");
                }
            )
        });
    },
    
    links: function () {
         $("a[@href^=http]:not([@href*='" + EUROFARM.url  + "'])").addClass('link-out');
         $('a[@href^="mailto:"]').addClass('mail');
         $("a[@href*=#]").not("[@href*=header]").each(function (i) {
             var href = $(this).get(0).getAttribute('href', 2);
             if (href.indexOf('#') === 0) {
                 $(this).addClass('link-down');
             }
         });
          
    }
}

$(document).ready(function () {
  EUROFARM.init();
});
