// JavaScript Document

function $(id){	return document.getElementById(id);	}

function processTitles(response,divId){
	var titles = eval("(" + response.responseText + ")");//que fa aixo???
	
	unfold (divId);
	
	var nbTitles = titles.length;
	
	//borrar el titles
	processRemoveAllTitles();
	var j=0;
	var k=2;
	var color = 0;
	var row = 0;
	var titleTable = document.createElement("table");
	var titleTablebody = document.createElement("tbody");
	//
	
	//alert (nbTitles)
	for(i=0;i<nbTitles;i++){
		
		var title = titles[i];//Liste des topics à afficher
	
		/*if (i==nbTitles-1) {
			alert (i+" im in creacio!! "+k)
			j=k;
			var titleTable_row = document.createElement("tr");
		}*/
		
		if (j==0){
			//alert(j+" "+i+" = creacio");
			
			var titleTable_row = document.createElement("tr");

			
		}
		//ecrit une cellule dans une ligne
		if ((j>=0)&&(j<=k)){
			//alert(j+" "+i+" = adjuncio");
			if ((row%2) == 0){color = 0;
				//if ((j%2) == 0 ) {color = 0}
				//else {color = 1}
			}else{color = 1;
				//if ((j%2) == 0 ) {color = 1}
				//else {color = 0}
			}
			Item_AddCellInRow( titleTable_row, null, getTitleHTML(title,color), "title_name" );
			
		}
		//termine une ligne
		if (j==k || i==nbTitles-1 ){
			//alert(j+" "+i+" = finalitzacio");
			//Item_AddCellInRow( titleTable_row, null, getTitleHTML(title), "title_name" );
			titleTablebody.appendChild(titleTable_row);
			//faire ca au-dessous que à la fin
	    	row++;
			j=0;
		}else{j++;}
		
	}
	titleTable.appendChild(titleTablebody);

	var titleTableHTML = document.createElement("div");	// faire ca 
	titleTableHTML.className="title_table";	
	titleTableHTML.appendChild( titleTable );
	// Ajout du Topic sur la page
	//
	$(divId).appendChild( titleTableHTML );
	//createButtons();	
}

function processTags(response,divId){
	var tags = eval("(" + response.responseText + ")");
	
	var nbTags = tags.length;
	unfold (divId);
	//borrar el titles
	processRemoveAllTitles();
	var j=0;
	var k=5;
	var titleTable = document.createElement("table");
	var titleTablebody = document.createElement("tbody");
	
	for(i=0;i<nbTags;i++){
		
		var tag = tags[i];//Liste des topics à afficher
	
		/*if (i==nbTags-1) {
			j=k;
			var titleTable_row = document.createElement("tr");
		}*/
		
		if (j==0){
			//alert(j+" "+i+" = creacio");
			
			var titleTable_row = document.createElement("tr");

			
		}
		//ecrit une cellule dans une ligne
		if ((j>=0)&&(j<=k)){
			//alert(j+" "+i+" = adjuncio");
			Item_AddCellInRow( titleTable_row, null, getTagHTML(tag), "title_name" );
			
		}
		//termine une ligne
		if (j==k || i==nbTags-1){
			//alert(j+" "+i+" = finalitzacio");
			//Item_AddCellInRow( titleTable_row, null, getTitleHTML(title), "title_name" );
			titleTablebody.appendChild(titleTable_row);
			//faire ca au-dessous que à la fin
	    	
			j=0;
		}else{j++;}
		
	}
	titleTable.appendChild(titleTablebody);

	var titleTableHTML = document.createElement("div");	// faire ca 
	titleTableHTML.className="title_table";	
	titleTableHTML.appendChild( titleTable );
	// Ajout du Topic sur la page
	//
	$(divId).appendChild( titleTableHTML );
		
}

function processTopTags(response,divId){
	var tags = eval("(" + response.responseText + ")");
	var maxEntries = 0;
	var minEntries = 0;
	var nbTags = tags.length;
	//alert (nbTags);
	processRemoveTags();

	maxEntries = tags[0].entries;
	minEntries = tags[nbTags - 1].entries;
	//alert (maxEntries/maxEntries+" "+minEntries/maxEntries)
	var llindar1 = 0.8;
	var llindar2 = 0.4;

	for(i=0;i<nbTags;i++){
		var tag = tags[i];	
		var level = tag.entries/maxEntries;
		if (level > llindar1) {color = 1};
		if ((level <= llindar1)&&(level > llindar2)) {color = 2}
		if (level <= llindar2) {color = 3}
		$(divId).appendChild( getTopTagHTML(tag,color) );
		//alert (tag.subject)
	}
		
}

