Array.prototype.remove=function(s)
{
	var i = this.indexOf(s);
	if(i!=-1)this.splice(i, 1);
}
var arrFavorite = null;
YAHOO.util.Event.on(window, "load", function(){
	
	var activeTemplateId     = null;
	var cookieName           = "favorites";
	var buttonAddToFavorites = YAHOO.util.Dom.get("buttonAddToFavorites");
	
	arrFavorite = YAHOO.util.Cookie.get(cookieName, function (stringValue) {
	    return stringValue.replace("templates=","").split(","); 
	});
	
	if(arrFavorite != null && arrFavorite.length !=0)
	{
		animateFavorites();
	}
//	else
//	{
//		YAHOO.util.Event.on("favoritesLink", "click", function(evt){YAHOO.util.Event.preventDefault(evt);});
//	}

	
	function animateFavorites()
	{
		YAHOO.util.Dom.removeClass("favoritesLink", "default");
		YAHOO.util.Dom.removeClass("favoritesLink", "intermediate");
		YAHOO.util.Dom.removeClass("favoritesLink", "favorite");
		
		YAHOO.util.Dom.addClass("favoritesLink", "default");
		setTimeout(function(){
			YAHOO.util.Dom.addClass("favoritesLink", "intermediate");
				setTimeout(function(){
					YAHOO.util.Dom.addClass("favoritesLink", "favorite");
				}, 1000);
		}, 1000);
	}
	
	YAHOO.util.Dom.getElementsByClassName("remove-favorite", "a", "bd",
			function()
			{
				
				YAHOO.util.Event.on(this, "click", function(evt){
					
					YAHOO.util.Cookie.remove(cookieName,				
							{
						expires: curentDate,
						path: "/"
							}
					);
					
					//alert(arrFavorite.toString());
					var curentDate           = new Date();
					YAHOO.util.Event.preventDefault(evt);
					var templateId = this.id.replace("remove-favorite-", ""); 
					arrFavorite.remove(templateId);	
					curentDate.setMonth(curentDate.getMonth() + 1);

					var ancestor = YAHOO.util.Dom.getAncestorByTagName(this, "li");
					if(ancestor == null)
						ancestor = YAHOO.util.Dom.getAncestorByTagName(this, "dl");
					
					if(ancestor != null)
					ancestor.parentNode.removeChild(ancestor);
					
					
					//alert(arrFavorite.toString());
					if(arrFavorite.length > 0)
					{
						YAHOO.util.Cookie.set(cookieName, "templates="+arrFavorite.join(), 
								{
									expires: curentDate,
									path: "/"
									
								}
						);

					}
					
					
				});	

			}	
		);
	

	if(buttonAddToFavorites)
	{
	YAHOO.util.Event.on(buttonAddToFavorites, "click", function(evt){
		YAHOO.util.Event.preventDefault(evt);	
		var templateId = (YAHOO.util.Dom.get("buttonCustomize").href.split("-")).pop().replace(".html", "");
		
		if(arrFavorite != null)
		{
			for(var i=0; i<arrFavorite.length; i++)
			if(arrFavorite[i] == templateId)
			{
				return;
			}
		}
		else
			arrFavorite = new Array();
		
		arrFavorite.push(templateId);
		var curentDate           = new Date();
		curentDate.setMonth(curentDate.getMonth() + 1);
		YAHOO.util.Cookie.remove(cookieName);
		if(arrFavorite.length > 0)
			YAHOO.util.Cookie.set(cookieName, "templates="+arrFavorite.join(), 
					{
						expires: curentDate,
						path: "/"
						
					}
			);
	
		YAHOO.util.Event.purgeElement(YAHOO.util.Dom.get("favoritesLink"));
		YAHOO.util.Dom.setStyle("buttonAddToFavorites", "background-image", "url(/images/add_to_favorites_grey.gif)");
		animateFavorites();
	});
	
	}
	
	
	
	
	var configuratorAddToFavorites = YAHOO.util.Dom.get("configuratorAddToFavorites")
	if(configuratorAddToFavorites)
	{
		var templateId = window.location.toString().split("-").pop().replace(".html", "");
		
		if(arrFavorite == null)
			arrFavorite = new Array();

		for(var i=0; i<arrFavorite.length; i++)
		{
			if(arrFavorite[i] != templateId)
				continue;
			YAHOO.util.Dom.setStyle("configuratorAddToFavorites", "background-image", "url(/images/favorites_hearts_grey.gif)");
			YAHOO.util.Dom.setStyle("configuratorAddToFavorites", "color", "#ccc");
			return;
		}
		
		
		YAHOO.util.Event.on(configuratorAddToFavorites, "click", function(){
		
			arrFavorite.push(templateId);
			var curentDate           = new Date();
			curentDate.setMonth(curentDate.getMonth() + 1);
			YAHOO.util.Cookie.remove(cookieName);
			if(arrFavorite.length > 0)
				YAHOO.util.Cookie.set(cookieName, "templates="+arrFavorite.join(), 
						{
							expires: curentDate,
							path: "/"
							
						}
				);

			YAHOO.util.Dom.setStyle("configuratorAddToFavorites", "background-image", "url(/images/favorites_hearts_grey.gif)");
			YAHOO.util.Dom.setStyle("configuratorAddToFavorites", "color", "#ccc");
			
			animateFavorites();
				
			
		});
	}
	
});




