$(document).ready(function(){
    $('#boxes_clientes').cycle({
        fx:     'fade',
        speed:  2000,
        timeout: 5500,
        next: '.avanca_cliente',
        prev: '.volta_cliente',
        before:  onBeforeCli
    });
    $('#boxes_parceiros').cycle({
        fx:     'fade',
        speed:  2000,
        timeout: 4500,
        next: '.avanca_parceiro',
        prev: '.volta_parceiro',
        before:  onBeforePar
    });
    //        var marginTopImgCli;
    //        $('.img_cli').each(function(i){
    //            marginTopImgCli = parseInt((115 - $(this).attr('height')) / 2);
    //            $(this).css('margin-top', marginTopImgCli + 'px');
    //            replaceSrc($(this));
    //        });

    var tpar = $("#imgpar_total").val() + 1;
    for (x=1;x<tpar;x++) {
        $("#imgpar"+x).one("load",function(){
            //do something
            }).each(function(){
            if(this.complete || (jQuery.browser.msie))
                $(this).trigger("load");
        });
    //        $('#imgpar'+x).load(function(){
    //            alert($(this).attr('height'));
    //            var marginTopImgPar = parseInt((115 - $(this).attr('height')) / 2);
    //            $(this).css('margin-top', marginTopImgPar + 'px');
    //            replaceSrc($(this));
    //        });
    }

    $('.img_par, .img_cli').load(function(){
        //        var marginTopImgPar = parseInt((115 - $(this).attr('height')) / 2);
        //        $(this).css('margin-top', marginTopImgPar + 'px');
        replaceSrc($(this));
    });


//
//function slideSwitch() {
//    //    var $active = $('#boxes_clientes > .box_cliente > .foto_cliente_parceiro > img.active');
//    var $active = $('#boxes_clientes img.active');
//    $active.addClass('last-active');
//    // verifica se existe um próximo objeto na div #slideshow, caso ele nao exista, retorna para o primeiro
//    var $next =  $active.next().length ? $active.next() : $('#boxes_clientes img:first');
//
//    // Codigo que define as transicoes entre as imagens
//    $next.css({
//        opacity: 0.0
//    })
//    .addClass('active')
//    .animate({
//        opacity: 1.0
//    }, 1000, function() {
//        $active.removeClass('active last-active');
//    });
//}
//
//$(function() {
////    slideSwitch();
////Executa a função a cada 5 segundos
//	setInterval( "slideSwitch()", 2000 );
//});

});
function replaceSrc($this){
    if($this.attr('height') > 115){
        var srcReplaced = $this.attr('src').replace('&w=140', '&h=115&w=140');
        $this.attr('src', srcReplaced);
    }
}

function onBeforeCli(){
    $('#nome_cli').html(this.id);
}

function onBeforePar(){
    var url = this.childNodes;
    //    jsDump(url.item(1));return false;
//    console.log(url);
    var link;
    for(var i=0;i < url.length;i++){
        if(url.item(i).id == 'parceiro_url'){
            link = url.item(i).value;
        }
    }
    if(link != ''){
        $('#nome_par').html('<a target="_blank" href="'+link+'">'+this.id+'</a>');
    }else{
        $('#nome_par').html(this.id);
    }
}
function jsDump( objeto, recursividade )
{
    document.writeln("<ul>")
    for ( prop in objeto )
    {
        var tipo = typeof objeto[prop];
        var valor = objeto[prop]+"";
        //Eliminando o conteudo das funcoes
        valor = valor.replace(/\{[^\{]*\}/g,"");
        document.writeln("<li> ("+tipo+") "+prop+" =>"+valor+"<\/li>");

        // implementando a recursividade
        if (recursividade > 0 && tipo ==  "object")
        {
            jsDump(objeto[prop],recursividade-1);
        }
    }
    document.writeln("<\/ul>");
}
