/*
	jQuery Wishlist Plugin
	
	Basic Idea:
	- onclick on a.wishlist take data from the element and store it in the wishlist cookie

	- if in the wishlist already on pageload, give alternate class and change innerHTML to "remove"

	- 


	- what to store:
		- ID (from a.wishlist[id])
		- title (from a.wishlist[name])
		- URL (optional)
		- image URL? (from img[rel=a.wishlist[id]])
	
*/

jQuery(document).ready(function($){
	wishlist = new Wishlist;
	wishlist.init();
	
});

function parseUrl1(data) {
    var e=/^((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?$/;

    if (data.match(e)) {
        return  {url: RegExp['$&'],
                protocol: RegExp.$2,
                host:RegExp.$3,
                path:RegExp.$4,
                file:RegExp.$6,
                hash:RegExp.$7};
    }
    else {
        return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
    }
}
var url = parseUrl1(window.location.href);


Wishlist = function() {
	// initialize the wishlist JSON
	this.json = {};
	// initialize a cookie to last a year (525600 minutes)
	this.COOKIE_NAME = 'wishlist';
	this.COOKIE_OPTIONS = {
		path: '/',
		expires: 365
	}
	if (jQuery.cookie(this.COOKIE_NAME) != null) {
		// wishlist_json = eval(unescape(jQuery.cookie(this.COOKIE_NAME)));
		this.json = JSON.parse(jQuery.cookie(this.COOKIE_NAME));
		// console.info(unescape(jQuery.cookie(this.COOKIE_NAME)));
		// console.info(this.json);
	} else {
		jQuery.cookie(this.COOKIE_NAME, '{}', this.COOKIE_OPTIONS);
		// console.info('No cookie set so we just set one.');
	}
	
	var me = this;
}
Wishlist.prototype.init = function() {
	var me = this;
	jQuery('a.wishlist').each(function(){
		var id = jQuery(this).attr('id').match(/id_(.+)/);
		id = id[1];
		// console.info('me.json[id] :::: ' + me.json[id]);
		if (me.json[id] != undefined) {
			// console.info(jQuery(this).attr('id') + ' :::: id: '+id+', :::: me.json[id]: ' + me.json[id]);
			jQuery(this).addClass('wishlist_remove').filter(':not(.wishlist_x)').html('<span>REMOVE</span> FROM YOUR WISHLIST');
		} else {
			jQuery(this).addClass('wishlist_add');
		}
	});
	jQuery('a.wishlist_add').live('click', function(){
		// console.info('ADD a.wishlist clicked');
		jQuery(this).removeClass('wishlist_add').addClass('wishlist_remove')
			.filter(':not(.wishlist_x)').html('<span>REMOVE</span> FROM YOUR WISHLIST');
		me.add(this);
	});
	jQuery('a.wishlist_remove').live('click', function(){
		// console.info('REMOVE a.wishlist clicked');
		jQuery(this).removeClass('wishlist_remove').addClass('wishlist_add')
			.filter(':not(.wishlist_x)').html('<span>ADD</span> TO YOUR WISHLIST');
		me.remove(this);
	});
	jQuery('a.wishlist_x').live('click', function(){
		// console.info('X a.wishlist_x clicked');
		me.remove(jQuery(this));
		jQuery(this).parents('div.ring_disp').fadeOut('slow', function(){
			jQuery(this).slideUp('slow', function(){
				jQuery(this).remove();
			});
		});
	});
	// console.info('Me? ' + me.COOKIE_NAME);
}
Wishlist.prototype.add = function(e) {
	// Get the wishlist item's attributes
	var id = jQuery(e).attr('id').match(/id_(.+)/);
	id = id[1];
	var name = jQuery(e).attr('rel').match(/wishlist_name\[ (.*?) \]/);
	name = name[1];
	var image = jQuery(e).attr('rel').match(/wishlist_img\[ (.*?) \]/);
	image = image[1];
	// console.info(id+', name: '+name+', '+image);
	// Add it to the JSON cookie
	this.json[id] = {"name": name, "image": image};
	jQuery.cookie(this.COOKIE_NAME, JSON.stringify(this.json), this.COOKIE_OPTIONS);
}
Wishlist.prototype.remove = function(e) {
	// console.info(e);
	// Get the wishlist item's attributes
	var id = jQuery(e).attr('id').match(/id_(.+)/);
	// console.info('Removing ID: ' + id);
	id = id[1];
	// console.info('Removing ID: ' + id);
	// Remove it from the JSON cookie
	delete this.json[id];
	// console.info(JSON.stringify(this.json));
	jQuery.cookie(this.COOKIE_NAME, JSON.stringify(this.json), this.COOKIE_OPTIONS);
}
Wishlist.prototype.template = '';







