/*
 *
 *   jQuery module Drop Down Menu
 *   --------------------------------------------- 
 *   by: Jakub Rybar 
 *   requires jquery.hoverIntent.js (jquery module)
 *
 */

(function($){ 
                
	// plugin itself
	$.fn.slideMenu = function(options) {
    
	    // extend options
	    options = $.extend({}, $.fn.slideMenu.defaultOptions, options);
	    
	    // private functions
	    var timeout = 100, closetimer = 0, slideMenuitem = 0, 
	    openSubMenu = function(el) {
			if (options.close_other_submenus_on_open) {        
				$.each($(el).parent().find('>li'), function(){
					if ($(this).find('>ul').size() > 0) {
						//alert($(this).find("a").html());
						closeSubMenu($(this));
					}
				});			
			}
			//alert('asdsad'); 
			$(el).find('>ul').slideDown(); 
			//$(el).children('a').children('span')[0].innerHTML = '&and;' ;
		}, 
		closeSubMenu = function(el) {
			$(el).find('>ul').slideUp();
			//$(el).children('a').children('span')[0].innerHTML = '&or;' ;
		};
	
		// plugin functionality binded to each matched element
		this.each(function() {         
			var element = $(this);
			// slide effect on click
/* 			element.find('li > a').click(function() { */
			element.find('li > div.left > a').click(function() {
				var parent = $(this).parent().parent();
				var ul = parent.find('>ul');
/*
				console.log(parent);
				console.log(ul);
*/
				if (!options.is_active_main_link_if_has_childs) {					
					if (ul.size() > 0 && ul.css('display') == 'none') {
						openSubMenu(parent);
						return false;
					}
					else if (ul.size() > 0 && ul.css('display') == 'block') {
						$(window.location).attr('href', $(this).attr('href'));
						closeSubMenu(parent);
						return false;
					}
				}
				if (options.use_first_child_link) {
					if (ul.size() > 0) {
						var new_href = parent.find('>ul > li:first > a').attr('href');
						window.location = new_href;
					}
				}
			});
			
		});
	    return this;
	}
  
	//Public Function
	$.fn.slideMenu.greet = function(name) {
		alert('Hello, ' + name + ', welcome to Script Junkies!');
	};
	$.fn.slideMenu.openCurrentSubMenu = function(menuElement) {
		//	alert('open current');
		$(menuElement).find('a.active').parents('ul').slideDown();
	};
	
	// default options
	$.fn.slideMenu.defaultOptions = {
		is_active_main_link_if_has_childs : true,
		use_first_child_link : false,
		close_other_submenus_on_open: true
	}
})(jQuery);
