﻿/**
 *	Movie Playlist (v 0.0.3)
 *
 *	@required
 *		jQuery (min. v 1.5.1)
 *		jQuery.SWFObject (min. v 1.1.1)
 *		
 *	@authors
 *		JB : Jakub Burkiewicz [jakub.burkiewicz@activepharma.pl]
 *
 *	@copyright
 *		(c) 2011 Copyright by Active Pharma Sp. z o.o [www.activepharma.pl]
 *
 *	@changelog
 *		2011-10-14 : v 0.0.3 : JB : Dodanie możliwości wyświetlania tytułów 
 *                                na liście filmów (showTitles).
 *		2011-03-12 : v 0.0.2 : JB : Kilka poprawek i oczyszczenie kodu.
 *		2011-03-02 : v 0.0.1 : JB : Wydanie pierwszej wersji
 **/
 
 (function() {
 
	$.fn.moviePlaylist = function(options)
	{
		var settings = {
			playerWidth       : 320,
			playerHeight      : 240,
			playerPath        : 'flash/player-licensed.swf',
			showTitles        : false,
			listStyle         : 'slider',
			showSliderStepNav : true,
			slideItemsNumber  : 3
		}
		
		this.each(function() {
			$.extend(settings, options);
			
			var movieListClone = $(this).clone();
			
			$(this).replaceWith('<div class="movie-playlist"><div class="player-wrapper"><h2></h2><div class="player"><p style="padding-top: 180px;"><strong>Błąd wtyczki Adobe Flash</strong></p><p>Pobierz wtyczkę:</p><p><a target="_blank" href="http://get.adobe.com/pl/flashplayer/" title="Pobierz i zainstaluj"><img style="border: 0px;" src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Pobierz wtyczkę" /></a></p></div></div><div class="movies-wrapper"></div></div>');
			$('.movie-playlist .movies-wrapper').append(movieListClone);
			
			$('.movie-playlist .player-wrapper h2').text($('.movie-playlist a:first').attr('title'));
			$('.movie-playlist .player').flash({
				swf       : settings.playerPath,
				width     : settings.playerWidth,
				height    : settings.playerHeight,
				flashvars : {
					file  : $('.movie-playlist a:first').attr('href'),
					image : new String($('.movie-playlist a:first').children('img').attr('src')).replace('-small', '')
				},
				allowfullscreen : true
			});
			$('.movie-playlist a:first').addClass('now-playing');
			
			switch(settings.listStyle) {
			
				case 'slider':
					var moviesListClone = $('.movies-wrapper .movies').clone();
					$('.movies-wrapper').html('<div class="slider-container"></div>');
					$('.slider-container').append(moviesListClone);
					
					$('.slider-container .movies').css('width', parseInt(($('.slider-container .movies li').outerWidth() + 10) * $('.slider-container .movies li').length) + 'px');
					
					if(settings.showSliderStepNav) {
						
						$('.movies-wrapper').append('<div class="nav step"><ul><li class="first"><a href="#" title="Poprzednie">&laquo;</a></li><li class="last"><a href="#" title="Następne">&raquo;</a></li></ul></div>');
						$('.nav.step .first a').hide();
						
						$('.nav.step .first a').click(function(e) {
							e.preventDefault();
							
							var currentPosition = parseInt($('.slider-container ul').css('left'));
							var itemWidth = parseInt($('.slider-container ul li').width() + 10);
							var itemsListWidth = itemWidth * $('.slider-container ul li').length;
							var containerWidth = $('.slider-container').width();
							var stepLength = parseInt(currentPosition + itemWidth * settings.slideItemsNumber)
							
							if((containerWidth + currentPosition) <= 0) {
								$('.slider-container ul').animate({
									'left' : stepLength + 'px'
								}, 300, function() {
									if((containerWidth + currentPosition) >= 0) {
										$('.nav.step .first a').hide();
									}
								});
								$('.nav.step .last a').show();
							}
						});
						
						$('.nav.step .last a').click(function(e) {
							e.preventDefault();
							
							var currentPosition = parseInt($('.slider-container ul').css('left'));
							var itemWidth = parseInt($('.slider-container ul li').width() + 10);
							var itemsListWidth = itemWidth * $('.slider-container ul li').length;
							var containerWidth = $('.slider-container').width();
							var stepLength = parseInt(currentPosition - itemWidth * settings.slideItemsNumber)
							
							if((containerWidth + currentPosition) >= 0) {
								$('.slider-container ul').animate({
									'left' : stepLength + 'px'
								}, 300, function() {
									if((containerWidth + currentPosition) <= 0) {
										$('.nav.step .last a').hide();
									}
								});
								$('.nav.step .first a').show();
							}
						});
						
					}
					
					break;
					
			}
			
			$('.movie-playlist .movies a').each(function() {
				if($(this).hasClass('now-playing')) {
					$('.movie-playlist .player-wrapper h2').text($(this).attr('title'));
					$('.movie-playlist .player').flash({
						swf       : settings.playerPath,
						width     : settings.playerWidth,
						height    : settings.playerHeight,
						flashvars : {
							file  : $(this).attr('href'),
							image : new String($(this).children('img').attr('src')).replace('-small', '')
						},
						allowfullscreen : true
					});
				}
				
				$(this).click(function() {
					$('.movie-playlist .player-wrapper h2').text($(this).attr('title'));
					$('.movie-playlist .player').flash({
						swf       : settings.playerPath,
						width     : settings.playerWidth,
						height    : settings.playerHeight,
						flashvars : {
							file  : $(this).attr('href'),
							image : new String($(this).children('.thumb').children('img').attr('src')).replace('-small', '')
						},
						allowfullscreen : true
					});
					
					$('.movie-playlist .movies a').removeClass('now-playing');
					$(this).addClass('now-playing');
					
					return false;
				});
				
				if(settings.showTitles) {
					var imageObj = $(this).children('img').clone();
					$(this).children('img').replaceWith('<span class="thumb"><span class="cover"></span></span><span class="title">'+ $(this).attr('title') +'</span>');
					$(this).children('.thumb').append(imageObj);
				}
			});
		});
	}
 
 })(jQuery);
 
