﻿var images = new Array();
var currentindex = 0;
var displayer = 0;
var displayers = null;

var autotimerid;

$(function() {



    $(document).ready(function() {

        displayers = $("#galleryslider .image")


        for (var idx = 0; idx < items.length; idx++) {
            var nimg = new Image();
            nimg.src = items[idx].url;
            images[idx] = nimg;

        }

        displayers.css("height", $("#gallery").height() + "px");
        displayers.css("width", $("#gallery").width() + "px");

        $("#loader").css("left", 550 / 2 + "px");
        $("#loader").css("top", ($("#gallery").height() - $("#loader").height()) / 2 + "px");

        waitImageLoad();


    });

    $(".navright").click(function() { goRight(); });
    $(".navleft").click(function() { goLeft(); });
    
    $(".navigate-container").mouseenter(function() {
        $(this).fadeTo(500, 1.0);
    }).mouseleave(function() {
        $(this).fadeTo(500, 0.8);
    });


});

function waitImageLoad() {


    for (var i = 0; i < items.length; i++) {

        if (images[i].complete == null)
            callback();

        if (!images[i].complete) {
            setTimeout("waitImageLoad()", 200);
            return false;
        }
    }

    imageDone();
}

function imageDone() {
    $("#loader").fadeOut(1000);

    if ($.support.opacity)
        $("#gallerycontainer").fadeIn(1000);
    else
        $("#gallerycontainer").show();

    gotoImage(0);


    if (images.length > 1) {
        $(".navigate-container").fadeTo(1000, 0.3);

        resetTimer();
    }

}

function resetTimer() {
    clearTimeout(autotimerid);
    autotimerid = setTimeout("autotimer();", 6000);
}

function autotimer(move) {
    goRight();
}

function display(image) {

    if (!image)
        return;
    
    $(displayers[displayer]).fadeOut(2000)

    displayer++;
    if (displayer >= displayers.length)
        displayer = 0;

    var disp = displayers[displayer];

/*    disp.src = image.src;*/
    var d = $(disp);

    d.css("background-image", "url('" + image.src + "')");

    $(disp).fadeIn(2000);
}

function gotoImage(index) {
    display(images[currentindex = index]);
}

function goLeft() {
    if (currentindex > 0)
        currentindex--;
    else
        currentindex = images.length - 1;

    resetTimer();

    gotoImage(currentindex);
}

function goRight() {

    if (currentindex < images.length - 1)
        currentindex++;
    else
        currentindex = 0;

    resetTimer();

    gotoImage(currentindex)
}


