﻿function ajax_resquest(pagina, parametro, contenedor, postback) {

    $(window).scroll(function() {
        $("#loading").css("top", $(window).scrollTop());
    });

    if (parametro == undefined)
        parametro = '';

    $.ajax({
        url: 'Json_Response.aspx',
        type: 'get',
        async: true,
        data: jQuery.param(parametro) + '&ramdon=' + Math.random(),
        dataType: 'json',
        beforeSend: function() { },
        complete: function(objeto, exito) { },
        success: function(data) {


            if (pagina == "home") {
                $("#seleccion").css("display", "block");
                $("#contenido").html("");
                $("#navegacion").html("Inicio");
            }

            if (pagina == "mifun") {
                $("#seleccion").css("display", "none");
                $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a> | Mi Fun | ");
            }

            if (pagina == "miinfo") {
                $("#seleccion").css("display", "none");
                $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a> | Mi Info | ");
            }


            if (pagina == "mapa") {
                $("#seleccion").css("display", "none");
                $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a> |  Mapa del Sitio | ");
            }


            if (pagina == "buscar") {
                $("#seleccion").css("display", "none");
                $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a> | Buscador | ");
            }

            if (pagina == "portal") {
                $("#seleccion").css("display", "none");
                $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a>");
            }


            var html = new Array();
            var miFun = new Array();
            var miInfo = new Array();
            var buscar = new Array();

            /*
            var Andres = new Print(data, pagina);
            Andres.fabricar();
            */


            if (data.Fabricante != undefined) {
                $.each(data.Fabricante, function(i, fabricante) {
                    addOption("#fabricante", { value: fabricante.id, text: fabricante.nombre, selected: "" });
                });
            }

            if (data.ModeloTelefono != undefined) {
                var miModeloTelefono = "";
                $.each(data.ModeloTelefono, function(i, modelotelefono) {
                    addOption(contenedor, { value: modelotelefono.id, text: modelotelefono.nombre, selected: modelotelefono.selected });
                    if (modelotelefono.selected == "selected")
                        imgModeloTelefono(modelotelefono.imagen)
                    if (pagina == "modelotelefono") {
                        miModeloTelefono = miModeloTelefono + '<img style="width:48px;  height:61px; margin:10px; cursor: pointer;" onclick="selector_continuar({id:' + modelotelefono.id + ',imagen:\'' + modelotelefono.imagen + '\'});" alt="' + modelotelefono.nombre + '"  src="' + modelotelefono.imagen + '"/>';
                        $("#detalle_modelo").html(miModeloTelefono);
                    }
                });

            }


            if (data.TipoItem != undefined) {
                $.each(data.TipoItem, function(i, tipoitem) {
                    if (pagina == "mifun") {
                        miFun[2] = "<div id='mifun-item-" + tipoitem.id + "' style='width:763px; height:auto; float:right;'>";
                        miFun[1] = "<a name='ancla-top' id='top'></a><div id='mifun-banner' class='sprite-banner-" + tipoitem.id + "'></div>";
                        miFun[3] = "<b>Por favor seleccione una categoría para ver el contenido.</b>"
                    } else {
                        addOption("#tipoitem", { value: tipoitem.id, text: tipoitem.nombre, selected: "" });
                    }
                });
            }


            if (data.Destacado != undefined) {

                $.each(data.Destacado, function(i, destacado) {

                    html[0] = "";
                    html[1] = "";
                    html[2] = false;

                    if (pagina == "home") {
                        html[0] = "<div id='swf-destacado'></div>";

                        $("#contenido").html(caja(destacado) + html[0]);

                    }

                    if (pagina == "mifun") {


                        $.each(destacado.TipoItem, function(i, tipoitem) {

                            html[0] = html[0] + "<div id='contenedor-banner-destacado'  style='width:auto; height:19px;'>";
                            html[1] = html[1] + "<div class='hr' style='height:1px;'></div>";
                            html[1] = html[1] + "<div id='contenedor-destacados-" + tipoitem.id + "' class='contenido' >";


                            $.each(tipoitem.Destacado, function(i, destacado) {
                                if ((destacado.id == 1) || (destacado.id == 2)) {

                                    html[0] = html[0] + "<a onclick=toggle_destacados(\'#contenedor-destacados-" + tipoitem.id + "\',\'#contenedor-contenido-destacado-" + tipoitem.id + destacado.id + "\');  target='_parent' class='da-" + destacado.id + destacado.id + "' style='float:left;'></a>";
                                    html[1] = html[1] + "<div id='contenedor-contenido-destacado-" + tipoitem.id + destacado.id + "' style='position:relative; width:" + ((destacado.Item[0].clave.length == 0) ? '763px' : '100%') + "; float:left; " + ((i != 0) ? "display:none'>" : "'>");
                                    html[1] = html[1] + contenido(destacado, "mifun");
                                    html[1] = html[1] + "</div>";
                                    html[2] = true;
                                }
                            });

                            html[1] = html[1] + "</div>";
                            html[0] = html[0] + "</div>";
                            html[0] = html[0] + html[1];

                        });

                        if (html[2] == true) {
                            miFun[3] = html[0];
                        }
                    }
                });
            }



            if (data.Portal != undefined) {
                $.each(data.Portal, function(i, portal) {

                    if (contenedor == undefined) {


                        $('#contenido').html("<div id='flash-portaltematico'></div>" + cajaPortal(portal, contenedor));
                        $('#flash-portaltematico').flash({
                            swf: 'http://mcp.net-people.biz/Estilos/ImgTematicoWeb/' + portal.id + '/banner.swf',
                            width: 922,
                            height: 282,
                            wmode: 'transparent',
                            scale: 'showall'
                        });

                    } else {

                        $.each(portal.TipoItem, function(i, tipoitem) {
                            $('#' + contenedor).html(contenidoPortal(tipoitem, ''));
                            $("#paginado-portaltematico-" + tipoitem.id).html(paginacionPortal(portal.id, tipoitem, 'portal', "contenido-portaltematico-" + tipoitem.id));
                        });
                    }

                });

            }


            if (data.Banner != undefined) {
                $.each(data.Banner, function(i, banner) {
             
                    $('#' + banner.div).flash({

                        swf: banner.archivo,
                        width: $('#' + banner.div).width(),
                        height: $('#' + banner.div).height(),
                        wmode: 'transparent',
                        scale: 'showall'
                    });

                });
            }

            if (data.Categoria != undefined) {

                html[0] = "<div id='mifun-categoria' class='lista' >";
                html[0] = html[0] + "<ul id='lista'>";
                $.each(data.TipoItem, function(i, tipoitem) {

                    $("#navegacion").html($("#navegacion").html() + tipoitem.categoria + ' | ' + tipoitem.nombre);
                    html[0] = html[0] + '<span><a class="titulo" href="javascript:ajax_resquest(\'mifun\',{metodo:\'categoria,tipoitem,destacado\',tiid:' + tipoitem.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" target="_parent" >' + tipoitem.nombre + '</a></span>';
                    $.each(data.Categoria, function(i, categoria) {
                        html[0] = html[0] + '<li id="p' + categoria.id + '" class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;">';
                        html[0] = html[0] + '<a href="javascript:ajax_resquest(\'mifun\',{metodo:\'item\',tiid:' + tipoitem.id + ',cid:' + categoria.id + ',i:1,t:28, fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'#mifun-item-' + tipoitem.id + '\');" >' + categoria.nombre + '</a>';
                        if (categoria.SubCategoria != undefined) {
                            html[0] = html[0] + '<ul id="h' + categoria.id + '">';
                            $.each(categoria.SubCategoria, function(i, subcategoria) {
                                html[0] = html[0] + '<li  id="h' + subcategoria.id + '" class="flecha" >';
                                html[0] = html[0] + ' <a href="#ancla-top" onclick="javascript:ajax_resquest(\'mifun\',{metodo:\'item\',tiid:' + tipoitem.id + ',cid:' + categoria.id + ',scid:' + subcategoria.id + ',i:1,t:28, fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'#mifun-item-' + tipoitem.id + '\');" >' + subcategoria.nombre + '</a>';
                                html[0] = html[0] + '</li>';
                            });
                            html[0] = html[0] + '</ul>';
                        }
                        html[0] = html[0] + '</li>';
                    });
                });

                html[0] = html[0] + '</ul>';
                html[0] = html[0] + '</div>';
                miFun[0] = html[0];

            }


            if (data.Info != undefined) {
                miInfo[0] = "<div id='miinfo-lista' class='lista'>";
                miInfo[0] = miInfo[0] + "<ul id='lista'>";
                $.each(data.Info, function(i, info) {

                    miInfo[0] = miInfo[0] + '<span class="titulo">';
                    miInfo[0] = miInfo[0] + info.nombre;
                    miInfo[0] = miInfo[0] + '</span>';

                    $.each(info.Info, function(i, info) {
                        miInfo[0] = miInfo[0] + '<li id="p' + info.id + '" class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;">';
                        if (info.Info != undefined) {
                            miInfo[0] = miInfo[0] + '<a href="javascript:void(0);" >' + info.nombre + '</a>';
                            miInfo[0] = miInfo[0] + '<ul id="h' + info.id + '">';
                            $.each(info.Info, function(i, info) {
                                miInfo[0] = miInfo[0] + '<li  id="h_' + info.id + '" class="flecha">';
                                if (info.Info != undefined) {
                                    miInfo[0] = miInfo[0] + '<a href="javascript:void(0);" >' + info.nombre + '</a>';
                                    miInfo[0] = miInfo[0] + '<ul id="h_' + info.id + '">';
                                    $.each(info.Info, function(i, info) {
                                        miInfo[0] = miInfo[0] + '<li  id="h_' + info.id + '" class="cuadro">';
                                        miInfo[0] = miInfo[0] + ' <a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'texto\',scid:' + info.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'#info\');" >' + info.nombre + '</a>';
                                        miInfo[0] = miInfo[0] + '</li>';
                                    });
                                    miInfo[0] = miInfo[0] + '</ul>';
                                } else {
                                    miInfo[0] = miInfo[0] + ' <a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'texto\',scid:' + info.id + ',fid:' + $("#fabricante").val() + ",mtid:" + $("#modelotelefono").val() + '},\'#info\');" >' + info.nombre + '</a>';
                                }
                                miInfo[0] = miInfo[0] + '</li>';
                            });
                            miInfo[0] = miInfo[0] + '</ul>';
                        } else {
                            miInfo[0] = miInfo[0] + '<a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'texto\',scid:' + info.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'#info\');" > ' + info.nombre + '</a>';
                        }
                        miInfo[0] = miInfo[0] + '</li>';

                    });

                });

                miInfo[0] = miInfo[0] + '</ul>';
                miInfo[0] = miInfo[0] + '</div>';

            }


            if (data.Mapa != undefined) {

                html[0] = '<div id="mapa" style="width:922px; height:500px;">'
                html[0] = html[0] + '<div id="mapa_inicio" style="  margin: 25px 25px  25px 432px;  "><div style="width:12px; height:12px;  background: #fff url(Imagenes/flecha.png) repeat top center; float:left; margin-right:5px;"></div><a class="titulo" href="javascript:ajax_resquest(\'home\',{metodo:\'destacado,banner\', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});">Inicio</a></div>'
                html[0] = html[0] + '<div style="  margin: 25px 25px  25px 146px;  ">';

                $.each(data.Mapa, function(i, mapa) {

                    if (mapa.TipoItem != undefined) {

                        html[0] = html[0] + '<div id="mapa-tipoitem" class="lista" >';
                        html[0] = html[0] + '<ul id="mapa-tipoitem-lista">';
                        html[0] = html[0] + '<span><a class="titulo" style="color:#000;" target="_parent" >Mi Fun</a></span>';
                        $.each(mapa.TipoItem, function(i, tipoitem) {
                            html[0] = html[0] + '<li id="mifun' + tipoitem.id + '" class="flecha" style="width:92px; border-top:1px solid #CCCCCC;">';
                            html[0] = html[0] + '<a onclick="javascript:ajax_resquest(\'mifun\',{metodo:\'categoria,tipoitem,destacado\',tiid:' + tipoitem.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" target="_parent"  > ' + tipoitem.nombre + '</a>';
                            html[0] = html[0] + '</li>';
                        });
                        html[0] = html[0] + '</ul>';
                        html[0] = html[0] + '</div>';

                    }

                    if (mapa.Info != undefined) {

                        html[0] = html[0] + "<div id='mapa-info' class='lista'>";
                        html[0] = html[0] + "<ul id='lista'>";
                        $.each(mapa.Info, function(i, info) {
                            html[0] = html[0] + '<span class="titulo" style="color:#000;">';
                            html[0] = html[0] + info.nombre;
                            html[0] = html[0] + '</span>';

                            $.each(info.Info, function(i, info) {
                                html[0] = html[0] + '<li id="p' + info.id + '" class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;">';
                                if (info.Info != undefined) {
                                    html[0] = html[0] + '<a href="javascript:void(0);" >' + info.nombre + '</a>';
                                    html[0] = html[0] + '<ul id="h' + info.id + '">';
                                    $.each(info.Info, function(i, info) {
                                        html[0] = html[0] + '<li  id="h_' + info.id + '" class="flecha">';
                                        if (info.Info != undefined) {
                                            html[0] = html[0] + '<a href="javascript:void(0);" >' + info.nombre + '</a>';
                                            html[0] = html[0] + '<ul id="h_' + info.id + '">';
                                            $.each(info.Info, function(i, info) {
                                                html[0] = html[0] + '<li  id="h_' + info.id + '" class="cuadro">';
                                                html[0] = html[0] + ' <a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'info,texto\',scid:' + info.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" >' + info.nombre + '</a>';
                                                html[0] = html[0] + '</li>';
                                            });
                                            html[0] = html[0] + '</ul>';
                                        } else {
                                            html[0] = html[0] + ' <a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'info,texto\',scid:' + info.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" >' + info.nombre + '</a>';
                                        }
                                        html[0] = html[0] + '</li>';
                                    });
                                    html[0] = html[0] + '</ul>';
                                } else {
                                    html[0] = html[0] + '<a href="javascript:ajax_resquest(\'miinfo\',{metodo:\'info,texto\',scid:' + info.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" > ' + info.nombre + '</a>';
                                }
                                html[0] = html[0] + '</li>';

                            });

                        });

                        html[0] = html[0] + '</ul>';
                        html[0] = html[0] + '</div>';

                    }

                    html[0] = html[0] + menuMiGente("#000");
                    html[0] = html[0] + menuMiUtil("#000");


                });

                html[0] = html[0] + '</div>';
                html[0] = html[0] + '</div>';
                $('#contenido').html(html[0]);
                setLista('#mapa-tipoitem,#mapa-info');

            }

            if (data.Texto != undefined) {
                html[0] = '<div id="miinfo-detalle" >';
                html[0] = html[0] + '<div id="info" >';
                html[1] = '';

                $.each(data.Texto, function(i, texto) {
                    if ((texto.id == 216) ||
                        (texto.id == 217) ||
                        (texto.id == 218) ||
                        (texto.id == 219) ||
                        (texto.id == 220) ||
                        (texto.id == 164) ||
                        (texto.id == 241) ||
                        (texto.id == 781)) {
                        if (texto.Categoria != undefined) {


                            html[0] = html[0] + '<div style="margin-top:10px;"><span class="titulo">' + texto.Categoria[0].nombre + '</span><br><br>';

                            $.each(texto.Categoria, function(c, categoria) {
                                if (categoria.SubCategoria != undefined) {
                                    $.each(categoria.SubCategoria, function(sc, subcategoria) {
                                        html[0] = html[0] + '<div class="sprite-flecha_abajo"></div><a href="javascript:ajax_resquest(\'none\',{metodo:\'texto\',scid:' + subcategoria.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" styl e=" text-decoration:underline;" > ' + subcategoria.nombre + '</a>';
                                    });
                                }
                            });
                            html[0] = html[0] + '</div>';
                        }



                    } else {

                        $.each(texto.Noticia, function(n, noticia) {

                            if (n <= 3) {

                                html[0] = html[0] + '<div id="info_categoria_' + noticia.id + '" class="info_categoria" style=" display:' + ((n == 0) ? 'block' : 'none') + ';  ">';

                                html[0] = html[0] + '<span class="titulo" >' + noticia.titulo + '</span>';

                                html[0] = html[0] + '<img src="' + noticia.imagen + '"  />';
                                html[0] = html[0] + '<div id="descripcion" >' + noticia.descripcion + '</div>';

                                if (noticia.horario != "") {
                                    html[0] = html[0] + '<div id="horario" ><b>Horario: </b>' + noticia.horario + '</div>';
                                    html[0] = html[0] + '<div id="canal" ><b>Canal: </b>' + noticia.canal + '</div>';
                                    html[0] = html[0] + '<div id="categoria" ><b>Categoria: </b>' + noticia.categoria + '</div>';
                                }


                                html[0] = html[0] + '</div>';

                                if (texto.Noticia.length > 1)
                                    html[1] = html[1] + '<div  style="height:25px; width:400px; padding:15px 0 0 10px;' + (((n % 2) == 0) ? 'background-color:#f2f2f2;' : '') + '"><a onclick="_toggle(\'#info_categoria_' + noticia.id + '\');" style=" text-decoration:underline;" > >> ' + noticia.titulo + '</a></div>';

                            }

                        });

                    }

                });

                html[0] = html[0] + '<div>';
                html[0] = html[0] + html[1];
                html[0] = html[0] + '</div>';
                html[0] = html[0] + '</div>';
                html[0] = html[0] + '</div>';
                miInfo[1] = html[0];
            }



            if (data.Menu != undefined) {
                html[0] = '';
                $.each(data.Menu, function(i, tipoitem) {
                    html[0] = html[0] + '<li><div><a href="javascript:ajax_resquest(\'mifun\',{metodo:\'categoria,tipoitem,destacado\',tiid:' + tipoitem.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});">' + tipoitem.nombre + '</a></div></li>';
                });
                $("#menu_tipoitem").html(html[0]);
            }

            if (data.Item != undefined) {

                buscar[1] = "";
                var totalNumReg = 0;
                var nombreTipoitem = "";
                var palabraClave = "";

                $.each(data.Item, function(i, item) {
                    $.each(item.TipoItem, function(i, tipoitem) {

                        if (pagina == "mifun") {
                            $("#navegacion").html($("#navegacion").html() + tipoitem.categoria + ' | ' + tipoitem.nombre);
                            miFun[3] = contenido(tipoitem, "mifun", pagina, contenedor);
                        }

                        if (pagina == "buscar") {
                            if (tipoitem.Item != undefined) {

                                if (contenedor == undefined) {
                                    buscar[1] = buscar[1] + '<div class="titulo" style=" position:relative; display:block; margin:0 0 10px 0;  width:922px; height:auto;">';
                                    buscar[1] = buscar[1] + '<a onclick=$(\'#buscar-item-' + tipoitem.id + '\').toggle();>' + tipoitem.Item[0].TipoItem[0].nombre + ' (' + tipoitem.Item[0].numreg + ')</a>';
                                    nombreTipoitem = nombreTipoitem + '<a onclick=$(\'#buscar-item-' + tipoitem.id + '\').toggle();>' + tipoitem.Item[0].TipoItem[0].nombre + ' (' + tipoitem.Item[0].numreg + ')</a>' + ' | ';
                                    buscar[1] = buscar[1] + '</div>';

                                }

                                buscar[1] = buscar[1] + '<div id="buscar-item-' + tipoitem.id + '" style="  display: block; width:922px; height:auto;  float: left; padding-bottom:10px;">';
                                buscar[1] = buscar[1] + contenido(tipoitem, "buscar", pagina, "#buscar-item-" + tipoitem.id);
                                buscar[1] = buscar[1] + '</div>';
                                totalNumReg = totalNumReg + parseInt(tipoitem.Item[0].numreg);
                                palabraClave = tipoitem.Item[0].clave;
                            }

                        }

                    });

                    if (totalNumReg > 0) {
                        if (contenedor == undefined) {
                            buscar[0] = '<div class="titulo" style="text-align:left;  width:100%; font-size:1.2em; color:#000; margin:15px 0px 15px 0;" >';
                            buscar[0] = buscar[0] + '<span class="titulo" style="color:#999;">Resultado de la Búsqueda - ' + palabraClave + ' </span>';
                            buscar[0] = buscar[0] + '<br>';
                            buscar[0] = buscar[0] + '<a onclick=$("div[id^=\'buscar-item-\']").toggle();> | Total (' + totalNumReg + ') | ' + nombreTipoitem;
                            buscar[0] = buscar[0] + '</div>';
                            buscar[1] = buscar[0] + buscar[1];
                        }

                    } else {
                        buscar[1] = '<div style="text-align:left;  width:100%; font-size:1.2em; color:#000; margin:15px 0px 15px 50px;" >';
                        buscar[1] = buscar[1] + '<b>No se ha encontrado ningún producto con esa palabra</b> ';
                        buscar[1] = buscar[1] + '<br><br>Sugerencias';
                        buscar[1] = buscar[1] + '</div>';
                        buscar[1] = buscar[1] + '<div style="text-align:left;  width:100%; color:#000; margin:15px 30px 150px 65px;" >';
                        buscar[1] = buscar[1] + '<ul style="list-style-type: disc">';
                        buscar[1] = buscar[1] + '<li>Asegúrate de que todas las palabras son correctas</li>';
                        buscar[1] = buscar[1] + '<li>Intenta con otras palabras</li>';
                        buscar[1] = buscar[1] + '<li>Intenta con menos palabras</li>';
                        buscar[1] = buscar[1] + '</ul>';
                        buscar[1] = buscar[1] + '</div>';
                        $("#clave").val('');

                    }

                });
            }

            if (data.Push != undefined) {
                $.each(data.Push, function(i, push) {
                    $("#mensaje").html(push.resultado);
                });
            }

            if (data.Email != undefined) {
                $.each(data.Email, function(i, email) {

                    $("#mensaje").html(email.resultado);
                });
            }

            if (data.Cookie != undefined) {
                $.each(data.Cookie, function(i, cookie) {
                    $('#fabricante').val(cookie.fabricante);
                    $('#modelotelefono').val(cookie.modelotelefono);
                    $('#msisdn').val(cookie.msisdn);
                    if (cookie.session == "True")
                        $('#session').attr('checked', true);
                    else
                        $('#session').removeAttr('checked');
                });
            }


            if (pagina == "mifun") {
                if (contenedor == undefined) {
                    $("#contenido").html(miFun[0] + miFun[1] + miFun[2] + miFun[3] + "</div>");
                    setLista('#lista');
                } else {
                    $(contenedor).html(miFun[3]);
                }
            }

            if (pagina == "buscar") {
                if (contenedor == undefined) {
                    $("#contenido").html(buscar[1]);
                } else {
                    $(contenedor).html(buscar[1]);
                }
            }

            if (pagina == "miinfo") {
                if (contenedor == undefined) {
                    $("#contenido").html(miInfo[0] + miInfo[1]);
                    setLista('#lista');
                } else {
                    $(contenedor).html(miInfo[1]);
                }
            }

        },
        error: function() {
            $("#loading").css("background-color", '#FF6666');
            $("#loading").html("Error...");
        }
    });
}

