/*
 * Plugin developed by Brendan Stennett of Mediaphase
 * brendan@mediaphase.com
 * 
 * http://mediaphase.com
 */
(function($, undefined) {
	
	$.fn.huff = function(url, args) {
		url = url.split('?')[0];
		return this.each(function() {
			
			var _this = $(this);
			
			var settings = {
				container : $("#content"),
				duration : 1000,
				changePage : function(page_url) {
					var c = settings.container;
					var d = settings.duration;
					var done = false;
					var response;

					function finish() {
						if (done) {
							window.title = $("head title", response).html();
							var inner_content = response.find(c.selector);
							c.html(inner_content.html()).attr('class', inner_content.attr('class'));
							var page = inner_content.attr('title').replace('-','_');
								
							c.fadeIn(d);
							hashLinks(c);
							if (ApplicationPages != undefined) {
								if (ApplicationPages != undefined && typeof ApplicationPages.application == 'function')
									ApplicationPages.application();
								if (typeof ApplicationPages[page] == 'function')
									ApplicationPages[page](response);
							}
						} else {
							done = true;
						}
					}
					
					if (c.is(":visible"))
						c.fadeOut(d, finish);
					else
						finish();
					$.get(page_url, function(r) {
						response = $(r);
						finish();
					});
				}
			};
			$.extend(settings, args);

			function getPageUrlFromHash() {
				return window.location.toString().replace("/#/",'/');
			}

			function getHashLink(page_url) {
				return "#/"+page_url.replace(url+'/','');
			}
			
			function linkClickHandler() {
				return;
				var href = $(this).data('href');
				if (_this.data('current_page') != href) {
					_this.data('current_page', href);
					settings.changePage(href);
				}
			}
			
			function hashLinks(container) {
				if (container == undefined)
					container = $("body");
				
				// setup page links
				$("a[rel='pagelink']", container).each(function() {
					var href = $(this).attr('href');
					if (href.charAt(href.length-1) != "/") href += "/";
					var hash = getHashLink(href);
					$(this).data('href', href).attr('href', hash).click(linkClickHandler);
				});
			}
			
			$(document).ready(function() {
				// select container selector
				settings.container = $(settings.container.selector);

				hashLinks();
			});
			
			// redirect to hash page if applicable
			(function() {
				if (!window.location.hash) {
					// navigate to page
					var hash = getHashLink(window.location.toString());
					if (hash == "#/") {		// home page, just add hash link
						window.location.hash = "#/";
					} else {				// other page, redirect to home page with hash
						window.location = url+'/'+hash;
					}
				}
				$(document).ready(function() {
					if (window.location.hash.toString() == "#/") {
						// fade in container div
						settings.container.fadeIn(settings.duration);
						if (ApplicationPages != undefined && typeof ApplicationPages.application == 'function') {
							ApplicationPages.application();
						}
					} else {
						var page_url = getPageUrlFromHash();
						settings.changePage(page_url);
					}
					$(window).hashchange(function() {
						var href = getPageUrlFromHash();
						if (_this.data('current_page') != href) {
							_this.data('current_page', href);
							settings.changePage(href);
						}
					});
				});
			})();

			// write style rule to hide container page
			document.write("<style type='text/css'>"+settings.container.selector+"{display:none;}</style>");
		});
	};
})(jQuery);

