﻿var oly = {};
    oly.navigation = {
        addEvents: function($el) {
        
            $el.find(".hwrap").bind("click", oly.navigation.toggle);
        
            $el.find(".scroll_up").bind("mouseover", {el: $el} ,oly.navigation.scrollUp);
            $el.find(".scroll_up").bind("mouseout", {el: $el} ,oly.navigation.scrollStop);
            $el.find(".scroll_up").bind("click", function(e) { e.preventDefault(); e.stopPropagation(); });
            $el.find(".scroll_down").bind("mouseover", {el: $el} ,oly.navigation.scrollDown);
            $el.find(".scroll_down").bind("click", function(e) { e.preventDefault(); e.stopPropagation(); });
            $el.find(".scroll_down").bind("mouseout", {el: $el} ,oly.navigation.scrollStop);
        
        },
        scroll: function(el, ul, dir) {
            var $el = el;
            var $ul = ul;
            var direction = dir;
            var lineheight = $ul.find("li").height();
            
            // set interval
            var interval = window.setInterval(function() {
                
                $ul.parent().find(".scroll_down").show();
                $ul.parent().find(".scroll_up").show();
                                
                if ( direction == "down" ) {
                    if ( (-($ul.height() - 165)) <    $ul.attr("offsetTop")) {                         
                        $ul.animate({
                            top: $ul.attr("offsetTop") - lineheight +"px"
                        },120);
                    } else {
                        $el.hide();
                    }
                    
                } else {
                    
                    if ( $ul.attr("offsetTop") < 10 ) {
                        $ul.animate({
                        top: $ul.attr("offsetTop") + lineheight +"px"
                        },120);
                    } else {
                        $el.hide();
                    }
                }
                
                    
            }, 200);
            
            $el.attr("interval",interval);
        },
        scrollStop: function(e) {
            var interval = jQuery(this).attr("interval");                
            window.clearInterval(interval);
        
        },
        
        scrollUp: function() {
            var $ul = jQuery(this).parent().find("ul");
            oly.navigation.scroll(jQuery(this), $ul, "up");
        },
        
        scrollDown: function(e) {
            var $ul = jQuery(this).parent().find("ul");
            oly.navigation.scroll(jQuery(this), $ul, "down");
        },
        open: function($el) {
            
            // close open element
            jQuery(".subnav div.open").removeClass("open");
            
            // show sublist                    
            $el.parent().addClass("open");
            jQuery(window).one("click", oly.navigation.close);
        
        },
        
        close: function(e) {
            jQuery(".subnav div.open").removeClass("open");
        },
        
        toggle: function(e) {
            e.preventDefault();
            e.stopPropagation();
            
            jQuery(this).parent().hasClass("open") ? oly.navigation.close(jQuery(this)) : oly.navigation.open(jQuery(this));
            
        },
        
        initialize: function() {
            
            var $el = jQuery(".subnav div").addClass("open");
            
            $el.find("ul").wrap('<div class="wrapper"></div>');
            $el.find("ul").wrap('<div class="scroller"></div>');
            
            $el.each(function() {
                jQuery(this).find(".scroller , .wrapper").css({
                    height: jQuery(this).find("ul").height() > 165 ? 165 : jQuery(this).find("ul").height() + "px",
                    width: jQuery(this).find("ul").width() < jQuery(this).find("h2").width() + 65  ? jQuery(this).find("h2").width() + 63 : jQuery(this).find("ul").width() + "px"
                });
                if ( jQuery(this).find("ul").width() < jQuery(this).find("h2").width() + 65) {
                    jQuery(this).find("ul").css("width", jQuery(this).find("h2").width() + 63+"px");
                }
                
                if ( jQuery(this).find("ul").height() > 165 ) {
                    
                    // append scroll buttons
                    jQuery(this).find(".scroller").append('<a class="scroll_up" href="#"></a>');
                    jQuery(this).find(".scroller").append('<a class="scroll_down" href="#"></a>');
                
                    jQuery(this).find("ul").css("top","10px");
                
                }
                
            });
            
                                
            $el.removeClass("open");
            
            $el.find("h2").wrap('<a href="#" class="hwrap"></a>');
            
            oly.navigation.addEvents($el);
            
        }
    
    };
    
    jQuery(oly.navigation.initialize);