function setLista(display) {

    var ulLista = $(display).find('li').filter('.flecha_abajo');
    ulLista.find('ul').toggle();
    ulLista.find('a').click(function() {
        $("ul[id^='h']").hide();
        $(this).parent().parent().show();
        $(this).parent().parent().parent().parent().show();
        $(this).next().toggle();
    });
}


function caja(data) {
    var caja = '';

    $.each(data.TipoItem, function(i, tipoitem) {

        caja = caja + '<div id="caja_' + tipoitem.id + '" class="caja_destacado" >';
        caja = caja + '<div id="banner_' + tipoitem.id + '" class="banner" >';
        caja = caja + '<a  href="javascript:ajax_resquest(\'mifun\',{metodo:\'categoria,tipoitem,destacado\',tiid:' + tipoitem.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" target="_parent" class="tia-' + tipoitem.id + '"></a>';
        caja = caja + '</div>';
        caja = caja + menu(tipoitem);
        caja = caja + '<div class="pie"><a href="javascript:ajax_resquest(\'mifun\',{metodo:\'categoria,tipoitem,destacado\',tiid:' + tipoitem.id + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '});" target="_parent">más descargas</a></div>';
        caja = caja + '</div>';
    });

    return caja;
}






function menu(tipoitem) {

    var menu = '<div class="menu">';
    var item = '<div id="tipo_contenido_' + tipoitem.id + '" class="contenido" >';
    var bg = '<div class="d-bg">';

    menu = menu + bg;
    if (tipoitem.Destacado != undefined) {
        $.each(tipoitem.Destacado, function(i, destacado) {
            menu = menu + '<a href="javascript:void(0);" target="_parent" class="da-' + destacado.id + '" style="float:left;"  onclick="toggle_destacados(\'#tipo_contenido_' + tipoitem.id + '\',\'#contenido_' + tipoitem.id + destacado.id + '\');"></a>';
            item = item + '<div id="contenido_' + tipoitem.id + destacado.id + '" style="position:relative; width:100%; float:left; ' + ((i != 0) ? 'display:none">' : '">');
            item = item + contenido(destacado, '');
            item = item + '</div>';
        });
    }

    menu = menu + '</div> ';
    menu = menu + '</div> ';
    item = item + '</div>';
    menu = menu + item;

    return menu;

}

