$().ready(function(){
    $("#modalwinInfo").dialog({
        modal: true,
        resizable: false,
        autoOpen: false,
        minHeight: 100,
        minWidth: 300,
        draggable: false,
        closeOnEscape: true,
        bgiframe: true
    });
    $('input[name=sortBusq]:radio').click(function(){
        ordename();
    });
    //Rollover table-resultados
    $(".table-resultados tr").mouseover(function(e) {
        $(this).addClass("hover");
    });
    $(".table-resultados tr").mouseout(function(e) {
        $(this).removeClass("hover");
    });
    //Rollover Cuadricula-resultados
    $(".cuadricula .list-fila .item").mouseover(function(e) {
        $(this).addClass("hover");
    });
    $(".cuadricula .list-fila .item").mouseout(function(e) {
        $(this).removeClass("hover");
    });
});


function cambia_url_filtros(el)
{
    //$el = $('#voto_negativo option:selected');
    $el = $(el);
    window.location = $(el).children('option:selected').val();
}

function ordename()
{
    orden = $('input[name=sortBusq]:radio:checked').val()
    //window.location.replace('/' + pestania + '/' + busqueda + '/o/' + $el.val());
    window.location = '/' + pestania + '/' + busqueda + '/o/' + orden;
}

function pagina_portadas(pagina)
{
    parametros = "tipo="+ tipo + "&pagina=" + pagina
    $.ajax({
        type: "POST",
        dataType: "json",
        /* url: "/ofertas/_ajax/paginadoPortadas",*/
        url: "/portadas/ajax/paginadoPortadas",
        data: parametros,
        beforeSend: function(xhr) {
            $('#modalwinInfo').dialog('option', 'title', 'Cargando').dialog('option', 'width', 300).dialog('option', 'height', 'auto').html("<p>" + cargando + "</p>").dialog( 'close' ).dialog( 'open' );
        },
        success: function(data) {
            if(data.error == 0){
                $('.colleft .texto .productos').html(data.datos);
                $('#modalwinInfo').dialog( 'close' );
            }else{
                $('#modalwinInfo').dialog('option', 'title', 'Error').html("<p>"+data.error+"</p>").dialog( 'close' ).dialog( 'open' );
            }
        },
        error: function (xhr, desc, exceptionobj) {
            $('#modalwinInfo').dialog('option', 'title', 'Error').html("<p>"+desc+"</p>").dialog( 'close' ).dialog( 'open' );
        }
    });
}

function contraerFiltros() {
    //Contrae Filtros
    $('div.plus').each(function () {
        if( ! $(this).children('input[type=checkbox]').is(':checked') && ! $(this).children('.minus').children('div').children('input[type=checkbox]').is(':checked')   ){
            $(this).children('a').removeClass('less').addClass('more');//
            $(this).children('div.minus').hide();
        }
    });
    //$('div.plus div.minus').hide();
    $('div.plus a.more, div.plus a.less').click(function(e){
        e.preventDefault();
        $(this).parent().children('.minus').toggle() ;
        if($(this).hasClass('more')){
            $(this).removeClass( 'more' );
            $(this).addClass( 'less' );
        }
        else{
            $(this).removeClass( 'less' );
            $(this).addClass( 'more' );
        }
    });

    $('input[name=price]:radio, input[type=checkbox]').click(function(e){
        document.location = $(this).val();
    });
}

function getFiltros()
{
    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getFiltros",
        data: parametros,
        beforeSend: function() {
            $('#rt01').html(cargando_img);
        },
        success: function(data) {
            if (data.error != '0') {
                $('#rt01').html('error en el proceso');
            }
            else {
                $('#rt01').html(data.datos);
                //Contrae Filtros
                $('div.plus').each(function () {
                    if( ! $(this).children('input[type=checkbox]').is(':checked') && ! $(this).children('.minus').children('div').children('input[type=checkbox]').is(':checked')   ){
                        $(this).children('a').removeClass('less').addClass('more');//
                        $(this).children('div.minus').hide();
                    }
                });
                contraerFiltros();
            }
        }/*,
        error: function (xhr, desc, exceptionobj) {
                $('#rt01').html(desc + "<br>" + exceptionobj + "<br>" + xhr);
        }*/
    });
}


function getFiltroZona()
{
    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getFiltroZona",
        data: parametros,
        beforeSend: function() {
            $('#filterArea').html('<legend>Zonas</legend>' + cargando_img);
        },
        success: function(data) {
            if (data.error != '0') {
                $('#filterArea').html('error en el proceso');
            }
            else {
                $('#filterArea').html(data.datos);
                contraerFiltros();
            }
        }/*,
        error: function (xhr, desc, exceptionobj) {
                $('#filterArea').html(desc + "<br>" + exceptionobj + "<br>" + xhr);
        }*/
    });
}
function getFiltroCats()
{

    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getFiltroCats",
        data: parametros,
        beforeSend: function() {
            $('#filterCategories').html('<legend>Categorías</legend>' + cargando_img);
        },
        success: function(data) {
            if (data.error != '0') {
                $('#filterCategories').html('error en el proceso');
            }
            else {
                $('#filterCategories').html(data.datos);
                contraerFiltros();
            }
        }/*,
        error: function (xhr, desc, exceptionobj) {
                $('#filterCategories').html(desc + "<br>" + exceptionobj + "<br>" + xhr);
        }*/
    });

}

function getFiltroTags()
{
    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getFiltroTags",
        data: parametros,
        beforeSend: function() {
            $('#filterTags').html('<h3>ETIQUETAS</h3>' + cargando_img);
        },
        success: function(data) {
            if (data.error != '0') {
                $('#filterTags').html('error en el proceso');
            }
            else {
                $('#filterTags').html(data.datos);
                contraerFiltros();
            }
        },
        error: function (xhr, desc, exceptionobj) {
                $('#filterTags').html(desc + "<br>" + exceptionobj + "<br>" + xhr);
        }
    });
}

function getFiltroPrecio()
{
    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getFiltroPrecio",
        data: parametros,
        beforeSend: function() {
            $('#filterPrice').html('<legend>Precio</legend>' + cargando_img);
        },
        success: function(data) {
            if (data.error != '0') {
                $('#filterPrice').html('error en el proceso');
            }
            else {
                $('#filterPrice').html(data.datos);
                contraerFiltros();
            }
        }/*,
        error: function (xhr, desc, exceptionobj) {
                $('#filterPrice').html(desc + "<br>" + exceptionobj + "<br>" + xhr);
        }*/
    });
}

function getBusquedasRelacionadas()
{
    parametros = "query="+ busqueda + "&tab=" + pestania;
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/ofertas/_ajax/getBusquedasRelacionadas",
        data: parametros,
        beforeSend: function() {
            $('#relatedSearches').html('<h2>Búsquedas Relacionadas</h2>' + cargando_img);
        },
        success: function(data) {
            if (data.error == '0') {
                $('#relatedSearches').html('<legend><span>Búsquedas<br />Relacionadas</span></legend>' + data.datos);
                contraerFiltros();
            }
            else {
                $('#relatedSearches').html('');
            }
        }
    });
}