$(function(){

	if($('.open-modal-video').length > 0){
	
		$('body').append("<div id='modal-video' ><iframe src='' id='videoFrame' name='videoFrame' scrolling='no' frameborder='0' ></iframe><div class='clipbar home-box'></div><div class='dialogBack'></div></div>")

		$("#modal-video").dialog({
			draggable:false,
			dialogClass: 'modalVideo',
			autoOpen: false,
			height: 590,
			width:639,
			position:['center','center'],
			open : function(){
				//$('.ui-widget-overlay').css({width:639,height:568})	
			},
			resizable:false,
			modal: true,
			close: function() {
				$modalVideo.find('#videoFrame').attr("src",""); 
			}
		})
		
		$('.open-modal-video').click(function(){
					
			var hrefx = $(this).attr("href")
			var hrefParsed = hrefx.split("?")[1];	
			//alert(hrefParsed);
			$modalVideo = $("#modal-video");
			$modalVideo.removeClass('small')
			$modalVideo.dialog( "option", "height", 590 );
			
			clearInterval(AniTimerInterval);
			
			
			$modalVideo.dialog('open');
			
			
			$modalVideo.find('#videoFrame').attr("src",hrefx); 
			
			
			
			
			$.get('ajax?'+hrefParsed,{f:'clipbar'},function(data){
				$modalVideo.find('.clipbar').html(data);
				
				$modalVideo.find('.clipbar a').click(function(){
				
				})
				initVideoClip();
			})			
			return false;
		});
	
		$('.open-modal-video-vimeo').click(function(){
					
			var hrefx = $(this).attr("href");
				
			//alert(hrefParsed);
			$modalVideo = $("#modal-video");
			$modalVideo.addClass('small')
			$modalVideo.dialog( "option", "height", 392 );
			
			//clearInterval(AniTimerInterval);
			
			
			$modalVideo.dialog('open');
			
			
			$modalVideo.find('#videoFrame').attr("src",hrefx); 
			
			$modalVideo.find('.clipbar').html('');
			
			
					
			return false;
		});
	
	
	
		var initVideoClip = function(){
			$('#modal-video ul').each(function()
			{
				var $list = $(this), h = 0, i = 0;
			
				$list.children("li").each(function() { i++; h = Math.max(h, $(this).height()); }).end().width(i * 523 + 188).height(h).wrap('<div class="clip"></div>').parent().before('<p class="left"><a>&lt;</a></p>').after('<p class="right"><a>&gt;</a></p>');
				$list.children("li").eq(0).addClass("selected");
				
				var $prev = $list.closest("#modal-video").find("p.left a"), $next = $list.closest("#modal-video").find("p.right a");
			
				$next.click(function(event, data)
				{
					if ($list.is(":animated"))
						return false;
					$list.animate({ left: "-523px" }, 500)
						.children(".selected").animate({ opacity: 1 }, 500, function() { $(this).clone(true).appendTo($list); $list.css("left", "0"); $(this).remove(); }).removeClass("selected")
						.next().addClass("selected");
					return false;
				});
			
				$prev.click(function()
				{
					
					if ($list.is(":animated"))
						return false;
					var $last = $list.children("li:last");
					$last.clone(true).prependTo($list);
					$last.remove();
					$list.css("left", "-523px").animate({ left: 0 }, 500)
						.children(".selected").animate({ opacity: 1 }, 500).removeClass("selected")
						.prev().animate({ opacity: 1 }, 500).addClass("selected");
					return false;
				});
			
			
			});

		}
	}

})
