/*!
 * Drop Menu
 *
 * For drop down effect on the primary Nav for Marketplace and FishFinder.
*/

$(document).ready(function($) {
    
    var animating = false; // set animation status
    
	var MarketplaceExpanded = false;
	
	var FishFinderExpanded = false;

    // Primary Nav Dropdown Functions

    // when Marketplace is clicked
    $("#nav-primary-Marketplace").mouseenter(function(e) {
    	e.preventDefault();
		MarketplaceExpanded = true;
		if(FishFinderExpanded == false){
			$('#nav-primary-Marketplace').stopTime("closeMarketplace");
			$("#nav-primary-FishFinder").removeClass("current");  // remove "current" class from FishFinder
	    	$("#nav-drop-FishFinder").slideUp(500);
			$("#nav-primary-Marketplace").addClass("current");  // add "current" class to Marketplace
			$("#nav-drop-Marketplace").slideDown(500, function() {  // slide down and end animation
				animating = false;
			});
		}else{
			$('#nav-primary-Marketplace').oneTime(300, "delayOpenMarketplace", function() {
				$('#nav-primary-Marketplace').stopTime("closeMarketplace");
				$("#nav-primary-FishFinder").removeClass("current");  // remove "current" class from FishFinder
		    	$("#nav-drop-FishFinder").slideUp(500);
				$("#nav-primary-Marketplace").addClass("current");  // add "current" class to Marketplace
				$("#nav-drop-Marketplace").slideDown(500, function() {  // slide down and end animation
					animating = false;
				});
			});
		}
		
    });
    $("#nav-primary-Marketplace").mouseleave(function(e) {
		$('#nav-primary-Marketplace').stopTime("delayOpenMarketplace");
		$('#nav-primary-Marketplace').oneTime(600, "closeMarketplace", function() {
			animating = true;
			$("#nav-drop-Marketplace").slideUp(500, function() {
				animating = false;
				$("#nav-primary-Marketplace").removeClass("current");
			});
		});
    });
	
	$('#nav-drop-Marketplace').mouseenter(function(e){
		e.preventDefault();
		$('#nav-primary-Marketplace').stopTime("closeMarketplace");
	})
	
	$('#nav-drop-Marketplace').mouseleave(function(e){
		e.preventDefault();
		$('#nav-primary-Marketplace').oneTime(600, "closeMarketplace", function() {
			animating = true;
			$("#nav-drop-Marketplace").slideUp(500, function() {
				animating = false;
				$("#nav-primary-Marketplace").removeClass("current");
			});
		});
	})
	
    $("#nav-primary-FishFinder").mouseenter(function(e) {
    	e.preventDefault();
		FishFinderExpanded = true;
		if(MarketplaceExpanded == false){
			$('#nav-primary-FishFinder').stopTime("closeFishFinder");
	    	$("#nav-primary-Marketplace").removeClass("current");
	    	$("#nav-drop-Marketplace").slideUp(500)
			$("#nav-primary-FishFinder").addClass("current");
	    	$("#nav-drop-FishFinder").slideDown(500, function() {
	    		animating = false;
	    	});
		}else{
			$('#nav-primary-FishFinder').oneTime(300, "delayOpenFishFinder", function() {
				$('#nav-primary-FishFinder').stopTime("closeFishFinder");
		    	$("#nav-primary-Marketplace").removeClass("current");
		    	$("#nav-drop-Marketplace").slideUp(500)
				$("#nav-primary-FishFinder").addClass("current");
		    	$("#nav-drop-FishFinder").slideDown(500, function() {
		    		animating = false;
		    	});
			});
		}
    });	
	
	$("#nav-primary-FishFinder").mouseleave(function(e) {
		$('#nav-primary-FishFinder').stopTime("delayOpenFishFinder");
		$('#nav-primary-FishFinder').oneTime(600, "closeFishFinder", function() {
			animating = true;
			$("#nav-drop-FishFinder").slideUp(500, function() {
				animating = false;
				$("#nav-primary-FishFinder").removeClass("current");
			});
		});
    });
	
	$('#nav-drop-FishFinder').mouseenter(function(e){
		e.preventDefault();
		$('#nav-primary-FishFinder').stopTime("closeFishFinder");
	})
	
	$('#nav-drop-FishFinder').mouseleave(function(e){
		e.preventDefault();
		$('#nav-primary-FishFinder').oneTime(600, "closeFishFinder", function() {
			animating = true;
			$("#nav-drop-FishFinder").slideUp(500, function() {
				animating = false;
				$("#nav-primary-FishFinder").removeClass("current");
			});
		});
	})
    

    // Category Changes
    
    // when sub category clicked
    $(".nav-drop-sub-nav a").click(function(e) {
    	e.preventDefault();
    	
    	//make sure animation not taking place
    	if(!animating) {
    		
    		//make sure it's not already the current category
    		if (!($(this).parent().hasClass("current"))) {
    			
    			//remove current category class from old category
    			$(this).parent().siblings(".current").removeClass("current");
    			
    			//add current category class to new category
    			$(this).parent().addClass("current");
    			
    			//parse cateogry ID
    			var myCatString = $(this).parent().attr("id");
    			var splitPoint = myCatString.indexOf("-") + 1;
    			var myCatId = "#slides-" + myCatString.slice(splitPoint);
				
				//hide categories then display new category based on ID of element clicked					
    			$(myCatId).siblings(".category-slides").css("display", "none");
    			$(myCatId).css("display", "block");
				
    		}
    	}
    });
    

	// enable the first sub-category slide container of each category
	$("#nav-drop-FishFinder").children('.nav-drop-frame').children('.category-slides').css("display", "none");
	$("#nav-drop-FishFinder").children('.nav-drop-frame').children('.category-slides:first').css("display", "block");
	$("#nav-drop-Marketplace").children('.nav-drop-frame').children('.category-slides').css("display", "none");
	$("#nav-drop-Marketplace").children('.nav-drop-frame').children('.category-slides:first').css("display", "block");
	
	$("#nav-drop-FishFinder").children('.nav-drop-frame').children('.category-slides').each(function() {
		if($(this).find('.slide-container-item').size() > 5){
			$(this).children(".nav-drop-arrow-r").removeClass("disabled");
		}
	})
	

    // Dropdown Slide Functions 
    
    // right arrow
    $(".nav-drop-arrow-r").click(function() {
    	var frameWidth = $(this).siblings(".nav-drop-slide-frame").css('width');
    	var numItems = $(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").children('.slide-container-item').length;
    	var itemWidth = $(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").children('.slide-container-item').css('width');
    	
    	/*
    	console.log($(this).siblings(".nav-drop-slide-frame"));
   		console.log($(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").children('.slide-container-item'));
    	console.log(parseInt(frameWidth));
    	console.log(parseInt(itemWidth));
    	console.log(numItems);
    	console.log(parseInt(frameWidth/numItems));
		*/
		
		// If there are less than the max number of items in the frame then don't scroll
		if ((numItems * parseInt(itemWidth)) <= parseInt(frameWidth))
			return;

    	// make sure not disabled and another animation isn't happening
    	if (!($(this).hasClass("disabled")) && !animating) {
    		
    		// get current margin
    		var myMargin = $(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").css("marginLeft");
    		
    		// calculate new margin
    		var myShift = parseInt(myMargin.split("p")[0]) - 192;
    		
    		// calculate end of slides
    		var maxShift = (($(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").children(".slide-container-item").size()) * (-192)) + 768;
    		
		}
    	// if not at end of slides
   		if (myShift != maxShift) {
			
   			// start animation
   			animating = true;
   			
   			// remove "disabled" class from other arrow			
   			$(this).siblings(".nav-drop-arrow-l").removeClass("disabled");
   			
   			// animate to new margin
   			$(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").animate({marginLeft: myShift.toString() + "px"}, 700, function() {
   				// when complete, check to see if end of slides and if so, disable arrow
				if (myShift - 192 == maxShift) $(this).parents(".nav-drop-slide-frame").siblings(".nav-drop-arrow-r").addClass("disabled");
				// end animation
				animating = false;
			});
  		}
    });
    
    
    // left arrow
    $(".nav-drop-arrow-l").click(function() {
    	if (!($(this).hasClass("disabled")) && !animating) {
    		var myMargin = $(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").css("marginLeft");
    		var myShift = parseInt(myMargin.split("p")[0]) + 192;
    		
    		if (myShift != 192) 
    			animating = true;
    			$(this).siblings(".nav-drop-arrow-r").removeClass("disabled");
    			$(this).siblings(".nav-drop-slide-frame").children(".nav-drop-slide-container").animate({marginLeft: myShift.toString() + "px"}, 700, function() {
    		if (myShift == 0) $(this).parents(".nav-drop-slide-frame").siblings(".nav-drop-arrow-l").addClass("disabled");
    	  	animating = false;
    	  });
    	}
    });
    
    
    // Third level category mouse enter / leave handling
    // Shows / Hides category description
    $('.sub_sub_cat').mouseenter(function() {
		var myId = $(this).attr("id");
		var id = myId.split("_");
		id = id[id.length-1];
		if($('#img_'+id).hasClass('drop-slide-image-FishFinder')){
			$('#img_'+id).animate({top: 24}, 150);
		}else if($('#img_'+id).hasClass('drop-slide-image-Marketplace')){
			$('#img_'+id).animate({top: -20}, 150);
		}
		
		//$('#hint_'+id).parent().children('p').animate({opacity: 0}, 150);
		$('#hint_'+id).parent().children('p').css("display", "none");
		if($('#hint_'+id).hasClass('category-hint-Marketplace')){
			$('#hint_'+id).animate({top: 150}, 150);
		}else if($('#hint_'+id).hasClass('category-hint-FishFinder')){
			$('#hint_'+id).animate({top: 120}, 150);
		}
    });

    $('.sub_sub_cat').mouseleave(function() {
		var myId = $(this).attr("id");
		var id = myId.split("_");
		id = id[id.length-1];
		if($('#img_'+id).hasClass('drop-slide-image-FishFinder')){
			$('#img_'+id).animate({top: 42}, 150);
		}else if($('#img_'+id).hasClass('drop-slide-image-Marketplace')){
			$('#img_'+id).animate({top: 0}, 150);
		}
		//$('#hint_'+id).parent().children('p').animate({opacity: 1}, 150);
		$('#hint_'+id).parent().children('p').css("display", "block");
		if($('#hint_'+id).hasClass('category-hint-Marketplace')){
			$('#hint_'+id).animate({top: 394}, 150);
		}else if($('#hint_'+id).hasClass('category-hint-FishFinder')){
			$('#hint_'+id).animate({top: 270}, 150);
		}
    });
});


