/* Home Page Module Sort */
/**
 * Turns a URL into an argument for links which function
 * purely as event launchers.
 */
/*String.prototype.getUrlArgument = function() {
	// Parses a URL like "javascript:void(42);" and returns "42"
	if (val = this.match(/[Jj]avascript:void\(\'?(.*?)\'?\);?/i)) {
		if (val[1]) return val[1];
	}
	return this;
}*/

var AddBookmark = Class.create();
AddBookmark.prototype = {
	//Properties
	el: null,
	parent:null,
	ajax: null,
	ajaxUrl: null,
	errors: true, // Show error alerts?
	debug: false,
	removeFunc: null,

	//Constructor
	initialize: function(el) {
		this.el = el;
		this.ajaxUrl = window.location;
		this.observeLink();

		//this.id = this.el.parentNode.id.split('_')[1] || null;
		this.id = this.el.parentNode.parentNode.id.split('_')[1] || null; 
		if (this.id  == null)
		this.id = this.el.parentNode.id.split('_')[1] || null; 
		
		this.parent = this.el.parentNode;
		if (this.id == null) this.error('Content_id could not be located.');
	},

	error: function(text) {
		if (this.errors) alert('AddBookmark.js Error:\n----------------------------------\n'+text);
	},

	//Methods
	observeLink: function () {
		this.removeFunc = this.addBookmark.bindAsEventListener(this);
		Event.observe(this.el, 'click', this.removeFunc);
		
		var temp = this.el.parentNode.id.split('_')[1] || null; 
		if (temp == null)
			Event.observe(this.el.parentNode.parentNode, 'click', this.removeFunc);

	},
	addBookmark: function () {
		Event.stopObserving(this.el, 'click', this.removeFunc);

		var temp = this.el.parentNode.id.split('_')[1] || null; 		
		if (temp == null)
			Event.stopObserving(this.el.parentNode.parentNode, 'click', this.removeFunc);

		this.AjaxOut();
		new Effect.Highlight(this.parent);
	},
	AjaxOut: function() {
		var vars = '__ajax__=BookmarkContentBlock&content_id=' + this.id;
		if (this.debug) alert(vars);
		this.ajax = new Ajax.Request(this.ajaxUrl,
			{
				parameters: vars,
				onSuccess: this.AjaxIn.bind(this)
			}
		);
	},
	AjaxIn : function(req) {
		//alert(req.responseText);
		if (req.responseXML) {
			var url = DOM.getElementText(req.responseXML.getElementsByTagName('result')[0]);
			if (!url) {
				alert('The XML for this module doesn\'t seem to return a URL.\n\nWhat it DOES return is: '+req.responseText);
			} else {
				//debugger;
				var temp = this.el.parentNode.id.split('_')[1] || null;
				if (temp == null)
				{

					this.el.parentNode.parentNode.innerHTML = 'Bookmarked: <a href="'+url+'" class="noblank">View</a>';
					Element.removeClassName('add_bookmark', this.parent.parentNode);
				}
				else
				{
					this.el.parentNode.innerHTML = 'Bookmarked: <a href="'+url+'" class="noblank">View</a>';
					Element.removeClassName('add_bookmark', this.parent);
				}
			}
		} else {
			this.error('XML parsing failed: '+req.responseText);
		}
		if (this.debug) alert(req.responseText);
	}
};
  


EventSelectors.register({
	'span.add_bookmark a': function(el) {
		new AddBookmark(el,true);
	}
}, true);

/*var AddBookmark_Rules = {
	'span.add_bookmark a' : function(el){
		new AddBookmark (el, true);
	}
};
Behaviour.register(AddBookmark_Rules);*/