/* AJAX FUNCTIONS */

// alert('ajax.js loaded');


/* KNOWN ISSUES
// 
// HISTORY:
// No 'hash'/url... but would be nice. Further testing requered.
// Write your own history plugin... what I want:
// keep an array of request_urls.
// when clicking forward or back, set the request_url and then evaulate...
//

*/


// CURRENT VARIABLES
var this_category; 		
var this_section;
var this_article; 	
var current_stats;

// REQUEST VARIABLES
var request_url;
var request_scope;
var preserve_section;
var category;
var section;
var article; 
var directory; 
var ajax_click; // used to insert email addresses when ajax content runs

// NOTIFICATION VARIABLES
var ajax_action_notification;



// 	DOCUMENT READY *	*	*	*	*	*	*	*	*	*	*	*
$(document).ready(function(){
	
	
	// EXCLUDE IE6   
 	if(!window.XMLHttpRequest) return;
    else {
 	
 	load_stats();
 	initialize_links();
	hijax_clicks();
	
	}

	/* NOT CURRENTLY USED
	$('#navigation_link').live('click',function() {
		// alert('navigation link clicked');
		$(document).scrollTo('#site_nav',400);
	});
	*/
	
});



// LOAD STATS	*	*	*	*	*	*	*	*	*	*	*	*	
function load_stats() {

	this_category = $('#category').attr('category'); 
	category = this_category;
	this_section = $('#section').attr('section');
    section = this_section;
	this_article = $('#article').attr('article');
	article = this_article;

	load_stats_notification = 'LOAD STATS' +
		'\n Attribute stats: ' + this_category + ' | ' + this_section + ' | ' + this_article;

	if (notify_load_stats == true) alert(load_stats_notification);

}
	

// INITIALIZE LINKS	*	*	*	*	*	*	*	*	*	*	*	*	
function initialize_links() {
 	
 	/* SCRUB LINKS (remove dotted lines around links)
	$('a').livequery('focus',function() {
	  this.blur();
	});
	*/
		// NOTES:
		// seems backwards but necessary to prevent 'hand' flash when mousing over links that should be inactive.
		// disable all links first by adding class 'idle'
		// then determine if link should be enabled... if so
		// removeClass('idle') and addClass('active') 
		// similar to old code for adding 'hover' to none <a> links
	
	
	// ADD .nojax TO LINKS THAT ARE CHILDREN OF AN ELEMENT WITH .nojax
		// use .nojax for links that you don't want the ajax action to be preformened.
	$('.nojax').livequery(function() {
		$(this).find('a').addClass('nojax')
	});
	
   	// MAKE QUALIFIED LINKS IDLE (exclude .nojax & .disabled) 
	$('a:not(.nojax,.disabled,.lightbox,.zoom)').livequery(function() { $(this).addClass('idle'); }); 	// links not disabled
	$('.link:not(.disabled)').livequery(function() { $(this).addClass('idle'); }); 		// elements with .link class that aren't .disabled
	
	
	// QUALIFY IDLE LINKS TO ACTIVATE (exclude links to current content) 
	$('.idle').live('mouseover',function () {
						
		// DETERMINE preserve_section_link STATUS
		var preserve_section_link = false;
		if (( $(this).parents().hasClass('section.widget') == true ) || 
			( $(this).parents().hasClass('preserve') == true ) ||
			( $(this).hasClass('preserve') == true ))  {
	   		preserve_section_link = true;
   		}
		if (( $(this).parents().hasClass('dont.preserve') == true ) ||
			// ( $(this).hasClass('dont.preserve') == true ) ||
			// ( $(this).parents().hasClass('msg') == true ) ||
			// ( $(this).parent().attr('id') == 'msg' ) ||
			( $(this).parents().hasClass('.nav') == true ))  {
		    preserve_section_link = false;
		}

		// DEFINE link_href
		var link_href = $(this).attr('href');
			// special cases
			if (link_href == '/') {
				link_href = '/dashboard';
				preserve_section_link = false;
			}
			if (link_href.indexOf('info') == true) {
				link_href = '/dashboard' + link_href;
			    preserve_section_link = false;
			}
		
		// DEFINE content_url
		var content_url;
			// defaults
			var article_content = this_article;
				if (this_article == 'default') article_content = null;
			var section_content = this_section;
				if (this_section == 'default') section_content = null;
			var category_content = this_category;
				if (this_category == 'dashboard') category_content = null;
			// breakout	
			if (article_content != null) content_url = '/' + this_category + '/' + this_section + '/' + this_article;
			else if (section_content != null) content_url = '/' + this_category + '/' + this_section;
			else if (category_content != null) content_url = '/' + this_category;

				
		
		// NOTIFICATION TESTS
			// alert('link_href: ' + link_href + '\n preserve_section_link: ' + preserve_section_link + '\n this_section: ' + this_section);


		
		// CANCEL ACTION FOR DASHBOARD IF SELF   
		if ((link_href == '/dashboard') && (this_category == 'dashboard') && (this_section == 'default')) return;  // don't activate
		
		// ACTIVATE PRESERVE SECTION LINKS 
		if (preserve_section_link == true) {
			// compare article_content to link_href's article delimiter Ð> /category/section/article ; activate link if different
			if (article_content != link_href.split('/',5)[3]) {
				$(this).removeClass('idle').addClass('active');
			}
		}
		
		// ACTIVATE NORMAL LINKS 
		else if ((link_href != content_url) || (link_href == '/dashboard')) {
				$(this).removeClass('idle').addClass('active');
		}
		
	});

	
	// DEACTIVATE LINKS ON MOUSEOUT
	$('.active').live('mouseout',function () { $(this).addClass('idle').removeClass('active'); });

}