function contenido(data, metodo, pagina, contenedor) {

    var contenido = '';
    var paginado = '';
    var div = '';

    if (data.Item != undefined) {

        $.each(data.Item, function(i, item) {


            if (item.TipoItem[0].categoria.toUpperCase() == 'IMAGEN') {
                if (i <= item.top) {
                    contenido = contenido + '<div  class="preview_imagen" >';

                    //contenido = contenido + '<a onclick="modalCompra(\'' + item.id + '\',\'' + item.catalogo + '\',\'' + item.TipoItem[0].id + '\')" title="' + item.nombre + '" >';
                    contenido = contenido + '<a onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + item.TipoItem[0].id + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" >';


                    contenido = contenido + '<img src="http://mcp.net-people.biz/WsUtilidadesWap/Utiles/Thumbnail.aspx?tiid=' + item.TipoItem[0].id + '&img=' + item.preview + '&h=50&w=50" style="width:50px; height:50px;  cursor:pointer;"  alt="" />';
                    contenido = contenido + '</a>';
                    contenido = contenido + '<div style="text-align:center; position:relative;" >';

                    //contenido = contenido + '<a style="font-weight:normal;" onclick="modalCompra(\'' + item.id + '\',\'' + item.catalogo + '\',\'' + item.TipoItem[0].id + '\')" title="' + item.nombre + '" >';
                    contenido = contenido + '<a style="font-weight:normal;" onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + item.TipoItem[0].id + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" >';

                    contenido = contenido + substrText(item.nombre, 20);
                    contenido = contenido + '</a>';
                    contenido = contenido + '</div>';
                    contenido = contenido + '</div>';
                }
            } else {

                if (i <= item.top) {

                    if ((i % 2) == 0)
                        contenido = contenido + '<div  class="preview_sonido" style="' + ((metodo.length == 0) ? "" : "padding-top:15px;") + '  background:' + ((metodo.length == 0) ? "#fff;" : "#E6E6E6;") + ((metodo.length == 0) ? "" : "border-bottom:#fff;") + ' height:' + ((metodo.length == 0) ? "15px;" : "50px;") + '" >';
                    else
                        contenido = contenido + '<div  class="preview_sonido" style="' + ((metodo.length == 0) ? "" : "padding-top:15px;") + ' height:' + ((metodo.length == 0) ? "15px;" : "50px;") + ((metodo.length == 0) ? "" : "border-bottom:#fff;") + '" >';




                    if (metodo.length != 0)
                        contenido = contenido + '<div class="sprite-bocina" style=" position:relative; float:left;"></div>';

                    if (metodo.length == 0)
                        contenido = contenido + '<a class="sprite-bolsa' + ((metodo.length == 0) ? "_small" : "") + '" onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + item.TipoItem[0].id + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" ></a>';

                    if (metodo.length == 0)
                        contenido = contenido + '<a onclick="javascript:modal({ div: \'#dialogsonido\', url: \'Sonido.aspx?preview=' + item.preview + '\'});" href="javascript:void(0);" class="sprite-play' + ((metodo.length == 0) ? "_small" : "") + '"></a>';

                    if (metodo.length == 0)
                        contenido = contenido + '<div><span>' + item.nombre + '</span> / <span style="font-weight:bold;">' + item.Artista[0].nombre + '</span></div>';

                    if (metodo.length != 0)
                        contenido = contenido + '<div style="position:relative;  width:400px; padding: 5px 5px 0px 5px; height:' + ((metodo.length == 0) ? "15px;" : "30px;") + ' float:left; "><span class="titulo" style="color:#000;  ">' + item.nombre + ' - </span><span class="titulo" >' + item.Artista[0].nombre + '</span></div>';


                    if (metodo.length != 0) {
                        contenido = contenido + '<a onclick="modal({ div: \'#dialogsonido\', url: \'Sonido.aspx?preview=' + item.preview + '\'});" href="javascript:void(0);" class="sprite-play' + ((metodo.length == 0) ? "_small" : "") + '"></a>';
                        contenido = contenido + '<a class="sprite-bolsa' + ((metodo.length == 0) ? "_small" : "") + '" onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + item.TipoItem[0].id + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\',width:560,height:400})" title="' + item.nombre + '" ></a>';
                    }

                    contenido = contenido + '</div>';
                }

            }

        });
        contenido = contenido + paginacion(data.Item[0], pagina, contenedor);
    }
    return contenido;
}


