// =========== 
// ! On Load   
// =========== 

$(document).ready(function(){
	/* initialize global actions */
	Loader.register('', function(){ GlobalActions.init(); });
	
	//register front page loader
	Loader.register('/$', function(){ FrontPageActions.init(); })
	
	//register p3 loader
	Loader.register('/p3/', function(){ P3Actions.init(); });

	//trigger the necessary loaders
	Loader.init();
});

// ================== 
// ! Global Actions   
// ================== 

var GlobalActions = {
	init: function() {

	}
};

// ========================= 
// ! Page Action Variables   
// ========================= 

//front page
var FrontPageActions = {
	init: function() {
		this.bindCycler();
	},
	
	bindCycler: function() {

	}
};

//p3 page
var P3Actions = {
	init: function() {
		this.bindLightbox();
	},
	
	bindLightbox: function() {
		
		/* Alt Images */
		$('#p3_altimages > div > a, #p3_nutrition').lightbox();
	}
};



// ========================== 
// ! Misc Functions/Objects   
// ========================== 

//modal framework
var Modal = {
	shade: function(state, callback) {
		var shade = $('#modal_shade');
		
		//if we're asked to show the shade, and the shade isn't already present
		if (state && shade.css('display') != 'block') {
			$('#modal_shade').remove();
			
			if (is_ie6()) $('select').css('visibility', 'hidden');
			
			$('<div id="modal_shade"></div>')
				.css({
					'position'		: 'absolute',
					'left'			: '0',
					'top'			: '0',
					'width'			: '100%',
					'height'		: $(document).height(),
					'opacity'		: '0',
					'z-index'		: '1000',
					'background'	: 'black'				
				})
				.appendTo('body')
				.animate({ opacity: 0.85 }, 'slow', callback);
			
			return $('#modal_shade');
		}
		
		//hide the shade if it doesn't already exist
		else if (!state && (shade.css('display') != 'none' || shade.length == 0)) {
			var doctored_callback = function() {
				$(this).remove();
				if (is_ie6()) $('select').css('visibility', 'visible');
				
				if (typeof callback == 'function') callback();
			};
			
			shade.animate({ opacity: 0 }, doctored_callback);
		}
	},
	
	load: function(url) {
		var shade = this.shade(true);
		
		$('<div id="modal_content" style="position: absolute;"></div>')
			.css({ visibility: 'hidden', 'opacity': 0 })
			.appendTo('body')
			.load(url, null, function(){
				if (is_ie6()) $(this).css('position', 'absolute');
				else $(this).css('position', 'fixed');
				
				$(this).css({
					'left'			: '50%',
					'top'			: $(window).height()/2,
					'background'	: 'white',
					'z-index'		: '1001',
					'visibility'	: 'visible',
					'margin-left'	: ($(this).width()/-2) -3,
					'margin-top'	: ($(this).height()/-2) -3,
					'border-width'	: '3px',
					'border-style'	: 'solid'
				})
			.animate({ opacity: 1 }, 'fast');				
			});
		
		//give the background shade click instructions:
		$(shade).click(function(){
			$('#modal_content').add(this).animate({ opacity: 0 }, function() { $(this).remove(); });
		});
	}
};

//everyone's favorite browser
function is_ie6() { return /MSIE 6/.test(window.navigator.userAgent); }

//logging tools
function log(obj) { if (window.console) window.console.log(obj); }
(function($) {$.fn.log = function() {return this.each(function(){ window.console.log(this); });}})(jQuery);

//loader
var Loader={registeredFunctions:{},register:function(a,c){if(!(a instanceof Array)){var a=[a]}for(var b in a){if(!a.hasOwnProperty(b)){continue}this.registeredFunctions[a[b]]=c}},escapeURL:function(a){var c=["/","."];for(var b in c){if(!c.hasOwnProperty(b)){continue}a=a.replace(new RegExp("\\"+c[b],"g"),"\\"+c[b])}return a},init:function(){for(var a in this.registeredFunctions){var b=new RegExp("^"+this.escapeURL(a));if(b.test(window.location.pathname)){this.registeredFunctions[a]()}}}};