$(function(e){	
	centerContainer();
	$(window.document).scrollTop(6);
	var Navigation = {
		pages : [],
		parentContainer : $('.content'),
		previousPage : null,
		currentPage : null,
		registerPage : function(page){
			this.pages.push(page);
			//Initial Page
			if (this.pages.length == 1) this.setInitialPage(page);
			this.parentContainer.append(page.domObj);
			this.setClickHandler(page);
		},
		setClickHandler: function(page){
			var me = this;
			page.anchor.click(function(){				
				if (me.currentPage != page){	
					me.previousPage = me.currentPage;
					me.currentPage = page;				
					$.ajax({
					  url: page.filePath,
					  success: function(data) {					
							//Exit Previous Page
							me.parentContainer.addClass('cover');
							me.previousPage.domObj.show();
							me.previousPage.domObj.animate({ 'left': me.previousPage.origPosX + 'px', 'top': me.previousPage.origPosY + 'px'}, 1000, 'linear', function(){	
							//Enter Currrent Page
								$('.page_title').text(me.currentPage.title);							
								me.previousPage.domObj.hide();
								me.currentPage.domObj.html(data);
								me.currentPage.domObj.show();
								me.currentPage.domObj.animate({ 'left': '0px', 'top': '0px'}, 1000, 'linear', function(){	
									me.parentContainer.removeClass('cover');
								});
							});
						}
					});
				}
			});
		},
		setInitialPage : function(page){
			var me = this;
			$.ajax({
			  url: page.filePath,
			  success: function(data) {
				  	me.currentPage = page;
				  	page.domObj.css({'left': '0px', 'top': '0px'}).show();	
					me.currentPage.domObj.html(data);
					$('.page_title').text(me.currentPage.title);					
				}
			});			
		},
		registerPreloader : function(preloader){
			
		},
		disableAnchors : function(){
				
		}
	}
	
	//Register pages - which ever page gets registerd first will be the initial page
	Navigation.registerPage(new Page('home.html', $('.logo'), 1500, 0, 'Home'));
	Navigation.registerPage(new Page('about_us.html', $('a.about_us_btn'), -1500, 1500, 'About Us'));
	Navigation.registerPage(new Page('for_hire.html', $('a.for_hire_btn'), -1500, -1500, 'For Hire'));	
	Navigation.registerPage(new Page('events.html', $('a.events_btn'), 0, -1500, 'Events'));	
	Navigation.registerPage(new Page('friends.html', $('a.friends_btn'), -1500, 0, 'Friends'));
	Navigation.registerPage(new Page('dancers.html', $('a.dancers_btn'), 1500, 1500, 'Dancers'));
	Navigation.registerPage(new Page('how_to_help.html', $('a.how_to_help_btn'), 1500, -1500, 'How to Help'));	
	Navigation.registerPage(new Page('contact.html', $('a.contact_btn'), 1500, -1500, 'Contact'));			
	Navigation.registerPage(new Page('media.html', $('a.media_btn'), 1500, 0, 'Media'));	
	Navigation.registerPage(new Page('classes.html', $('a.classes_btn'), 0, 1500, 'Classes'));		
});

var Page = function(filePath, anchor, posX, posY, title){
	this.origPosX;
	this.origPosY;
	this.filePath;
	this.anchor;
	this.title;
	this.domObj;
	this.id;
	this.hasLoaded = false;
	
	this.constructor = function(){
		this.filePath = filePath;
		this.anchor = anchor;
		this.origPosX = posX;
		this.origPosY = posY;
		if (title) this.title = title;
		
		this.buildPage();
	};
	
	this.buildPage = function(){
		this.id = title.replace(/\s/g,'_').toLowerCase();
		this.domObj = $('<div class="page" id="' + this.id + '">No Content!</div>');
		this.domObj.css({'top': this.origPosY,  'left': this.origPosX}).show();
	};
	
	this.constructor();
}

var centerContainer = function(){
	var body = $(this);
	var container = $('.container');
	container.css({'top' : body.height() / 2 - container.height() / 2 + 5 +'px', 'left' : body.width() / 2 - container.width() / 2 + 'px'}); 
};
