/*
	FRONTEND DESIGN WEBSITE SCRIPT
	© Frontend Design 2011
	
	For you developers deconstructing our code.. 
	here are the library & plugins we have used:
		jQuery			- http://jquery.com/
		bxSlider 		- http://bxslider.com/
		Easing			- http://gsgd.co.uk/sandbox/jquery/easing/
		Tweet				- http://tweet.seaofclouds.com/
		FX					- http://fx.inetcat.com/
		hashChange 		- http://benalman.com/projects/jquery-hashchange-plugin/ 
		
*/

var removeKingfisher = false;

// INITALIZE BLOCK
$(function () {
	// Remove no-javascript hide class
	$('.noScriptMessage').remove();
	$('.noScript').removeClass('noScript');
	// Apply active state to correct nav item
	if($('#this-page-is').length)
	{
		var pageNav = $('#this-page-is').attr('class').split('-')[0];
		$('#nav-' + pageNav).addClass('active');
	}
	// Initiate twitter badge in footer
	$('#footer .tweet').tweet({
		username: "frontendbunker",
		avatar_size: 0,
		count: 1,
		loading_text: ""//"Gathering tweetiful tweetables..."
	}); 
	$(window).load(function () {
		var tweetAuthor = $('.tweet_text').text().split(': ')[0];
		var profileID = 2;
				
		switch(tweetAuthor) { // optimisation: switch statement is slightly faster than series of if elses
		case "J":
		case "j":
			profileID = 4;
			break;	
		case "C":
		case "c":
			profileID = 3;
			break;
		case "K":
		case "k":
			profileID = 5;
			break;		
		case "D":
		case "d":
			profileID = 6;
			break;	
		case "S":
		case "s":
			profileID = 2;
			break;
		case "Q":
		case "q":
			profileID = 2;
			break;
		default:
			tweetAuthor = 'default';
			break;
		}
		
		var tweetAvatar = "<a href='/team.html#"+ profileID + "'><img src=\"/i/team/twitter/twitter-" + tweetAuthor.toLowerCase() + ".jpg\" alt=\"Twitter user avatar\" /></a>"
		$('div.twitterUser').html(tweetAvatar);
		$('div.twitterUser a').addClass('profile-'+profileID);
			
		$('div.twitterUser a, #flickr-container a').hover(function () {
			$(this).find('img').stop().animate({'opacity' : 0.8}, 100);
		},
		function () {
			$(this).find('img').stop().animate({'opacity' : 1}, 100);
		});
	});
	
	/*var	firstPush = $('.thePush .client').first(),
			secondPush = $('.thePush .client').last(),
			firstPushText = $(firstPush).find('.quote').text(),
			secondPushText = $(secondPush).find('.quote').text();
	if(firstPushText === secondPushText)
	{
		$(secondPush).find('.quote').text('\"Smell the roses.\"');
		$(secondPush).find('.author').text('Frontend Design | Simon Dickey');
	}*/
	
	$('#flickr-container a').attr('target', '_blank');
	setTimeout(function() {
		$('#twitter-container a').attr('target', '_blank');
	}, 1000);
	
	// TEMPORARY - hide kingfisher if on mobile browser
	if( navigator.userAgent.match(/Android/i) ||
		navigator.userAgent.match(/webOS/i) ||
		navigator.userAgent.match(/iPhone/i) ||
		navigator.userAgent.match(/iPod/i) ||
		navigator.userAgent.match(/iPad/i)
		){
			$('#kingfisher, #kingfisher-image-file').remove();
			removeKingfisher = true;
		}

});

// SHOWCASE LANDING PAGE
function showcaseHovers() {
	$('.pushHover').css({'opacity' : 0});
	$('#push-chain-up a').hover(function () {
		$(this).find('.pushHover').stop().css({'display' : 'block', 'top' : -86}).animate({'opacity' : 1, 'top' : -98}, 150);
	},
	function () {
		$(this).find('.pushHover').stop().animate({'opacity' : 0, 'top' : -86}, 150, function () {
			$(this).css({'display' : 'none'});
		});
	});
	$('#push-chain-down a').hover(function () {
		$(this).find('.pushHover').stop().css({'display' : 'block', 'top' : -2}).animate({'opacity' : 1, 'top' : -12}, 150);
	},
	function () {
		$(this).find('.pushHover').stop().animate({'opacity' : 0, 'top' : -2}, 150, function () {
			$(this).css({'display' : 'none'});
		});
	});
}

// BLOG PAGE
function setBlogAvatars() {
	$('div.post').each(function () {
		var avatar = $(this).find('div.postAvatar img');
		var author = $(this).find('span.postAuthor').text().split(' ')[0];
		
		$(avatar).attr('src', '/i/blog/avatars/blog-avatar-' + author + '.png');
	});
}

// KINGFISHER LOAD
function loadKingfisher() {
	$('#kingfisher-image-file').load(function () {
		if(removeKingfisher === false)
		{
			$.getScript('/j/frontend-kingfisher.js');
		}
	});
}

/* SELECTIVE SCRIPT LOADERS */
$(function () {

	var page = $('#this-page-is');

	// HOME
	if ($(page).hasClass('home-page'))
	{
		loadKingfisher();
	}
	// ABOUT
	else if ($(page).hasClass('about-page'))
	{
		$.getScript('/j/frontend-about-slider.js');
		loadKingfisher();
	}
	// TEAM
	else if ($(page).hasClass('team-page'))
	{
		$.getScript('/j/jquery.ba-hashchange.min.js', function () {
			$.getScript('/j/frontend-dock.js');
		});
	}
	// PROCESS
	else if ($(page).hasClass('process-page'))
	{
		$.getScript('/j/frontend-stack.js');
		loadKingfisher();
	}
	// EXPERTISE
	else if ($(page).hasClass('expertise-page'))
	{
		$.getScript('/j/frontend-expertise.js');
		loadKingfisher();
	}
	// SHOWCASE LANDING
	else if ($(page).hasClass('showcase-landing-page'))
	{
		showcaseHovers();
		loadKingfisher();
	}
	// SHOWCASE DETAIL
	else if ($(page).hasClass('showcase-detail-page'))
	{
		$.getScript('/j/jquery.ba-hashchange.min.js', function () {
			$.getScript('/j/frontend-dock.js');
		});
	}
	// BLOG
	else if ($(page).hasClass('blog-page'))
	{
		setBlogAvatars();
	}
	//CONTACT
	/*else if ($(page).hasClass('contact-page'))
	{		
		
	}*/

});