function paginacion(item, pagina, contenedor) {


    var paginacion = '<div class="paginado">';
    var indice = parseInt(item.indice);
    var nrototalpaginas = Math.round((item.numreg / item.top));

    
    var desde = ((indice - 5) < 1) ? 1 : (indice - 5);
    var hasta = ((indice + 4) >= nrototalpaginas) ? nrototalpaginas : (indice + 4);


    if (indice > 1)
        paginacion = paginacion + '<a  onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'item\',tiid:' + item.TipoItem[0].id + ',cid:' + item.Categoria[0].id + ',scid:' + item.SubCategoria[0].id + ',clave:' + ((item.clave.length == 0) ? '\'\'' : '\'' + item.clave + '\'') + ',i:' + (indice - 1) + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');" >Anterior<a/>';


    if (hasta > 1) {
        for (ii = desde; ii <= hasta; ii++) {
            if (ii == indice) {
                paginacion = paginacion + '&nbsp;<span style=\' color:#000; font-weight:bold;  font-size:1.1em;\'  >' + ii + '</span>&nbsp;';
            } else {
                paginacion = paginacion + '&nbsp;<a  onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'item\',tiid:' + item.TipoItem[0].id + ',cid:' + item.Categoria[0].id + ',scid:' + item.SubCategoria[0].id + ',clave:' + ((item.clave.length == 0) ? '\'\'' : '\'' + item.clave + '\'') + ',i:' + ii + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');" >' + ii + '<a/>&nbsp;';
            }
        }
    }


    if (nrototalpaginas > indice)
        paginacion = paginacion + '<a onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'item\',tiid:' + item.TipoItem[0].id + ',cid:' + item.Categoria[0].id + ',scid:' + item.SubCategoria[0].id + ',clave:' + ((item.clave.length == 0) ? '\'\'' : '\'' + item.clave + '\'') + ',i:' + (indice + 1) + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');">Siguiente<a/>';

    paginacion = paginacion + '</div>';

    return paginacion;

}
/*

var Metodo = new Class({
_data: 0,
_pagina: 0,
_pagina:0, 
_parametro:'', 
contenedor:'', 
postback:false,    
init: function(data, pagina) {
_data = data;
_pagina = pagina;
},
fabricante: function() {
$.each(_data.Fabricante, function(i, fabricante) {
alert(fabricante.nombre + _pagina);
});
}
});


*/

