﻿$(function() {
    $(document).ready(function() {

        dbate_galleryfader.initialize();
        
    });
});

var dbate_galleryfader = {
    images: new Array(),
    items: new Array(),
    currentindex: -1,
    displayer: 0,
    displayers: null,
    autotimerid: 0,
    onInitialize: null,
    onImageChanged: null,
    interval: 6000,
    fadeTime: 2000,
    autoplay: true,
    onLoadComplete: null,


    initialize: function() {
        this.displayers = $("#galleryslider .image")

        for (var idx = 0; idx < this.items.length; idx++) {
            var nimg = new Image();
            nimg.src = this.items[idx].url;
            this.images[idx] = nimg;

        }



        this.displayers.css("height", $("#gallery").height() + "px");

        $("#gallery").mouseenter(function() {
            if (dbate_galleryfader.autoplay)
                dbate_galleryfader.pause();
        }).mouseleave(function() {
            if (dbate_galleryfader.autoplay)
                dbate_galleryfader.play();
        });


        $("#loader").css("left", ($("#gallery").width() - $("#loader").width()) / 2 + "px");
        $("#loader").css("top", ($("#gallery").height() - $("#loader").height()) / 2 + "px");


        $(".navright").click(function() { dbate_galleryfader.goRight(); });
        $(".navleft").click(function() { dbate_galleryfader.goLeft(); });

        if (this.onInitialize)
            this.onInitialize(this, { items: this.items });

        this.waitImageLoad();


    },

    waitImageLoad: function() {
        for (var i = 0; i < this.items.length; i++) {

            //if (this.images[i].complete == null)
            //    callback();

            if (!this.images[i].complete) {
                setTimeout(function() { dbate_galleryfader.waitImageLoad() }, 200);
                return false;
            }
        }

        this.imageDone();

    },

    pause: function() {
        clearTimeout(this.autotimerid);
    },

    play: function() {
        autoplay: true;
        this.resetTimer();
    },

    imageDone: function() {

        $("#loader").fadeOut(1000);

        if ($.support.opacity)
            $("#gallerycontainer").fadeIn(1000);
        else
            $("#gallerycontainer").show();

        this.gotoImage(0);


        if (this.images.length > 1) {
            $(".navigate-container").fadeTo(1000, 0.3);

            this.resetTimer();
        }

        if (this.onLoadComplete)
            this.onLoadComplete(this, {});

    },

    resetTimer: function() {
        if (this.autotimerid != 0)
            clearTimeout(this.autotimerid);

        if (this.images.length > 1)
            this.autotimerid = setTimeout(function() { dbate_galleryfader.autotimer(); }, this.interval);
    },

    autotimer: function() {
        if (this.autoplay) {
            this.goRight();
            
            if (this.images.length > 1)
                this.autotimerid = setTimeout(function() { dbate_galleryfader.autotimer(); }, this.interval);

        }
    },

    display: function(image) {
        $(this.displayers[this.displayer]).fadeOut(this.fadeTime)

        this.displayer++;
        if (this.displayer >= this.displayers.length)
            this.displayer = 0;

        var disp = this.displayers[this.displayer];

        /*    disp.src = image.src;*/
        var d = $(disp);

        if (image && image.src) {
            d.css("background-image", "url('" + image.src + "')");

            if (this.items[this.currentindex].address) {

                //d.attr("onclick", "window.location='" + items[currentindex].address + "';");
                d[0].onclick = function() { window.location = dbate_galleryfader.items[dbate_galleryfader.currentindex].address; };
                d.css("cursor", "pointer");
            }
            else {
                d[0].onclick = null;
                d.css("cursor", "default");
            }


            $(disp).fadeIn(this.fadeTime);
        }
    },

    gotoImage: function(index) {
        if (index != this.currentindex) {
            this.display(this.images[this.currentindex = index]);

            if (this.onImageChanged)
                this.onImageChanged(this, { index: this.currentindex, image: this.images[this.currentindex] });

            this.resetTimer();
        }

    },

    goLeft: function() {
        var idx = this.currentindex;

        if (idx > 0)
            idx--;
        else
            idx = this.images.length - 1;

        this.gotoImage(idx);
    },

    goRight: function() {
        var idx = this.currentindex;

        if (idx < this.images.length - 1)
            idx++;
        else
            idx = 0;

        this.gotoImage(idx)
    }


};