function processTagsXCat(response,divId){
	var tags = eval("(" + response.responseText + ")");
	var nbTags = tags.length;	
	processRemoveTags();
	for(i=0;i<nbTags;i++){
		var tag = tags[i];	
		color = 2;	
		if (nbTags >20)	{color = 3;}
		$(divId).appendChild( getTopTagHTML(tag,color) );
		//alert (tag.subject)
	}
		
}

function processTagsXTopic(response,topicId){
	var tags = eval("(" + response.responseText + ")");
	var nbTags = tags.length;	
	
	$("tagXtopic_"+topicId).appendChild( document.createTextNode("Tags: ") );	
	for(i=0;i<nbTags;i++){
		var tag = tags[i];	
		var color = 4;	
		$("tagXtopic_"+topicId).appendChild( getTopTagHTML(tag,color) );
		//alert (tag.subject)
	}
		
}

function processCatsXTopic(response,topicId){
	var cats = eval("(" + response.responseText + ")");
	var nbcats = cats.length;
	
	$("catXtopic_"+topicId).appendChild( document.createTextNode("Categorias: ") );	
	for(i=0;i<nbcats;i++){
		var cat = cats[i];			
		$("catXtopic_"+topicId).appendChild( getCatRankHTML(cat) );
		//alert (tag.subject)
	}
		
}


// funcio per omplir el tagPannel
function processCategories(response,divId){
	//alert ("Es aqui??");//revisio IE
	var categs = eval("(" + response.responseText + ")");
	
	var nbCats = categs.length;
	//alert (sort(tags));
	cats = categs;


	for(i=0;i<nbCats;i++){
		var cat = cats[i];	
		
		$(divId).appendChild( getCategoryHTML(cat) );
	}
	//*/	
}

function processTopicsREDUX(response,divId){
	var topics = eval("(" + response.responseText + ")");
	if (topics == null){alert ("ningun resultado"); return null;} 
	var nbTopic = topics.length;
	 
	if (nbTopic >= 8) {
		processRemoveAllTopics();
		processTitles(response,"top");
		return;
	}
	//fold ("top");	
	var topicIds = new Array();
	
	// Nettoyage complet de la DIV principale
	//
	processRemoveAllTopics();
	
	
	for(i=0;i<nbTopic;i++){
			
		//Liste des topics à afficher
		//				
		var topic = topics[i];
		
		// Stockage des identifiants
		//
		
		topicIds.push(topic.id);
		
		// Ajout du Topic sur la page		
		
		$(divId).appendChild( getTopic2HTML(topic) );
		
		checkFavorit(topic.id);
		//Drag.init( 	$( "topic_title_"+topic.id ) , $( "topic_"+topic.id ) );
						
	}
		
	// Ensuite pour chaque topicId, recharger le contenu par une méthode XmlRpc
	//
	/*for(j=0;j<topicIds.length;j++){
		loadItems(topicIds[ j ]);
	}*/
	
	searchResults();
}

