$(document).ready(function(){
	$('.png').pngfix();
	
	var newYear = new Date(2009, 4-1, 30, 18); /*30 April 2009 - 6pm */
	if($('.myCount').html() != undefined){
		$('.myCount').countdown({until: newYear, format: 'dHM', layout: '%D%nn %l%D  &nbsp; %H%nn %l%H &nbsp; %M%nn %l%M', labels: ['', '', '', '', '', '', ''], labels1: ['', '', '', '', '', '', '']});
	}
	$('.channel10Days').hover(function(){
		$('.channel10Days').each(function(){
			$(this).parent().children('div').removeClass('hover');
		});
		$(this).parent().children('div').addClass('hover');
	}, function(){
		$('.channel10Days').each(function(){
			$(this).parent().children('div').removeClass('hover');
		});
	});
	$('.channel10Days a').click(function(){
		return false;
	});
	
	$('.ingredientBoxes li').toggle(function(){
		$(this).addClass('active');
		$('.hide', $(this)).attr("checked", true);
	}, function(){
		$(this).removeClass('active');
		$('.hide', $(this)).removeAttr("checked");
	});
//	$('.ingredientBoxes a').click(function(){
//		return false;
//	});
//	$('.ingredientBoxes a').toggle(function(){
//		$(this).parent().addClass('active');
//		$(this).next().val($(this).html());
//	},function(){
//		$(this).parent().removeClass('active');
//		$(this).next().val('');
//	});
	
//	$('#print_shopping_list').click(function(){
//
//		var title = escape($(this).next(':hidden').val());
//
//		var postData = '';
//		$('.hide', $('.ingredientsinner li')).each(function(){
//			if($(this).attr('checked')){
//				postData += $(this).attr('name') + '=' + $(this).val();
//				postData += '&';
//			}
//		});
//		$.ajax({
//			type:"POST",
//			url:"/shopping-list",
//			data:postData+'&title='+title,
//			success:function(res){
//				window.open(res,'shoppingList','width=800,height=600,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
//			}
//		});
//		return false;
//	});
	$('#searchbox').focus(function(){
		if($(this).val() == 'enter keywords'){
			$(this).val('');
		}
		$('#enter_keywords_popup').hide();
	});
	$('#searchBar a.coming').click(function(){
		return false;		
	});
	$('#searchBar #type').change(function(){
		var id = 'recipes';
		if($(this).val()=='events'){
			id = 'events';
		}else if($(this).val()=='articles'){
			id = 'articles';
		}
		$('#searchBar .goWrapper').next().html($('#c_'+id).html());
		return false;
	});
	$('#searchBar #type').change();
	$('a#more_options').toggle(function(){
		$('#searchbox').before('<input type="hidden" id="searchExpandVar" name="searchExpandVar" value="1" />');
		$('#searchBar').css('height', '168px');
		$(this).html('close this');
		$(this).css('backgroundPosition', 'right bottom');

		$('#searchBar .goWrapper').hide();
	}, function(){
		$('#searchBar #searchExpandVar').remove();
		$('#searchBar').css('height', '56px');
		$('#searchBar .goWrapper').show();
		$(this).html('more options');
		$(this).css('backgroundPosition', 'right top');
	});
	$('#recipe_actions .sr').cBox({
		width:640,
		height:600,
		method:'GET',
		callback:function(){
			$('#email_recipe_container .png').pngfix();
			$('#email_recipe_container form').ajaxForm({
				beforeSubmit:function(){
					$('#email_recipe_container #popup_error').hide();
					$('#email_recipe_container #popup_prcess').show();
				},
				dataType:'json',
				success:function(res){
					$('#email_recipe_container #popup_prcess').hide();
					$('#email_recipe_container label').removeClass('error');
					if(res.status==1){
						for(var i=0;i<res.errors.length;i++){
							$('#email_recipe_container #'+res.errors[i].id).prev('label').addClass('error');
							if(res.errors[i].id == 'captcha'){
								var required = $('#email_recipe_container #'+res.errors[i].id).parent().next(); 
								required.prev().remove();
								required.prev().remove();
								required.before(res.captcha);
								$('#email_recipe_container #'+res.errors[i].id).parent().prev('label').addClass('error');
							}
						}
						$('#email_recipe_container #popup_error').show();
					}else{
						$('#email_recipe_container #popup_success').show();
						setTimeout('remove_cBox();', 3000);
					}
				}
				
			});
		}
	});
	$('#recipe_actions .ar').click(function(e){
		var url = $(this).attr("href");
		add_to_favs(url, e, $(this));
		return false;
		
	});
	$('.my_f_link').click(function(){
		var link = $(this);
		$.ajax({
			dataType:'json',
			type:'POST',
			url:link.attr('href'),
			data:'',
			success:function(res){
				if(res.status==1){
					link.parent().parent().fadeOut('faset', function(){
						link.parent().parent().remove();	
					});
					if($('#f_r_'+res.id).html()!=undefined){
						$('#f_r_'+res.id).fadeOut('faset', function(){
							$(this).remove();	
						});
					}
				}else{
					alert('Cannot delete, please try again');
				}
			}
			
		});
		
		return false;
	});
	/**login section background**/
	$('.focus_remove').each(function(){
		if($(this).val()!=''){
			$(this).css('backgroundPosition', '5px -100px');
		}
	});
	
	$('.focus_remove').focus(function(){
		$(this).css('backgroundPosition', '5px -100px');
	});
	$('.focus_remove').blur(function(){
		if($(this).val() ==''){
			$(this).css('backgroundPosition', '5px 5px');
		}
	});
	/** end login section background**/
});
$(function(){
	$('.hueyShopRound').corner();
	$('.inner').corner({
	tl: { radius: 3 },
	tr: { radius: 3 },
	bl: { radius: 3 },
	br: { radius: 3 }});
});
$(function(){
	$('.win').corner();
	$('.wininner').corner({
	tl: { radius: 3 },
	tr: { radius: 3 },
	bl: { radius: 3 },
	br: { radius: 3 }});
});
$(function(){
	$('.kitchen').corner();
	$('.kitcheninner').corner({
	tl: { radius: 3 },
	tr: { radius: 3 },
	bl: { radius: 3 },
	br: { radius: 3 }});
});
jQuery().ready(function(){
	jQuery('#list1a').accordion({header: ".ui-accordion-header", fillSpace:false, autoheight:false});
});

