current_overID = "";
last_overID = "";
SITIO_RUTA="http://semana.expreso.ec/";


var WEB_ROOT="/semana";
var _busqueda;


function buscar() {

    _busqueda = $('#txtbusqueda1').val();
    //alert(_busqueda);
    if (_busqueda != "") {
        $('#centro').fadeOut();
        $('#lateral').fadeOut();
        $('#centromulti').fadeOut();
        $('#resultados-busqueda').fadeIn();
        $('div#loader').fadeIn();
       // $('div#contenedor-cuerpo').css('background', 'none');
//        $('div#cuerpo').css('background', 'none');
//        $('#headerResultados').html('<p><strong>Busqueda: </strong>' + _busqueda + '</p>');
//        
//        xmlHttp = GetXmlHttpObject()
//        if (xmlHttp == null) {
//            alert("Browser does not support HTTP Request");
//            return;
//        }
//        var url = WEB_ROOT + "/webservices/noticia.aspx";
//        url = url + "?opcion=Busqueda";
//        url = url + "&texto=" + _busqueda;
//        xmlHttp.onreadystatechange = stateChangedBusqueda;
//        xmlHttp.open("GET", url, true);
//        xmlHttp.send(null);


    } else {
        alert('Debe ingresar un texto para buscar');        
    }
}
function stateChangedBusqueda() 
{ 		
	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{			
		if(xmlHttp.status == 200){
		    xml = xmlHttp.responseText;		    
			ManejarRespuestasBusqueda()
		}					
	}else{		
	}	
}
function ManejarRespuestasBusqueda() {
    $('div#loader').fadeOut();
    $('#headerResultados').html('<p><strong>RESULTADOS DE BUSQUEDA:</strong> '+_busqueda+'</p>');
    if (xml != "") {
        $('#resultados').html(xml);
        initPagination();
    } else {
        $('#resultados').html('<p>No existen resultados para mostrar.<p>');
    }
}

function pageselectCallback(page_index, jq) {
	
    //var new_content = $('#hiddenresult div.result ul:eq(' + page_index + ')').clone();
	//alert($('#hiddenresult div.result ul:eq(' + page_index + ')').html());
	//alert($('#hiddenresult div.result ul li:eq(1)').html());
	//alert($('#hiddenresult div.result ul li').length);
	
	var items_per_page = 10;
	var max_elem = Math.min((page_index+1) * items_per_page, $('#hiddenresult div.result ul li').length);
	var newcontent = '<ul>';
                
	// Iterate through a selection of the content and build an HTML string
	for(var i=page_index*items_per_page;i<max_elem;i++)
	{
		//newcontent += '<dt>' + members[i][0] + '</dt>';
		//newcontent += '<dd class="state">' + members[i][2] + '</dd>';
		newcontent += '<li>'+$('#hiddenresult div.result ul li:eq(' + i + ')').clone().html()+'</li>';
	}
  	newcontent += '</ul>';
	$('#Searchresult').html(newcontent);
	
//    $('#Searchresult').empty().append(new_content);
    return false;
}

function initPagination() {
    var num_entries = $('#hiddenresult div.result').length;
	var tot_entries = $('#hiddenresult div.result ul li').length;
    //alert(num_entries);
    $('#totalbusqueda').html('<p><strong>TOTAL RESULTADOS: </strong> ' + tot_entries + '</p>');
    // Create pagination element
	if(tot_entries>0){
		$("#PaginationBusqueda").pagination(tot_entries, {
			//num_edge_entries: 1,
			//num_display_entries: 10,
			callback: pageselectCallback,
			items_per_page: 10
		});
	}
}

/******/
function consultarCategoria(categoria) {
    
        $('#centro').fadeOut();
        $('#lateral').fadeOut();
        $('#centromulti').fadeOut();
        $('#resultados-busqueda').fadeIn();
        $('div#loader').fadeIn();
        $('div#contenedor-cuerpo').css('background', 'none');
        $('div#cuerpo').css('background', 'none');
        $('#headerResultados').html('<p><strong>' + categoria + '</strong></p>');

        xmlHttp = GetXmlHttpObject()
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }
        var url = WEB_ROOT + "/webservices/noticia.aspx";
        url = url + "?opcion=BusquedaCategoria";
        url = url + "&texto=" + categoria;
        xmlHttp.onreadystatechange = stateChangedBusquedaCategoria;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
		//alert(url);
    
}
function stateChangedBusquedaCategoria() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if (xmlHttp.status == 200) {
            xml = xmlHttp.responseText;
            ManejarRespuestasBusquedaCategoria()
        }
    } else {
    }
}
function ManejarRespuestasBusquedaCategoria() {
    $('div#loader').fadeOut();
    //$('#headerResultados').html('<p><strong>RESULTADOS DE BUSQUEDA:</strong> ' + _busqueda + '</p>');
    if (xml != "") {
        //$('div#resultados').css('border', 'solid 1px red');
        //$('div#resultados').css('floar', 'left');
        //$('div#resultados').css('width', '600px');
        $('#resultados').html(xml);
        initPagination();
    } else {
        $('#resultados').html('<p>No existen resultados para mostrar.<p>');
    }
}





/***************************************/

var cont = 0
function abreVentana(pagina) {
    cont++
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    if (is_chrome) {
        location.href = pagina;
        //alert(pagina);
    } else {
        eval('ventana' + cont + "=window.open(pagina,'ventana'+cont,'')")
    }
}
function cerrar() {
    for (m = 1; m <= cont; m++) {
        if (eval('ventana' + m)) {
            eval('ventana' + m + ".close()")
        }
    }
    cont = 0
}

			
		
