$(document).ready(function() {
	Page.height = ($(document).height() > $(window).height() ? $(document).height() : $(window).height()) - 55;
	
	$('#loading').css({ opacity: 0.99999, height: Page.height });
	$('#content').css({ height: Page.height });
	
	$('#nav li a').click(function() {
		if(Page.name != Page._getSection($(this).attr('href'))) {
			pageTracker._trackPageview($(this).attr('href'));
			Page.load($(this).attr('href'));
		}
		return false;
	});
	
	Page.num_replacements = $('.sifr').length;
	if(!window.location.hash.length) {
		if(!!Page.num_replacements) {
			Page._loadSifr();
		}
		Page._checkStatus();
	} else {
		//Page._ignoreCurrentAction = true;
		Page.load(window.location.hash.replace('#', ''));
	}
	
	$(window).resize(function() {
		Page.fixPosition();
	});
});

var Page = new function PageObject() {
	this.ready = false;
	this.sifr_loading = false;
	this.num_replaced = 0;
	this.num_replacements = 0;
	this.name;
	this._ignoreCurrentAction = false;
	this._blinds_closed = true;
	this._new_content = '';
	
	this.validateHash = function() {
		var _locationHash = window.location.hash;
		var _location = window.location.href.replace(/^http:\/\/(www\.)?/, '').replace(/#.*$/, '').replace(/\/$/, '');
		var _locationArray = _location.split('/');
		var _section;
		
		if(!!_locationHash) {
			_section = _locationHash.replace('#/', '').split('/');
			_section = _section.shift();
		} else {
			_section = _locationArray[0];
		}
		
		_section = !!_section ? _section : 'home';
		
		try {
			Page._config(_section);		
		} catch(err) {
			$(document).ready(function() {
				Page._config(_section);
			});
		}

		if(!!_locationArray[_locationArray.length - 1] && !!_locationHash) {
			window.location.href = '/' + _locationHash; //(_locationHash == '#' ? '' : _locationHash);
		}
	};
	
	this._config = function(section) {
		Page.name = !!section && !section.match('mway.me') ? section.toLowerCase() : 'home';
		
		$('#nav li a').each(function() {
			$(this).attr('class', $(this).attr('class').replace('-active', '-inactive'));
		});
		$('#nav_' + Page.name).attr('class', $('#nav_' + Page.name).attr('class').replace('-inactive', '-active'));
	};
	
	this._loadSifr = function() {
		if(!!Page.sifr_loading) { return false; }
		Page.sifr_loading = true;
		
		var estrangelo = {
			src: '/flash/estrangelo.swf',
			ratios: [7, 1.32, 11, 1.31, 13, 1.24, 14, 1.25, 19, 1.23, 27, 1.2, 34, 1.19, 42, 1.18, 47, 1.17, 48, 1.18, 69, 1.17, 74, 1.16, 75, 1.17, 1.16]
		};
		
		var styles = {
			'splash': {
				'.sIFR-root': {
					'color': '#cccccc',
					'letter-spacing': -1.0,
					'text-align': 'center',
					'font-size': '28px'
				},
				'a': { 'text-decoration': 'none' },
				'a:hover': {
					'color': '#ffffff',
					'text-decoration': 'underline'
				},
				'.green': { 'color': '#d9f629' },
				'.pastelblue': { 'color': '#8393ca' },
				'.seafoam': { 'color': '#6edeae' },
				'.orange': { 'color': '#ffa800' },
				'.spacer': {
					'color': '#262626',
					'font-size': '20px'
				},
				'.brightblue': { 'color': '#00aeef' },
				'.yellow': { 'color': '#fdf568' }
			}
		};
		
		var _locationArray = window.location.href.replace(/#$/, '').split('/');
		var _locationHash = window.location.hash.replace(/^#?\/?/, '');
		var current = !!_locationHash ?
			_locationHash :
			_locationArray.length > 1 && !!_locationArray[_locationArray.length - 1] ?
				_locationArray[_locationArray.length - 1] :
				'splash';
		
		sIFR.useStyleCheck = true;
		sIFR.activate(estrangelo);		
		sIFR.replace(estrangelo, {
			wmode: 'transparent',
			selector: '.sifr',
			color: '#ffffff',
			leading: 2,
			css: styles[current],
			onReplacement: function(fInt) {
				Page.num_replaced++;
				Page._checkStatus();
			}
		});
	};
	
	this._reset = function() {
		Page.num_replaced = 0;
		Page.num_replacements = 0;
		Page.sifr_loading = false;
		Page._blinds_closed = false;
		Page._new_content = '';
	};
	
	this._checkStatus = function() {
		if(Page.num_replacements == Page.num_replaced) {
			setTimeout('Page._loadComplete()', 250);
		}
	};
	
	this._loadStart = function() {
		Page._reset();
		//setTimeout("$('#loading .message').fadeIn(250)", 250);
		//$('#loading .message').fadeIn(250);
		setTimeout("$('#loading .message').fadeIn(250)", 50);
		$('#loading').stop().animate({ height: Page.height }, 500, 'easeOutQuart', function() {
			Page._blinds_closed = true;
		});
	};
	
	this._loadComplete = function() {
		Page.fixPosition();
		setTimeout("$('#loading .message').fadeOut(250)", 250);
		$('#loading').stop().css({ height: Page.height }).animate({ height: 0 }, 350, 'easeInQuart', function() {
			Page._blinds_closed = false;
		});
	};

	this.fixPosition = function() {
		if(Page.name == 'splash') {
			$('#splash').css({ top: "50%", marginTop: -(($('#splash').outerHeight() / 2) - 15) });
		}
	};
	
	this._getSection = function(uri) {
		var _section = !!uri ? uri.replace(/^\//, '').split('/').shift() : 'home';
		return !!_section ? _section : 'home';
	};
	
	this.load = function(uri) {
		var _section = Page._getSection(uri);
		
		pageTracker._trackPageview(uri);
		
		try {
			Page._config(_section);
		} catch(err) {
			$(document).ready(function() {
				Page._config(_section);
			});
		}

		Page._loadStart();
		$.ajax({
			url: /^\//.test(uri) ? uri : ('/' + (!!uri ? uri : '')),
			type: 'get',
			complete: function(response) {
				var num_replacements;
				uri = !!uri && !!uri.replace(/^\//, '') ? uri.replace(/^\//, '') : null;
				window.location.hash = ['#', uri].join(/^\//.test(uri) ? '' : '/');
				document.title = !!uri ? [uri, 'mway.me'].join(' | ') : 'mway.me';
				Page._new_content = $(response.responseText); //$('#content').html($(response.responseText));
				Page.insertContent();
				/*num_replacements = $('#content .sifr').length;
				if(!num_replacements) {
					setTimeout('Page._loadComplete()', 500);
				} else {
					Page.num_replacements = num_replacements;
					Page._loadSifr();
					Page._checkStatus();
				}
				Page.fixPosition();*/
			}
		});
	};
	
	this.insertContent = function() {
		if(Page._blinds_closed == true) {
			$('#content').html(Page._new_content);			
			num_replacements = $('#content .sifr').length;
			if(!num_replacements) {
				setTimeout('Page._loadComplete()', 500);
			} else {
				Page.num_replacements = num_replacements;
				Page._loadSifr();
				Page._checkStatus();
			}
			Page.fixPosition();
		} else {
			setTimeout('Page.insertContent()', 50);
		}
	};
};