// First Lets Add DHTML History, Then Handle AJAX FUNCTIONS

//DHTML HISTORY
window.onload = initialize;

function initialize() {
	
	// initialize RSH
	dhtmlHistory.initialize();
	
	// add ourselves as a listener for history
	// change events
	dhtmlHistory.addListener(handleHistoryChange);
	var initialLocation = dhtmlHistory.getCurrentLocation();
	// if no location specified, use the default
	var defaultLocation = "Home:default:default";
	if (initialLocation == null || initialLocation == '') { 
		initialLocation = defaultLocation; 
		window.location.hash = initialLocation;
	}

	//var initialType = initialLocation.split(":")[0];
	//if(initialType != 'page') { updateUI(defaultLocation, null); }
	
	// now initialize our starting UI
	updateUI(initialLocation, null);
}

/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation, historyData) {
	if(newLocation == "") { newLocation = "Home:default:default"; }
	// use the history data to update our UI
	updateUI(newLocation, historyData);                           
}

/** A simple method that updates our user
    interface using the new location. */
function updateUI(newLocation, historyData) {
	var links = newLocation.split(":");
	var page = links[0];
	var blurb = links[1];
	var content = links[2];
	
	if(historyData == null || historyData == undefined) { historyData = new Object(); }
	if(historyData.type == null || historyData.type == '') { historyData.type = 'page'; }

	if(historyData.type == "page") {
		
		updateActivePage(page);
		setActiveStyleSheet( getTheme(page) );
		if(blurb == 'default') {
			narupdate('blurb'  , ajax_url,'page='+page+'&position=blurb&id=default');
		} else {
			narupdate('blurb'  , ajax_url,'id='+blurb);
		}
		if(content == 'default') {
			narupdate('content', ajax_url,'page='+page+'&position=content&id=default');
		} else if(content == 'calendar') {
			var month = links[3];
			var year = links[4];
			
			if(year == null || year == undefined) {
				// month = id
				narupdate('content' , calendar_url,'eventid='+month);
			} else {
				narupdate('content' , calendar_url,'month='+month+'&year='+year);
			}
		} else if(content == 'blog') {
			var action = links[3];
			var id = links[4];
			
			if(action == 'section') { action = 'sectionid'; }
			if(action == 'post') { action = 'postid'; }	
			
			if(action == 'archives') {
				narupdate('content' , blog_archives_url,'');
			} else {
				narupdate('content' , blog_main_url,action+'='+id);
			}
		} else {
			narupdate('content', ajax_url,'id='+content);
		}
		
	} else if( historyData.type == "blurb" ) {
		
		narupdate('blurb' , ajax_url,'id='+blurb);
		
	} else if( historyData.type == "content") {
			narupdate('content' , ajax_url,'id='+content);
	}
}

function updateActivePage(page) {
	//get rid of all other
	var actives = document.getElementsByClassName('active');
	for(i=0;i<actives.length;i++){
			actives[i].removeClassName('active');
	}
	var linky = $(page+'Link').addClassName('active');

}

function getTheme(page) {
	var theme;
	
	switch(page) {
		case "Home":
			theme = "green";
			break;
		case "Passion":
			theme = "blue";
			break;
		case "Color":
			theme = "purple";
			break;
		case "Inspiration":
			theme = "red";
			break;
		case "Knowledge":
			theme = "orange";
			break;
		default:
			theme = "green";
			break;	
	}
	
	return theme;
}

// AJAX HELPERS 

//constants...

var ajax_url 	 = 'include/tools/ajax.php';
var calendar_url = 'include/calendar/essentials.inc.php';
var blog_main_url = 'blog/index.php';
var blog_archives_url = 'blog/archives.php';


function changePage(page) {
		var obj = new Object();
		obj.type = 'page';
		dhtmlHistory.add(page+':default:default',obj);
		updateUI(page+':default:default',obj);
}

function changeBlurb(blurb) {
		var obj = new Object();
		obj.type = 'blurb';
		
		var loc = dhtmlHistory.getCurrentLocation().split(":");
		var page = loc[0];
		var content = loc[2];
		dhtmlHistory.add(page+':'+blurb+':'+content,obj);
		updateUI(page+':'+blurb+':'+content,obj);
}

function changeContent(content) {
		var obj = new Object();
		obj.type = 'content';
		
		var loc = dhtmlHistory.getCurrentLocation().split(":");
		var page = loc[0];
		var blurb = loc[1];
		dhtmlHistory.add(page+':'+blurb+':'+content,obj);
		updateUI(page+':'+blurb+':'+content,obj);
}

function getCalendar(month, year) {
		var obj = new Object();
		obj.type = 'page';
		
		var loc = dhtmlHistory.getCurrentLocation().split(":");
		var page = loc[0];
		var blurb = loc[1];
		
		if(month == null || month == undefined
		|| year == null || year == undefined ) {
			var d = new Date();
			month = d.getMonth() + 1;
			year = d.getFullYear();
		}
		dhtmlHistory.add(page+':'+blurb+':calendar:'+month+":"+year,obj);
		updateUI(page+':'+blurb+':calendar:'+month+":"+year,obj);
}

function getCalendarEvent(id) {
		var obj = new Object();
		obj.type = 'page';
		
		var loc = dhtmlHistory.getCurrentLocation().split(":");
		var page = loc[0];
		var blurb = loc[1];
		
		dhtmlHistory.add(page+':'+blurb+':calendar:'+id,obj);
		updateUI(page+':'+blurb+':calendar:'+id,obj);
}

function getBlog(action, id) {
		var obj = new Object();
		obj.type = 'page';
		
		var loc = dhtmlHistory.getCurrentLocation().split(":");
		var page = loc[0];
		var blurb = loc[1];
		
		if(action == null || id == null ) {
			action = 'home';
			id = 'default';
		}
		
		dhtmlHistory.add(page+':'+blurb+':blog:'+action+":"+id,obj);
		updateUI(page+':'+blurb+':blog:'+action+":"+id,obj);
}

function narupdate(div,url,params) {
	//set load
	$(div).update('<div style="text-align:center;padding:15px;"<img style="border:0;" src="/images/ajax-loader.gif" alt="loading..." /></div>');
	//then do request
	var ajax = new Ajax.Updater(
		{success: div},
		url,
		{method: 'get', parameters: params, evalScripts: true, onFailure: reportError});
}

function reportError(request) {
		$('message') = "Error: " + request;
}

function playFlashVideo(video, title) {
	//GB_showCenter(title,'/player/player.php?vid='+video, 280, 320);
	var url = '/player/player.php?vid='+video;
	video = window.open(url,"video", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=320,height=280");
}

var blogPage = '/include/tools/blog_comment.php';

function openBlogComment(blogID, postID, postTitle) {
	//GB_showCenter(postTitle,blogPage+'?bid='+blogID+'&pid='+postID, 300, 360, refreshMe);
	var url = blogPage+'?bid='+blogID+'&pid='+postID;
	comment = window.open(url,"comment", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=360,height=300");
}

function openBlogReplyComment(blogID, postID, postTitle, commentID) {
	//GB_showCenter(postTitle,blogPage+'?bid='+blogID+'&pid='+postID+'&cid='+commentID, 300, 360, refreshMe);
	var url = blogPage+'?bid='+blogID+'&pid='+postID+'&cid='+commentID;
	comment = window.open(url,"comment", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=360,height=300");
}

function refreshMe() {
	var obj = new Object();
	obj.type = 'page';
	var here = window.location.hash.substr(1,window.location.hash.length);
	updateUI(here, obj);
}