$(document).ready(function(){	
    $('.coupon_out').fadeTo('slow',0.9).draggable();
    $('.c_top').click( function() {
        $('.coupon_out').hide('slow');
    });
	
	
	// Tag the current top level anchor as "active" based on URL
		
	function getUrlVars() {
	    var vars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++) {
	        hash = hashes[i].split('=');
	        vars.push(hash[0]);
	        vars[hash[0]] = hash[1];
	    }
	    return vars;
	}
	
	//Simplify finding undefined variables
	var undef;	
	
	var path = location.pathname.substring(1);
	var category = getUrlVars()["cPath"];
	var products_id = getUrlVars()["products_id"];
	
	// Swap out nav based on URL variables in store
	if (category != undef  && products_id == undef) {
		switch (category) {
			case '1' :	path='mens/'; break;
			case '2' :	path='womens/'; break;
			case '3' :	path='unisex/'; break;
		}
	} else if (category != undef  && products_id != undef) {
		switch (products_id) {
			case '1' :	path='mens/skins.php'; break;
			case '2' :	path='mens/briefs.php'; break;
			
			case '4' :	path='unisex/skins.php'; break;
			case '6' :	path='unisex/sport-tee.php'; break;
			
			case '7' :	path='womens/skins.php'; break;
			case '8' :	path='womens/briefs.php'; break;
			case '9' :	path='womens/sport-briefs.php'; break;
		}
	}
	
	// Add active to current link and to the main link heading
	$('#nav a[href="/' + path + '"]').addClass('active').parents('li').find('.top').addClass('active');			   
	
	 
	
	/**
	 * Find if the next or previous top level is active, and set the current items background.
	 * This is used to set background images accordingly without resorting to PNG's
	 **/
	
	var active = -10;
	
	// Find the active anchor
	$('#nav a.top').each(function (i) {
		if($(this).hasClass('active')) { active = i; $(this).next('ul').css('visibility', 'visible');}
	});
	
	// assign next, prev, or current
	$('#nav a.top').each(function (i) {
		if(i == active+1) {
			$(this).attr('bkg_type', 'prev');
		} else if(i == active-1) {
			$(this).attr('bkg_type', 'next');
		} else {
			$(this).attr('bkg_type', 'current');		
		}
	});


	// Dropdown Menus
	var timeout    = 500;
	var closetimer = 0;
	var ddmenuitem = 0;

	function jsddm_open() {
		jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $(this).find('ul').css('visibility', 'visible');
		$current = $(this).find('.top');
		$current.addClass($current.attr('bkg_type'));
	}

	function jsddm_close() {
		if(ddmenuitem && !ddmenuitem.prev().hasClass('active')) {
			ddmenuitem.css('visibility', 'hidden');
			$('#nav a').removeClass('next prev current');
		}
	}
	
	function jsddm_timer() {
		closetimer = window.setTimeout(jsddm_close, timeout);
	}
	
	function jsddm_canceltimer() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	
	$('#nav > li').bind('mouseover', jsddm_open);
	$('#nav > li').bind('mouseout',  jsddm_timer);
	
	document.onclick = jsddm_close;

});

