var addImages = function(images) {
		images.each(function(image) {
			var el = new Element('div', {'class': 'thumb',
			'events': {
				'click': function(){
							location.href = image.file;
				}
			}
			});
			var lin = new Element('a', {
				'href': image.file
			}).inject(el);
			var span = new Element('span', {'text': image.tit}).inject(el);			
			var img = new Element('img', {'alt': '', 'src': '/inc/php/img.php?parametri='+image.id+',150,150'}).inject(lin);
			el.inject($('blocco-thumbnail'));
		});
	};

function tag_cloud(tag)
{
	get_videos(tag,1,"tagcloud","keyword");
}

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 == "ricerca")
		{
			var what = 'ricerca='+categoria;
		}
	}
	
	var request = new Request.JSON({
	url: '/get.php?a=getvideo&f=mobile&tipo='+tipo+'&ref='+ref+'&pag='+pagina+'&'+what,
	onComplete: function(jsonObj) {
		if (jsonObj.previews == undefined) {
			$('blocco-thumbnail').removeClass("loaderbar");
			$('blocco-thumbnail').set('html', "<div>Nessun video trovato</div>");
		}
		else {
			addImages(jsonObj.previews);
			$('blocco-thumbnail').removeClass("loaderbar");
			$('paginatore').set('html', jsonObj.paginatore);
		}
		}
	}).send();
}

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

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

});
