

var a = new anchor_handler();

/**
 * Link handling class.
 * 
 * Allows links to be registered to elements so anchor
 * parameters are changed on click.
 * 
 * Auto loads page into correct context for given parameters
 * with the a.init function. This should be defined by the specific
 * page.
 * 
 * Allows forward/back navigation with a.do_change function.
 * @return
 */
function anchor_handler() {
	
	this.params = new Object();
	this.paramSec = new Object();
	this.links = new Object();
	this.dblLinks = new Object();
	this.url = '';
	this.pageURI = '';
	var me = this;
	
	// monitoring for back/forward events
	$(document).ready(function() {
		setInterval('a.check_anchor_change();', 500);
	});
	
	/**
	 * Checks if the anchor link has changed and calls
	 * do_change if it has.
	 * @return
	 */
	this.check_anchor_change = function() {
		/*if(!user.loggedIn)
			return;
		*/
		var myFile = document.location.toString();
		if(myFile.match('#')) {
			var myAnchor = myFile.split('#')[1];
			if(myAnchor != this.url) {
				this.do_change();
			}
		}
	};
	
	
	/**
	 * registers a link using the anchor handler.
	 * 
	 * will update the element so clicks will be passed
	 * through the anchor handler and the anchor will be
	 * updated with paramters specified.
	 * @param element	element to register
	 * @param param		parameters to change/add
	 * @param callback	string version of function to run
	 * 					for onclick attribute.
	 * @return	true on success, false otherwise
	 */
	this.register_link = function(element, param, callback, name) {
		//alert(callback);
		// remove any existing registration for the link
		if(this.links != null && this.links[element] != null)
			this.links[element] = null;
		
		$(element).unbind('click');
		$(element).attr('onclick', '');
		// add to links array
		this.links[element] = { 'element': element, 'param': param, 'callback': callback};
		// update link
		if($(element).is("a"))
			$(element).attr('href', this.generate_link(param));
		else
			$(element).css('cursor', 'pointer');
		
		// name parameter
		if(name)
			$(element).attr('name', name);
		
		$(element).bind('click', function() {
			me.take_click(element);
			eval(callback);
		});
		
		return true;
	};
	
	
	/**
	 * registers a link using the anchor handler.
	 * 
	 * will update the element so clicks will be passed
	 * through the anchor handler and the anchor will be
	 * updated with paramters specified.
	 * @param element	element to register
	 * @param param		parameters to change/add
	 * @param callback	string version of function to run
	 * 					for onclick attribute.
	 * @return			true on success, false otherwise
	 */
	/*this.register_link_object = function(element, param, callback, name) {
		// remove any existing registration for the link
		if(this.links != null && this.links[element] != null)
			this.links[element] = null;
		
		$(element).unbind('click');
		$(element).attr('onclick', '');
		// add to links array
		this.links[element] = { 'element': element, 'param': param, 'callback': callback};
		// update link
		if($(element).is("a"))
			$(element).attr('href', this.generate_link(param));
		else
			$(element).css('cursor', 'pointer');
		
		// name parameter
		if(name)
			$(element).attr('name', name);
		
		$(element).bind('click', function() {
			me.take_click(element);
			eval(callback);
			//return callback;
		});
		
		return true;
	};
	*/
	
	
	
	/**
	 * Removes any binding for given link.
	 * @param element	string element ID
	 * @return
	 */
	this.deregister_link = function(element) {
		if(this.links != null && this.links[element] != null)
			this.links[element] = null;
		
		$(element).unbind('click');
		$(element).attr('onclick', '');
	};
	
	/**
	 * same as register_link but for a dropbox box you want to do something
	 * on change.
	 */
	this.register_dropdown = function(element, param, callback, name) {
		// remove any existing registration for the link
		if(this.links != null && this.links[element] != null)
			this.links[element] = null;
		$(element).unbind('change');
		$(element).attr('onchange', '');
		// add to links array
		this.links[element] = { 'element': element, 'param': param, 'callback': callback};
		
		// name parameter
		if(name)
			$(element).attr('name', name);
		
		$(element).bind('change', function() {
			me.take_click(element);
			eval(callback);
		});
	};
	
	/**
	 * Same as register link but a double click event is bound instead.
	 * @param element	string element identifier
	 * @param param		anchor parameters
	 * @param callback	string callback function name.
	 * @param name
	 * @return
	 */
	this.register_double_click = function(element, param, callback, name) {
		// remove any existing registration for the link
		if(this.dblLinks != null && this.dblLinks[element] != null)
			this.dblLinks[element] = null;
		
		$(element).unbind('dblclick');
		$(element).attr('onclick', '');
		// add to links array
		this.dblLinks[element] = { 'element': element, 'param': param, 'callback': callback};
		
		// name parameter
		if(name)
			$(element).attr('name', name);
		
		$(element).bind('dblclick', function() {
			me.take_dbl_click(element);
			eval(callback);
		});
		
		return true;
	};
	
	/**
	 * Performs a click action for the specified link.
	 * @param e	string element ID
	 * @return
	 */
	this.take_click = function(e) {
		window.location.href = a.pageURI + me.generate_link(me.links[e].param);
		me.get_params_from_anchor();
	};
	
	/**
	 * Performs a double click action.
	 * @param e	string element ID
	 * @return
	 */
	this.take_dbl_click = function(e) {
		window.location.href = a.pageURI + me.generate_link(me.dblLinks[e].param);
		me.get_params_from_anchor();
	};
	
	/**
	 * Updates any &gt;a&lt; links with new parameters.
	 * @return
	 */
	this.regenerate_links =  function() {
		$.each(this.links, function(i, link) {
			if(link) {
				if($(link.element).is("a"))
					$(link.element).attr('href', me.generate_link(link.param));
			}
		});
	};
	
	/**
	 * Forces a change in parameters without calling do_change.
	 * @param params	parameters to change to.
	 * @return
	 */
	this.redirect = function(params) {
		me.params = params;
		window.location.href = a.pageURI + me.generate_link(me.params);
		me.get_params_from_anchor();
	};
	
	/**
	 * gets current params from anchor.
	 */
	this.get_params_from_anchor = function() {
		var myFile = document.location.toString();
		if(myFile.match('#')) {
			var myAnchor = myFile.split('#')[1];
			this.url = myAnchor;
			this.params = new Object;
			// case: multiple params
			if(myAnchor.match('/')) {
				$.each(myAnchor.split('/'), function(i, val) {
					//var par = val.split('=');
					me.params[i] = val;
				});
			} 
			// case: 1 param
			else {
				//var par = myAnchor.split('=');
				me.params[0] = myAnchor;
			}
		} 
	};
	
	/**
	 * generates the anchor link given the params that
	 * it will change combined with the current global params.
	 * @param paramters to change
	 */
	this.generate_link = function(param) {
		
		return '#'+ param.join('/');
	};
	
	/**
	 * simplified version of init for just performing
	 * state changes.
	 */
	this.do_change = function() {
		
	};
	
	this.do_default = function() {

	};
	this.redirect_to_url = function(urlString) {
		window.location.href = urlString;
		//me.get_params_from_anchor();
	};
	
}

