$(document).ready( function() {

	var fav_prods = 0;
	$('#prodlist_favs .prodlist_prod').each(function() { fav_prods++; });
	//$('#prodlist_all a.reseller_prod_fav').each(function() { $(this). });
	if (fav_prods>0) $('#prodlist_preferred_empty').hide(); else $('#prodlist_preferred_empty').show();
	//alert(fav_prods);

	$('#prodlist_favs a.reseller_prod_fav').attr('title','Aus den Favoriten entfernen');
	$('#prodlist_all a.reseller_prod_fav').attr('title','Zu den Favoriten hinzufügen');
	$('#prodlist_all .prodlist_prod').each(
		function(index) {
			anch=$(this).find('a.reseller_prod_fav');
			if (anch.hasClass('prodlist_fav')) {
				$(this).removeClass('prodlist_fav').hide();
				anch.removeClass('prodlist_fav');
				anch.html('&#9734;');
			}
		}
	);
	$('#prodlist_all a.reseller_prod_fav').live('mouseover', function() {
			$(this).html('&#9733;');
	});
	$('#prodlist_all a.reseller_prod_fav').live('mouseout', function() {
			$(this).html('&#9734;');
	});

	$('#prodlist_favs a.reseller_prod_fav').live('mouseover', function() {
			$(this).html('&#9734;');
	});
	$('#prodlist_favs a.reseller_prod_fav').live('mouseout', function() {
			$(this).html('&#9733;');
	});
	$('a.reseller_prod_fav').live('click',
		function () {
			if ($(this).hasClass('prodlist_fav')) {
				$(this).attr('title','Zu den Favoriten hinzufügen');
				$('#prodlist_favs div.prod'+$(this).attr('rel')).remove();
				$('#prodlist_all div.prod'+$(this).attr('rel')).show().removeClass('prodlist_fav');
				fav_prods--;
				if (fav_prods==0) {
					$('#prodlist_preferred_empty').show();
					$('#favs_count').html(': keine');
					$('.prodlist_action.hideable').hide();
					emptyparam();
				} else {
					$('#favs_count').html(': '+fav_prods);
					makeparam();
				}
			} else {
				$(this).die();
				$('div.prod'+$(this).attr('rel')).clone().appendTo($('#prodlist_favs'));
				$('#prodlist_favs div.prod'+$(this).attr('rel')+' a.reseller_prod_fav').addClass('prodlist_fav');
				$('#prodlist_all div.prod'+$(this).attr('rel')).hide();
				$('#prodlist_favs div.prod'+$(this).attr('rel')+' a.reseller_prod_fav').attr('title','Aus den Favoriten entfernen');
				$('#prodlist_preferred_empty').hide();
				if (fav_prods==0) {
					$('.prodlist_action.hideable').show();
					emptyparam();
				}
				fav_prods++;
				$('#favs_count').html(': '+fav_prods);
				makeparam();
			}
		}
	);
	$('#prodlist_clear').live('click',
		function () {
			$('#prodlist_favs div.prodlist_prod').each(
				function(index) {
					anch = $(this).find('a.reseller_prod_fav');
					$(this).remove();
					$('#prodlist_all div.prod'+anch.attr('rel')).show();
					$('#prodlist_preferred_empty').show();
					$('#favs_count').html(': keine');
					$('.prodlist_action.hideable').hide();
					fav_prods=0;
					emptyparam();
				}
			);
		}
	);
	var makeparam = function() {
		param = "";
		$('#prodlist_favs div.prodlist_prod a.reseller_prod_fav').each(
			function(index) {
				if (index>0) param += ",";
				param += $(this).attr('rel');
			}
		);
		//alert(param);
		$('#prodlist_save').attr('href','../reseller/index.php?save='+param);
		$('#prodlist_xml').attr('href','../reseller/xml.php?p='+param);
	}
	var emptyparam = function() {
		$('#prodlist_save').attr('href','../reseller/index.php?save=');
		$('#prodlist_xml').attr('href','../reseller/xml.php');
	}
	$('#save_msg').fadeOut(1000,function() {$('#save_msg').remove();});
});
