// You tube API - Retorna o incorporado do vídeo em forma de swf
// Raphael Collares Toselli
// Ultima modificação - 24-06-2010

var videos = new Array();

function embedVideo(id){
		
		var divYT = '#apiYT';
		
		// Corta os caracteres
	
		var vId = id;
		
		if (videos.length > 0) 
		
			var i = videos.length;
		
		else {
			
			var i = 0;
		}
		
		videos[i] = {
						'id' : vId,
						'playerID' : "vidYT" + i
					};
	
		
		// Cria uma div nova para abrigar o swf
		$(divYT).append('<div id="v'+i+'" class="marginYT"><div id="'+videos[i].playerID+'"></div><a href="#" onclick="removeYT('+i+',\''+videos[i].playerID+'\'); return false;" class="x"></a></div>');
		
		//alert(videos[i].id);
		var params = {};
			params.menu = "false";
			params.scale = "noscale";
			params.wmode = "transparent";			
			params.allowScriptAccess = "always";	
			
		// carrega o video do youtube atráves da API
	    var atts = { id: videos[i].playerID };
	    swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid="+videos[i].playerID, 
	                       videos[i].playerID, "120", "80", "8", null, null, params, atts);	
		
		// Coloca a margem na div
		//$("#"+videos[i].playerID).addClass('marginYT');
		
		
	
		
}

function removeYT(i,playerID){
	
	//alert(playerID);
	
	$("#v"+i).remove();

	for (var j=0; j<videos.length; j++) {
		
		//alert('ID do array ->'+videos[j].playerID +' Id da requisição ->'+playerID);
			if (videos[j].playerID == playerID) {
					videos.splice(j,1);
					break;
				//	alert(videos[j].playerID);
			
		}
};
	return false;
	
	
	
	
}


function onYouTubePlayerReady(playerId){
	//alert (playerId);
	ytplayer = document.getElementById(playerId);
	//Load an initial video into the player
	for (var i = 0; i < videos.length; i++) {
	
		if (videos[i].playerID == playerId) {
			ytplayer.cueVideoById(videos[i].id);
			break;
		}
		
	}
}
$(document).ready(function(){
	
	

	

// modal dos videos	
	tips = $(".validateTips");

	function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

	function checkRegexp(o, regexp, n){
	
		if (!(regexp.test(o.val()))) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		}
		else {
			return true;
		}
		
	}


		$("#dVid").dialog({
			autoOpen: false,
			height: 220,
			width: 500,
			modal: true,
			buttons: {
					   'Inserir Video': function(){
						var bValid = true;
						
						var video = $("#video");
						
						allFields = $([]).add(video);
						allFields.removeClass('ui-state-error');
	
				
						bValid = bValid && 
						checkRegexp(video,/http:\/\/w+\.youtube\.com.*\/watch\?v=[a-zA-Z0-9_]+/i,"Somente videos do youtube são aceitos!");
						
						var rxVid = /v=[a-zA-Z0-9_]+/i;
						
						if (bValid) {
							text = video.val();
							id = ""+text.match(rxVid);
							embedVideo(id.substr(2));
							$(this).dialog('close');
						}
					},

					'Fechar': function(){
						$(this).dialog('close');
						}

				}
		});
	
	
	});

