﻿var images = new Array();
var currentindex = 0;
var displayer = 0;
var displayers = null;

var autotimerid = 0;

$(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");
        /* Removed 25/10/10 to use width: 100% instead
        
        displayers.css("width", $("#gallery").width() + "px");*/

        $("#loader").css("left", ($("#gallery").width() - $("#loader").width()) / 2 + "px");
        $("#loader").css("top", ($("#gallery").height() - $("#loader").height()) / 2 + "px");

        waitImageLoad();


    });

    $(".navright").click(function() { goRight(); });
    $(".navleft").click(function() { goLeft(); });



});

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() {
    if(autotimerid != 0)
        clearTimeout(autotimerid);
        
    if (images.length > 1)
        autotimerid = setTimeout("autotimer();", 6000);
}

function autotimer(move) {
    goRight();
}

function display(image) {
    $(displayers[displayer]).fadeOut(2000)

    displayer++;
    if (displayer >= displayers.length)
        displayer = 0;

    var disp = displayers[displayer];

/*    disp.src = image.src;*/
    var d = $(disp);

    if (image && image.src) {
        d.css("background-image", "url('" + image.src + "')");

        $(disp).fadeIn(2000);
    }
}

function gotoImage(index) {
    display(images[currentindex = index]);
    resetTimer();
}

function goLeft() {
    if (currentindex > 0)
        currentindex--;
    else
        currentindex = images.length - 1;

    gotoImage(currentindex);
}

function goRight() {

    if (currentindex < images.length - 1)
        currentindex++;
    else
        currentindex = 0;

    gotoImage(currentindex)
}


