/**
 * Homepage-specific js
 *
 */

$(window).load(function() {

	var buyTicketsBalloon = $('nav#balloons ul li.buy_tickets');
	var likeFacebookBalloon = $('nav#balloons ul li.like_facebook');
	
	if ( !(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/webOS/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)) ) {
		
		// animate the balloons
		buyTicketsBalloon.css({ top: "860px" }).animate({ top: '-=800' }, 5000, function(){	
			animateTicketsBalloon();	
		});
		likeFacebookBalloon.css({ top: "860px"}).animate({ top: '-=760' }, 5000, function(){		
			animateFacebookBalloon();
		});
		
	}
	

	// create the slider
	createSlider();

	// get the official photo feed
	getOfficialPhotoFeed();
	
	// get the fan photo feed
	getFanPhotoFeed();
	
	// get the official video feed
	getOfficialVideoFeed();
	
	// get the fan video feed
	getFanVideoFeed();
});







/**
 * Creates the slider
 *
 */
function createSlider()
{
	// create the nivo slider
	$('#slider').nivoSlider({
		effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
		animSpeed:1000, // Slide transition speed
		pauseTime:10000, // How long each slide will show
		directionNav:false, // Next & Prev navigation
		controlNav:false, // 1,2,3... navigation
		keyboardNav:false, // Use left & right arrows
		pauseOnHover:false, // Stop animation while hovering
		manualAdvance:false // Force manual transitions
	});
}


/**
 * Creates the official photo feed on-screen
 *
 */
function getOfficialPhotoFeed()
{
	// get the official photo feed
	var photoFeed = $.jStorage.get('official-photos-feed');
	if (photoFeed)
	{
		// attempt to get the facebook data...
		$.ajax({
		  	url: photoFeed,
			error: function ( data ) { 
				// todo 
			},
		  	success: function( data ) {
			
				// create the html to display on-screen
				var the_html = "";
				
				// ensure the limit of 5 isn't surpassed
				var limit = 0;
				
				// loop through the data and display the items
				$(data.data.reverse()).each(function(){
					
					// ensure the limit of 5 isn't surpassed
					if (limit == 5)
						return false;
					
					// set the img name
					img_name = (this.tags && this.tags.data[0] ? this.tags.data[0].name + " - " : "") + this.name;
					
					the_html += "<li>";					
					the_html += "<a href=\"" + this.link + "\" title=\"" + img_name + "\"";
					the_html += " style=\"background: #FFF url('" + this.images[1].source + "') no-repeat center center;\">&nbsp;</a>";
					the_html += "</li>";
					
					// increment the limit counter
					limit++;
				});
				
				// add to the screen
				$("div.official_photos ul").html(the_html);
		  	},
			dataType: 'jsonp',
			type: 'GET'
		});
	}
}


/**
 * Creates the fan photo feed on-screen
 *
 */
function getFanPhotoFeed()
{
	// get the official photo feed
	var photoFeed = $.jStorage.get('fan-photos-feed');
	if (photoFeed)
	{
		// attempt to get the facebook data...
		$.ajax({
		  	url: photoFeed,
			error: function ( data ) { 
				// todo 
			},
		  	success: function( data ) {
			
				// create the html to display on-screen
				var the_html = "";
				
				// ensure the limit of 5 isn't surpassed
				var limit = 0;
				
				// loop through the data and display the items
				$(data.data).each(function(){
					
					// ensure the limit of 5 isn't surpassed
					if (limit == 5)
						return false;
					
					// set the img name
					img_name = (this.tags && this.tags.data[0] ? this.tags.data[0].name + " - " : "") + this.name;
					
					the_html += "<li>";					
					the_html += "<a href=\"" + this.link + "\" title=\"" + img_name + "\"";
					the_html += " style=\"background: #FFF url('" + this.images[1].source + "') no-repeat center center;\">&nbsp;</a>";
					the_html += "</li>";
					
					// increment the limit counter
					limit++;
				});
				
				// add to the screen
				$("div.fan_photos ul").html(the_html);
		  	},
			dataType: 'jsonp',
			type: 'GET'
		});
	}
}


/**
 * Creates the official video feed on-screen
 *
 */
function getOfficialVideoFeed()
{
	// get the official photo feed
	var feed = $.jStorage.get('official-videos-feed');	
	displayVideoFeed(feed, "div.official_videos ul");
}

/**
 * Creates the fan video feed on-screen
 *
 */
function getFanVideoFeed()
{
	// get the fan photo feed
	var feed = $.jStorage.get('fan-videos-feed');
	displayVideoFeed(feed, "div.fan_videos ul");
}



/**
 * Creates the video feed on-screen for playlists
 *
 */
function displayVideoFeed(feed, displayTo) 
{
	if (feed)
	{
		// attempt to get the data...
		$.ajax({
		  	url: feed,
			error: function ( data ) { 
				// todo 
			},
		  	success: function( data ) {
			
				// create the html to display on-screen
				var the_html = "";
				
				// loop through the data and display the items
				$(data.data.items).each(function(){
					
					the_html += "<li>";					
					the_html += "<a href=\"http://www.youtube.com/watch?v=" + this.video.id + "\" title=\"" + this.video.title + "\"";
					the_html += " style=\"background: #FFF url('" + this.video.thumbnail["sqDefault"] + "') no-repeat center center;\">&nbsp;</a>";
					the_html += "</li>";
				});
				
				// add to the screen
				$(displayTo).html(the_html);
		  	},
			dataType: 'jsonp',
			type: 'GET'
		});
	}
}

