var addImages = function(images) {
		images.each(function(image) {
			var el = new Element('div', {'class': 'singolo-thumbnail',
			'events': {
				'mouseover': function(){
						$('des'+image.id).setStyle('visibility', 'visible');
						$('des'+image.id).setStyle('top', '20px');
				},
				'mouseout': function(){
						$('des'+image.id).setStyle('visibility', 'hidden');
						$('des'+image.id).setStyle('top', '-9999px');
				},
				'click': function(){
						if(link=='1')
						{
							location.href = "/index.php?video="+image.id;
						}
						else
						{
							var myFx = new Fx.Scroll(window).toElement('wrapper');
							setTimeout("video("+image.id+",'12gialli',1);", 1000); 	
						}
				}
			}
			});
			var cat = new Element('h4', {'html': image.cat}).inject(el);
			var wra = new Element('div', {'class':'wrap-img'}).inject(el);
			var img = new Element('img', {
				'src': '/inc/php/img.php?id='+image.id+'&x=157&y=100'}).inject(wra);
			var tit = new Element('p', {'html': image.tit}).inject(el);
			var dur = new Element('p', {'class': 'durata', 'html': '<b>Data</b>: '+image.dat}).inject(el);
			var eye = new Element('p', {'class': 'guarda'}).inject(el);
			var lin = new Element('a', {
				'events': {
					'click': function(){
						var myFx = new Fx.Scroll(window).toElement('wrapper');
						setTimeout("video(" + image.id + ",null,1);", 1000);
					}
				},
					'html': 'guarda&nbsp;&raquo;'
			}).inject(eye);
			var des = new Element('div', {'id':'des'+image.id, 'class':'png des-thumbnail', 'html': '<p>'+image.des+'</p>'}).inject(el);
			el.inject($('blocco-thumbnail'));
		});
	};

function tag_cloud(tag)
{
	var myFx = new Fx.Scroll(window).toElement('blocco-giallo');
	get_videos(tag,1,"tagcloud","keyword");
}

function banner(action){
	var myFx = new Fx.Scroll(window).toElement('blocco-giallo');
	if(action=="petruzzelli") {
		get_videos(5, 1, 'banner', 'categoria');
	}
	if(action=="regione") {
		get_videos(0, 1, 'banner', 'regione');
	}
	if(action=="elezioni") {
		get_videos(12, 1, 'banner', 'categoria');
	}
}

function get_videos(categoria,pagina,ref,tipo)
{
	$('blocco-thumbnail').empty();
	$('blocco-thumbnail').addClass("loaderbar");
	$('paginatore').empty();
	$$('a').each(function(el){el.removeClass("corrente");});
	if (categoria != "all") {
	
		if(tipo=="keyword")
		{
			var what = 'key='+categoria;
		}
		else if(tipo == "categoria")
		{
			var what = 'cat='+categoria;
			$('cat_'+categoria).addClass("corrente");
		}
		else if(tipo == "sottocategoria")
		{
			var what = 'cat='+categoria+'&opt=sottocategoria';
			$('cat_'+categoria).addClass("corrente");
		}
		else if(tipo == "ricerca")
		{
			var what = 'ricerca='+categoria;
		}
	}
	
	var request = new Request.JSON({
	url: '/get.php?a=getvideo&tipo='+tipo+'&ref='+ref+'&pag='+pagina+'&'+what,
	onComplete: function(jsonObj) {
		if (jsonObj.previews == undefined) {
			$('blocco-thumbnail').removeClass("loaderbar");
			$('blocco-thumbnail').set('html', "<h3>Nessun video trovato</h3>");
		}
		else {
			addImages(jsonObj.previews);
			$('blocco-thumbnail').removeClass("loaderbar");
			$('paginatore').set('html', jsonObj.paginatore);
		}
		}
	}).send();
}

function mostra_sottocategorie(id,opz)
{
	if($("sotto_"+id).hasClass("hidden")==true)
	{
		$("sotto_"+id).removeClass("hidden"); 
	}
	else 
	{
		//if(opz==0) 
		$("sotto_"+id).addClass("hidden");
	}
}

function isDefined(variable)
{
return (!(!(document.getElementById(variable))))
}

