﻿/*
	Favorites v1.0 - required: mootools.js v1.2.1 core
		
	by Wingweb Communications (http://www.wingweb.nl) February 2009.
	SPECIAL THANKS TO THE MOOTOOLS DEVELOPERS
*/

var Favorite = new Class({

	Implements:[Options,Events],
	
	options:{//set all the options here
		domain: 'giftsandgadgets.wingweb.nl',
		favoriteInfo: 'favorieten', 		// Div to display No. of items.
		favoriteBtn: '#inhoud a.favorite',  // Favorites button in DOM (Needs a rel)
		cookieName: 'favorites', 			// Default favorites, change if in conflict.
		cookieValid: 5,						// Numeric in days, default 5 days.
		currentCookie: null
	},
	
	initialize: function(options){
		
		this.setOptions(options);
		this.initFavorites(options);

	},//end init
	
	initFavorites:function(options){
		
		var currentCookie = Cookie.read(this.options.cookieName);
		
		this.checkFavorites( currentCookie );
		this.addFavoritesEvent( currentCookie );
		this.removeFavorite();
	},
	
	checkFavorites:function( currentCookie ) {
		
		if( currentCookie != null && JSON.decode(currentCookie).length > 0 ) {
			currentCookie = JSON.decode(currentCookie);
			var favText = ((currentCookie.length > 1) ? ' favoriete producten' : ' favoriet product');
			$(this.options.favoriteInfo).set('html', 'U heeft momenteel<br /> <a href="/favorieten">'+ currentCookie.length + favText +'</a>.');
		}
		else
		{ $(this.options.favoriteInfo).set('html', 'U heeft momenteel geen favoriete producten.'); }


	},
	
	addFavoritesEvent:function( currentCookie ) {
		
		$$(this.options.favoriteBtn).addEvent('click', function(el) {
		
			currentCookie = Cookie.read(this.options.cookieName);
			
			if (currentCookie == null) {
				var favorites = new Array();
				Cookie.write(this.options.cookieName, JSON.encode(favorites), {
					domain: this.options.domain,
					path: '/',
					duration: this.options.cookieValid
				});
			}
			
			currentId = $(el.target).get('rel');
			
			currentCookie = Cookie.read(this.options.cookieName);
			favorites = JSON.decode(currentCookie);
			
			if (!favorites.contains(currentId)) {
				favorites.push(currentId);
				
				var favText = ((favorites.length > 1) ? ' favoriete producten' : ' favoriet product');
				$(this.options.favoriteInfo).set('html', 'U heeft momenteel<br /> <a href="/favorieten">'+ favorites.length + favText +'</a>.');
				
				favorites = JSON.encode(favorites);
				Cookie.write(this.options.cookieName, favorites, {
					domain: this.options.domain,
					path: '/',
					duration: this.options.cookieValid
				});
	
				$(el.target).setStyle('background-image', 'url(/images/fav.jpg)');
				
			}
			else
			{
				favorites.erase(currentId);
				var favText = ((favorites.length > 1) ? favorites.length +' favoriete producten' : favorites.length + ' favoriet product');
				favText = ((favorites.length == 0) ? 'geen favoriete producten' : favText);
				$(this.options.favoriteInfo).set('html', 'U heeft momenteel<br /> <a href="/favorieten">'+ favText +'</a>.');
				
				favorites = JSON.encode(favorites);
				Cookie.write(this.options.cookieName, favorites, {
					domain: this.options.domain,
					path: '/',
					duration: this.options.cookieValid
				});
				
				$(el.target).setStyle('background-image', 'url(/images/nofav.jpg)');
			}
		}.bind(this));
		
	},
	
	removeFavorite:function() {
		
		$$('#favTable a.favorietenLinks').addEvent('click', function(el) {
			
			var currentElement = $(el.target);
		 	
				favorites = Cookie.read(this.options.cookieName);
				favorites = JSON.decode(favorites);
				
				favorites.erase(currentElement.get('rel'));
				var favText = ((favorites.length > 1) ? favorites.length +' favoriete producten' : favorites.length + ' favoriet product');
				favText = ((favorites.length == 0) ? 'geen favoriete producten' : favText);
				$(this.options.favoriteInfo).set('html', 'U heeft momenteel<br /> <a href="/favorieten">'+ favText +'</a>.');
				
				favorites = JSON.encode(favorites);
				Cookie.write(this.options.cookieName, favorites, {
					domain: this.options.domain,
					path: '/',
					duration: this.options.cookieValid
				});
				
				currentElement.getParent('tr').dispose();
		}.bind(this));
	}
});//END FAVORITES;

window.addEvent('domready', function(){ 
	favorite = new Favorite();
});
