﻿
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}



/** Cookie **/
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}



/** Image preload **/
function ImagePreloader(startIdx,callback)
{
    this._loadIndex = !isNaN(startIdx) ? startIdx : 0;
    this._loadList = new Array();
    this._loadCount = 0;
    this._onLoadComplete = callback;

}

ImagePreloader.prototype.load = function()
{
    if (this._loadList.length > 0) {
        this._loadIndex = (this._loadIndex >= this._loadList.length) ? 0 : this._loadIndex;
        this.loadNextImage();
    }
    else {
        this.loadComplete();
    }
}

ImagePreloader.prototype.addImage = function(url)
{

    this._loadList.push(url);
}

ImagePreloader.prototype.loadNextImage = function() {
    if (this._loadCount < this._loadList.length) {

        var url = this._loadList[this._loadIndex];
        if (!this.imageCached(url)) {
            var img = new Image();
            img._loader = this;
            img.onload = function() { this._loader.imageLoaded(); };
            img.onerror = function() { this._loader.imageLoadFail(); };
            img.src = url;
        }
        else {
            this.imageLoaded();
        }
    }
    else {
        this.loadComplete();
    }
}

ImagePreloader.prototype.imageLoaded = function()
{
    this._loadCount ++;
    this._loadIndex = ((this._loadIndex + 1) < this._loadList.length) ? (this._loadIndex + 1) : 0;
    this.loadNextImage();
 
}

ImagePreloader.prototype.imageLoadFail = function()
{
    this.imageLoaded();
}

ImagePreloader.prototype.loadComplete = function()
{
    if (this._onLoadComplete != null) {
        this._onLoadComplete();
    }
}

ImagePreloader.prototype.imageCached = function(url) {

    var isCached = false;
    var imgUrl;
    if (url.toLowerCase().indexOf("http") == -1) {
        var path = location.pathname;
        var host = location.href.substring(0, location.href.indexOf(path));
        url = (url.charAt(0) != '/') ? '/' + url : url;
        imgUrl = host.concat(url);
    }
    else {
        imgUrl = url;
    }

    for (var i = 0; i < document.images.length; i++) {
        if (document.images[i].src.toLowerCase() == url.toLowerCase()) {
            isCached = true;
        }
    }

    return isCached;
}


function GetElementById(dNode, id) {

    var dResult = null;

    if (dNode.getAttribute('id') == id) {
        return dNode;
    }

    for (var i = 0; i < dNode.childNodes.length; i++) {
        if (dNode.childNodes[i].nodeType == 1) {
            dResult = GetElementById(dNode.childNodes[i], id);
            if (dResult != null) {
                break;
            }
        }
    }

    return dResult;
}