function addOption(select, attr) {
    $(select).append('<option value="' + attr.value + '" ' + attr.selected + ' >' + attr.text + '</option>');
}
function _toggle(id) {
    $("div[id^='info_categoria_']").hide();
    $(id).toggle();
}

function toggle_destacados(contenedor, objeto) {
    $(contenedor).children().hide();
    $(objeto).show();
}


function migente() {
    $("#seleccion").css("display", "none");
    $("#navegacion").html("<a href=javascript:ajax_resquest(\'home\',{metodo:\'destacado\',fid:" + $('#fabricante').val() + ",mtid:" + $('#modelotelefono').val() + "});>Inicio</a> | Mi Gente | ");
    $.get("MiGente.htm", function(data) {
        $("#contenido").html(menuMiGente("#FF6600") + data);
    });
}

function miutil(id) {
    $("#seleccion").css("display", "none");
    $("#navegacion").html("<a href='Default.aspx?fid=" + $('#fabricante').val() + "&mtid=" + $('#modelotelefono').val() + "'>Inicio</a> | Mi Util | ");
    $.get("MiUtil.htm", function(data) {
        $("#contenido").html(menuMiUtil("#FF6600") + data);
        _toggle_miutil(id);
    });

}

function faqs() {
    $("#seleccion").css("display", "none");
    $("#navegacion").html("<a href='Default.aspx?fid=" + $('#fabricante').val() + "&mtid=" + $('#modelotelefono').val() + "'>Inicio</a> | Preguntas Frecuentes | ");
    $.get("Faqs.htm", function(data) {
        $('#contenido').html(data);
    });
}

