/**
 * @author Nils Kuhn
 */
var cache = [];
function preLoadImages(){
    // preload images
    $("a.gallery").each(function(index){
        // console.log($(this).attr("href"));
        var cacheImage = document.createElement('img');
        cacheImage.src = $(this).attr("href");
        cache.push(cacheImage);
    });
    
    $("td div img").each(function(index){
    
        if ($(this).attr("alt") != "") {
            // console.log($(this).attr("alt"));
            var cacheImage = document.createElement('img');
            cacheImage.src = "/light_all_assets/img/leftcol/" +
            $(this).attr("alt");
            cache.push(cacheImage);
        }
    });
    
}

// setup leftcol image changes on load
$(document).ready(function(){
    preLoadImages();
    
    // setup galleries
    $("table.logolist a.gallery").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'imageScale': false
    });
    
    // setup tooltips
    if ($('#langinfo').text() == "de") {
        var tooltiptext = "bitte klicken";
    }
    else {
        var tooltiptext = "please click";
    }
    $('table.logolist a.gallery img').qtip({
        content: tooltiptext,
        show: 'mouseover',
        hide: 'mouseout',
        style: {
            name: 'dark',
            tip: 'topLeft',
            border: {
                width: 1,
                radius: 3,
                color: '#000'
            }
        }
    })
    
    // setup leftcol image changes on load
    $("table.logolist img").bind("mouseover", function(){
        if ($(this).attr("alt")) {
            $("#leftcol img:first").hide().attr("src", "light_all_assets/img/leftcol/" +
            $(this).attr("alt")).load(function(){
                $("#leftcol img:first").fadeIn("fast");
            });
        }
    });
    
    $("table.logolist img").bind("mouseout", function(){
        if ($(this).attr("alt")) {
            $("#leftcol img:first").hide().attr("src", $("#leftcol img:first").attr("alt")).fadeIn("fast");
        }
    });
});

function galleryloopinit(max){
    // set preloader attributes as data stores for endless loop
    $("#preloader").attr("max", max);
    $("#preloader").attr("current", 1);
    setTimeout('galleryloop()', 1000);
}

// endless loop for reference gallery
function galleryloop(){
    var max = $("#preloader").attr("max");
    var current = $("#preloader").attr("current");
    if (current == max) {
        current = 1;
    }
    else {
        current++;
    }
    $("#preloader").attr("current", current);
    
    // set src of preloader as src of leftcol image and show
    $("#leftcol img:first").fadeOut("slow", function(){
        $("#leftcol img:first").attr("src", $("#preloader").attr("src")).fadeIn("slow");
    });
    // $("#leftcol img:first").hide().attr("src",
    // $("#preloader").attr("src")).fadeIn("slow");
    
    // set preloader src to next image
    $("#preloader").attr("src", "light_all_assets/img/references/" + current + ".jpg");
    setTimeout('galleryloop()', 4000);
}
