$(document).ready(function() {
    
    //Carga de canales inicial
    $.ajax({
        url: "/webservices/get_canales.php",
        type: 'post',
        dataType: 'json',
        data:"idioma=es&num=21&pag=0",
        async: false,
        success: function(data){
            var html = '';
            $.each(data, function(index,value) {
                if(data.length>0){
                    html += canal(value.nombre,value.idfoto,value.idiomas,value.gratis,value.calidad);           
                } else {
                    html += 'No hay canales disponibles'; 
                }
            }); 
            html += '<div id="1" class="showmore">Mostrar más canales</div>';
            $("DIV#canales").append(html);            
        }
    });     
    
    $('#showbyphoneiframe').click(function(){
        referencia = $('#refbyphone').val();
        DisableSASChat();
        $.ajax({
            url: "/ajax/html-byphoneiframe.php",
            type: 'post',
            dataType: 'html',
            data:"ref="+referencia,
            async: false,
            success: function(data){
                $('#byphoneiframe').html(data);
            }
        });                      
    });
    
    //Mostrar más canales
    $("DIV.showmore").live('click',function(){
        pag = $(this).attr("id");
        $.ajax({
            url: "/webservices/get_canales.php",
            type: 'post',
            dataType: 'json',
            data:"idioma=es&num=18&pag="+pag,
            async: false,
            success: function(data){
                var html = '';
                $.each(data, function(index,value) {
                    if(data.length>0){
                        html += canal(value.nombre,value.idfoto,value.idiomas,value.gratis,value.calidad);           
                    } else {
                        html += 'No hay canales disponibles'; 
                    }
                }); 
                html += '<div id="' + (parseInt(pag)+1) + '" class="showmore">Mostrar más canales</div>';
                $("DIV#canales").append(html); 
                $("DIV.showmore[id="+pag+"]").removeClass("showmore").addClass("pag").text("Página " + (parseInt(pag)+1))
            }
        });         
    });

    //Blog home
    /*
    $.getJSON("http://blogs.webcamspornosex.com/wp-posts.php?posts_per_page=1&orderby=date&jsonp=?", function(data){
        dataContenidos = '';

        $.each(data[0].response, function(i,item){alert("ok")
            dataContenidos += "<div style='background:#FFF;padding:8px;'>";
                dataContenidos += "<h3>&nbsp;" + item.date.substr(8,2) + "/" + item.date.substr(5,2) + "/" + item.date.substr(0,4) + "&nbsp;<a class='clickmedios' href='javascript:void(0)'>" + item.title + "</a></h3>";
                dataContenidos += "<p style='height:28px;padding:3px 0 0 8px;overflow:hidden;line-height:14px;'>" + item.excerpt + "...</p>&nbsp;";
                dataContenidos += "&nbsp;<a class='clickmedios' href='javascript:void(0)'>Leer post completo</a>&nbsp;·&nbsp;Comentarios (<a class='clickmedios' href='javascript:void(0)'>" + item.comments + "</a>)&nbsp;";
            dataContenidos += "</div><br />";
        });
        
        $("DIV#contenidosblogmini").html(dataContenidos);
    });
    */       

    $("FORM[name='sms'] input[name='entrar']").click(function(){
    
        code = $("FORM[name='sms'] input[name='login']").val();
        if(code.length > 5 && code!=="Código"){
            DisableSASChat();
            $("FORM[name='sms']").submit();    
        } else{
            alert("Introduce un código válido")
        }
        
    });
    
    $(".clickmedios").live('click',function(){
        $('html,body').animate({scrollTop: $(".barraentraahora").offset().top},'slow').queue(function(){
            $("DIV.resaltamedios IMG.format").fadeOut('medium').fadeIn('medium').fadeOut('medium').fadeIn('medium');
            $(this).dequeue();
        });
    });
    
}); 

function canal(nombre,idfoto,idiomas,gratis,calidad){
    var html = '';
    html += '<div class="canal">';
        //Imagen canal
        html += '<a href="/canal/' + nombre + '"><img src="http://fotos.sexywebcam.com/prev/176x132/' + idfoto + '.jpg" width="176" height="132" alt="' + nombre + '" /></a>';
        html += '<div class="nombre_canal">' + nombre.replace(/[-_]/g," ").toLowerCase() + '</div>';
        //Etiqueta gratis
        if(gratis && 1==2) {
            html += '<div class="thgratis"><img src="img/gratis_es.gif" alt="GRATIS! FREE!" width="40" height="11" /></div>';
        } 
        //Iconos HD HQ              
        html += '<div class="thhd">';
        if (calidad === 'HD') {
            html += '<img src="img/icon_hd.png" alt="HD" />';
        } else if (calidad === 'HQ') {
            html += '<img src="img/icon_hq.png" alt="HQ" />';
        }
        html += '</div>';         
        //Icono WebCam        
        html += '<div class="thwebcamon"></div>';
        //Banderas
        html += '<div class="thflags">';
        var flags = idiomas.split(',');
        for(lang in flags) {
            html += '<img src="img/flag_' + flags[lang] + '.gif" width="16" height="11" alt="' + flags[lang] +'" />';
        }
        html += '</div>';                        
    html += '</div>';
    return html;     
}