function stopRKey(evt) {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
}

function selector_continuar(attr) {
    $('#fabricante').val($('#fabricante_selector').val());
    $('#modelotelefono').val(attr.id);
    $('#dialog').dialog('close');
    imgModeloTelefono(attr.imagen);
    ajax_resquest('home', { metodo: 'destacado,menu,banner', fid: $("#fabricante").val(), mtid: attr.id }, '#modelotelefono');
}



function imgModeloTelefono(img) {
    $('#splash_modelotelefono').flash({
        swf: 'swf/imageLoader.swf',
        width: 48,
        height: 80,
        flashvars: { imagen: img },
        wmode: 'transparent',
        scale: 'showall'
    });
}


function menuMiGente(color) {

    var html = '<div id="mapa-migente" class="lista" style="background:Transparent; height:250px;">';
    html = html + '<ul id="mapa-tipoitem-lista">';
    html = html + '<span class="titulo" style=" color:' + color + ';">Mi Gente</span>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a href="http://www.myspace.com/" target="_blank"  >My Space</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a href="http://www.facebook.com/" target="_blank"  >Facebook</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a href="http://www.hi5.com/" target="_blank"  >hi5</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a href="http://ussp.match.com" target="_blank"  >Matchcom</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a href="http://www.youtube.com/" target="_blank"  >Youtube</a></li>';
    html = html + '</ul>';
    html = html + '</div>';
    return html;

}

