/*

jQuery.nav.seo.js
Ross

this version is used to make the site crawlable when js is not involved

This plugin takes in a selector for nav links and fills in a content area by ajaxing php files,
also stores them to avoid repeat ajax calls. Can hav several instances on class on page. Be sure not 

For main navs it can use the hash location to load the correct content on page load. Doesn't have this feature working hierarchically yet though

use:

$(something).nav({
	navLinkSelector		: ".navLink", //selector for links that this instance responds to
	contentAreaSelector	: "#content", //selector for content to be replaces
	defaultPage			: false, //do we have a default page to show
	changeHash			: false,  //if true plugin will have the hash change on click
	$loadingBar			: false, //jquery object gets shown and hidden between pages
	modifyURL			: false   // used to implement dynamic pages, must return object {href, data} where href is the modified href string and data is an object for post data
	ex.
	modifyURL	: function(reqObj){
						var href = reqObj.href, d = reqObj.data;
						//articles are pulled dynamically (implementation specific)
						hrefArr = href.split("/");
						if(hrefArr[1] === "article"){
							//articles are dynamic hrefArr
							href = hrefArr[0]+"/"+hrefArr[1]+".php";
							d.article_id = hrefArr[2];
											
						}
						return {href:href, data:d}
					}
	
});

in index.php - do something like this

<div id="content">
	<noscript>
		<?php
			$href = isset($_GET['route'])?$_GET['route']:"/index";
			//articles are pulled dynamically (implementation specific)
			$hrefArr = split($href, "/");
			
			//recreate dynamic page var assignment here			
										
			if(substr($href, -1) == "/"){
					$href .= "index.php";
			}else{
				$href .= ".php";
			}			
			echo $href;							
			include("_includes".$href);
		?>
	</noscript>
</div>	

*/

(function($) {
	//public functions
	var methods = {
		init : function(options) {
			var defaults = {
				navLinkSelector		: "a.navLink", //selector for links that this instance responds to
				contentAreaSelector	: "#content", //selector for content to be replaces
				defaultPage			: false, //do we have a default page to show
				changeHash			: false,  //only the main nav should change the hash for now
				modifyURL			: false,
				$loadingBar			: false,
				root				: "_includes"
			}, t, $t, thisVars;			
					
			thisVars = $.extend(defaults, options);
			thisVars.$content = $(thisVars.contentAreaSelector);
			thisVars.$pages = [];
			
			//attatch instance var			
			$.extend(this, thisVars);
			$t = $(this);
						
			//if a this is a direct link, go to that page right away
			if(window.location.hash.length > 1){
				methods.showPage.apply(this, [window.location.hash]);			
			//otherwise if there is a default page show that content
			}else if(this.defaultPage){
				//otherwise show index
				methods.showPage.apply(this, [this.defaultPage]);
			}
			
			//store this to attached and click ot nav links
			t = this;			
			checkUseHashChange.apply(t);
			
			//if the hash is suppose to change and the hashchange function is supported
			if(t.useHashChange){
				window.onhashchange = function(){					
					methods.showPage.apply(t, [window.location.hash]);
				};
				
				$(this.navLinkSelector).die("click").live("click", function(){
					try{
						var href = $(this).attr("href"),
						hrefArr = href.split("?route=");
						window.location.hash = hrefArr[hrefArr.length-1];					
					}catch(e){
						alert(e);
					}
					
					return false;
				});				
			}else{
				$(this.navLinkSelector).die("click").live("click", function(){
					var href = $(this).attr("href");
					href = href.replace("?route=", "#");		
					methods.showPage.apply(t, [href]);
					
					//change the hash maybe but don't return true because of seo bull
					if(t.changeHash){
						window.location.hash = href;
					}					
					return false;
				});			
			}
						
			return this;
		},
		clearPage : function(href){
			href = href.replace("#", "");
			try{
				delete this.$pages[href];
			}catch(e){}			
		},
		showPage : function(href, force, reqData){		
			//force forces the page to reload from ajax no matter what
			if(force === null || force === undefined){
				force = false;
			}
			if(reqData === null || reqData === undefined){
				reqData = {};
			}
			
			var t = this, hrefArr, hrefArr2, newHref;					
			
			//take everything brefore hash off
			if(href && href.length > 0){
				hrefArr = href.split("#");
				href = hrefArr[hrefArr.length-1];
			}else{
				href = "/";
			}
			newHref = href;
						
			//check for location change
			if(!force && newHref === currLocation){
				//no location change so return right here
				return;
			}
			currLocation = newHref;
			
			/* 
				Allow url modification to be defined for dynamic pages
			 */
			if(t.modifyURL){
				modifyResp = t.modifyURL({
					href: newHref,
					data: reqData
				});
				newHref	= modifyResp.href;
				reqData	= modifyResp.data;
			}
				
			//add php if filename is given, otherwise just show the slash			
			hrefArr2 = newHref.split("?");
			if(hrefArr2[0].search(".php") === -1 && hrefArr2[0].charAt(hrefArr2[0].length-1) !== '/'){
				hrefArr2[0] += ".php";
				newHref = hrefArr2.join("?");
			}
			
			//fade out content area, 
			t.$content.fadeOut(250, function(){
				//load new content
				if(force || t.$pages[href] === undefined){
					//show loader
					if(t.$loaderBar){
						t.$loaderBar.show();
					}
					
					$.ajax({
						url: t.root+newHref, 
						type: "POST",
						data: reqData,
						success: function(resp){
							//fade back in
							t.$pages[href] = resp;
							
							if(t.$loaderBar){
								t.$loaderBar.hide();
							}
							if(ie8){
								t.$content.html(resp).show();
							}else{
								t.$content.html(resp).fadeTo(250,1);
							}
						}
					});
				}else{
					//don't do fade in ie 8 or less					
					if(ie8){
						t.$content.html(t.$pages[href]).show();
					}else{
						t.$content.html(t.$pages[href]).fadeTo(250,1);
					}					
				}
				
			});
		},
		useHashChange : function(){
			return this.useHashChange;
		}
	},
	
	//Class Vars
	currLocation="", $loaderBar,
	ie8 = ($.browser.msie && $.browser.version < 9);
	function checkUseHashChange(){
		this.useHashChange = this.changeHash && ("onhashchange" in window);
		this.useHashChange = this.useHashChange && !($.browser.msie && $.browser.version < 8);
		return this.useHashChange;
	}		
	
	//Initializer
	$.fn.nav = function(method) {
		//Method Calling Login
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.');
		}
	};
})(jQuery);
