/* Author: 

	four23, 2011 - four23.net
	
	MO FARAH jQ SCRIPTS
	www.mofarah.com
	
*/

// FUNCTIONS

$(document).ready(function()
{
	var isHome = 1;
	
	function trigger(data)
	{
  		var el = $('#sidebar .navigation').find('a[href$="' + data.id + '"]').get(0);
  		switchSection.call(el);  	
  	}
  	
	$.getJSON("http://twitter.com/statuses/user_timeline/Mo_Farah.json?callback=?", function(data)
	{
		var text = data[0].text.substr(0,91); 
		$(".tweet a").html( data[0].text );
	});
	
	// HIDE ASSETS
	$('#sidebar').hide();
	$('#twitter-footer-bg').hide();
	$('#twitter-footer').hide();
	$('#latest-news-bg').hide();
	$('#latest-news').hide();	
	$('#wrapper').hide();	
	$('.content-wrapper').hide();	
	$(".content-blocker").css("display", "block");
	$(".content-blocker").hide();	
	$('#bg').hide();
	$("#sig").css("display", "block");
	$('#sig').hide();	
	
	// PAGE READY
	// PRELOAD IMAGES BEFORE ANYTHING ELSE
  	$.preLoadImages(
  	[
  		'img/bgs/home.jpg',
  		'img/bgs/about.jpg',
		'img/bgs/news.jpg',
		'img/bgs/images.jpg',
		'img/bgs/videos.jpg',
		'img/bgs/performance.jpg',
		'img/bgs/career.jpg',
		'img/bgs/events.jpg',
		'img/bgs/team.jpg',
		'img/bgs/contact.jpg'
	],function()
	{
		// HIDE THE LOADER
		$('#loading').fadeOut('fast');
		//
		// IF IN SECTION
		if (window.location.hash)
		{			
			// INIT NAVIGATION
    		$('#sidebar').delay().show("slide", { direction: "left", easing: 'easeOutCubic' }, 650, function()
    		{
				var flashvars = {};
				var params = {};
				params.bgcolor = "#000000";
				var attributes = {};
				swfobject.embedSWF("swf/sponsors.swf", "sponsorAnim", "100px", "43px", "8", "swf/expressInstall.swf", flashvars, params, attributes);
					
				$('#sidebar .navigation').find('a').click(switchSection);
    			$('#twitter-footer-bg').fadeIn(550, 'easeOutCubic');
				$('#twitter-footer').fadeIn(550, 'easeOutCubic');
				//
				var isHome = 0;
				trigger({ id : window.location.hash.substr(1)});
				$('#wrapper').show();
			});
			
		}
		// ELSE
		else
		{
			// SHOW HOME
			$('#bg').attr("src", "img/bgs/home.jpg");
			//aspectRatio = $bg.width() / $bg.height();
   			resizeBg();
   			//
   			// FADE IN IMAGE
   			$('#bg').delay(300).fadeIn(650, 'easeInQuart');
    		$('#sig').delay(300).fadeIn(650, 'easeInQuart', function()
    		{
    			// INIT NAVIGATION
    			$('#sidebar').delay(300).show("slide", { direction: "left", easing: 'easeOutCubic' }, 650, function()
    			{
					$('#sidebar .navigation').find('a').click(switchSection);
    				
    				var flashvars = {};
					var params = {};
					params.bgcolor = "#000000";
					var attributes = {};
					swfobject.embedSWF("swf/sponsors.swf", "sponsorAnim", "100px", "43px", "8", "swf/expressInstall.swf", flashvars, params, attributes);
						
					$('#latest-news').show("slide", { direction: "right", easing: 'easeOutCubic' }, 650);
					$('#latest-news-bg').show("slide", { direction: "right", easing: 'easeOutCubic' }, 650, function()
					{
						$('#twitter-footer-bg').fadeIn(550, 'easeOutCubic');
						$('#twitter-footer').fadeIn(550, 'easeOutCubic');
					});				
				});				
			});			
		}
		
	});
	
	// SWITCH SECTION FUNCTIONS
	function switchSection()
	{
		if(isHome)
		{
			$('#wrapper').show();
		}
		else
		{
			var previousSection = currentSection;
		}
		var currentSection = $(this).attr("href").substr(1);
				
		$(this).parents('ul:first').find('a').removeClass('selected').end().end().addClass('selected');
		
		
		if(currentSection!="")
		{
			if(isHome)
			{
				$('#sig').fadeOut('fast');
				$('#latest-news').fadeOut('fast');
				$('#latest-news-bg').fadeOut('fast');
				
			}
			
			
			$('#bg').fadeOut(400, 'easeInOutCubic', function()
			{
				$('<img />').attr('src', 'img/bgs/'+currentSection+'.jpg').load(function()
				{
        			$('#bg').attr("src", "img/bgs/"+currentSection+".jpg");
        			aspectRatio = $bg.width() / $bg.height();
        			resizeBg();        	
        	
        			if(isHome)
					{
        				isHome = 0;
        				$('.content-wrapper').addClass(''+currentSection);
						$('.content-blocker.header').addClass(''+currentSection);
						$('.content-blocker.footer').addClass(''+currentSection);
						
        				$('.content-wrapper').show("slide", { direction: "left", easing: 'easeInOutCubic' }, 550, function()
						{
							$('#bg').fadeIn(750, 'easeInOutCubic');
							$(".content-blocker").show();
						
							// LOAD CONTENT
							$('#loading').show();
							$(".content").load('sections/'+currentSection+'.php', function()
							{
								$('#loading').hide();
								$('.content').show();
							});						
						
						});
					}
					else
					{
						$(".content-blocker").hide();
						$(".content").hide();
						
						$('.content-wrapper').hide("slide", { direction: "left", easing: 'easeInOutCubic' }, 550, function()
						{
							$('.content-wrapper').removeClass( $('.content-wrapper').attr('class').split(' ')[1] );
        					$('.content-blocker.header').removeClass( $('.content-blocker.header').attr('class').split(' ')[2] );
        					$('.content-blocker.footer').removeClass( $('.content-blocker.footer').attr('class').split(' ')[2] );
        					
							$('.content-wrapper').addClass(''+currentSection);
							$('.content-blocker.header').addClass(''+currentSection);
							$('.content-blocker.footer').addClass(''+currentSection);
							$('.content-wrapper').show("slide", { direction: "left", easing: 'easeInOutCubic' }, 550, function()
							{
								$('#bg').fadeIn(750, 'easeInOutCubic');
								$(".content-blocker").show();
						
								// LOAD CONTENT
								$('#loading').show();
								$(".content").load('sections/'+currentSection+'.php', function()
								{
									$('#loading').hide();
									$('.content').show();
								});						
						
							});
						});
					}
    			});
			});
		}
		else
		{
			isHome = 1;
			$('.content').hide();
			
			$('#sidebar .navigation').find('a').removeClass('selected');		
			$('#bg').attr("src", "img/bgs/home.jpg");
			$('#sig').show();
			$('.content-wrapper').removeClass( $('.content-wrapper').attr('class').split(' ')[1] );
        	$('.content-blocker.header').removeClass( $('.content-blocker.header').attr('class').split(' ')[2] );
        	$('.content-blocker.footer').removeClass( $('.content-blocker.footer').attr('class').split(' ')[2] );
        	
        	$(".content-blocker").hide();
        	$('.content-wrapper').hide();
        	$('#wrapper').hide();
        	
        	$('#latest-news').show("slide", { direction: "right", easing: 'easeOutCubic' }, 650);
			$('#latest-news-bg').show("slide", { direction: "right", easing: 'easeOutCubic' }, 650, function()
			{
					
			});
		}
	// END FUNCTION	
  }
  
  function showImage()
  {
  	document.getElementById('largeImage').innerHTML = '';

  	var currentImage = $(this).attr("href").substr(1);
  	$(this).parents('ul:first').find('a').removeClass('selected').end().end().addClass('selected');
  	
  	$('#largeImage').show();
  	$('#videoShadow').show();
  	
  	var htmlGallery = ['<img '];
  	htmlGallery.push('src="img/gallery/large/',currentImage,'.jpg" />');
    
  	document.getElementById('largeImage').innerHTML = htmlGallery.join('');
  	
  	var themenu = ['<img '];
  	themenu.push('src="img/numbers/',currentImage,'.gif" />');
    
  	document.getElementById('Image1').innerHTML = themenu.join('');

  	
  	//alert();
  	return false;
  }

	//
	// FULL BACKGROUND IMAGE FUNCTIONALITY
	var theWindow        = $(window),
     	$bg              = $("#bg"),
     	aspectRatio      = $bg.width() / $bg.height();
     	
	function resizeBg()
 	{
  		if ( (theWindow.width() / theWindow.height()) < aspectRatio )
    	{
    		$bg.removeClass().addClass('bgheight');
    	}
    	else
    	{
    		$bg.removeClass().addClass('bgwidth');
    	}
	}
      
  	theWindow.resize(function()
  	{
  		resizeBg();
  	}).trigger("resize");
  	
});

// PRELOAD IMAGES FUNCTION


(function ($)
{
	$.preLoadImages = function(imageList,callback)
	{
		var pic = [], i, total, loaded = 0;
		
		if (typeof imageList != 'undefined')
		{
			if ($.isArray(imageList))
			{
				total = imageList.length; // used later
				for (i=0; i < total; i++)
				{
					pic[i] = new Image();
					
					pic[i].onload = function()
					{
						loaded++; // should never hit a race condition due to JS's non-threaded nature
						if (loaded == total)
						{
							if ($.isFunction(callback))
							{
								callback();
							}
						}
					};
					
					pic[i].src = imageList[i];
				}
			}
			else
			{
				pic[0] = new Image();
				pic[0].onload = function()
				{
					if ($.isFunction(callback))
					{
						callback();
					}
				}
				pic[0].src = imageList;
			}
		}
	pic = undefined;
};
})(jQuery);


