/**
 * Panel Navigation 1.0.2 - jQuery plugin
 * @author Peter Matal (fatal@markiza.sk)
 * @requires jQuery library
**/
jQuery.fn.panelNavigation = function(options){
	
	var settings = jQuery.extend({
		debug: false,
		container: '#panel-navigation',
		panel: '#panel-navigation #panel',
		active: 'active',
		tMoveOverNavigationBar: 300,
		tMoveOutNavigationBar: 500,
		panelHideSpeed: 200
	}, options); 
	var panelTimer = null;
	var flags = {
		mInNav: false,
		mInPanel: false
	}
	
	jQuery(settings.container).find('.items li.child')
		.bind('updateNavigationPanel', updateNavigationPanel)
		.bind('hideNavigationPanel', hideNavigationPanel)
		.hover(handleHoverIn, handleHoverOut);
	
	jQuery(settings.panel)
		.bind('hideNavigationPanel', hideNavigationPanel)
		.hover(handlePanelHoverIn, handlePanelHoverOut);
	
	/** handle main menu mouse in event **/
	function handleHoverIn(e){
		window.clearTimeout(panelTimer);
		flags.mInNav = true;
		jQuery(e.target).trigger("updateNavigationPanel");
	}
	
	/** handle content panel mouse in event**/
	function handlePanelHoverIn(e){
		window.clearTimeout(panelTimer);
		flags.mInPanel = true;
	}
	
	/** handle main menu mouse out event**/
	function handleHoverOut(e){
		flags.mInNav = false;
		jQuery(e.target).trigger("hideNavigationPanel");
	}
	
	/** handle content panel mouse out event**/
	function handlePanelHoverOut(e){
		flags.mInPanel = false;
		jQuery(e.target).trigger("hideNavigationPanel");
	}
	
	/** update panel navigation content **/
	function updateNavigationPanel(e){
		var delay = settings.tMoveOverNavigationBar;
		
		var mItem = jQuery(this);
		var pName = mItem[0].id.replace('item', settings.panel);
		if (jQuery(pName).length > 0){
			window.clearTimeout(panelTimer);
			panelTimer = window.setTimeout(function(){
				jQuery(settings.panel).html(jQuery(pName).html()).css("display", "block");
				jQuery(settings.panel).removeClass().addClass(jQuery(pName).attr('id'));
				jQuery(settings.container).find('.items li.child').removeClass(settings.active);
				mItem.addClass(settings.active);
			}, delay);
		}else{
			jQuery(e.target).trigger("hideNavigationPanel");
		}
	}
	
	/** hide subnavigation panel **/
	function hideNavigationPanel(e){
		
		var delay = settings.tMoveOutNavigationBar;
		
		window.clearTimeout(panelTimer);
		panelTimer = window.setTimeout(function(){
			if (!flags.mInNav || !flags.mInPanel) {
				jQuery(settings.panel).fadeOut(settings.panelHideSpeed, function(){
					jQuery(settings.container).find('.items li.child').removeClass(settings.active);
				});
			}
		}, delay);
		
	}
	
	/** console debug **/
	function debug(o){
		if(typeof(console) !== 'undefined' && console != null && settings.debug)
			console.log(o);
	} 
} 

jQuery(document).ready(function() {
	jQuery.fn.panelNavigation({debug: true});
});
