$(function() {
	if ($('.favourites-email-cnt').hasClass('open')) {
		$('.search-results a').click(a_set_checkbox);
	}
	$('.favourites-email-cnt').hover(function() {
		if (!$(this).hasClass('open')) {
			$(this).addClass('hover');
		}
	}, function() {
		$(this).removeClass('hover');
	});
	$('.favourites-email-cnt').click(function() {
		if ($(this).hasClass('open')) return;
		$(this).data('pwidth', $(this).width());
		$(this).data('pheight', $(this).height());
		$(this).removeClass('hover');
		$(this).addClass('open');
		$(this).data('opened', 1);
		$(this).css({'min-width': $(this).width(), 'width': '40%'});
		$('.fav-remove').fadeOut(function() {
			$('.fav-checkbox').fadeIn();
		});
		$('.search-results a').click(a_set_checkbox);
 		$(this).find('.email-link').fadeOut(400); 
		$(this).animate({width: '100%'}, function() { $(this).css({'min-width': '0'}); $(this).find('.email-form').slideDown(400); });
	});
	$('.favourites-email-cnt .email-cancel').click(function() {
		var pwidth = $('.favourites-email-cnt').data('pwidth') || 160;
		var pheight = $('.favourites-email-cnt').data('pheight') || 30;
		
		$('.fav-checkbox').fadeOut(function() {
			$('.fav-remove').fadeIn();
		});
		$('.search-results a').unbind('click', a_set_checkbox);
		$('.favourites-email-cnt').data('fullheight', $('.favourites-email-cnt').height());
		$('.favourites-email-cnt').css({height: $('.favourites-email-cnt').height(), overflow: 'hidden', width: $('.favourites-email-cnt').width()});
		$('.favourites-email-cnt .email-form').fadeOut(400, function() {
						
			$('.favourites-email-cnt').animate({height: pheight}, function() {
				
				$('.favourites-email-cnt .email-link').css({padding: '0 5px 0 5px'}).fadeIn(400);
				
				$('.favourites-email-cnt').css({height: 'auto'});
				$('.favourites-email-cnt').animate({width: pwidth}, function() {
					$(this).css({width: 'auto'});
					$(this).removeClass('open');
					$(this).data('opened', 0);
				});
			});
		});
		return false;
	});
	
	function a_set_checkbox() {
		$(this).find('.fav-checkbox').toggleClass('selected');
		if ($(this).find('.fav-checkbox').hasClass('selected')) {
			$(this).parent().find('input').val('1');
		}
		else {
			$(this).parent().find('input').val('0');
		}
		var sel = $('input.variant_input[value=1]').length;
		$('.selected-products').html(' <strong>' + sel + '</strong> product' + (sel == 1 ? '' : 's') + ' selected.');
		return false;
	}
	
	$('.fav-select-all').click(function() {
		$('.fav-checkbox').addClass('selected');
		$('input.variant_input').val('1');
		var sel = $('input.variant_input[value=1]').length;
		$('.selected-products').html(' <strong>' + sel + '</strong> product' + (sel == 1 ? '' : 's') + ' selected.');
	});
	
	$('.fav-select-none').click(function() {
		$('.fav-checkbox').removeClass('selected');
		$('input.variant_input').val('0');
		var sel = $('input.variant_input[value=1]').length;
		$('.selected-products').html(' <strong>' + sel + '</strong> product' + (sel == 1 ? '' : 's') + ' selected.');
	});
	
	$('a.fav-close-parent').click(function() {
        $(this).parent().slideUp(500, function() {
            $(this).remove();
        });
    });

	$('div.fav-remove').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	}).click(function() {
		var id = (/variant\[(\d+)\]/i).exec($(this).parents('li').find('input').attr('name'))[1];
		document.location = '/account/favourites/remove/' + id;
		return false;
	});
})