function processTopics(response,divId){
	var topics = eval("(" + response.responseText + ")");
	if (topics == null){alert ("ningun resultado"); return null;} 
	var nbTopic = topics.length;
	var hops;
	processRemoveAllTopics();
	clearfold();
	 
	if (nbTopic > maxDisp) {
		$("fold").appendChild(document.createTextNode(nbTopic+" Resultados"));
		if (showall == 0) {
		var varem = nbTopic%maxDisp;
		if (varem == 0) {hops = parseInt(nbTopic/maxDisp);}
		else{hops = parseInt(nbTopic/maxDisp)+1;}
		
		
		$("fold").appendChild(document.createElement("br"));
		for (ki=1; ki <= hops; ki++) {
			if (actualHop == ki) {
				$("fold").appendChild(document.createTextNode(ki+" "));
			}else{
				var index = document.createElement("a");
				index.id = ki;
				index.setAttribute('href','#');
				//alert (index.id)
				index.appendChild(document.createTextNode(ki+" "));
				index.onclick = function(){ actualHop = this.id; processTopics(response,divId);}
				$("fold").appendChild(index); 
			}
		}
		var index = document.createElement("a");
		index.setAttribute('href','#');
		//alert (index.id)
		index.appendChild(document.createTextNode(" (ver todos)"));
		index.onclick = function(){ showall = 1; processTopics(response,divId);}
		$("fold").appendChild(index);
		$("fold").appendChild(document.createElement("br"));
		var anterior = document.createElement("a");
		anterior.appendChild(document.createTextNode("<< anterior "));
		anterior.setAttribute('href','#');
		anterior.onclick = function() {actualHop = parseInt(actualHop) - 1; processTopics(response,divId);}
		var siguiente = document.createElement("a");
		siguiente.appendChild(document.createTextNode("siguiente >>"));
		siguiente.setAttribute('href','#');
		siguiente.onclick = function() {actualHop = parseInt(actualHop) + 1; processTopics(response,divId);}
		
		if (actualHop == 1) {			
			$("fold").appendChild(siguiente);
		}if ((actualHop > 1)&&(actualHop < hops)) {			
			$("fold").appendChild(anterior);			
			$("fold").appendChild(siguiente);
		}if (actualHop == hops){
			$("fold").appendChild(anterior);
		}
	}
	}
	//$("fold").appendChild(document.createElement("br"));
	//$("fold").appendChild(document.createTextNode("Todos"));
	
	
	var min = (actualHop-1)*maxDisp;
	var max = parseInt(min) + parseInt(maxDisp);
	if (max > nbTopic) {max = nbTopic;}
	
	//alert("hops = "+hops+" actualHop = "+actualHop+" nbTopic = "+nbTopic+" min = "+min+" max = "+max)
	if (showall == 1){min = 0; max = nbTopic;}	
	
	for(i=min;i<max;i++){		
		var topic = topics[i];		
		$(divId).appendChild( getTopic2HTML(topic) );	
		checkFavorit(topic.id);						
	}	
	searchResults();
	showall = 0;
}


function processTopic(response,divId){
	var topic = eval("(" + response.responseText + ")");
	
	// Suppression du topic

	processRemoveTopic(topic.id);
		
	$(divId).appendChild( getTopicHTML(topic) );
	
	checkFavorit(topic.id);
			
	getCatsXTopic(topic.id);
	getTagsXTopic(topic.id);
	commentsNum(topic.id);
	loadItems(topic);
	

	
}

function processRelated(response,topicId){
	var items = eval("(" + response.responseText + ")");
	
	var nbItem = items.length;
	var color = 0;
	//processRemoveAllTopicItems(topicId);
	for(i=0;i<nbItem;i++){

		var item = items[i];
			if (i%2==0) {color = 2;}
			else{color = 3;}
			$("topic_content_"+topicId).appendChild( getTitleHTML(item,color) );
			$("topic_content_"+topicId).appendChild(document.createElement("br"));

	}		
	
}

/*
function processNewTopic(){
	
	
	var topicText = document.createElement("input");
	  topicText.setAttribute('type','text');
	  topicText.setAttribute('name','item');
	var b=document.createElement('input');
	  b.setAttribute('type','submit');
	  b.setAttribute('value','Añadir');
	  b.onclick = function() {addTopic(topicText.value);}
	var topicTitleHTML = document.createElement("div");
	topicTitleHTML.className = "topic_title";
	var topicContentHTML = document.createElement("div");
	topicContentHTML.className = "topic_content";
	var topicHTML = document.createElement("div");
	topicHTML.className = "topic";
	topicHTML.style.left = 600;
	topicHTML.style.top = 250;
	topicHTML.style.width = 200;
	topicHTML.style.height = 170;
	topicHTML.id = "topic_"+0;
	textTitle = document.createTextNode("TITULO:");
	textTitle.className = "topic_title_subject";
	topicTitleHTML.appendChild(textTitle);
	topicTitleHTML.appendChild( topicText );
	topicTitleHTML.appendChild( b );
	topicHTML.appendChild( topicTitleHTML );	
	topicHTML.appendChild( topicContentHTML );
	
	$("main").appendChild( topicHTML );
		
	//Drag.init( 	$( "topic_title_"+topic.id ) , $( "topic_"+topic.id ) );
								
	// Chargement du contenu par une méthode XmlRpc
	//
	//loadItems(topic.id);
	
}
*/
function processNewTopic2(){
	
	var topicHTML = document.createElement("div");
	topicHTML.className = "topic_max";	
	topicHTML.id = "topic_"+0;	
	
	topicHTML.appendChild( createTableNew() );
	
	$("main").appendChild( topicHTML );
		
}

