/*

include de google APIs obrigatório:
<script src="http://www.google.com/jsapi"></script>

*/
var y = new Youtube("y");
// this.atl = "alt=json-in-script";//especifica o formato de retorno {atom, rss, json, json-in-script}
// this.callback = "callback=";//função chamada no retorno
// this.author = "author=";//lista de autores para restringir a pesquisa. Máximo de 20 autores, pode ser separado por vírgula.
// this.maxResults = "max-results=8";//máximo de resultados (define o tamanho da pagina)
// this.startIndex = "start-index=1";//define o primeiro resultado a ser mostrado
// this.version = "v=1";//versão da API utilizada - padrão é 1.0, mas pode ter outras.
// this.strict = "strict=false";//define se o youtube deve ignorar ou não parametros invalidos
// this.orderby = "orderby=relevance";//define a ordem {relevance, published, viewCount, rating}
// this.vq = "vq=";//query de busca
// this.format = "format=5";//define o formato do video, para celular, tamanho minimo, ou no caso 5, que permite swf embeded

function Youtube(tmp_id){
	this.id = tmp_id;
	this.callback = "";
	this.videos = new Array();
	
	//mais detalhes em: http://code.google.com/intl/pt-BR/apis/youtube/reference.html
	this.params = new Array();
	this.params.push(new Array("alt","json-in-script"));
	this.params.push(new Array("callback",this.id+".baseCallback"));
	//this.params.push(new Array("author",""));
	this.params.push(new Array("max-results",8));
	this.params.push(new Array("start-index",1));
	this.params.push(new Array("v",1));
	this.params.push(new Array("strict","false"));
	this.params.push(new Array("orderby","relevance"));
	this.params.push(new Array("format",5));
	
	this.getRequest = "http://gdata.youtube.com/feeds/api/videos?";//endereço da api
	this.embededTemplate = 	'<object width="#W#" height="#H#"><param name="movie" value="http://www.youtube.com/v/#ID#&hl=pt_BR&fs=1&rel=0"></param>' +
							'<param name="allowFullScreen" value="true"></param>'+
							'<param name="allowscriptaccess" value="always"></param>'+
							'<embed src="http://www.youtube.com/v/#ID#&hl=pt_BR&fs=1&rel=0" type="application/x-shockwave-flash" '+
							'allowscriptaccess="always" allowfullscreen="true" width="#W#" height="#H#"></embed></object>';
	
	this.search = function(tmp_search,tmp_callback){
		this.callback = tmp_callback;
		this.params.push(new Array("vq",tmp_search));
		
		var script = document.createElement("script");
		script.setAttribute("id", "script_"+this.id);
		script.setAttribute("type", "text/javascript");
		script.setAttribute("src", this.getRequest + this.getParams());
		document.documentElement.firstChild.appendChild(script);
	}
	this.getParams = function(){
		var strParams = "";
		for(var x=0;x<this.params.length;x++){
			var k = this.params[x][0];
			var v = this.params[x][1];
			
			if(x > 0){
				strParams += "&"+k+"="+v;
			} else {
				strParams += k+"="+v;
			}
		}
		return strParams;
	}
	this.baseCallback = function (data){
		var feed = data.feed;
		var entries = feed.entry || [];
		var arr = new Array();
		for (var i = 0; i < entries.length; i++){
			var entry = entries[i];
			arr[i] = new Array();
			arr[i][0] = entry.author[0].name.$t;//autor
			arr[i][1] = entry.link[0].href;//link
			arr[i][2] = entry.media$group.media$thumbnail;//array com as propriedades width, height, time, url
			arr[i][3] = entry.title.$t;//titulo do video
			aux = entry.id.$t.split("/");
			if(aux.length > 0)
				arr[i][4] = aux[aux.length-1];//id
		}
		this.videos = arr;
		
		if(this.callback != ""){
			eval(this.callback+"()");
		}
	}
	this.getEmbeded = function(id,width,height){
		var template = this.embededTemplate.replace(/#ID#/g,id);
		template = template.replace(/#W#/g,width);
		template = template.replace(/#H#/g,height);
		return template;
	}
}


