$(document).ready(function() {

	$('.playVideo a').click( function() {
		var relObj	= $(this).attr('rel').replace(/\s+/g, '').split(',');
		var rFilm		= relObj[0].replace('file:', '');
		var rWidth	= relObj[1].replace('width:', '');
		var rHeight	= relObj[2].replace('height:', '');
		getMediaPopup(rFilm, rWidth, rHeight);
	});


	function getMediaPopup(yFilm, yWidth, yHeight) {
		//#
		objMaska = '<div class="fullScreenDiv"></div>';
		objMedia = '';
		objMedia += '<div class="mediaContainer">';
			objMedia += '<div class="mediaClose"></div>',
			objMedia += '<div class="bT">',
				objMedia += '<div class="L"></div><div class="M"></div><div class="R"></div>',
			objMedia += '</div>',
			objMedia += '<div class="bM">',
				objMedia += '<div class="L"></div><div class="M"><div class="mediaContent"><!-- text --></div></div><div class="R"></div>',
			objMedia += '</div>',
			objMedia += '<div class="bB">',
				objMedia += '<div class="L"></div><div class="M"></div><div class="R"></div>',
			objMedia += '</div>',
		objMedia += '</div>';

		//#
		$('body').append( objMaska + objMedia);
		$('.fullScreenDiv').css({ opacity: '0.40' });
		$('.mediaContainer .bT .M, .mediaContainer .bB .M').width(yWidth);
		$('.mediaContainer .bM .L, .mediaContainer .bM .R').height(yHeight);
		$('.mediaContent').width(yWidth).height(yHeight);
		$('.fullScreenDiv, .mediaContainer').animate( {opacity: 'show'}, {queue: false, duration: 150} );
		middleMedia();

		//#
		$('.fullScreenDiv').promise().done(function() {
			//var videoIframe = '<iframe width="'+yWidth+'" height="'+yHeight+'" src="http://www.youtube.com/embed/'+yFilm+'?hd=0" frameborder="0" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" allowfullscreen></iframe>';
			var videoIframe = '<object width="'+yWidth+'" height="'+yHeight+'"><param name="movie" value="http://www.youtube.com/v/'+yFilm+'?fs=1&amp;hl=pl_PL&amp;hd=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+yFilm+'?fs=1&amp;hl=pl_PL&amp;hd=0" type="application/x-shockwave-flash" width="'+yWidth+'" height="'+yHeight+'" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
			$('.mediaContent').html( videoIframe );
		});

		//#
		$(window).resize(function() {
			middleMedia();
		});

		//#
		$(window).scroll( function() {
		  $('.mediaContainer').animate( {marginTop: $(window).scrollTop()+'px' }, {queue: false, duration: 250} );
		});

		//#
		$('.mediaClose').click( function() {
			$('.fullScreenDiv, .mediaContainer').animate( {opacity: 'hide'}, {queue: false, duration: 150} );
			$('.mediaContainer').html('');
			$('.fullScreenDiv').promise().done(function() {
				$('.mediaContainer').remove();
				$('.fullScreenDiv').remove();
			});
		});


		//#
		function middleMedia() {
			var centerContent = ($(window).width() - $('.mediaContainer').width()) / 2;
			var middleContent = ($(window).height() - $('.mediaContainer').height()) / 2;

			$('.mediaContainer').css({ left: centerContent+'px', top: middleContent+'px' });

			if ( $(window).width() < $('.pageContainer').width() ) {
				$('.fullScreenDiv').css({ width: $(document).width()+'px', height: $(document).height()+'px' });
			} else {
			  $('.fullScreenDiv').css({ width: '100%', height: $(document).height()+'px' });
			}
		}
	}

});
