$(document).ready(function() {
	var timeout = 8000;
	var showreelItem = 1;
	
	/** functions **/
	
		/* show item */
		function showItem(movieId)
		{
			if (!$('li#info-' + movieId).hasClass('active'))
			{
				//hide active video
				$('#images li.active div.video').css('display', 'none');
				
				//hide other accordion content
				$('#accordion div.hidden').not('li#info-' + movieId + ' div.hidden').css('display', 'none');
				
				//show clicked accordion content
				if($.browser.msie){
					$('li#info-' + movieId + ' div.hidden').css('display', 'block');
				}	
				else {
					$('li#info-' + movieId + ' div.hidden').fadeIn();
				}
				
				//set other accordion items to inactive
				$("#accordion li").removeClass('active');
				
				//set clicked accordion item to active
				$('li#info-' + movieId).addClass('active');
				
				//fade out previous movie image
				if($.browser.msie){
					$('#images li a').css('display', 'none');
				}
				$('#images li.active').fadeOut('slow');

				$('#images li.active').removeClass('active');
				
				// fade in next image
				$("#images li#media-" + movieId).addClass('active');
				$("#images li#media-" + movieId).fadeIn('slow', function() {
					//show active screenshot/play button
					$('#images li img, #images li a').css('display', 'block');
				});
				//$("#images li#media-" + movieId + ' a').css('display', 'block');
			}		
		}

	/** showreel timer  **/
	showreelTimer = setInterval(function(){
 		if (showreelItem == 4) {
			showreelItem = 1;
		}
		else {
			showreelItem = showreelItem + 1;
		}
		// show next item
		showItem(showreelItem);
	}, timeout);

	/** destroy timer when clicking on showreel links **/
	$("#showreel, #accordion li h2 a, #images a.play").click(function () {
		clearInterval(showreelTimer);
	});
	
	/** click accordion item **/
	$("ul#accordion li h2 a").click(function () {
		
		//get movie id
		movieId = $(this).parent().parent().attr("id");
		movieId = movieId.replace('info-', '');
	
		showItem(movieId);
			
		return false;
	});
		
	/** click play button on screenshot **/
	$("#images a.play").click(function () {
	
		//get movie id
		movieId = $(this).parent().attr("id");
		movieId = movieId.replace('media-', '');	
		
		//hide screenshot, show and play video		
		$("li#media-" + movieId + " div.video").css('display', 'block');
		$("li#media-" + movieId + " img, li#media-" + movieId + " a.play").pause(100).fadeOut('slow', function () {
			$('#video-' + movieId).jwPlayer('play');
		});
		
		return false;
		
	});
	
});