function menuMiUtil(color) {

    var html = '<div id="mapa-migente" class="lista" style="background:Transparent; height:250px;">';
    html = html + '<ul id="mapa-tipoitem-lista">';
    html = html + '<span class="titulo" style=" color:' + color + ';">Mi Util</span>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a onclick="miutil(\'#miutil-dealers\');" target="_blank" >Dealers Orange</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a onclick="miutil(\'#miutil-micuenta\');" target="_blank"  >Mi cuenta</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a onclick="miutil(\'#miutil-guia\');" target="_blank"  >Gu&iacute;a Tel&eacute;fonica</a></li>';
    html = html + '<li class="flecha_abajo" style="width:92px; border-top:1px solid #CCCCCC;"><a onclick="miutil(\'#miutil-buscador\');" target="_blank"  >Buscador</a></li>';
    html = html + '</ul>';
    html = html + '</div>';
    return html;

}




function _toggle_miutil(id) {

    $("#miutil-dealers,#miutil-micuenta,#miutil-guia,#miutil-buscador").hide();
    $(id).show();
}


function fabricanteselector() {
    $("#fabricante").val($("#fabricante_selector").val());
    $("#modelotelefono").html('');
    addOption("#modelotelefono", { value: 0, text: '- Modelo Teléfono - ' });
    ajax_resquest("modelotelefono", { metodo: 'modelotelefono', fid: +$('#fabricante').val() }, "#modelotelefono");
}






