var Favorites = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			moduleName: 'AddBookmark',
			json: {
				ver: '0.1',
				meta: {},
				data: {
					requests: []
				}
			},
			uniqueID: null,
			type: null
			/* put extensions to collections here */
		});
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			link: null,
			links: null,
			linkSpans: null
			/* put extensions to nodes here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			requests: []
			/* put extensions to collections here */
		});
		
		this._addEvents();
		
		// Get query params
		var args = this.n.el.href.substr(this.n.el.href.indexOf('?')+1);
		// Split using the &
		if (args != "#")
		{
			args = args.split("&");
			
			for(var x=0; x<args.length; x++){
				kv = args[x].split("=");
				if (kv[0] == "type")
					this.s.type = kv[1];
				
				if (kv[0] == "id")
					this.s.uniqueID = kv[1]
			}
		}
	},
	
	linkOnClick: function(e){
		Event.stop(e);
		if(Element.hasClassName(this.n.el, 'bookmark_add')){
			this.addRequest("addFavorites", {
				uniqueID: this.s.uniqueID,
				type: this.s.type
			});
			this.JsonOut();
		}else if(Element.hasClassName(this.n.el, 'bookmark_remove')){
			this.addRequest("removeFavorites", {
				uniqueID: this.s.uniqueID,
				type: this.s.type
			});
			this.JsonOut();
		}
	},
	
	addRequest: function(requestName, dataObject) {
		this.c.requests.push({
			id: this.c.requests.length,
			type: requestName,
			data: dataObject
		});
	},
	
	//json methods
	JsonOut: function() {
		//construct the post
		this.s.json.data.requests = this.c.requests;
		var data = '__json=' + this.s.moduleName + '&data=' + toJSONString(this.s.json);
		this.c.requests = [];
		var myAjax = new Ajax.Request(window.location,
		{
			method: 'post',
			parameters: data,
			onSuccess: this.JsonIn.bind(this)
		});
	},
	
	JsonIn: function(t) {
		var result = t.responseText.parseJSON();
		(result.responses.length).times(function(i) {
			if(result.responses[i].type == 'addFavorites'){
				var newLink = result.data.html.toDOMNodes()[0];
				DOM.replace(this.n.el, newLink);
				this.n.el = newLink;
			}else if(result.responses[i].type == 'showMessage'){
				new kDialog2({
					'+zones': {
						heading: result.responses[i].data.headline,
						text: result.responses[i].data.html
					},
					position: {  
						exemplarAnchor: 'bottom right',
						selfAnchor: 'top right',
						exemplar: this.n.el,
						offsetY: 0,
						offsetX: 0
					},
					groupId: 'showMessage',
					groupLimit: 1,
					startHidden: false
				});
			}
		}.bind(this));
	},
	
	_addEvents: function() {
		// Bind the a element to listen for the click event
		this.eObserve(this.n.el, 'click', this.linkOnClick.bind(this));
	}
});

EventSelectors.register({
	'.bookmark_add': function(el, index) {
		new Favorites(el);
	}
}, true);
