document.getElementsByTagName('html')[0].className += 'js-active';

(function($){

/* !--- Details --- */
/* --------------------------------------------------------------------------------------------------------------

Global Javascript

 ---------------------------------------------------------------------------------------------------------------- */



/* !--- Document Load ---                                                                                         */
/* -------------------------------------------------------------------------------------------------------------- */

jQuery(function(){

	templateInit();
	
	var pageId = $('body').attr('id');
	
	switch( pageId ){
		case 'home':
			homepageInit();
			break;
		
		case 'bio':
			bioInit();
			break;
		
		case 'media':
			mediaInit();
			break;
		
		case 'press':
			pressInit();
			break;
		
		default:
	}
	
	
});


/* !--- Misc Functions ---                                                                                        */
/* -------------------------------------------------------------------------------------------------------------- */
function templateInit(){
	$('#nav-global, #footer').css({ position: 'fixed' });
	
	bindExternalLinks();
	bindWindowResize();
	bindWindowScroll();
	posterwallInit();
}

function homepageInit(){
	newsletterInit( $('#newsletter-signup') );
	
	// JW Player MP3 Player
	var flash_vars = {
		playlistfile: '/wp/wp-content/themes/thepineapplethief/mp3/playlist.xml',
		playlist: 'bottom',
		playlistsize: 0,
		backcolor: '000000',
		frontcolor: 'CCCCCC',
		lightcolor: '666666',
		id: 'jwplayer'
	}
	
	var flash_params = {
  		allowfullscreen: true,
  		allowscriptaccess: 'always',
  		wmode: 'opaque'
  	};
  	
  	var flash_attributes = {
  		id: 'jwplayer',
  		name: 'jwplayer'
  	}
	
	$('#jwplayer').wrap('<div id="jwplayer-wrap" />');
	$('#jwplayer-wrap').prepend('<div id="jwplayer-track" />');
	
	swfobject.embedSWF('/wp/wp-content/themes/thepineapplethief/assets/swf/jwplayer.swf', 'jwplayer', '250', '24', '9.0.0', false, flash_vars, flash_params);
}

function bioInit(){
	sectionNavInit();
}

function mediaInit(){
	prettyPhotoInit();
	sectionNavInit();
	
	// Youtube TV Playlist
	var expressInstall = '/wp/wp-content/themes/thepineapplethief/assets/swf/expressInstall.swf';
	
	var flash_params = { 
		wmode: 'transparent' 
	}
	
	swfobject.embedSWF('http://uk.youtube.com/cp/vjVQa1PpcFOgyzwbprOPZSD5f2rdygUVJKC2Pnx6Vqc=', 'pt-tv', '740', '476', '8.0.0', '', null, flash_params);
	
	
	// JW Player MP3 Player
	var flash_vars = {
		playlistfile: '/wp/wp-content/themes/thepineapplethief/mp3/playlist.xml',
		playlist: 'bottom',
		playlistsize: 300,
		backcolor: '000000',
		frontcolor: 'CCCCCC',
		lightcolor: '666666',
		id: 'jwplayer'
	}
	
	var flash_params = {
  		allowfullscreen: true,
  		allowscriptaccess: 'always',
  		wmode: 'opaque'
  	};
  	
  	var flash_attributes = {
  		id: 'jwplayer',
  		name: 'jwplayer'
  	}
	
	swfobject.embedSWF('/wp/wp-content/themes/thepineapplethief/assets/swf/jwplayer.swf', 'jwplayer', '740', '324', '9.0.0', expressInstall, flash_vars, flash_params);
}

function pressInit(){
	prettyPhotoInit();
	sectionNavInit();
}

function prettyPhotoInit(){
	// Pretty Photo
	var pp_settings = {
		animationSpeed: 'fast',
		allowresize: true,
		theme: 'dark_square',
		image_markup: '<img id="fullResImage" src="">'
	}
	
	$("a[rel^='prettyPhoto']").prettyPhoto( pp_settings );
}

function sectionNavInit(){
	var sections = $('#content .section');
	var sectionLinks = $('#section-nav a');
	
	var currentSection;
	var currentLink = sectionLinks.filter('.current');
	
	if( !currentLink.length ){
		currentLink = $(sectionLinks[0]);
		currentLink.addClass('current');
		currentSection = $(sections[0]);
	}
	else {
		currentSection = $(sections).filter(currentLink.attr('href'));
	}
	
	sectionLinks.bind('click', showSection);
	
	function showSection(e){
		
		var target = $(this).attr('href');

		currentLink.removeClass('current');
		currentLink = $(this).addClass('current');
		
		currentSection.addClass('js-hide');
		currentSection = $(target).removeClass('js-hide');
		
		e.preventDefault();
	}
}

function bindWindowResize(){

	// Window resize used to track the social links position. When the window gets too
	// small, to prevent an overlap their position is adjusted.
	
	var footer = $('#footer');
	
	$(window).bind('resize', function(e){
		
		var winHeight = $(this).height();
		
		if( winHeight < 655 ){
			footer.css({ top: 600, bottom: 'auto' });
		
		} else {
			footer.css({ top: 'auto', bottom: 20 });
			
		}
		
		$(window).trigger('scroll');
	});
	
	// Trigger resize at load, in case the default window size it too small
	
	$(window).trigger('resize');
}

function bindWindowScroll(){
	
	var ng = $('#nav-global');
        var ft = $('#footer');
	
	$(window).bind('scroll', function(e){
	
		var winLeft = $(this).scrollLeft();
		
		ng.css({ left: (winLeft * -1) + 58 });
		ft.css({ left: (winLeft * -1) + 50 });
	});
	
}

function posterwallInit(){
	$('#posterwall').posterwall({
		vertical_center: 0,
		ratio: [1200, 900],
		min_size: [1200, 900]
	});
}

function setInputDefault(el){
		
	var titleVal = el.attr('title');			
	
	el
	
	.data('defaults', { title: titleVal })
	
	/* Remove the title attribute to stop IE using it as a tooltip */
	.attr('title', '')
			
	/* If user is focusing on element with default text, remove it to allow them to enter data on focus */
	.focus(
		function(){
			if( $(this).val() == titleVal ){
				$(this).val('');
			}
		}
	)
	
	/* If user has losst focus on the element without entering content, replace with default text */
	.blur(
		function(){
			if( $(this).val() == '' ){
				$(this).val(titleVal);
			}
		}
	);
	
	if( el.val() == '' ){
		el.val(titleVal);
	}
}

function newsletterInit(el){
	
	if( !el.length ) return;
	
	// cache form object
	var form = $('form', el);
	
	// cache email input object
	var email = $('input', form);
	
	setInputDefault(email);
	
	
	// timer to show loading
	var timer;
	
	form.submit(function(e){
		
		// get value of input
		var val = $.trim(email.val());
							
		// make ajax request
		$.ajax({
			url: form.attr('action'),
			
			type: 'POST',
			
			data: '&email='+escape(val)+'&ajax=true',
			
			dataType: 'html',
			
			beforeSend: function(){
				// wait a quarter of a second before showing loading message
				timer = setTimeout(function(){
					//msg.text('Sending...').show();
				}, 500);
			},
			
			error: function(XMLHttpRequest, textStatus, errorThrown){
				alert('An error has occurred. Please try again in a few moments.');
			},
			
			success: function(data, textStatus){
				el.replaceWith(data);
				newsletterInit($('#newsletter-signup'));
			},
			
			complete: function(XMLHttpRequest, textStatus){
				clearTimeout(timer);
			}
		});
		
		e.preventDefault();
	});
}

function bindExternalLinks(){
	$('a[rel="external"]').bind( 'click keypress', newWindow );
}

function newWindow(e){
	if( e.type == 'click' || e.type == "keypress" && e.keyCode == 13 ){
		var url = $(this).attr('href');
		window.open( url );
		e.preventDefault();
	}	
}

function  printProps(obj, objName) {
	var output = "" ;
	for (var prop in obj) {
		output += objName + "." + prop + " = " + obj[prop] + "\n" ;
	}
	return output ;
}

function preloadImages(images){
	$.each(images, function(i, image){
		var img = $('<img>').attr('src', image);		
	});
}


/* ------------------------------------------------------------------------------------------------------------ */
})(jQuery);


// JWPlayer Ready Callback
// This gets fired when JWPlayer has loaded and is ready...
	
function playerReady(obj) {
	var player = document.getElementById(obj.id);
	
	if( document.getElementsByTagName('body')[0].id == 'home' ){
		player.addControllerListener("ITEM", "itemTracker");
	}
};
	
function itemTracker(obj){
	var player = document.getElementById(obj.id);
	var playlist = player.getPlaylist();
	var track = document.getElementById('jwplayer-track');
	track.innerHTML = playlist[obj.index].title;
}