function cajaPortal(data, contenedor) {

    var caja = '';

    $.each(data.TipoItem, function(i, tipoitem) {


        caja = caja + '<div class="caja-portaltematico" >';
        caja = caja + '<div class="header-portaltematico-' + tipoitem.id + '" >';

        caja = caja + '<div id="contenido-portaltematico-' + tipoitem.id + '" class="contenido-portaltematico">';
        caja = caja + contenidoPortal(tipoitem, '')
        caja = caja + '</div>';

        caja = caja + '<div id="paginado-portaltematico-' + tipoitem.id + '" class="paginado-portaltematico">';
        caja = caja + paginacionPortal(data.id, tipoitem, 'portal', "contenido-portaltematico-" + tipoitem.id);
        caja = caja + '</div>';

        caja = caja + '</div>';
        caja = caja + '<div class="footer"></div>'
        caja = caja + '</div>';


    });

    return caja;
}




function contenidoPortal(tipoitem, metodo) {

    var contenido = '';

    $.each(tipoitem.Item, function(i, item) {
        if (tipoitem.categoria.toUpperCase() == 'IMAGEN') {
            contenido = contenido + '<div  class="preview_imagen" >';
            contenido = contenido + '<a onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + tipoitem.id + '&mid=' + item.medio + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" >';
            contenido = contenido + '<img src="http://mcp.net-people.biz/WsUtilidadesWap/Utiles/Thumbnail.aspx?tiid=' + tipoitem.id + '&img=' + item.preview + '&h=50&w=50" style="width:50px; height:50px;  cursor:pointer;"  alt="" />';
            contenido = contenido + '</a>';
            contenido = contenido + '<div style="text-align:center; position:relative;" >';
            contenido = contenido + '<a style="font-weight:normal;" onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + tipoitem.id + '&cid=' + item.catalogo + '&mid=' + item.medio + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" >';
            contenido = contenido + substrText(item.nombre, 20);
            contenido = contenido + '</a>';
            contenido = contenido + '</div>';
            contenido = contenido + '</div>';
        } else {
            contenido = contenido + '<div  class="preview_sonido" style="height:15px;" >';
            contenido = contenido + '<a class="sprite-bolsa_small" onclick="modal({div:\'#dialogcompra\',url:\'Compra.aspx?iid=' + item.id + '&tiid=' + tipoitem.id + '&mid=' + item.medio + '&cid=' + item.catalogo + '&mtid=' + $("#modelotelefono").val() + '\'})" title="' + item.nombre + '" ></a>';
            contenido = contenido + '<a onclick="javascript:modal({ div: \'#dialogsonido\', url: \'Sonido.aspx?preview=' + item.preview + '\'});" href="javascript:void(0);" class="sprite-play_small"></a>';
            contenido = contenido + '<div><span>' + item.nombre + '</span></div>';
            contenido = contenido + '</div>';
        }
    });


    return contenido;

}


function paginacionPortal(portal, tipoitem, pagina, contenedor) {


    var paginacion = '';
    var item = tipoitem.Item[0];
    var indice = parseInt(item.indice);
    var nrototalpaginas = Math.round(((item.numreg / item.top) + 0.5));
    var desde = ((indice - 5) < 1) ? 1 : (indice - 5);
    var hasta = ((indice + 4) >= nrototalpaginas) ? nrototalpaginas : (indice + 4);


    if (indice > 1)
        paginacion = paginacion + '<a  onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'portal\',tiid:' + tipoitem.id + ',ptwid:' + portal + ',i:' + (indice - 1) + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');" >Anterior<a/>';


    if (hasta > 1) {
        for (ii = desde; ii <= hasta; ii++) {
            if (ii == indice) {
                paginacion = paginacion + '&nbsp;<span style=\' color:#000; font-weight:bold;  font-size:1.1em;\'  >' + ii + '</span>&nbsp;';
            } else {
                paginacion = paginacion + '&nbsp;<a  onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'portal\',tiid:' + tipoitem.id + ',ptwid:' + portal + ',i:' + ii + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');" >' + ii + '<a/>&nbsp;';
            }
        }
    }

    if (nrototalpaginas > indice)
        paginacion = paginacion + '<a onclick="ajax_resquest(\'' + pagina + '\',{metodo:\'portal\',tiid:' + tipoitem.id + ',ptwid:' + portal + ',i:' + (indice + 1) + ',t:' + item.top + ', fid:' + $("#fabricante").val() + ", mtid:" + $("#modelotelefono").val() + '},\'' + contenedor + '\');">Siguiente<a/>';



    return paginacion;

}


function linkPortalTematicoWeb() {
    ajax_resquest('portal', { metodo: 'portal', ptwid: 1, i: 1, t: 6, fid: $('#fabricante').val(), mtid: +$('#modelotelefono').val() });
}
