
// INCLUDE hoverIntent jQuery Plugin
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// WiCast MENU PLUGIN
(function($){
    $.fn.extend({
        menu: function( options ){
            var defaults = {
                name_menu: 'wicast_menu',
//                click_item_event: function(){
//                  //alert('cal definir una funcio "click_item_event"');
//                },
		disappear: true,
                active_title: false
            };
            var options = $.extend(defaults, options);
            return this.each(function() {
                // INIT PLUGIN
               var obj = $(this);
               var ingesco_menu = false;
               var activeClass = 'dropdown-active', showingDropdown, showingMenu, showingParent;
               $('.dropdown-menu .submenu_lateral').hide();

               // HIDE MENU FUNCTION
	            var hideMenu = function() {
                  ingesco_menu = false;
		            if(showingDropdown) {
			            showingDropdown.removeClass(activeClass);
			            showingMenu.hide();
                                    if( options.active_title ){
                                        showingDropdown = showingDropdown.addClass('dropdown-active');
                                    }
		            }
	            };

	            $('.dropdown').each(function() {
		            var dropdown = $(this);
		            var menu = dropdown.next('div.dropdown-menu'), parent = dropdown.parent();


                // SHOW MENU FUNCTION
	            var showMenu = function() {
                        ingesco_menu = true;
                        hideMenu();
                        showingDropdown = dropdown.addClass('dropdown-active');
                        showingMenu = menu.slideDown('fast');
                        showingParent = parent;
                    };
                    var toggleMenu = function() {
                        showMenu();
                    };

                  // BUTTON EVENT FUNCTIONS
		            dropdown.bind('click',function(e) {
			            if(e) e.stopPropagation();
			            if(e) e.preventDefault();
			            showMenu();
		            });
		            /* function to show menu when someone tabs to the box */
		            dropdown.bind('focus',function() {
			            hideMenu();
		            });


	            });

               // HIDE CLICK ANY PLACE
	            $(document).bind('click',function(e) {
		            if(showingParent) {
			            var parentElement = showingParent[0];
			            if(!$.contains(parentElement,e.target) || !parentElement == e.target) {
				            hideMenu();
			            }
		            }
	            });
	            // HOVER ITEMS
	            $('#'+obj.attr('id')+' .dropdown-menu a').hoverIntent( function(){
	            clearTimeout($(this).data('timeout'));
    			if( $(this).hasClass( 'lateral_section' ) ){

			        $('.dropdown-menu .submenu_lateral').slideUp('fast');
                    $('.lateral_section').removeClass( 'active' );

                    var w_aux = $(this).width();
                    w_aux = w_aux + 25;
                    w_aux = w_aux+"px";
                    $(this).next().css( 'left', w_aux );

			        if( ! $(this).hasClass( 'active' ) ){
    			        $(this).addClass( 'active' );
                        $(this).next().show();
		        }


    			}
		        else if( ! $(this).hasClass("subitem") ){
		            $('.dropdown-menu .submenu_lateral').slideUp('fast');
                    $('.lateral_section').removeClass( 'active' );
		        }
			}, function(){
    			if( $(this).hasClass( 'lateral_section' ) ){
//    	            $(this).css( "background", "red" );
	            }
			});

	            // CLICK ITEMS
	            $('#'+obj.attr('id')+' .dropdown-menu a').click( function(){
    			if( $(this).hasClass( 'lateral_section' ) ){





			}
			else if( $(this).hasClass( 'section' ) ){

			        $(this).next().slideToggle('fast');

			        if( $(this).hasClass( 'active' ) ){
        			        $(this).removeClass( 'active' );
			        }
			        else{
        			        $(this).addClass( 'active' );
			        }


			}
			else if( $(this).parent().hasClass( 'submenu' ) ){
                                options.click_item_event( $(this).attr('id') );
                                options.click_item_event( );
				if( options.disappear == true ){
				        $('.dropdown-menu .submenu').slideUp('fast');
				        $('.dropdown-menu .slidedown').slideUp('fast');
        		        $('.dropdown-menu .slidedown').prev().removeClass( 'active' );

        				hideMenu();
        			}
			}
			else{
			        $('.dropdown-menu a').removeClass( 'active' );
			        $('.dropdown-menu .submenu').slideUp('fast');
    		        $('.dropdown-menu .slidedown').slideUp('fast');
    		        $('.dropdown-menu .slidedown').prev().removeClass( 'active' );
//        			options.click_item_event( $(this).attr('id') );
//        			options.click_item_event( );
//				if( options.disappear == true ){
//
//        				hideMenu();
//        			}
			}

	            } );

            });
        }
    });
})(jQuery);

