$(document).ready(function() {
	//jQuery goes here
    
    $('.recipe').mouseenter(function(){
    	$(this).find('.details-behind').show();
    	$(this).find('.details').show();
    	$(this).find('.namecard-behind').hide();
    	$(this).find('.namecard').hide();
    }).mouseleave(function(){
    	$(this).find('.details-behind').hide();
    	$(this).find('.details').hide();
    	$(this).find('.namecard-behind').show();
    	$(this).find('.namecard').show();
    });
    
    $('.submenu-arrow').click(function(){
    	$(this).parent().find('.submenu').toggle();
    	if($(this).parent().hasClass('open')){
    		//Switch to closed
    		$(this).removeClass('arrow-up').addClass('arrow-down');
    		$(this).parent().removeClass('open');
    	}else{
    		//Switched to open
    		$(this).removeClass('arrow-down').addClass('arrow-up');
    		$(this).parent().addClass('open');
    	}
    });
    
    //Targets "Remove" link in each ingredient row
    $('.ingredient-list li a').live("click", function(){
    	var row_to_remove = $(this).parent();
    	remove_ingredient(row_to_remove);
    	
    	if(space_for_ingredients()){
			$('#add-ingredient').show();
		}
		
    	//Restripe remaining items
    	restripe_ingredients();
    });
    
    $('.add-button').click(function(){
    	//Has DOM issues. Does not remove items added dynamically yet.
		if($.trim($('#ingredient').val()) != '')
		{
			var new_ingredient = $('#ingredient').val();
			$('.start-here').hide();
			add_ingredient(new_ingredient);
			restripe_ingredients();
		}
    	return false;
    });
    
    $('input').keypress(function (event){ 
    
    	if (event.keyCode == 13 && $(this).attr('id') == "ingredient"){
			if($.trim($('#ingredient').val()) != '')
			{
				var new_ingredient = $('#ingredient').val();
				$('.start-here').hide();
				add_ingredient(new_ingredient);
				restripe_ingredients();
			}
    		return false;
    	}else if(event.keyCode == 13){
    		return false;
    	}else{
    		return true;
    	}
    });
    
    //For the homepage
    if ($('#slideshow').length != 0){    
	    $('#slideshow').cycle({
	    	fx:    'fade', 
			cleartype: true, 
			cleartypeNoBg: true,
			timeout:5000,
	   		speed:  3000,
	   		pager:	'.slideshow-pager',
	   		pagerAnchorBuilder: function(idx, slide) { 
	       		// return selector string for existing anchor 
	       		return '.slideshow-pager li:eq(' + idx + ') a'; 
	    	} 
	    });
    }
    
    //For the Recipe Featured Slides
    if ($('.feature-slideshow').length != 0){    
	    $('.feature-slideshow').cycle({
	    	fx:    'fade',
			cleartype: true, 
			cleartypeNoBg: true,
	    	timeout:6000,
	   		speed:  2000,
	   		pager:	'.slideshow-pager',
	   		pagerAnchorBuilder: function(idx, slide) { 
	       		// return selector string for existing anchor 
	       		return '.slideshow-pager li:eq(' + idx + ') a'; 
	    	} 
	    });
    }
    
    //For the Boutique Featured Slides
    if ($('.feature-items').length != 0){    
	    $('.feature-items').cycle({
	    	fx:    'fade',
			cleartype: true, 
			cleartypeNoBg: true,
	    	timeout:5000,
	   		speed:  2000
	    });
    }
    
    $('.harmonie-bottle .trigger').mouseover(function(){
    	$('.tooltip-harmonie').stop(true, true).fadeIn('fast');
    	$('.harmonie-bottle').stop(true, false).animate({
    		top: '-10px'
    	}, 100);
    }).mouseleave(function(){
    	$('.tooltip-harmonie').stop(true, true).fadeOut('fast');
    	$('.harmonie-bottle').stop(true, false).animate({
    		top: '0px'
    	}, 100);
    });
	
	$('.original-bottle .trigger').mouseover(function(){
    	$('.tooltip-original').stop(true, true).fadeIn('fast');
    	$('.original-bottle').stop(true, false).animate({
    		top: '-10px'
    	}, 100);
    }).mouseleave(function(){
    	$('.tooltip-original').stop(true, true).fadeOut('fast');
    	$('.original-bottle').stop(true, false).animate({
    		top: '0px'
    	}, 100);
    });
    
    if ($('.photo-feed').length != 0){
    	$('.photo-feed').totemticker({
    		row_height	:	'170px',
    		speed		:	2000
    	});
    }
});

function remove_ingredient(row_to_remove){
	var targeted_ingredient = row_to_remove.find('span').text();
	row_to_remove.remove();
	$('input[value=' + targeted_ingredient +']').val('');
}

function add_ingredient(new_ingredient){
	$('.ingredient-list').append('<li><span>' + new_ingredient + '</span><a href="#" title="Remove this ingredient">Remove</a></li>');
	$('.ingredient-fields input').each(function(){
		if ($(this).val() == "") {
			$(this).val(new_ingredient);
			return false;
		}
	});
	$('#ingredient').val('');
	if(space_for_ingredients()){
		//There is still space
	}else{
		$('#add-ingredient').hide();
	}
}

function space_for_ingredients(){
	var used_count = 0;
	$('.ingredient-fields input').each(function(){
		if($(this).val() == ""){
			//It's unused
		}else{
			//It's used
			used_count++;
		}
	});
	console.log(used_count);
	if (used_count == 0){
		$('.start-here').show();
	}
	return (used_count >= 6) ? false : true;
}

function restripe_ingredients(){
	var count=0;
	$('.ingredient-list li').each(function(){
		if(count%2 == 0){
			//Even number
			$(this).removeClass('alt');
		}else{
			//Odd number
			$(this).addClass('alt');
		}
		count++;
	});
}