function processClearTopics(response,divId){
	processRemoveAllTopics();
	
	/*var result = eval("(" + response.responseText + ")");
	if (result[0]=="clear"){
		
	}else{alert("what da fuck!!!!")}*/
}

function processItems(response,topic){
	var items = eval("(" + response.responseText + ")");
	
	var nbItem = items.length;
	
	/*if (nbItem ==2){ 
		alert ("Ya existe este item!!"); 
		loadItems(topicId);
	}*/
	// Avant le chargement des items, il faut supprimer tout le contenu du topic
	processRemoveAllTopicItems(topic.id);		
	if (topic.absolut == 0) {getCounter(topic.id);}
	
	//alert (nbItem)
	if (topic.absolut == 0){
		for(i=0;i<nbItem;i++){
			
			var item = items[i];
				//alert (item.subject)
				$("topic_content_"+topic.id).appendChild( getItemHTML(items[i], topic) );
		
			}if (nbItem <= 25) {$("topic_content_"+topic.id).appendChild( getAddItemHTML(topic) );}
	}else{		
		for(i=0;i<nbItem;i++){

			var item = items[i];
					//alert (item.subject)
				$("topic_content_"+topic.id).appendChild( getItemHTMLAbs(items[i], topic) );

		}if (category == 3) {$("topic_content_"+topic.id).appendChild( getAddItemHTML(topic) );}
	}	
	
}

function processCounter(response,topicId){
	var counter = eval("(" + response.responseText + ")");
	
	// var corner = $("bottom_right_"+topicId);
	var corner = $("right_"+topicId);
	
	// processRemoveCounter("bottom_right_"+topicId);
	processRemoveCounter("right_"+topicId);
	//if (counter == "0") {alert ("Has consumido todos los votos para este ranking")}
	var counterNew = document.createElement("a");
	counterNew.appendChild(document.createTextNode(counter));
	counterNew.className = "counter";
	counterNew.setAttribute('title',"Votos restantes");
	counterNew.onmouseover = function(){ linkShow(this);}
	counterNew.onmouseout = function(){linkHide(this);}
	corner.appendChild(counterNew);
}

function processFavorit(response, topicId){
	var favorit = eval("(" + response.responseText + ")");
	var estrella = $("estrella_"+topicId);
	if (estrella){
		if (favorit == 1){estrella.src = rootImage+"estrellaOn"+extension;}
		else{estrella.src = rootImage+"estrellaOff"+extension;}
	}
}

function processRemoveAllTitles(){
	var top = $("top");
	if( ! top ) return ;

	var titles = top.childNodes;
	var nbTitles = titles.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	//
	for(var i = nbTitles - 1 ; i >= 0; i--){
		var o = titles.item(i);
		top.removeChild(o);
	}
}

function processRemoveTags(){
	var top = $("tags_content");
	if( ! top ) return ;

	var titles = top.childNodes;
	var nbTitles = titles.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	//
	for(var i = nbTitles - 1 ; i >= 0; i--){
		var o = titles.item(i);
		top.removeChild(o);
	}
}

function processRemoveAllTopics(){
	var main = $("main");
	if( ! main ) return ;

	var topics = main.childNodes;
	var nbTopics = topics.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	//
	for(var i = nbTopics - 1 ; i >= 0; i--){
		var o = topics.item(i);
		main.removeChild(o);
	}
}

/*function processRemoveAllItems(topicContentId){
	var topic = $(topicContentId);
	if( ! topic ) return ;

	var items = topic.childNodes;
	var nbItems = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	//
	for(var i = nbItems - 1 ; i >= 0; i--){
		var o = items.item(i);
		topic.removeChild(o);
	}
}*/

