/**
 * cbpHorizontalMenu.js v1.0.0
 * http://www.codrops.com
 *
 * Licensed under the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Copyright 2013, Codrops
 * http://www.codrops.com
 */
var cbpHorizontalMenu = (function() {

	var $listItems = $( '#cbp-hrmenu > ul > li' ),
		$menuItems = $listItems.children( 'a' ),
		$body = $( 'body' ),
		current = -1;

	function init() {
		$menuItems.on( 'click', open );
		$listItems.on( 'click', function( event ) { event.stopPropagation(); } );
	}

	function open( event ) {
		 // If the current menu item is open, close it.
		if( current !== -1 ) {
			$listItems.eq( current ).removeClass( 'cbp-hropen' );
		}
		  // Get the current menu item that the user is hovering over.
		var $item = $( event.currentTarget ).parent( 'li' ),
			idx = $item.index();
		 // if oher menu items opened then close 
		if( current === idx ) {
			$item.removeClass( 'cbp-hropen' );
			$('.close-icon').css('display','none');
			current = -1;
		}
		else {
		// open the current menu item.
			$item.addClass( 'cbp-hropen' );
			$('.close-icon').css('display','block');	
			current = idx;
			$body.off( 'click' ).on( 'click', close );
			$('.close-icon').on( 'click', close );
		}

		return false;

	}

	function close( event ) {
		$listItems.eq( current ).removeClass( 'cbp-hropen' );
		$('.close-icon').css('display','none');
		current = -1;
	}

	return { init : init };

})();