// HIJAX CLICKS	*	*	*	*	*	*	*	*	*	*	*	*	
function hijax_clicks() {

	$('.active').live('click',function(){
   			
		ajax_click = true;
		request_url = $(this).attr('href');
		
		// FIX IE7 AJAX BUG
			// when clicking a link in the sidebar to change article
			// IE7 adds the protocal and host to the request_url
		if ($.browser.msie == true) { 
			request_url = $(this).attr('href').replace('http://' + this_domain + '/','/');
			// Remove http://this_domain/ from the request_url (do not try to shorten!)
			// this_domain is defined on the control panel
			// alert('request_url =  ' + request_url);			
		}
		 
	
		// DETERMINE preserve_section STATUS
		preserve_section = false;   
		if (( $(this).parents().hasClass('section.widget') == true ) || 
			( $(this).parents().hasClass('preserve') == true ) ||
			( $(this).hasClass('preserve') == true ))  {
	   		preserve_section = true
   		}
		if (( $(this).parents().hasClass('dont.preserve') == true ) ||
			( $(this).hasClass('dont.preserve') == true ))  {
		    preserve_section = false;
		} 
	
		// CANCEL DEFAULT LINK BAHAVIOR AND RUN AJAX REQUEST
		ajax_request();
		
		return false;  
	});                               
	$('.idle, .disabled').live('click',function(){ return false; });
}




//	AJAX REQUEST 	*	*	*	*	*	*	*	*	*	*	*	* 
function ajax_request() {	
	
	// BREAKOUT VARIABLES
    url_parts = request_url.split('/',5);
	category = url_parts[1];  	// 	/crew	
	section = url_parts[2];		//	/crew/bartenders	
	article = url_parts[3];		//	/crew/bartenders/achilles		
	directory = url_parts[4];	//	/parties/gorgeous/pool_party/08 
		
		// special cases
		if (request_url == '/') category = 'dashboard';
		if (category == 'info') {
			// transopose to add dashboard
			if (section != null) article = section;
			section = 'info'
			category = 'dashboard'
			/* MINE REQUEST VARIABLES FROM URL
				if (/^(?:info|mission|faq|map|jobs)$/.test(category)) {}  
			*/
		}
	
	// SET request_scope
	if (directory != null) request_scope = 4;
	else if (article != null) request_scope = 3;
	else if (section != null) 	request_scope = 2;
	else if (category != null) request_scope = 1;
	else request_scope = 0;

	// SET DEFAULTS
	if (category == null) {
		category = 'dashboard';
		section = 'default';
		article = null;
		preserve_section = false;
	}
	else if (section == null) { 
		section = 'default';
		article = null;
	}
	else if (article == null) article = 'default';

	
	// RUN AJAX ACTION
	ajax_action();
	
}


