/**
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * JQuery anchornav plugin: convert selected links to AJAX content loaders.                    *
 *                                                                                             *
 * @requires BBQ plugin which may be found at http://benalman.com/projects/jquery-bbq-plugin/  *
 *                                                                                             *
 * anchornav code is partly derived from an example at                                         *
 * http://benalman.com/code/projects/jquery-bbq/examples/fragment-basic/                       *
 *                                                                                             *
 * @author vbolshov <crocodile2u@gmail.com>                                                    *
 * @license freeware/beerware                                                                  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * @param object options:
 *  - target: selector or JQuery object representing target element where content is to be displayed
 *  - loading_class: CSS class name assigned to the element while the content is being loading
 *  - loading_text: Text to be displayed inside the target element while the content is being loading
 *  - namespace: links namespace. Anchornav generates links in the format "#<namespace>:<path>"
 *		when Anchornav does trace of the browser history moves, it will only try to process links
 *		in a matching namespace
 *  - handle_empty_anchor: whether empty anchors should be handled by this Anchornav instance
 */
$.fn.anchornav = function(options) {

	var result_options = {};
	$.extend(result_options, $.anchornav.defaults, options || {});

	var nav = new $.anchornav(result_options, this);
	$.anchornav.instances[result_options.namespace] = nav;
	if (result_options.handle_empty_anchor) {
		$.anchornav.empty_anchor_handlers.push(nav);
	}

	if (! $.anchornav.initiated) {

		$.anchornav.initiated = true;

		// listen to hashchange event ( remember the BBQ! )
		$(window).bind('hashchange', function(e) {
			// support jQuery 1.4's e.fragment
			var hash = e.fragment ? e.fragment : $.param.fragment();

			var initialPageLoad = ! $.anchornav.initDone;
			$.anchornav.initDone = true;
			
			// handle empty anchor, only if the user wants us to
			if (0 == hash.length) {
				if (initialPageLoad) {
					// the entire HTML page has just been loaded:
					// we should skip this 'hashchange'
					return ;
				} else {
					for (var i in $.anchornav.empty_anchor_handlers) {
						$.anchornav.empty_anchor_handlers[i].load(location.toString());
					}
					return ;
				}
			}

			// get namespace & URL
			var hash_parts = hash.split(':');
			var ns = hash_parts.shift();

			if (ns in $.anchornav.instances) {
				var url = hash_parts.join(':').replace(/^\:/, '');
				$.anchornav.instances[ns].load(url);
			}
		});

		$(window).bind('load', function() {
			$(window).trigger('hashchange');
		});
	}

};

// $.anchornav "class"
// @param object options
$.anchornav = function(options, jquery) {
	this.jquery = jquery;
	this.options = options;
	this.namespace = options.namespace;
	this.target = $(this.options.target);
	this.requests = [];

	// get current location's schema and host
	var schema_and_host = location.toString().match(/(^[a-z0-9]+\:\/\/[^\/]+)/i)[0];

	var self = this;

	var ajaxify = function() {
		if (this.anchornav) {
			return ;
		}
		this.anchornav = true;
		// trim the URL schema and host parts:
		// this is required for MSIE to have nicer and shorter URLs
		// (MSIE prepends relative - or starting with slash - URLs with schema and domain)
		this.href = '#' + self.options.namespace + ':' + this.getAttribute('href').replace(schema_and_host, '');
	}
	// change href in selected links
	this.jquery.each(ajaxify);
	// change href in selected links - in dynamically loaded content
	this.jquery.live('click', ajaxify);
}

$.anchornav.instances = {};
$.anchornav.empty_anchor_handlers = [];

// default options
$.anchornav.defaults = {
	target: '#content',
	loading_class: 'loading',
	loading_text: '<center><img style="border:0;" src="/images/ajax-loader.gif"></center>',
	active_class: 'anchornav-active',
	namespace: 'a',
	handle_empty_anchor: true
}

$.anchornav.initiated = false;
$.anchornav.initDone = false;

// instance methods
$.anchornav.prototype = {
	// load URL into selected target
	load: function(url) {

		url = url.replace(/#.*$/, '');

		// cancel pending requests
		while (this.requests.length) {
			this.requests.shift().abort();
		}

		// Hide any visible content.
		
		this.target.addClass(this.options.loading_class);
		this.target.html(this.options.loading_text);
		
		

		$('a.'+this.options.active_class).removeClass(this.options.active_class);
		$('a[href="#'+this.options.namespace+':'+url+'"]').addClass(this.options.active_class);

		var self = this;
		
if($.browser.msie){url = encodeURI(url);}

this.requests.push($.ajax({
			type: 'GET',



			url: url,
					
			
			success: function(html) {
				self.target.removeClass(self.options.loading_class);
				self.target.html(html);
			},
			cache: false
		}));
	}
};
