/* Loader functions for the wychwood brewery website */

var postload;

var time, link, animTime, buffer, current_page, requested_page, requested_beer, header, footer, scene, old_scene, title, iefix, animProgress = true, signsup = false;

old_scene = '';

$(document).ready(function() {

	iefix = ( $.browser.msie );

	// Navigation loader and animation
	$('#navigation a:not(.external), .getbyajax').live('click', loader);

	current_page = window.location.href;

	// Deeplink the page
	if( iefix )
	{
		$('#ihistory').contents().find('#location').html(window.location.hash);
		$('#ihistory').attr('src', site_url + 'ihistory.html?hash=' + encodeURI(window.location.hash.replace('#', '')));
	} else {
		// hash.deeplink();
	}

	setTimeout(hash.poll, 1000);

});

var hash = new function() {
	
	var old, _wh;
	
  this.update = function(ch) {
		if( window.location.hash.replace('#', '') != 'noload' ) {
			hash.deeplink();
		}
	}

	this.poll = function() {
		
		// Work around for ie bug breaking everything
		if( iefix )
		{
			_wh = $('#ihistory').contents().find('#location').html();
		}
		else
		{
			_wh = window.location.hash;
		}
		
		try {
			_wh = _wh.replace('#', '');

			if( _wh != '' ) {
				if( old != _wh ) {

					if( iefix )
					{
						top.location.hash = _wh;
						$('#ihistory').attr('src', site_url + 'ihistory.html?hash=' + encodeURI(_wh));
						hash.update();
					} else {
						hash.update();
					}

				}
				old = _wh;

			}

		} catch(err) { }
		
		setTimeout(hash.poll, 500);

	}
	
	this.deeplink = function() {

		// Check if we need to deeplink
		if( iefix )
		{
			var options = $('#ihistory').contents().find('#location').html().split('#');
		}
		else {
			var options = window.location.hash.split('#');
		}

		options = options[options.length-1].split('//');

		var key,sceneanim,request_href;

		if(options instanceof Array) {
			sceneanim = options[0].replace('/', '');
			request_href = '/' + options[1];
		} else {
			sceneanim = 'home';
			request_href = '';
		}
		if( request_href != undefined ) {
			load_scene(sceneanim, site_url + request_href);
		}
	}
	
}


function loader(event) {

	try {
		if( signsup ) {
			header.signsDown();
			signsup = false;
		}
	} catch(err) { }

	event.preventDefault();

	if( ( ! iefix ) && ( $.cookie('lite') == 'true' ) && ( event.currentTarget.href.search('fbpage') != -1 ) ) {
		window.location = event.currentTarget.href + '#noload';
		return;
	}

	if( iefix )
	{
		if( ( $.browser.msie && $.browser.version < 7 ) ) {
			if( event.currentTarget.href.search('fbpage') != -1 ) {
				window.location = event.currentTarget.href + '#noload';
				return;
			}
			$('#ihistory').contents().find('#location').html(event.currentTarget.rel);
		} else {
			$('#ihistory').contents().find('#location').html(event.currentTarget.rel);
		}
	}
	else
	{
		window.location.hash = event.currentTarget.rel;
	}

}

function load_scene(sceneanim, request_href) {

	requested_page = request_href;

	if(sceneanim == 'noanimate') {
		scene = old_scene;
	} else {
		scene = sceneanim;
	}

	if( current_page != requested_page ) {
		$('.sub_nav').each(function() {
			if($(this).is(':visible')) {
				$(this).hide('slide', {direction: 'up'}, 'slow');
			}
		});

		animProgress = true;

		animTime = 500;

		$('#ajax_loader').show();

		wychwood_nav.hide();

		// Setup the external interface for the header and footer flash
		footer = swfobject.getObjectById('flash_footer_object');
		header = swfobject.getObjectById('flash_header_object');

		$('#flash').replaceWith('<div id="flash" style="display: none"></div>');

		$('#flash-book-holder').html('');

		// Load the content into a buffer div
		// Get time stamp to force the page not to cache
		time = new Date().getTime();

		$('#buffer').hide();

		$('#flash_container').fadeOut();
		$('#flash-book-holder').animate({ opacity: 0 }, animTime, 'jswing');
		$('#flashbook-navigation-bg').animate({ opacity: 0 }, animTime, 'jswing');
		
		var	page = $(this).attr('rel');

		// Play the header admination outro
		try {
			header.outroAnim(old_scene, scene);
		} catch(err) {
		}
		
		$.ajax({
			url: request_href,
			type: 'GET',
			dataType: 'html',
			success: function(data) {

				$('#buffer').html(data);

				// Hide any elements that should not be displayed
				$('#buffer .onLoadHide').css('display', 'none');

				$('#buffer').prepend('<div id="loadbuffer" class="isloaded"></div>');

				buffer = $('#buffer').html();

			},
			error: function() {
				alert('Error loading');
				$('#ajax_loader').hide();
			},
			complete: function() {
				animationDone();

				_gaq.push(['_trackPageview', request_href.replace(site_url, '')]);

				// Update the location has
			}
		});

		current_page = request_href;
	}
}

function animationDone() {
	
	if( animProgress && ($('#loadbuffer.isloaded').length > 0) ) {
		
		animProgress = false;

		var loadHeight = $('#buffer').height() > $('#dynamic_content').height(); 

		try {
			if(loadHeight) {
				footer.selectPage('down');
				header.cogsDown();
			} else {
				footer.selectPage('up');
				header.cogsUp();
			}
		} catch(err) {
		}

		$('#dynamic_content').animate({ height: $('#buffer').height() }, { duration: animTime, queue: false } );

		$('#buffer').html('');

		$('#dynamic_content, #dynamic_content .iefadefix').animate({ opacity: 0 }, animTime, 'jswing', function() {

			// Fix IE not inheriting Opacity
			$('#buffer .iefadefix').css('opacity', 0);

			// $('#content, #dynamic_content').css('overflow', 'visible');
			// 
			// Load the contents of the load buffer
			$('#dynamic_content').html(buffer);

			if($.browser.msie) {
				$('#dynamic_content, #dynamic_content .iefadefix').animate({ opacity: '' }, { duration: animTime, queue: false });
			}
			else {
				$('#dynamic_content, #dynamic_content .iefadefix').animate({ opacity: 1 }, { duration: animTime, queue: false });
			}

			if($('#beers.loaded, #history.loaded, #legends.loaded').length>0) {
				$('#flash_container').fadeIn();
			}

			// Run post load events then clear
			postload.run().unload();

			if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
				$('#dynamic_content .ifixpng, #dynamic_content .ifixpng img, .fixPng').ifixpng();
			}

			old_scene = scene;

			setTimeout(function(){try {
				if(scene == 'home')
				{
					header.signsUp();
					signsup = true;
				}
			} catch(err) { }
			}, 3000);

			wychwood_nav.show();

			document.title = "Wychwood Brewery";

			$('#ajax_loader').hide();

			// Play the header admination from old page to new page
			try {
				header.selectPage(scene);
			} catch(err) {
			}

		});
	}

}


