

(function($) {

jQuery.fn.sgVertMenu = function(settings) {

	// Settings
	settings = jQuery.extend({
		menuHeight: 'auto',
		menuWidth: 'auto',
		onInit: function() {},
		onShow: function() {},
		onHide: function() {},
		activeClass: 'sgVertMenuActive',
		childClass: 'sgVertMenuChild',
		menuId: 'sgVertMenu',
		durationShow: 400,
		durationHide: 100,
		timerDuration: 500,
		offsetTop: 0,
		offsetSide: 0
	}, settings);

	var sgMenuObj = this;

	_initialize();

	return sgMenuObj.each(function() {});

	function _initialize() {
		
		// Hide submenu items
		sgMenuObj.find("ul").css({display:'none', visibility:'visible'});
		
		// Loop over all parent ul items
		sgMenuObj.find("ul").parent().each(function(i) {
												
			//console.log($(this));	
			//console.log($(this).parent().children("ul:eq(0)"));
												
			// Assign child visual aid
			$(this).children("a:eq(0)").addClass(settings.childClass);
			
			// Hover functions
			$(this).hover(
				function() { _show($(this)); },
				function() { _timeout($(this)); }
			);
		});


		settings.onInit.call();
	}
	
	function _show(el) {
		
		clearTimeout(settings.sgTimer);
		
		var _offsets= { left:el.width(), top:el.offset().top }
		
		el.siblings('li').each(function() {
			_hide($(this))
		});
		
		if (el.queue().length<=1) {
			el.children("a:eq(0)").addClass(settings.activeClass);
			el.children("ul:eq(0)").css('top','0').css('left',_offsets.left + 'px').slideDown(settings.durationShow);
			settings.onShow.call(el);
		}
	}
	
	function _hide(el) {
		el.children("a:eq(0)").removeClass(settings.activeClass);
		el.children("ul:eq(0)").slideUp(settings.durationHide);
		settings.onHide.call(el);	
	}
	
	function _timeout(el) {
		clearTimeout(settings.sgTimer);
		settings.sgTimer = setTimeout(function() { _hide(el); },settings.timerDuration);	
	}

	return jQuery;

};

})(jQuery);



// Custom load func to show menu boat image
function loadMenuPic() {
	// Add the menu pic div to the body
	$('body').append('<div id="menuPic"></div>');
	
	// Menu Image functionality
	$('li.sgMenuImage').each(function() {
		
		$(this).hover(
			function() {
				
				var sgMenuImagePath = 'images/menu/' + $(this).attr('rel') + '.jpg';
				var sgMenuImageLoading = 'images/menu/loading.gif';
				var posLeft = Math.round($(this).offset().left + $(this).outerWidth() + 2);
				
				
				if (!$.browser.msie) {
					//console.log('not ie');
					var defStr = '<div><img src="' + sgMenuImageLoading +'" border="0" style="margin-top:60;"><span>Loading...</span></div>';
					var ni = new Image();
					$(ni).load(function() { $('#menuPic div').html('<img src="' + sgMenuImagePath +'" border="0">'); });
					$(ni).attr('src',sgMenuImagePath);
				} else {
					var defStr = '<img src="' + sgMenuImagePath +'" border="0">'
				}
				
				
				
				$('#menuPic').html(defStr).show().css('left',posLeft + 'px');
				
			},
			function() {
				$('#menuPic').hide();
			}
		);
		
		
	});	
}

$(window).load(function() {
	$('#sgMainNav').sgVertMenu({onInit:function() { loadMenuPic(); } });
});