//	AJAX ACTION 	*	*	*	*	*	*	*	*	*	*	*	* 
function ajax_action() {
	
	// alert('preserve_section = ' + preserve_section);

	
	// PRESERVE SECTION ACTIONS
	if (preserve_section == true) {
		
		// CHANGE CATEGORY (set preserve_section to false)
		if (request_scope == 1) {
			preserve_section = false;
		}
		
		// CHANGE ARTICLE; PRESERVE SECTION
		else if (article != this_article) {
			// notifications
			ajax_action_notification = 'CHANGE ARTICLE' +
				'\n Preserve Section: (' + preserve_section + ') ' + this_section + 
				'\n Load Article: ' + article + '\n request_scope: ' + request_scope;     
   			if (notify_ajax_stats == true) ajax_stats();
   			if (notify_ajax_action == true) alert('AJAX ACTION \n\n' + ajax_action_notification);
			// action 
			change_article();
		}
		
	}
	
	
	// NORMAL ACTIONS
	if (preserve_section == false) {
		
	   	// CHANGE CATEGORY
	   	if (category != this_category) {
			// notifications
			ajax_action_notification = 'CHANGE CATEGORY' +
				'\n Load Category: ' + category +
				'\n Load Section: ' + section + 
				'\n Load Article: ' + article + 
				'\n request_scope: ' + request_scope;  
   			if (notify_ajax_stats == true) ajax_stats();
   			if (notify_ajax_action == true) alert('AJAX ACTION \n\n' + ajax_action_notification);
			// action 
			change_category();
		}
		
		// RELOAD SECTION DEFAULT
		else if (section == this_section && article == 'default') {
			// notifications
			ajax_action_notification = 'RELOAD SECTION DEFAULT' +
				'\n(' + section + ')' +
				'\n request_scope: ' + request_scope;  
   			if (notify_ajax_stats == true) ajax_stats();
   			if (notify_ajax_action == true) alert('AJAX ACTION \n\n' + ajax_action_notification);
			// action 
			change_article();	
		}
		
		// CHANGE SECTION
		else if (section != this_section) {
			// notifications
			ajax_action_notification = 'CHANGE SECTION' +
				'\n Load Section: ' + section +
				'\n request_scope: ' + request_scope;  
   			if (notify_ajax_stats == true) ajax_stats();
   			if (notify_ajax_action == true) alert('AJAX ACTION \n\n' + ajax_action_notification);
			// action 
			change_section();		
		}
		
		// CHANGE ARTICLE
		else if (article != this_article) {
			// notifications
			ajax_action_notification = 'CHANGE ARTICLE' +
				'\n Load Article: ' + article +
				'\n request_scope: ' + request_scope;  
   			if (notify_ajax_stats == true) ajax_stats();
   			if (notify_ajax_action == true) alert('AJAX ACTION \n\n' + ajax_action_notification);
			// action 
			change_article();		
		}
		
		// NO ACTION NECESSARY
		else { 
			ajax_action_notification = 'no action necessary';
		}
		
	}	

}


	
//	AJAX STATS 	*	*	*	*	*	*	*	*	*	*	*	* 
function ajax_stats() {
	
	// CURRENT STATS	
	var current_stats = 'CURRENT STATS' +
					'\n Variable stats: ' + this_category + ' | ' + this_section + ' | ' + this_article +
					'\n Attribute stats: ' + $('#category').attr('category') + ' | ' + $('#section').attr('section') + ' | ' + $('#article').attr('article');
	
	// THIS REQUEST 
	var this_request;
		if (request_scope == 1 || request_scope == 2) this_request = '\n Section Request: ' + section;
		else this_request = '\n Article Request: ' + article;
	
	var preserve_section_for_request;
		if (preserve_section == true) preserve_section_for_request = preserve_section + ' (' + this_section + ')';
		else preserve_section_for_request = preserve_section; 
	
	// REQUEST STATS
	var request_stats = 'REQUEST STATS' +
					'\n Request url: ' + request_url + 
					'\n Request Scope: ' + request_scope +
					//'\n Preserve Section: ' + preserve_section + ' (' + this_section + ')' +
					'\n Preserve Section: ' + preserve_section_for_request +
					//'\n Article Request: ' + article;
					this_request;
					

	// *** ALERT AJAX STATS ***	
	alert('AJAX STATS \n\n' + current_stats + '\n\n' + request_stats); 
	
	return;
}
 


