(function($){

	Page = function(options){		
		$.extend(this, options);		
		return this;
	};
	
	Page.prototype = {
		
		// Constants
		LEFT  : 0,
		RIGHT : 1,

		// Properties
		animate    : true,
		callbacks  : {},
		current    : 0,
		direction  : null,
		loader     : {},
		transition : 'slideKnock',
		
		// Methods
		create : function (html)
		{
			var $page = $('<div class="wrapper" id="'+this.id
			             +'"><div class="page">'+html+'</div></div>');		
				
			$page.css({
				width: $window.width(),
				height: $window.height() - 220,
				opacity: 0
			});	

			$('#content').append($page);	
			if (this.callbacks.loaded) this.callbacks.loaded(this.loader.id, $page);

			this.swap();
		},
		
		load : function ()
		{
			this.direction = this.RIGHT;
			this.current   = this.loader.index;
			this.id        = this.loader.id;

			$.get(this.loader.url, $.proxy(this, 'create'));
		},
		
		remove : function ()
		{
			$('.wrapper').first().remove();			
		},
		
		swap : function ()
		{
			var _this = this;
			
			if (this.animate) {
					
				var duration_1 = 600;
				var duration_2 = 600;

				switch (this.transition) {

					case 'slideKnock':
						duration_1 = 1900;
						duration_2 = 1600;

					case 'slide':

						if (this.direction == Page.LEFT) {
							$('.wrapper').first()
							.animate({left: -$window.width()}, {
								queue: false,
								duration: duration_1,
								easing: 'easeInOutExpo',
								complete: $.proxy(this, 'remove')
							});
							$('.wrapper').eq(1)
							.css({opacity: 1, left: $window.width()})
							.animate({left: 0},{
								queue: false,
								duration: duration_2,
								easing: 'easeInOutExpo'
							});
						} else {
							$('.wrapper').first()
							.animate({left: $window.width()}, {
								queue: false,
								duration: duration_1,
								easing: 'easeInOutExpo',
								complete: $.proxy(this, 'remove')
							});
							$('.wrapper').eq(1)
							.css({opacity: 1, left: -$window.width()})
							.animate({left: 0},{
								queue: false,
								duration: duration_2,
								easing: 'easeInOutExpo'
							});
						}
						break;

					case 'fade':
					default:
						$('.wrapper').first()
						.animate({opacity: 0}, {
							queue: false, 
							duration: duration_1, 
							complete: function(){
								_this.remove();
								$('.wrapper').animate({opacity: 1},{
									queue: false,
									duration: duration_1
								});
							}
						});
						break;						
				}

			} else {				
				this.remove();
				$('.wrapper').css({opacity: 1});					
			}
		}
		
	};

})(jQuery);
