
var RECIPE = (function($) {
	
	var cookbookEID = 'cookbook';
	var cookbookAjaxURL = '/typo3conf/ext/mycookbook/ajax.php';
	
	return {
		init: function() {
		
			jQuery('#submit-search').click(function(){
				RECIPE.searchform.submit();
				return false;
			});
			
			jQuery('#dt').change(RECIPE.searchform.submit);
			jQuery('#t').change(RECIPE.searchform.submit);
			jQuery('#r').change(RECIPE.searchform.submit);
			jQuery('#ovp').change(RECIPE.searchform.submit);
			
			this.cookbook.init();
			//this.cookbooklist.init();
			
			jQuery('.rating-box').addClass('rating-box-active').hover(function(){
				jQuery('.total-ratings').hide();
				jQuery('.rate-trigger').show();
				
			},function(){
				jQuery('.rate-trigger').hide();
				jQuery('.total-ratings').show();
			}).click(function(){
				jQuery('#rate-interface').show().css('zIndex',999).parent().css('zIndex',1000).parent().css('zIndex',1111).parent().css('zIndex',2222);
				
				jQuery('#rate-cancel').click(function() {
					jQuery('#rate-interface').hide();
					jQuery('.rate-link').unbind('click');
					jQuery('#rate-cancel').unbind('click');
					return false;
				});
				
				jQuery('.rate-link').click(RECIPE.submitRating);
			});
		},
		
		submitRating: function(event) {
			
			var starRating = event.currentTarget.id.match(/^rate\-([1-5]{1})$/)[1];
			
			jQuery.ajax({
				data: 'eID=ovrecipe&action=rate&value='+starRating+'&uid='+RECIPE.cookbook.getRecipeId(),
				dataType: 'json',
				async: true,
				cache: false,
				type: 'GET',
				success: function(result,status,xhr) {
					if (result.status == 'success') {
						jQuery('#ratings-count').html(result.total);
						var numStars = Math.floor(parseFloat(result.average)+.5);
						jQuery('.recipe-rating').removeClass().addClass('recipe-rating stars-'+numStars.toString());
						jQuery('.number-stars').html(numStars);
					}
				}
			});
			
			jQuery('#rate-interface').hide();
			jQuery('.rate-link').unbind('click');
			jQuery('#rate-cancel').unbind('click');
			jQuery('.rating-box').unbind().removeClass('rating-box-active');
			jQuery('.rate-trigger').hide();
			jQuery('.total-ratings').show();
			
			
			return false;
		},
		searchform: {
			submit: function() {
				jQuery('#recipe-search .hidden').trigger('click');
			}
		},
		
		cookbooklist: { 
			
			init: function() {
			
				
			},
			
			removeRecipe: function(){
				
				//var recipeID = jQuery(this).find('span').first().text();
				
				/*
				RECIPE.cookbook.removeRecipe(recipeID,function(){
					jQuery(this).parent().parent().remove();
				});*/
				
				return false;
			}
			
		},
		
		cookbook: {
			
			init: function() {
			
				//var cookbookHash = jQuery.cookies.get('cb');
				var savedRecipes = jQuery('#savedRecipes').text().split(',');
				if (savedRecipes) {
					var recipeID = RECIPE.cookbook.getRecipeId().toString();
					
					if (jQuery.inArray(recipeID,savedRecipes) != -1) {
						jQuery('.addtocookbook').removeClass('addtocookbook').addClass('removefromcookbook');
					}
				}
								
				jQuery('.addtocookbook a').live('click',RECIPE.cookbook.add);
				jQuery('.removefromcookbook a').live('click',RECIPE.cookbook.remove);
				
			},
			
			get: function() {

				var recipeID = RECIPE.cookbook.getRecipeId();
				
				jQuery.ajax({
					type: 'post',
					data: 'eID='+cookbookEID+'&uid='+recipeID+'&action=fetch',
					dataType: 'json',
					success: function(result,data,xhr) {
						
						if (result.status == 'success') {
							jQuery('.addtocookbook').removeClass('addtocookbook').addClass('removefromcookbook');
							jQuery('.number-saved').text(result.cookbook.number_recipes);
						}
					}
				
				});
				
			},
			
			add: function() {
				var recipeID = RECIPE.cookbook.getRecipeId();				
				jQuery.ajax({
					type: 'post',
					data: 'eID='+cookbookEID+'&uid='+recipeID+'&action=add',
					dataType: 'json',
					success: function(result,data,xhr) {
						if (result.status == 'success') {
							jQuery('.addtocookbook').removeClass('addtocookbook').addClass('removefromcookbook');
							jQuery('.number-saved').text(result.cookbook.number_recipes);
						}
					}
				
				});
				
				return false;
			},
			
			removeRecipe: function(recipeID,callback) {
				jQuery.ajax({
					type: 'post',
					data: 'eID='+cookbookEID+'&uid='+recipeID+'&action=remove',
					dataType: 'json',
					success: callback
				});
			},

			remove: function() {
				var recipeID = RECIPE.cookbook.getRecipeId();
				RECIPE.cookbook.removeRecipe(recipeID,function(result,data,xhr) {
					if (result.status == 'success') {
						jQuery('.removefromcookbook').removeClass('removefromcookbook').addClass('addtocookbook');
						jQuery('.number-saved').text(result.cookbook.number_recipes);
					}
				});
				return false;
			},
			
			getRecipeId: function() {
				return parseInt(jQuery('#recipe-uid').text());
			}
		}
	};
	
})(jQuery);

$(document).ready(function(){ 
	RECIPE.init();
});
//initActionsOV.push('RECIPE.init();');