//	CHANGE CATEGORY ACTIONS	*	*	*	*	*	*	*	*	*	*	*	*	     
function change_category() {
	 
	if (notify_change_function == true) alert('change_category(); \n\n' + ajax_action_notification);
    
	// UNLOAD ANNIMATIONS
	$(document).scrollTo('top','300');
	$('#sidebar').hide('slide', { direction:'up' }, 400, function() {
		$(this).empty().show();
	});	
	
	$('#banner:visible').fadeOut('fast');     
	$('#strip:visible').fadeOut('fast');     
	
	$('#category').fadeOut(function() {
		$('#category').load('/content/' + category + '/' + category + '.php', function() {     
			change_section();
			refresh_category();
		});
	});
	
}                                        

function refresh_category() {
	
	$('#category').removeClass(this_category).addClass(category); 
	
	// RELOCATE ARTICLE RESOURCES
	$('#article_widget').html( $('#article .article.widget') );
	$('#strip').html( $('#article .strip') );  

	// SHOW CONTENT
	$('#category').fadeIn();
	$('#sidebar').show('slide', { direction:'up' }, 400);		
	
	return;
}
             


//	CHANGE SECTION ACTIONS	*	*	*	*	*	*	*	*	*	*	*	*	     
function change_section() {     
	 	
	if (notify_change_function == true) alert('change_section(); \n\n' + ajax_action_notification);
	
	// UNLOAD ANNIMATIONS
	$(document).scrollTo('top','300');
   	$('#sidebar').hide('slide', { direction:'up' }, 400);
	$('#banner:visible').fadeOut();     
	$('#strip:visible').fadeOut();     
	$('#section').fadeOut(function() {      
				
		// CHANGE SECTION
		switch(request_scope) {
			case 1: 
				// LOAD DEFAULT SECTION - /category/(default)
				$('#section')
					.load('/content/' + category + '/default/_' + category + '.php', function() {
						refresh_section();
						refresh_stats();
					});			
				break;
			case 2:
				// CHANGE SECTION (same category)
				$('#section')
					.load('/content/' + category + '/' + section + '/' + section + '.php', function() {
						refresh_section();
						refresh_stats();
					});
				break; 
			case 3:
				// CHANGE SECTION, THEN ARTICLE 
				$('#section')
					.load('/content/' + category + '/' + section + '/' + section + '.php', function() {
						$('#article').hide();
						change_article();
						refresh_section();
					});
				break;        
			default :
			    document.location.href = request_url;                             
		}
	});
	
}

