var vista = "nube";
var orden = 0;

var timeout;

function rand() {
	return Math.round(Math.random() * 9999);
}

function trim(cadena) {
	for(i=0; i<cadena.length; )	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

function openInfoDialog(str) {
  Dialog.info(str,
               {className: "alphacube", width:150, height:50, showProgress: false});
  timeout = 3;
  setTimeout(infoTimeout, 1000);
}

function infoTimeout() {
	timeout--;
	if (timeout > 0) {
		setTimeout(infoTimeout, 1000)
	} else {
		Dialog.closeInfo();
	} 
}

function ordena(txFecha, txOrden) {
	if (txOrden.options[txOrden.selectedIndex].value == 3) {
		cargaLista(txFecha, txOrden);
	} else {
		if (vista == "nube") {
			cargaNube(txFecha, txOrden);
		} else {
			cargaLista(txFecha, txOrden);
		}
	}
}

function cargaNube(txFecha, txOrden) {
	var url = 'nube.php?r='+rand()+'&orden='+txOrden.options[txOrden.selectedIndex].value+"&"+txFecha.options[txFecha.selectedIndex].value;
	new Ajax.Updater('contenido_principal', url, { method: 'get' });
	vista = "nube";
	
	window.document.verNube.src = "images/ver_nube_0.jpg";
	window.document.verLista.src = "images/ver_lista_1.jpg";
}

function cargaLista(txFecha, txOrden) {
	var url = 'lista.php?r='+rand()+'&orden='+txOrden.options[txOrden.selectedIndex].value+"&"+txFecha.options[txFecha.selectedIndex].value;
	new Ajax.Updater('contenido_principal', url, { method: 'get' });
	vista = "lista";

	window.document.verNube.src = "images/ver_nube_1.jpg";
	window.document.verLista.src = "images/ver_lista_0.jpg";
}

function buscar(fr) {
	new Ajax.Updater('contenido_principal', 'buscar.php?r='+rand(), { 
		method: 'post',
		parameters: {texto: fr.texto.value}
	});
	vista = "lista";

	window.document.verNube.src = "images/ver_nube_1.jpg";
	window.document.verLista.src = "images/ver_lista_0.jpg";
}

function goURL(id) {
	menu = document.getElementById("menuDiv");
	menu.innerHTML = "";
	menu.style.display = "none";
	
	//Dialog.info({url: "getVoto.php?id=" + id, options: {method: 'get'}}, {className: "alphacube", width:540});
}

function insertar(fr) {
	var ok = true;
	var msg = "";
	
	if (trim(fr.tema.value) == "") {
		msg += "- Debes ingresar un Tema<br>";
		ok = false;
	}
	
	if (trim(fr.url.value) == "" || fr.url.value == "http://") {
		msg += "- Debes ingresar una URL<br>";
		ok = false;
	}
	
	if (trim(fr.tags.value) == "") {
		msg += "- Debes ingresar los Tags asociados<br>";
		ok = false;
	}
	
	if (ok) {
		fr.submit();
	} else {
		Dialog.alert(msg, {className: "alphacube", width:350, height:100, okLabel: "Aceptar", ok:function(win) {debug("validate alert panel"); return true;}}); 
		//alert(msg);
	}
}

function votar(i, t) {
	Dialog.closeInfo();
	
	new Ajax.Request('votar.php', {
		method: 'get',
		parameters: {id: i, tipo: t},
		onSuccess: function(transport) {
			Dialog.alert(transport.responseText, {className: "alphacube", width:400, height:120, okLabel: "Cerrar", ok:function(win) {debug("validate alert panel"); return true;}}); 
			//Dialog.info({url: "getVoto.php?id=" + id, options: {method: 'get'}}, {className: "alphacube", width:540});
			//openInfoDialog(transport.responseText);
		}
	});
	
	if (vista == "nube") {
		cargaNube(window.document.frm_fecha.ordenFecha, window.document.ordenar.orden);
	} else {
		cargaLista(window.document.frm_fecha.ordenFecha, window.document.ordenar.orden);
	}
}

function showMenu(event, str, txFecha) {
	obj = document.getElementById(str);
	var y = event.clientX;
	var x = event.clientY;
	var top = document.documentElement.scrollTop || window.pageYOffset || 0;
	var left = document.documentElement.scrollLeft || window.pageXOffset || 0;

	txt = "x = " + x + "\n";
	txt += "y = " + y + "\n";
	txt += "document.documentElement.scrollTop = " + document.documentElement.scrollTop + "\n";
	txt += "document.documentElement.scrollLeft = " + document.documentElement.scrollLeft + "\n";
	txt += "window.pageYOffset = " + window.pageYOffset + "\n";
	txt += "window.pageXOffset = " + window.pageXOffset + "\n";
	txt += "top = " + top + "\n";
	txt += "left = " + left + "\n";
	
	//alert(txt);

	Xpos = top + x;
	Ypos = left + y + 10;
	
	var url = 'getMenu.php?t='+str+"&"+txFecha.options[txFecha.selectedIndex].value;
	new Ajax.Updater('menuDiv', url, { method: 'get' });
	
	menu = document.getElementById("menuDiv");
	menu.style.top = Xpos + "px";
	menu.style.left = Ypos + "px";
	menu.style.display = "block";
}

function contar(txt) {
	largo = txt.length;
	window.document.getElementById("contador").innerHTML = 30-largo;
}

function hideMenu() {
	menu = document.getElementById("menuDiv");
	menu.innerHTML = "";
	menu.style.display = "none";
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
