/**
* content loader javascript file
* 
* @author michael morrissey
* @
* 
*/
var original_location = '';

window.dhtmlHistory.create( {
		//debugMode: true,
		 
		 toJSON: function(o) {
			return $.toJSON(o);
		},
		fromJSON: function(s) {
			return $.evalJSON(s)
		}
		
	});
	
$(document).ready(function() {
	
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(cprHistoryListener);
	
	if (dhtmlHistory.isFirstLoad())
	{
		if(loadedContentFromStart)
		{
			if(window.location.hash == "")
			{
				//dhtmlHistory.add(loadedAction+"|"+loadedArticle, loadedArticle);
				//grab the elements from the url
				original_location = window.location.pathname;
				
			}
			else
			{
				
				var temp = window.location.hash.split("|");
		
				var action = temp[0];
				var keyword = temp[1];
				action = action.replace("#", "");
				
				if ( action == "load_article" || action == "load_tag_page" || action == "load_category_page")
				{
					loadContentAction(action, keyword);
				}
				if( action == "load_profile")
				{
					$("#profileContentContainer").load("/lib/profile/"+keyword+".php");
					
				}
			}
		}
		else
		{
			
		}
	}
	if (window.location.hash != "" && window.location.hash != undefined && window.location.hash != null)
	{
		//alert('first load with ajax content to go to');
		var temp_replace = window.location.hash.replace("%7C", "|");
		var temp = temp_replace.split("|");
		
		var action = temp[0];
		var keyword = temp[1];
		
		action = action.replace("#", "");
		//alert(action);
		//alert(keyword);
		if ( action == "load_article" || action == "load_tag_page" || action == "load_category_page")
		{
			loadContentAction(action, keyword);
			
		}	
		if( action == "load_profile")
		{	
		$("#profileContentContainer").load("/lib/profile/"+keyword+".php");
		
		}
	}
});
//setup the listener for the historyChange event


function loadProfile(type, keyword)
{
	$("#profileContentContainer").load("/lib/profile/"+keyword+".php");
	
	dhtmlHistory.add("load_profile|"+keyword, keyword);
	$("#myProfileTabHeader").show();
	$('#profileLeftNav').removeClass('navOff');
	$("#tabs").tabs("select", "#myProfileTab"); 
	return false;
}

function loadContentAction(type, keyword)
{
	//determine the type of action
	var action = "";
	switch (type)
	{
		case "article":
			action = "load_article";
			break;
			
		case "tag":
			action = "load_tag_page";
			break;
			
		case "category":
			action = "load_category_page";
			break;
		

		default:
			action = type;
			break;
	}
	
	$.post(
		"/lib/content/content_gateway.php",
		{
			action : action,
			page : keyword
		},
		function(data, textStatus) {
			
			switch(textStatus)
			{	
				case "timeout":
				
					break;
					
				case "error":
					
					break;
					
				case "notmodified":
				
					break;
					
				case "success":
					
					if($("#site_error_content").length)
					{
						$("#site_error_content").remove();
					}
					if($("#contentWrapper").length == 0)
					{
						
						$("#contentTabWrapper").append(data);
						$("#contentTabHeader").show();
						
						
					}
					else
					{
						$("#contentTabHeader").show();
						$("#contentWrapper").fadeOut(500).remove();
						$("#contentTabWrapper").append(data);
					}
					
					$("#tabs").tabs('select', '#contentTab');
					
					
					break;
					
				case "parseerror":
				
					break;
					
				default:
				
					break;
			}
		},
		"html"
	);	
}

/**
 * This function posts to the content_gateway and injects the result into the content tab
 * @param {String} type this is either article, tag, or category
 * @param {String} keyword this is the article custom_url, tagname, or category name
 */
function loadContent(type, keyword)
{
	//determine the type of action
	var action = "";
	switch (type)
	{
		case "article":
			action = "load_article";
			break;
			
		case "tag":
			action = "load_tag_page";
			break;
			
		case "category":
			action = "load_category_page";
			break;
		

		default:
			action = type;
			break;
	}
	
	dhtmlHistory.add(action+"|"+keyword, keyword);
	if (BrowserDetect.browser != "Safari")
	{
		loadContentAction(type, keyword);
	}
	
}

function load_cms_article(e) {
	
}

function cprHistoryListener(newLocation, historyData) {
	//alert(newLocation);
	//alert('history');
	if (window.location.hash == "")
	{
		var tempsplit = original_location.split("/");
		var firstAction = tempsplit[1];
		var firstKeyword = tempsplit[2];
	}
	else
	{
		var temp = window.location.hash.split("|");
		
		var action = temp[0];
		var keyword = temp[1];

		action = action.replace("#", "");
		//alert(action);
		//alert(keyword);
		if (action == "load_article" || action == "load_tag_page" || action == "load_category_page")
		{
			loadContentAction(action, keyword);
		}
		
		if(action == "load_profile")
		{
			$("#profileContentContainer").load("/lib/profile/"+keyword+".php");
		}
	}
}
//DEBUG STUFF:

/*basic DHTML logging for our test page*/
function log(msg) {
	var logNode = document.getElementById("logWin");
	var content = "<p>" + msg + "</p>" + logNode.innerHTML;
	logNode.innerHTML = content;
}
function clearLog(msg) {
	var logNode = document.getElementById("logWin");
	logNode.innerHTML = "";
}
function printBrowserInfo() {
	//Print out browser details for QA
	log("<b>Begin browser dump</b>");
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.appCodeName: " + navigator.appCodeName);
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.appName: " + navigator.appCodeName);
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.appVersion: " + navigator.appVersion);
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.userAgent: " + navigator.userAgent);
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.platform: " + navigator.platform);
	log("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;navigator.vendor: " + navigator.vendor);
	log("<b>End browser dump</b>");
}