function refresh_section() {
		
	$('#section').removeClass(this_section).addClass(section); 
	
	// RELOCATE ARTICLE RESOURCES
	$('#article_widget').html( $('#article .article.widget') );
	$('#strip').html( $('#article .strip') );
	$('#banner').html( $('#article .banner') );        

	// SHOW CONTENT
	$('#banner:has(.banner):hidden').fadeIn();
	$('#strip:has(.strip):hidden').fadeIn();
	$('#section').fadeIn();
	$('#sidebar').show('slide', { direction:'up' }, 400);

	
	
	return;
}



//	CHANGE ARTICLE ACTIONS	*	*	*	*	*	*	*	*	*	*	*	*	     
function change_article() { 
			
	if (notify_change_function == true) alert('change_article(); \n\n' + ajax_action_notification);
	
	// UNLOAD ANNIMATIONS
	if( $('#banner:visible').length > 0) {
		$(document).scrollTo('#section','300');
	} 
	else {
		$(document).scrollTo('top','300');
	}
    $('#strip:visible').fadeOut();    
	$('#article_widget').fadeOut();     
	$('#article').fadeOut(function() {      

		// CHANGE ARTICLE		
		switch(request_scope) {                        
			case 2:
			    // LOAD SECTION DEFAULT - /category/section/(default)
				$('#article').load('/content/' + category + '/' + section + '/' + section + '.php', function() { 
					refresh_article();
					refresh_stats();
				});    
			 	break;                                    
			case 3:                                       
			   	// CHANGE ARTICLE
				$('#article').load('/content/' + category + '/' + section + '/' +  article + '/' + article + '.php', function() {
					refresh_article(); 
					refresh_stats();
				});
				break;                                    
			case 4:
			    // CHANGE ARTICLE/DIRECTORY                                  
				$('#article').load('/category' + category + '/' + section + '/' + article + '/' +  directory + '/' + article + '.php', function() { 
					refresh_article();
					refresh_stats();
				});
				break;
   	    	default :
			    document.location.href = request_url;                             
	 	}
	
	});                     

}

function refresh_article() {
		
	$('#article').removeClass(this_article).addClass(article); 
	
	// RELOCATE ARTICLE RESOURCES
	$('#article_widget').html( $('#article .article.widget') );
	$('#strip').html( $('#article .strip') );

	// SHOW CONTENT
	$('#article').fadeIn();
	$('#article_widget:hidden').show('slide', { direction:'up' }, 400);
	$('#strip:has(.strip):hidden').fadeIn();
	
	
 
		  
 	return;
	
}	 



//	REFRESH STATS, HISTORY	*	*	*	*	*	*	*	*	*	*	*	*	     
function refresh_stats() {
		
	// REMOVE OLD CLASSES
	$('body').removeClass(this_section).removeClass(this_article);

	
	// REFRESH VARIABLE STATS
	if (preserve_section == false) {
		this_category = category;
		this_section = section;
	} 
	this_article = article;
	
	
	// REFRESH ATTRIBUTE STATS + CLASS STATS
	$('body').attr('id',this_category).addClass(this_section).addClass(this_article);
	$('#category').attr('category',this_category);
	$('#section').attr('section',this_section);
	$('#article').attr('article',this_article);

	
	// *** ALERT REFRESHED STATS ***	
	refreshed_stats = 'REFRESHED STATS' +
					'\n Variable stats: ' + this_category + ' | ' + this_section + ' | ' + this_article +
					'\n Attribute stats: ' + $('body').attr('id') + ' | ' + $('#section').attr('section') + ' | ' + $('#article').attr('article') +
					'\n\n Article url: ' + request_url;
	if (notify_refreshed_stats == true) alert(refreshed_stats);
	
	
	// SET HISTORY, BOOKMARKABLE URL
    // $.history(request_url);
	// alert(request_url);


}


// HISTORY BACK FUNCTION
/* $.history.callback = function ( reinstate, cursor ) {
	// function to handle the data coming back from the history upon forw/back hit
	// check to see if were back to the beginning without any stored data
	if (typeof(reinstate) != 'undefined')
	document.location.href = reinstate;
};*/   


// 	*	*	*	*	*	*	*	*	*	*	*	*

