var $j = jQuery.noConflict();

$j(function() {
    var gallery2 = $j('#gallery-fotos').clone().insertAfter($j('#gallery-fotos'));
    gallery2.attr("id", "gallery-fotos2");

    var popup = $j('#popup'); 
    popup.css({'margin-left': '-'+(popup.width()/2)+'px'});
    popup.css({'margin-top': '-'+(popup.height()/2)+'px'});  
    popup.fadeIn(500);
    
    $j('#popup').click(function() {
        popup.fadeOut(500);
    });
});

var firstSlide = 0;
var speed = 40000;

$j(window).load( function() {
    var galleryHeight = $j('#gallery').height();
    var fotosHeight = $j('#gallery-fotos').height();
    var delay = Math.ceil((fotosHeight-galleryHeight)/(fotosHeight+galleryHeight) * speed);
    gallerySlide(fotosHeight, galleryHeight, 1000);
    gallerySlide2(fotosHeight, galleryHeight, delay+1000);
// klienciSlide2(logaWidth, contentWidth, delay);
});

function gallerySlide(fotosHeight, galleryHeight, delay) {
    var sp = speed;
    if(firstSlide == 0) {
        $j('#gallery-fotos').css({
            'top': '0px'
        });
        firstSlide = 1;
        sp = sp - Math.ceil(galleryHeight/(fotosHeight+galleryHeight) * speed);
    } else {
        $j('#gallery-fotos').css({
            'top': galleryHeight+'px'
        });
    }
    $j('#gallery-fotos').delay(delay).animate({
        'top': '-'+(fotosHeight)+'px'
    }, sp, 'linear', function() {
        var delay = ""
        if(firstSlide == 0) {
            delay = Math.ceil((fotosHeight-2*galleryHeight)/(fotosHeight+galleryHeight) * speed)
        } else {
            delay = Math.ceil((fotosHeight-galleryHeight)/(fotosHeight+galleryHeight) * speed);
        }
        gallerySlide(fotosHeight, galleryHeight, delay);
    });
}

function gallerySlide2(fotosHeight, galleryHeight, delay) {
    $j('#gallery-fotos2').css({
        'top': galleryHeight+'px'
    });
    $j('#gallery-fotos2').delay(delay).animate({
        'top': '-'+(fotosHeight)+'px'
    }, speed, 'linear', function() {
        var delay = Math.ceil((fotosHeight-galleryHeight)/(fotosHeight+galleryHeight) * speed);
        gallerySlide2(fotosHeight, galleryHeight, delay);
    });
}

function klienciSlide2(logaWidth, contentWidth, delay) {   
    $j('#loga2').css({
        'left': contentWidth+'px'
    });	
	
    $j('#loga2').delay(delay).animate({
        'left': '-'+(logaWidth)+'px'
    }, speed0, 'linear', function() {
        var delay = Math.ceil((logaWidth-contentWidth)/(logaWidth+contentWidth) * speed0);
        klienciSlide2(logaWidth, contentWidth, delay);
    });
}


