function equalizeHeight(el1, el2, adjust1, adjust2){
	if(el1.length > 0  && el2.length > 0){
		h1 = el1.height();
		h2 = el2.height();
		
		if(h1 < h2){
			el1.height(h2 + adjust1);
		} else {
			el2.height(h1 + adjust2);
		}
	}
}

function equalizeAllHeight($els){
	var maxHeight = 0;
	
	$els.each(function(){
		var h = $(this).height();
		if(h > maxHeight){
			maxHeight = h;
		}
	});
	
	if(maxHeight > 0){
		$els.height(maxHeight);
	}
}

function modalSetup(){
	$('body').append('<div id="dialog"></div>');
	
	$('a.modalBox').live('click', function(){
		var url = this.href;
		var linkClasses = this.className || '';
		
		$.getJSON(url+'?format=json', function(data) {
			var width = 500;
			var height = 300;
			
			var newWidth = linkClasses.match('modalBoxWidth([0-9]+)');
			if(newWidth){ width = newWidth[1]; }
			var newHeight = linkClasses.match('modalBoxHeight([0-9]+)');
			if(newHeight){ height = newHeight[1]; }
			
			$('#dialog').dialog({
				height: parseInt(height, 10),
				width: parseInt(width, 10),
				position: 'center',
				modal: true,
				title: data['title']
			});
			
			$('#dialog').html(data['content']);
			
			if(data['extraJS']){
				eval(data['extraJS']);
			}
		});

		return false;
	});
	
	$('#dialog form input[type="submit"]').live('click', function(){
		$(this).addClass('clickedForSubmit');
	});	
	$('#dialog form').live('submit', function(event){
		var url = this.action;
		
		var formData = $(this).serialize();
		var $clickedButton = $('input[type="submit"].clickedForSubmit',this);
		if($clickedButton.length > 0){
			formData += '&'+ $clickedButton.attr('name') +'='+ $clickedButton.val();
			$clickedButton.removeClass('clickedForSubmit');
		}
		
		$.ajax({
			url: url+'?format=json',
			type: 'POST',
			data: formData,
			success: function(data){
				if(data['redirect']){
					document.location = data['redirect'];
				}
			
				var content = data['content'];
				var height = data['dialogHeight'];
				var width = data['dialogWidth'];
				
				$('#dialog').dialog({
					title: data['title'],
					width: parseInt(width, 10),
					height: parseInt(height, 10),
					position: 'center'
				}).html(content);
		
				if(data['extraJS']){
					eval(data['extraJS']);
				}
			}
		});
		
		// Prevent the form submission (default action).
		event.preventDefault();
	});
	
}

function searchIngredients(){
	var url = '/recipes/addingredients';
	var $form = $('#recipe_ingredients');
	
	var formData = $form.serialize();
	var $clickedButton = $('input[type="submit"].clickedForSubmit',$form);
	if($clickedButton.length > 0){
		formData += '&'+ $clickedButton.attr('name') +'='+ $clickedButton.val();
		$clickedButton.removeClass('clickedForSubmit');
	}
	
	$searchField = $('#new_ingredient_name');
	$searchField.data('originalValue', $searchField.val());
	
	$.ajax({
		url: url+'?format=json',
		type: 'POST',
		data: formData,
		success: function(data){
			var content = $(data['content']).find('#ingredientsList').html();
			
			$('#dialog #ingredientsList').html(content);
	
			if(data['extraJS']){
				eval(data['extraJS']);
			}
		}
	});
}

function recipeIngredientCreationSetup(){
	
	$('#recipe_ingredients div.recipeIngredient input[type="checkbox"]').each(function(){
		$input = $(this);
		$parent = $input.parent().parent();
		if($input.attr('checked') && !$parent.hasClass('selected')){
			$parent.addClass('selected');
		}
		$input
		//	.css('position', 'absolute')
		//	.css('visibility', 'hidden')
			.change(function(){
				$(this).parent().parent().toggleClass('selected');
			})
		;
		
	});
	
	$('#recipe_ingredients div.recipeIngredient span.imgReplacement, #recipe_ingredients div.recipeIngredient span.imgMini')
		.click(function(){
			$(this).parent().toggleClass('selected');
			$checkbox = $(this).parent().find('input[type="checkbox"]');
			$checkbox.attr('checked', ($checkbox.attr('checked') ? false : true));
		})
	;
	
	$('#recipe_ingredients div.recipeIngredientAmount input')
		.keyup(function(){
			$parent = $(this).parent().parent();
			if(!$parent.hasClass('selected')){
				$parent.addClass('selected');
				$checkbox = $parent.find('input[type="checkbox"]');
				$checkbox.attr('checked', ($checkbox.attr('checked') ? false : true));
			}
		})
	;
	
	$('#new_ingredient_name')
	.focus(function(){
		$(this).data('originalValue', $(this).val());
	})
	.keyup(function(){
		clearTimeout($(this).data('timeout'));
		if($(this).val() != $(this).data('originalValue')){
			$(this).data('timeout', setTimeout(searchIngredients, 400));
		}
	})
	;
	
}

$(document).ready(function(){
	$('a.external').attr('target', '_blank');

	modalSetup();
	
	/*$(".rating").rating({
		showCancel: false, 
		cancelValue: null
	});*/
});