window.onload = function (){

	if (typeof Json == 'undefined'){
		Json = JSON;
		Json.toString = JSON.encode;
		Json.evaluate = JSON.decode;
	}


	var ajax = new PaginaAJAX();

	ajax.oncomplete= function(){

		getThreadsOnDate = function (anno,mese){

			var list = $$("#ul"+anno+" .ul"+mese);

			if(!list[0].loadingThreads){
				list[0].loadingThreads = true;
			}else{
				return;
			}
			var liste = $$(".subMenuArchivio");

			var loader = new Element('li');
			loader.injectAfter(list[0]);
			loader.setProperty("class","li loader");
			loader.innerHTML = '<img src="../GIFs/commons/ajax-loader_black_new.gif" alt="LOADING..." width="16" height="16" />';

			liste.each(function(item){
				item.dispose();
			});

			var nextLi = list[0].nextSibling;

			if(nextLi !== null && typeof nextLi != "undefined"){
				var classList = nextLi.className.split(" ");
				if (!classList.contains("li")){
					nextLi.dispose();
				}
			}

			ajax.getThreadsOnDate.oncomplete = function (){
				list[0].loadingThreads = false;
				if(typeof list[0].nextSibling != "undefined"){
					if(list[0].nextSibling.className== "li loader"){
						list[0].nextSibling.dispose();
					}
				}
				var newLi = new Element('li');
				newLi.injectAfter(list[0]);
				newLi.innerHTML=this.response;
			}
			ajax.getThreadsOnDate(anno,mese);
		}

		submitSearch = function (){

			var list = $("results");

//			ESISTE GIà LA LISTA
			if(list){
				var listLabel = $("resultTitle");
//			LA LISTA NON ESISTE QUINDI VENGONO GENERATI E RENDERIZZATI GLI ELEMENTI H2 E UL
			}else{
//				TITOLO
				listLabel = new Element('h2');
				listLabel.setProperty("class","titoloRight");
				listLabel.setProperty("id","resultTitle");
				listLabel.injectAfter($('searchForm'));

//				LISTA NON ORDINATA
				list = new Element('div');
				list.setProperty("id","results");

				list.injectAfter($('resultTitle'));
			}

			if(!list.loadingThreads){
				list.loadingThreads = true;
			}else{
				return;
			}

			listLabel.setStyle("display","none");
			list.empty();

			var loaderUl = new Element('ul');
			loaderUl.injectInside(list);
			var loader = new Element('li');
			loader.setProperty("class","li loader");
			loader.innerHTML = '<img src="../GIFs/commons/ajax-loader_black_new.gif" alt="LOADING..." width="16" height="16" />';
			loader.injectInside(loaderUl);

			ajax.getThreadsBySearch.oncomplete = function (){
				list.loadingThreads = false;
				loaderUl.dispose();
				listLabel.setStyle("display","block");
				listLabel.innerHTML=this.response.titleSearch;
				list.innerHTML = this.response.ul;
			}

			ajax.getThreadsBySearch($('searchText').value);
		}

	}

}


