var ordencampo	//campo por el que se ordena
var ordentipo	//ascendente o descendente
var tablatipo	//tabla de secciones o de subsecciones
var db		//scripts o herramientas

//indica novedad con imagen
function EsNuevo2( fecha ) {
	var ano = fecha.substr(0,4)
	var mes = fecha.substr(4,2)
	var dia = fecha.substr(6,2)
	var fecha1 = new Date( ano, mes - 1, dia )
	var hoy = new Date ()
	var fecha2 = new Date( hoy.getFullYear(), hoy.getMonth(), hoy.getDate() )
	var diferencia = fecha2.getTime() - fecha1.getTime()
	var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
	if (dias < 30) {
		return '&nbsp;<img src="/images/nuevo.gif" title="Añadido contenido el ' + dia +'/' + mes + '/' + ano + '" border="0">'
	} else {
		return ''
	}
}

//guardando datos
function ObjetoSec( tit, sec, tot, fech, desc, rut ) {
	this.titulo = tit
	this.seccion = sec
	this.total = tot
	this.fecha = fech
	this.descripcion = desc
	this.ruta = rut
}

//crea la tabla
//tipo 0 >> secciones
//tipo 1 >> subsecciones
function creaTabla( tipo, db ) {
	var n, celda, color
	var enlace_titulo, enlace_fecha, como
	var tabla = '<table id="dl-tbl-list">'
	var urlini, urlfin, titulo
	if ( tipo ) {	//subsecciones
		urlini = '<a href="/cgi-bin/DBread.cgi?tabla=' + db + '&campo=5&clave='
		urlfin = '" class="prod">'
		titulo = 'Categoría'
	}
	else {		//secciones
		urlini = '<a href="/'
		urlfin = '/indices.html">'
		titulo = 'Sección'
	}
	
	//flecha marca ordenado por
	var flecha
	if ( ordentipo ) {
		flecha = '<img src="/images/flecha_up.gif">'
		como = 0
	} else {
		flecha = '<img src="/images/flecha_down.gif">'
		como = 1
	}
	
	if ( ordencampo ) {
		enlace_titulo = '<a href="javascript:ordenar(0, 1, ' + tipo + ', \'' + db + '\')">' + titulo + '</a>'
		enlace_fecha = '<a href="javascript:ordenar(1,' + como + ', ' + tipo + ', \'' + db + '\')">Ultimo ' + flecha + '</a>'
	} else {
		enlace_titulo = '<a href="javascript:ordenar(0,' + como + ', ' + tipo + ', \'' + db + '\')">' + titulo + ' ' + flecha + '</a>'
		enlace_fecha = '<a href="javascript:ordenar(1, 1, ' + tipo + ', \'' + db + '\')">Ultimo</a>'
	}

	tabla += '<tr class="whttitle"><th>' + enlace_titulo + '</th>' +
		'<td class="count">Total</td>' +
		'<td class="date">' + enlace_fecha + '</td>' +
		'</tr>'

	for ( n = 0; n < datos.length; n++) {
		if ( (n%2)==0  ) color = 'ylw'
		else color = 'wht'

		tabla += '<tr class="' + color + '">' +
			'	<th>' +
			urlini + (tipo ? datos[n].seccion : datos[n].ruta) + urlfin +
			datos[n].titulo +
			'		</a>' +
			'		<div class="thd">' +
			datos[n].descripcion +
			'		</div>' +
			'	</th>' +
			'	<td class="count">' +
			datos[n].total + EsNuevo2( datos[n].fecha ) +
			'	</td>' +
			'	<td class="date">' +
			datos[n].fecha.substring(6) + '/' + datos[n].fecha.substring(4,6) + '/' + datos[n].fecha.substring(0,4) +
			'	</td>' +
			'</tr>'

	}

	tabla += '<tr><td class="btmbd"></td><td class="btmbd"></td><td class="btmbd"></td></tr></table>'
	return tabla
}

//muestra la tabla
function pintaTabla( tipo, db ) {
	if (document.getElementById) {
		document.getElementById("miTabla").innerHTML = creaTabla( tipo, db )
	} else if (document.all) {
     		document.all.miTabla.innerHTML = creaTabla( tipo, db )
	}
}

//ordena por titulo ascendente
function ordenaTituloA(a, b) {
	if ( a.titulo < b.titulo ) return -1
	else if ( a.titulo > b.titulo ) return 1
	return 0
}

//ordena por fecha ascendente
function ordenaFechaA(a, b) {
	if ( a.fecha < b.fecha ) return -1
	else if ( a.fecha > b.fecha ) return 1
	return 0
}

//ordena por titulo descendente
function ordenaTituloD(a, b) {
	if ( a.titulo > b.titulo ) return -1
	else if ( a.titulo < b.titulo ) return 1
	return 0
}

//ordena por fecha descendente
function ordenaFechaD(a, b) {
	if ( a.fecha > b.fecha ) return -1
	else if ( a.fecha < b.fecha ) return 1
	return 0
}

//reordena
//	que = 0: por titulo
//		1: por fehca
//	como = 0: ascentente
//		 1: descendente
function ordenar( que, como, tipo, db ) {
	ordencampo = que
	ordentipo = como	
	if ( que == 0 ) {			//titulo
		if ( como ) datos.sort( ordenaTituloA )
		else datos.sort( ordenaTituloD )
	} else if ( que == 1 ) {	//fecha
		if ( como ) datos.sort( ordenaFechaA )
		else datos.sort( ordenaFechaD )
	}
	pintaTabla( tipo, db )
}

//inicia
function nueva_tabla() {
	ordenar( ordencampo, ordentipo, tablatipo, db )
}
