﻿/// <reference path="~/js/jshashtable/jshashtable.js" />
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

// Register namespace
//Type.registerNamespace('My');

PreloaderImg = function() {
    // register the object as disposable, so the application will call it's dispose method when needed
    // if (typeof (Sys) !== "undefined") Sys.Application.registerDisposableObject(this);

    this._arraySrc = new Array();
    this._arrayImg = new Array();
    this._index = 0;
    this._timer = null;
}

PreloaderImg.prototype = {
    add: function(src) {
        this._arraySrc.push(src);
    },

    start: function() {
        if (this._arraySrc.length > this._index) {
            var img = new Image();
            img.src = this._arraySrc[this._index];
            this._arrayImg.push(img);

            this.stop();
            this._timer = window.setInterval(Function.createDelegate(this, this._onTimer), 100);
        }
    },

    stop: function() {
        if (this._timer != null) {
            window.clearInterval(this._timer);
            this._timer = null;
        }
    },

    _onTimer: function() {
        if (this._arrayImg[this._index].complete)
            this._gotoNext();
    },

    _gotoNext: function() {
        this.stop();
        
        if (this._arraySrc.length > this._index + 1) {

            this._index++;
            var img = new Image();
            img.src = this._arraySrc[this._index];
            this._arrayImg.push(img);
            this._timer = window.setInterval(Function.createDelegate(this, this._onTimer), 100);
        }
    },

    initialize: function() {
    },

    dispose: function() {
        //Add custom dispose actions here
    }
}

// claim the class is implementing IDisposable
//if (typeof (Sys) !== "undefined") My.PreloaderImg.registerClass('My.PreloaderImg', null, Sys.IDisposable);

// standard call for non-embedded scripts.
//if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();