/*
 * copain.nav.js
 * Navigation controller for copainwines.com
 * Author: Eric Arenson, Jumping Fenced Design, SF
 * URI: http://jfdsf.com/
 * @version 2.2
 */

$(document).ready(function(){
	
	// HANDLE THE SUBNAVS
	var item = null;
	var subNavStatus = 'open';
	var currNavItem = 'purchase';
	var prevNavItem = null;

	// Pick something
	$('p.nav-group-title a').click(function(){
		// Get the ID of the selected item
		item = $(this).attr('id');

		// Check to see if a menu is open
		if(subNavStatus != "open"){
			// Check to see if the item selected is different from the current item
			if(item!=currNavItem){
				// If yes, open it
				openSubMenu(item);
			} else {
				// If no, say so.
				//console.log('same');
			}
			
		// If it's already open...
		} else {

			// Check to see if the selected item is already open...
			if(item!=currNavItem){
				closeSubMenu(item);
			} else {
				//console.log(currNavItem+" already open");
			}
		}
	});

	function openSubMenu(item){
		currNavItem = item;
		if(subNavStatus!="open"){
			subNavStatus = "opening";
			$('ul#'+item).slideToggle(
				function(){
					subNavStatus = "open";
				}
			);
		}
	}

	function closeSubMenu(item){
		// Set the previous item to close...
		prevNavItem = currNavItem;

		// Set the current item to open...
		currNavItem = item;

		// Set the status of the animation...
		subNavStatus = "closing";

		//console.log('Closing Subnav: '+currNavItem);
		$('ul#'+prevNavItem).slideToggle(
			function(){
				//console.log("Closed "+item);
				openSubMenu(currNavItem);
			}
		);
	}
});