function processComments(response,topicId){
	var comments = eval("(" + response.responseText + ")");
	
	var nbComments= comments.length;
	processRemoveCounter(topicId);
	processRemoveAllItemComments(topicId);	
	
	
	$("topic_content_"+topicId).appendChild( getAddCommentHTML(comments[0].item_id,topicId) );	
	
	for(i=0;i<nbComments;i++){
			
		var comment = comments[i];
		if (comment.date!="void") {
			$("topic_content_"+topicId).appendChild( getCommentHTML(comment, topicId) );
		}
	}//$("topic_content_"+topicId).appendChild( getAddCommentHTML(comment.item_id,topicId) );	
	
}

function processCommentsCounter(response,topicId){
	var counter = eval("(" + response.responseText + ")");
	
	var corner = $("commentsXtopic_"+topicId);
	
	var	comments = document.createElement("a");		
		comments.setAttribute('href','#');
		comments.className = 'SendAfriend';
		
	if (category == 3) {
		var	comment = document.createTextNode( "Administrar" );
		comments.onclick = function() {window.open("./admin/topicEdit.php?topicId="+topicId, this.target, 'toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=600');}
		comments.appendChild(comment);
		corner.appendChild(comments);
	}else{
		if (counter != 0) {
			var	comment = document.createTextNode( "Ranking Extendido("+counter+")" );
		}else{
			var	comment = document.createTextNode( "Ranking Extendido" );
		}
		comments.onclick = function() {window.open("./topicComments.php?topicId="+topicId, this.target, 'width=600','height=400');}
		comments.appendChild(comment);
		corner.appendChild(comments);
	}
	
		
		
	
}

function clearfold(){
	
	var topic = $("fold");
	if( ! topic ) return ;

	var items = topic.childNodes;
	var nbItems = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	
	for(var i = nbItems - 1 ; i >= 0; i--){
		var o = items.item(i);
		topic.removeChild(o);
	}
	
}
function processRemoveTopic(topicId){
	var main = $("main");
	var topic =  $( "topic_"+topicId );
	if( ! main ) return ;
	if( ! topic ) return ;
	
	main.removeChild(topic);
	
}
function processRemoveTopicDef(response,topicId){
	//alert(topicId+" se va a la hoguera");
	var main = $("main");
	var topic =  $( "topic_"+topicId );
	if( ! main ) return ;
	if( ! topic ) return ;
	
	main.removeChild(topic);
	loadTitles();
}
function processRemoveAllTopicItems(topicId){
	
	var topic = $("topic_content_"+topicId);
	if( ! topic ) return ;

	var items = topic.childNodes;
	var nbItems = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	
	for(var i = nbItems - 1 ; i >= 0; i--){
		var o = items.item(i);
		topic.removeChild(o);
	}
	
}
function processRemoveAlmostAllTopicItems(topicId){
	
	var topic = $("topic_content_"+topicId);
	if( ! topic ) return ;

	var items = topic.childNodes;
	var nbItems = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	if (nbItems > 3) {
		//alert (nbItems)
		for(var i = 0 ; i <= nbItems - 3 ; i++){
			//alert(i+" "+nbItems)
			var o = items.item(i);
			topic.removeChild(o);
		}
	}
}
function processRemoveLastTopicItem(topicId){
	var topic = $("topic_content_"+topicId);
	if( ! topic ) return ;

	var items = $("topic_content_"+topicId).childNodes;
	var nbItem = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	//
	var i = nbItem - 1
	var o = items.item(i);
	topic.removeChild(o);
}
function processRemoveAllItemComments(topicId){
	
	var topic = $("topic_content_"+topicId);
	if( ! topic ) return ;

	var items = topic.childNodes;
	var nbItems = items.length;
	
	// Suppression par le dernier car le pointeur se décalle au fur et à mesure
	
	for(var i = nbItems - 1 ; i >= 1; i--){
		var o = items.item(i);
		topic.removeChild(o);
	}
	
}
function processRemoveCounter(divId){
	var corner = $(divId);
	if (!corner){return false;}
	var counterOld = corner.childNodes;
	if (counterOld.length == 1){	corner.removeChild(counterOld.item(0));}
}
function deleteReception(){}