function video(id,ref,autoplay)
{
	lastid = id;
	var request = new Request.JSON({
			url: '/get.php?a=playvideo&ref='+ref+'&id='+id,
			onComplete: function(jsonObj) {
					$("titolo").set("html",jsonObj.tit);
					$("desc").set("html",jsonObj.desc);
					$("cat").set("html",jsonObj.cat);
					$("dur").set("html",jsonObj.dur);
					$("dat").set("html",jsonObj.dat);
					if (Browser.Engine.gecko == true) {
						myURI.set('fragment', id);
						myURI.go();
					}
					if (jsonObj.ext == 'wmv') {
						last = 'wmv';
						var ap = autoplay==1 ? "1" : "0";
						$("video").set("html","<object style='z-index:0' type=\"video/x-ms-wmv\" standby=\"Loading Windows Media Player components...\" data=\"/inc/php/get_asx.php?id="+jsonObj.id_old+"&idn="+id+"&ref="+ref+"\" width=\"480\" height=\"360\"><param name=\"src\" value=\"/inc/php/get_asx.php?id="+jsonObj.id_old+"&idn="+id+"&ref="+ref+"\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"autostart\" value=\""+ap+"\" /><param name=\"controller\" value=\"true\" /><param name=\"AllowChangeDisplaySize\" value=\"1\" /><param name=\"DisplaySize\" value=\"0\" /><param name=\"ShowControls\" value=\"1\" /><param name=\"ShowStatusBar\" value=\"0\" /></object>");
					}
					else {
						var ap = autoplay==1 ? "true" : "false";
						var obj = {file:jsonObj.file,image:'/inc/php/img.php?parametri='+id+',480,360',type:"video",title:jsonObj.tit};
						last2 = last;
						last = 'mp4';
						if(last2=="mp4")
						{
							$('mpl').sendEvent("LOAD",obj);
							$('mpl').sendEvent('PLAY', 'true');
						}
						else
						{	
							if(logo==1)
							{
								logovar = '&logo=/inc/images/41icon.png';
							}
							else logovar = '';
							var so = new SWFObject('/inc/swf/player.swf','mpl','480','359','9.0.98');
							so.addParam('allowscriptaccess','always');
							so.addParam('allowfullscreen','true');
						    so.addParam('wmode', "transparent");
							so.addParam('flashvars','image=/inc/php/img.php?parametri='+id+',480,360&stretching=fill&controlbar=over&skin=/inc/swf/stylish.swf&file='+jsonObj.file+'&backcolor=ffffff&frontcolor=ffffff&screencolor=292929&plugins=yourlytics-1&autostart='+ap+'&yourlytics.callback=http://www.41agenziatv.com/inc/php/vstats.php'+logovar);
							so.write('video');	
								if (Browser.Platform.name != "linux") {
									//$('mpl').addModelListener("STATE", "stateListener");
								}
						}
						last = 'mp4';
					}
			}
		}).send();
		videocorrelati(lastid);
}

window.addEvent('domready', function() {
	$('form-ricerca').addEvent('submit', function(e) {
		e.stop();	
		//alert();
		get_videos($('ricercaf').value,1,"ricerca","ricerca");
	});

	new Autocompleter.Request.JSON($('ricercaf'), '/get.php?op=cercavideo', {
	'width': '',
	'injectChoice': function(token){
		var choice = new Element('li');
		new Element('span', {'class':'titolo','html': this.markQueryValue(token)}).inject(choice);
		new Element('span', {
			'class':'play',
			'html': '<a>play</a>',
			'events': {
				'click': function(e){
					e.stop();
			var risposta = new Request.HTML({url:'/get.php',
			onSuccess: function(responseTree, responseElements, responseHTML) {
					if(responseHTML>0 && responseHTML!="")
					{
						var myFx = new Fx.Scroll(window).toElement('wrapper');
						setTimeout("video(" + responseHTML + ",'ricerca','1');", 1000);
						$('ricercaf').value = "";					
					}
				}
			}).post('/get.php?op=trovavideo&value=' + token);
        	}
		 }
		}).inject(choice);
		choice.inputValue = token;
		this.addChoiceEvents(choice).inject(this.choices);
	}
	});

});