function popupform() {
	var windowname = 'pop';
	window.open('subscribe/', windowname, 'height=400,width=400,scrollbars=yes');
	return false;
}

Event.observe(window, 'load', initializePage, false);

var bookmark;
var siteURL		= "http://thesleepyjackson.com/";
var sections	= ["news","tours","about", "video", "downloads", "artwork", "shop", "contact", "email"];


//CREATE THE LINKS FOR THE AJAX CALLS
var AjaxLink = Class.create();
AjaxLink.prototype = {
	initialize: function(el) {
		this.url		= el.href;
		this.section= this.getSection();
		
		Event.observe(el, 'click', this.activate.bindAsEventListener(this), false);
		el.onclick = function(){return false;};
	},
	
	getSection: function() {
		//Loop through the allowable sections array to set the section
		var paths = this.url.split('/');
		var i = 0;		
		while(i < paths.length) {
			if (sections.include(paths[i])) {
				return paths[i];
				break;
			}
			i++;
		}
	},
	
	activate: function() {
		var hashPath = this.url.replace("www.", "");
		var hashPath = hashPath.replace(siteURL, "");
		var hashPath = hashPath.replace(".htm", "");
		bookmark.loadNextBookmark(hashPath);
	}
};


//CREATE HASHED BOOKMARKS FOR THE AJAX PAGES SO BACK/FORWARD BUTTONS WORK
var Bookmarker = Class.create();
Bookmarker.prototype = {
	initialize: function() {
		// add the listener to unFocus.History
		unFocus.History.addEventListener('historyChange', this.historyListener);

		// do initialization
		this.historyListener(unFocus.History.getCurrent());
	},
	
	// the sole public method to manipulate this application
	loadNextBookmark: function(hashPath) {
		unFocus.History.addHistory(hashPath);
	},
	
	// create history listener
	historyListener: function(hashPath) {
		if (hashPath) {
			loadSection(hashPath+".htm");
		}
	}
};


// AJAX PAGE LOADING FUNCTIONS
function loadSection(hashPath) {	
	//If the nav exists then the headers have been loaded so pass a variable so they wont be loaded again	
	//Reset the navigation if the nav element exists
	if ( $('nav') ) {
		resetNavigation(hashPath);
		var pars = "includesLoaded=1";
	}
	new Ajax.Request( siteURL + hashPath, { method:'post', parameters:pars, onComplete: processAjax.bindAsEventListener(this) } );	
};

function resetNavigation(hashPath) {
	var section = hashPath.split(".")[0];
	
	var a = $('nav').getElementsByTagName("a");
	$A(a).each( function(el) {
		var cl = el.className;
		el.className = cl.replace(" selected", "");
		if (el.className == section) { el.className += " selected"; }
		//el.className = (el.parentNode.className == section) ? "selected" : "";
	});
};

function processAjax(request) {
	var response = request.responseText;
	
	//render the new content
	if ( div = $('content') ) {
		div.innerHTML = response;
		var a = div.getElementsByTagName("a");
	} else {
		var body = document.getElementsByTagName('body')[0];
		body.innerHTML = response;
		var a = body.getElementsByTagName("a");
	}

	//Initialize links within the new section	
	initializeLinks(a);

};

function initializeLinks(array) {
	$A(array).each( function(el) {
		if ((el.getAttribute("target") != "_blank")  && (el.getAttribute("onClick")==null)) {
			if (el.className == 'video') { 
      			el.onclick = function() {
        			window.open(siteURL+"pages/video.php", "video", "width=480, height=460, left=300, top=50, scrollbars=yes");
          			return false;
				}
       		}
			else {
				var aj = new AjaxLink(el);
				//console.debug("message", aj);
			}
		}
	});
};


// SITE INITIALIZATION
function initializePage() {

	// get all the links on the nav
	var nav_links = $('nav').getElementsByTagName("a");
	
	// initialize the links for ajax requests
	initializeLinks(nav_links);
	
	// set up the ajax hash bookmarking
	bookmark = new Bookmarker();
};

