var FoxMovies = {

	// Holds the title details.
	titles: {},

	// Specifies the currently-selected title
	current_id: 0,

	init: function(){

		$('.tip[title]').tooltip({
			effect:			'fade',
			delay:			0,
			fadeInSpeed:	100,
			fadeOutSpeed:	100,
			position:		'top center',
			offset:			[-5, 0]
		});

		$('html, body').click(function(e){
			$('.header > ul > li > ul, .scroller_dropdown').slideUp('fast');
		});

		$('.header > ul > li > ul').each(function(){
			var parent			= $(this).prev();
			var parent_offset	= parent.position();
			var offset_left		= parent_offset.left;
			var offset_top		= parent_offset.top + parent.outerHeight();

			$(this).css({
				left:	offset_left +'px',
				top:	offset_top +'px'
			});
		});

		$('.header > ul > li').hover(function(){

				$(this).data('opening', true);
				$(this).data('close', false);

				$(this).find('ul').slideDown('fast', function(){
					$(this).parent().data('opening', false);
					if($(this).parent().data('close')){
						$(this).slideUp('fast');
					}
				});

		}, function(){

			if($(this).data('opening')){
				$(this).data('close', true);
			}else{
				$(this).data('close', false);
				$(this).find('ul').slideUp('fast');
			}

		});

		$('.header > ul > li > ul > li > a').bind({
			click: function(e){
				e.preventDefault();
				var id = $(this).parent('li').attr('title_id');
				FoxMovies.selectTitle(id);
			}
		});

		// Start cycling the featured items
		$.cycle("titles", {
			selector:	'.scroller > ul > li',
			delay:		5000,
			speed:		500,
			offset:		2
		});

		// Handle the previous click
		$('.scroller a.previous').click(function(e){
			e.preventDefault();
			$.cycle("titles", "previous");
		});

		// Handle the next click
		$('.scroller a.next').click(function(e){
			e.preventDefault();
			$.cycle("titles", "next");
		});

		// Handle the next/previous buttona
		$('.main .nav .prev a').click(function(e){
			e.preventDefault();
			FoxMovies.displayPrevTitle();
		});

		// Handle the popdown buttons
		$('.main .nav .next a').click(function(e){
			e.preventDefault();
			FoxMovies.displayNextTitle();
		});

		// Handle the popdown buttons
		$('.scroller a.btn').click(function(e){
			e.preventDefault();
		});

		// Handle hover/blur of scroll items
		$('.scroller > ul > li').mouseover(function(e){
			$(this).addClass('hover');
		});
		$('.scroller > ul > li').mouseout(function(e){
			$(this).removeClass('hover');
		});

		// Handle the scroller links
		$('.scroller > ul > li > a').click(function(e){
			e.preventDefault();

			var id = $(this).parents('li').attr('title_id');

			FoxMovies.selectTitle(id);

		});

		$('.scroller > ul > li > a.btn').hover(function(){
			
			// Stop the cycler
			$.cycle("titles", "stop");
			
			var button = $(this);
			var title_id = button.parent('li').attr('title_id');
			var dropdown = $('.scroller_dropdown[title_id="'+ title_id +'"]');

			// Get the button offset
			var offset = button.offset();

			dropdown.data('opening', true);
			dropdown.data('close', false);

			// Re-position the dropdown so it's aligned with the element, then open it
			dropdown.css('left', offset.left +'px');
			dropdown.css('top', offset.top +'px');
			dropdown.slideDown('fast', function(){
				$(this).data('opening', false);
				if($(this).data('close')){
					$(this).slideUp('fast');
						
					// Re-start the cycler
					$.cycle("titles", "start");
				}
			});

		});

		$('.scroller > ul > li > ul').each(function(){

			var dropdown = $(this);

			// Set the title ID to associate this with
			dropdown.attr('title_id', dropdown.parent('li').attr('title_id'));

			// Get the current position
			var offset = dropdown.offset();

			// Hide the dropdown
			dropdown.hide();

			// Set the position
			dropdown.css('top', (offset.top - 25) +'px');

			// Detach the dropdown and append it to the body
			dropdown.detach().appendTo('body');

			// Add the event handler
			dropdown.bind({
				mouseleave: function(){
					if($(this).data('opening')){
						$(this).data('close', true);
					}else{
						$(this).data('close', false);
						$(this).slideUp('fast');
						
						// Re-start the cycler
						$.cycle("titles", "start");
					}
				}
			});


		});

		// Handle subnav link
		$('.scroller_dropdown > li > a').click(function(e){
			var panel = $(this).parents('li').attr('class');

			var title_id = $(this).parents('ul').attr('title_id');

			// Close the dropdown
			$(this).parents('ul').slideUp();

			// Make sure the specified title is being displayed
			FoxMovies.selectTitle(title_id);

			// Stop the trailer, if it's playing
			if(player = swfobject.getObjectById('player')){
				player.stopVideo();
			}

			switch(panel){
				case 'tickets':
				case 'trailer':
				case 'about':
					e.preventDefault();

					// Show the close button
					$('.main .film .close').show();

					// Hide all panels
					$('.main .film .panels > *:not(.clear)').hide();

					// Show the selected panel
					$('.main .film .panels > .'+ panel).show();

					break;
			}

		});

		// Handle subnav link
		$('.main .nav li a').click(function(e){

			var panel = $(this).parents('li').attr('class');

			// Stop the trailer, if it's playing
			if(player = swfobject.getObjectById('player')){
				player.stopVideo();
			}

			switch(panel){
				case 'tickets':
					e.preventDefault();
					break;
				case 'trailer':
				case 'about':
					e.preventDefault();

					// Show the close button
					$('.main .film .close').show();

					// Hide all panels
					$('.main .film .panels > *:not(.clear)').hide();

					// Show the selected panel
					$('.main .film .panels > .'+ panel).show();

					break;
			}

		});

		// Handle close button
		$('.main .film .close').click(function(e){
			e.preventDefault();

			$('.main .film .panels > *').hide();
			$('.main .film .panels > .banner').show();

			// Hide the close button
			$(this).hide();

			// Stop the trailer, if it's playing
			if(player = swfobject.getObjectById('player')){
				player.stopVideo();
			}
		});

		// Handle close button
		$('.main .nav .tickets').hover(function(e){

			$(this).data('opening', true);
			$(this).data('close', false);

			$(this).find('ul').slideDown('fast', function(){
				$(this).parent().data('opening', false);
				if($(this).parent().data('close')){
					$(this).slideUp('fast');
				}
			});
		}, function(){
			if($(this).data('opening')){
				$(this).data('close', true);
			}else{
				$(this).data('close', false);
				$(this).find('ul').slideUp('fast');
			}
		});;

	},

	/**
	* Adds a title to the list.
	*/
	addTitle: function(data){
		if(!this.titles[data.id]){
			this.titles[data.id] = data;
		}
	},

	/**
	* Displays the title information for the previous title.
	*/
	displayPrevTitle: function(){

		// Get the currently-selected title
		var current = $('.scroller > ul > li.selected');
		var prev = current.prev();

		// If this is the first title then select the last one
		if(!prev.length){
			prev = $('.scroller > ul > li:last-child');
		}

		// Set the title details
		this.selectTitle(prev.attr('title_id'));
	},

	/**
	* Displays the title information for the next title.
	*/
	displayNextTitle: function(){

		// Get the currently-selected title
		var current = $('.scroller > ul > li.selected');
		var next = current.next();

		// If this is the last title then select the first one again
		if(!next.length){
			next = $('.scroller > ul > li:first-child');
		}

		// Set the title details
		this.selectTitle(next.attr('title_id'));
	},

	/**
	* Sets the title details in the HTML.
	*/
	selectTitle: function(id){
		if(id && this.titles[id] && this.current_id != id){

			// Set the current id
			this.current_id = id;

			// Remove the selection from the current title
			$('.scroller > ul > li.selected').removeClass('selected');

			// Add the selection to the selected title
			$('.scroller > ul > li[title_id="'+ id +'"]').addClass('selected');

			// Get the title details
			var title = this.titles[id];

			$('.main .film, .gutter_left, .gutter_right').fadeOut('fast', function(){

				$('.gutter_left').css('background-image', 'url('+ title.gutter_left_image_url +')');
				$('.gutter_right').css('background-image', 'url('+ title.gutter_right_image_url +')');

				// Set the title details
				$('.main .film .banner img').attr('src', title.main_image_url);
				$('.main .film .about h3').html(title.title);
				$('.main .film .about .date').text(title.release_date);
				$('.main .film .about .synopsis').html(title.synopsis);
				$('.main .film .packshot img').attr('src', title.packshot_image_url);

				if(title.site_url){
					$('.main .nav .official_site').show().find('a').attr('href', title.site_url);
				}else{
					$('.main .nav .official_site').hide();
				}

				if(title.facebook_url){
					$('.main .nav .facebook').show().find('a').attr('href', title.facebook_url);
				}else{
					$('.main .nav .facebook').hide();
				}
				
				if(title.twitter_url){
					$('.main .nav .twitter').show().find('a').attr('href', title.twitter_url);
				}else{
					$('.main .nav .twitter').hide();
				}
				
				if(title.youtube_url){
					$('.main .nav .youtube').show().find('a').attr('href', title.youtube_url);
				}else{
					$('.main .nav .youtube').hide();
				}

				if(title.fandango_url || title.moviefone_url || title.movietickets_url || title.yahoomovies_url){
					var tickets = $('.main .nav .tickets');
					tickets.show();

					if(title.fandango_url){
						tickets.find('.fandango').show().find('a').attr('href', title.fandango_url);
					}else{
						tickets.find('.fandango').hide();
					}
					if(title.moviefone_url){
						tickets.find('.moviefone').show().find('a').attr('href', title.moviefone_url);
					}else{
						tickets.find('.moviefone').hide();
					}
					if(title.movietickets_url){
						tickets.find('.movietickets').show().find('a').attr('href', title.movietickets_url);
					}else{
						tickets.find('.movietickets').hide();
					}
					if(title.yahoomovies_url){
						tickets.find('.yahoomovies').show().find('a').attr('href', title.yahoomovies_url);
					}else{
						tickets.find('.yahoomovies').hide();
					}
				}else{
					$('.main .nav .tickets').hide();
				}

				// Inser tthe trailer player
				if(title.trailer_url){
					$('.main .nav .trailer').show();
					$('.main .film .trailer').html('<div id="player"></div>');

					var flashvars = {
						ap:		1
					};
					var params = {
						menu: "false",
						allowFullScreen: "true",
						allowScriptAccess: "always",
						wmode: "transparent"
					};

					var video_height = Math.round(925 / title.video_aspect_ratio);

					swfobject.embedSWF(title.trailer_url +'&ap=1', "player", "925", video_height, "9.0.0","expressInstall.swf", {}, params);
				}else{
					$('.main .film .trailer').text('');
					$('.main .nav .trailer').hide();
				}

				// Fade back in
				$(this).fadeIn();
				$('.gutter_left, .gutter_right').fadeIn();
			});

		}
	}

}

$(document).ready(function(){

	// Initialize the FoxMovies object
	FoxMovies.init();

});
