function Ready_up(type){
	var self = this;
	this.root = $('#container');
	this.filling = self.root.find('.left.wide.column.deskman');
	this.loaderChoose = function() {
		self.jaxChoose();
		new Page_chooser(self.root.find('#header a'));
		new Navi_anim(self.root.find('#header a'));
		new Zeldajax(self.root.find('#header a'), self.filling.wrapInner('<div></div>'));
		new Donate_fade(self.root.find('.right.thin.column a img:first-child'));
	}	
	this.jaxChoose = function() {	
		//new Home_change(self.root.find('.home_header'));
	}
	if (type == 'jax') 
		this.jaxChoose();
	else 
		this.loaderChoose();
}

function Load_up() {
	
}
function Donate_fade(element) {
	if (!jQuery.support.boxModel)
		return false;
	var self = this;
	this.root = element;
	this.root.show();
	self.out = true;
	this.fade_loop = function() {
		if(self.out)
			self.root.fadeTo(4000,0.75, self.fade_loop, "easeOutBounce");
		else
			self.root.fadeTo(4000,0.1, self.fade_loop, "easeOutQuad");
		self.out = !self.out;
	}
	setTimeout(this.fade_loop, 2000);
}
function Page_chooser(element) {
	var self = this;
	this.root = element;
	this.choose_page = function() {
		if ($(this).attr('title') == 'Home')
			$(document.body).addClass('home');
		else
			$(document.body).removeAttr('class');
	}
	this.root.bind('click', this.choose_page);
}
function Home_change(element) {
	var self = this;
	this.root = element;
	if ($(document.body).hasClass('home'))
		self.root.slideDown(400, "easeInQuad").fadeTo(500, 1);
	else
		self.root.fadeTo(200, 0.01).slideUp(400, "easeInQuad");
}

function Navi_anim(element) {
	var self = this;
	this.root = element;
	this.root.siblings('.help').each(function() {
		$(this).clone(true).addClass('a_mouseenter dull').insertAfter(this);
	}).addClass('a_click dull');
	this.navi_help = null;
	this.navi = this.action_click = self.action_mouseenter = this.root.filter(function(i) { return  (this.href == window.location.hash)});
	
	
	this.set_active = function(e) {
		var me = $(this);
		self.navi_help = me.siblings('.help.a_' + e.type);
		if (e.type == 'click') {
			self.navi = self.action_click;
			if (me.parent().hasClass('main') && self.action_click != me) {
				me.stop(true);
				me.animate({
					color: '#ffffff'
				}, 350, "easeOutQuad");
			}
			if (self.action_click.parent().hasClass('main') && self.action_click.html() != me.html()) {
				self.action_click.stop(true);
				self.action_click.animate({
					color: '#468CC8'
				}, 350, "easeOutQuad");
			}
		}
		else
			self.navi = self.action_mouseenter;
		
		if (self.navi_help.hasClass('ready')) {
			self.navi_help.stop(true);
			self.navi_help.animate({
				width: me.outerWidth(),
				left: me.offset().left - me.parent().offset().left
			}, 350, "easeOutQuad");
		}
		else if (self.navi.siblings('.help.a_' + e.type).hasClass('ready')) {
			self.navi.siblings('.help.a_' + e.type).removeClass('ready').animate({
				top: -100
			}, 350, "easeInQuad", function() {
				$(this).addClass('dull');
			});
		}
		if (self.navi_help.hasClass('dull')) {
			self.navi_help.removeClass('dull').css({
				width: me.outerWidth(),
				left: me.offset().left - me.parent().offset().left
			}).animate({
				top: 0
			}, 350, "easeOutQuad", function() {
				$(this).addClass('ready');
			});
		}

		if (e.type == 'click') {
			self.action_click = me;
		}
		else
			self.action_mouseenter = me;
			
	}
	this.reset_hover = function(e) {
		if ($(this).children('.help.a_click').hasClass('dull')) {
			$(this).children('.help.a_mouseenter').removeClass('ready').animate({
				top: -100
			}, 350, "easeInQuad", function() {
				$(this).addClass('dull');
			});
		}
		else {
			$(this).children('.help.a_mouseenter').animate({
				width: self.action_click.outerWidth(),
				left: self.action_click.offset().left - self.action_click.parent().offset().left
			}, 350, "easeInQuad");
		}
	}
	this.root.bind('click', this.set_active);
	this.root.bind('mouseenter', this.set_active);
	this.root.parent().bind('mouseleave', this.reset_hover);
}

function Zeldajax(element, holder) {
	var self = this;
	this.root = element;
	this.basin = holder;
	
	for (var i = 0; i < this.root.length; i++)
		this.root.eq(i).attr('href', '#' + this.root.eq(i).attr('href'));

	this.history_hack = function() {
		if (document.location.hash != self.current)  
			if (document.location.hash)
				self.root.filter('a[href=' + document.location.hash + ']:first').click();
			else
				self.root.eq(5).click();
	}
	
	this.jaxify = function(e) {
		var me = $(this);
		me.jax_href = me.attr('href');
		if (me.jax_href.match('http://'))
			return true;
		else if (e.isDefaultPrevented())
			return false;
		e.preventDefault();
		
		self.current = document.location.hash = me.jax_href;
			
		$.ajax({
			dataType: 'text',
			type: 'GET',
			async: true,
			url: me.jax_href.replace('#', ''),
			beforeSend: function(XMLHttpRequest) {
				self.basin.stop(true);
				self.basin.queue(function() {
					var que_top = $(this);
					self.basin.children().fadeTo('500', 0, function() {que_top.dequeue();});
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				// typically only one of textStatus or errorThrown 
				// will have info
			},
			success: function(data, textStatus) {
				self.basin.queue(function() {
					self.basin.children().html(data);
					new Ready_up('jax');
					if (self.basin.find('img').length > 0) {
						var temps = $(this);
						temps.load_check = self.basin.find('img')
						temps.done_count = 0;
						for (var i = 0; i < temps.load_check.length; i++)
							temps.load_check.load( function() {
								if(++temps.done_count == temps.load_check.length)
									temps.dequeue();
							});	
					}
					else {
						$(this).dequeue();
					}
				});
			},
			complete: function(XMLHttpRequest, textStatus) {
				self.basin.queue(function() {
					$(this).dequeue();
					self.basin.animate({
						height: self.basin.children().outerHeight(true)
					}, 500, "easeOutQuad", function() {
						self.basin.children().fadeTo('500', 1);
					});
				});
			}
		});
	}
	this.root.bind('click', self.jaxify);
	window.setInterval(function() {self.history_hack()}, 150);
}

$(document).ready(function() {
		new Ready_up('load');
});
$(document).load(function() {
		new Load_up();
});