$(document).ready(function(){
	$('span.icon').live('mouseover', function(e){$(this).toggleClass('ui-state-hover');});
	$('span.icon').live('mouseout', function(e){$(this).toggleClass('ui-state-hover');});	
	$('#recipe tr').live('mouseover', function(e){$('.delete .icon', this).css('visibility', 'visible');});
	$('#recipe tr').live('mouseout', function(e){$('.delete .icon', this).css('visibility', 'hidden');});
	$('.v').live('click', function(e){
		$('.v, .i', $(this).parent()).toggle();
		$('input:first, select', $(this).parent()).focus();
	});	
	$('.i .icon').live('click', function(e){
		$('.v, .i', $(this).closest('td')).toggle();
	});
	var setParams = function(e){
		$('#params .ui-dialog-content').load(
			window.location+'/set-params?'+$.param($('input, select', $('#params'))), false, 
			function(){
				$('#stats .ui-dialog-content').load(window.location+'/stats');
				$('#comments').load(window.location+'/style-comments');
			}
		);
	};
	$('#params .icon').live('click', setParams);	
	$('#params input, #params select').live('keypress', function(e){
		if(e.type == 'keypress' && e.which != 13) return;
		setParams();
	});
	var loadRecipe = function(url){
		$('#recipe .ui-dialog-content').load(url, false, function(){
			$('#stats .ui-dialog-content').load(window.location+'/stats');
			IEFIX();
		});
	};	
	$('#recipe td.delete .icon').live('click', function(e){
		if(confirm('Are you sure you want to delete '+$.trim($('th', $(this).closest('tr')).text())+' from your recipe?')) loadRecipe(window.location+'/remove-ingredient?'+$.param($('input, select', $(e.target).closest('tr'))));
	});
	$('#recipe .i .icon').live('click', function(e){	
		loadRecipe(window.location+'/set-ingredient?'+$.param($('input, select', $(this).closest('tr'))));
	});
	$('#recipe input').live('keypress', function(e){
		if(e.type == 'keypress' && e.which != 13) return;
		loadRecipe(window.location+'/set-ingredient?'+$.param($('input, select', $(this).closest('tr'))));
	});	
	$('#ingredients .icon').live('click', function(e){
		$('h4', $(this).closest('li')).effect("transfer", {to:'#recipe .ui-widget-header', className:'ui-corner-all'}, 500, function(){
			loadRecipe(window.location+'/add-ingredient?'+$.param($('input, select', $(this).closest('li'))));
		});			
	});
	$('#ingredients input, #ingredients select').live('keypress', function(e){
		if(e.type == 'keypress' && e.which != 13) return;
		$('h4', $(this).closest('li')).effect("transfer", {to:'#recipe .ui-widget-header', className:'ui-corner-all'}, 500, function(){
			loadRecipe(window.location+'/add-ingredient?'+$.param($('input, select', $(this).closest('li'))));
		});		
	});	
	$('#ingredients h4').live('click', function(e){
		if($(this).next().length == 0) return $(this).after('<div></div>').next().load(
				window.location+'/ingredient_detail/'+$(this).attr('id').replace(/[^\d]/g, ''), false, 
				function(){
					$('tr:last', this).addClass('last');
					$(this).prev().click();
				}
			);
		$(this).next().toggle();
		$('span', this).toggleClass('ui-icon-triangle-1-s');
		$('div:visible input:first', $(this).parent()).focus();
	});
	$('#recipe select, #ingredients select', this).live('change', function(e){
		if($(this).val() == '') $(this).prev().show().prev().show();
		else $(this).prev().hide().prev().hide();
	});	
	$('#btn-new').live('click', function(e){
		if(confirm('Remove all ingredients and start a new recipe?')) window.location = window.location+'/init';
	});	
	$('#btn-buy').live('click', function(e){	
		window.location = window.location+'/buy-recipe';
	});	
	$('#btn-save').live('click', function(e){	
		window.location = window.location+'/save-recipe';
	});		
	$('#btn-load').live('click', function(e){	
		window.location = window.location+'/../account/recipes';
	});
	$('#btn-inst').live('click', function(e){
		window.open(window.location+'/instructions', 'bbinstructions', 'width=640,height=480,scrollbars=yes');
	});	
	$('#sidebar img.cap').after('<div id="comments" class="content"></div>').next().load(window.location+'/style-comments');	
	$('#interface').load(window.location+'/interface', false, function(){
		$('#ingredients').tabs({ 
			cache	: true,
			spinner	: '',
			show: function(event, ui){
				if($('.ui-accordion', ui.panel).length != 0) return;
				$('.igroup', ui.panel).accordion({header: 'h3', fillSpace: true});
			}
		});
		fuckIE();
	});
});