function add_to_favs(url, e, alink){
	if(url!=''){
		$.ajax({
			'type':"POST",
			'dataType':'json',
			'url':url,
			'data':'',
			'success':function(res){
				update_side_bar(res.html);
				show_response(e);
				alink.attr('href', '').html('already in your favourites');
			}
			
		});
	}
}
function update_side_bar(html)
{
	var ul = $('#list1a .myFavourites').next().children('ul');
	$('h1', ul).parent().remove();
	var list = ul.children();
	if(list.size()==4){
		ul.children(':last').fadeOut('fast', function(){$(this).remove()});
	}
	$('li', $(html)).hide();
	ul.prepend(html);
	ul.children(':first').fadeIn('fast');
}
function show_response(e){
	$('#add_to_favourite_popup').css({'left': e.pageX, 'top':e.pageY-50}).show();	
}
function check_keyword(){
	//for recipe
	if($('#searchExpandVar').val() != undefined && $('#option_type').val() == 2){
		return true;
	}
	if($('#searchExpandVar').val() == undefined){
		if($('#searchbox').val()!='' && $('#searchbox').val()!= 'enter keywords'){
			return true;
		}
		$('#enter_keywords_popup').show();
		return false;
	}
	return true;
}
function print_shopping_list(lnk){
	
	var title = escape(lnk.next(':hidden').val());

	var postData = '';
	$('.hide', $('.ingredientsinner li')).each(function(){
		if($(this).attr('checked')){
			postData += $(this).attr('name') + '=' + $(this).val();
			postData += '&';
		}
	});
	$.ajax({
		type:"POST",
		url:"/shopping-list",
		data:postData+'&title='+title,
		success:function(res){
			window.open(res,'shoppingList','width=800,height=600,toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no');
		}
	});
	return false;
}