jQuery(document).ready(function() {

	$("a.video").click(function() {
		$("a[rel=image-gallery]").fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'showNavArrows'		: true,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'			: this.title,
			'width'			: 640,
			'height'		: 385,
			'titlePosition'		: 'inside',
			'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'			: 'swf',
			'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true'
			},
			'autoScale'			: false,
				'onStart'			: function(currentArray, currentIndex, currentOpts){
					//clean up previos titles
					$('#fancybox-desc').remove();
					$('#fancybox-title span.title-img').remove();
					$('#fancybox-title span.number').remove();
					$('span.number','#nav-wrapper-video').remove();
					$('span.title-img','#nav-wrapper-video').remove();
					
					if(!$('#nav-wrapper-video').length){
						$('#fancybox-left').css('display','none');
						$('#fancybox-right').css('display','none');
						$('#fancybox-close').css('display','none');
					}
					
			},
			'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
					return '<div id="fancybox-title-over">' + '<span class="title-img">'+ (title.length ? '  ' + title : '') + '</span>' + '<span class="number">Изображение &nbsp;' + (currentIndex + 1) + '&nbsp; из &nbsp;' + currentArray.length + '</span>' + '</div>'
			},
			'onComplete': function(currentArray, currentIndex, currentOpts){
					
					$('#nav-wrapper-video').css('display','block');
					
					if (!$('#nav-wrapper-video').length) {
						$('#fancybox-left').css('display','block');
						$('#fancybox-right').css('display','block');
						$('#fancybox-close').css('display','block');
						
						$wrap = $('<div id="nav-wrapper-video"></div>')
							.append($('#fancybox-left').addClass('btn-left'))
							.append($('#fancybox-right').addClass('btn-right'))
							.append($('#fancybox-close'))
						;
						$wrap.insertAfter($('#fancybox-content'));
					}
					$('#fancybox-title span.title-img').insertBefore($('#fancybox-left'));
					$('#fancybox-title span.number').insertBefore($('#fancybox-right'));
					//$.fancybox.resize();
					
					var desc_Item =($('ul.gallery li:eq('+currentIndex+')').find('img').attr('alt'));
					$('#fancybox-outer')
						.append('<div id="fancybox-desc"><p>'+ desc_Item +'</p></div>')
					;
					/*
					$('#fancybox-right').click(function(){
						$.fancybox.next();
						return false;
					});
					*/
				},
				'onCleanup': function(){
					$('#nav-wrapper-video').css('display','none');
					//console.log( $('#fancybox-outer') );
					//var a = 1;
				}
		});

		return false;
	});



});


