//var PicSlides = new Array();
//PicSlides.push("Design/Slides/no1.png");
//PicSlides.push("Design/Slides/no2.png");
//PicSlides.push("Design/Slides/no3.png");
//PicSlides.push("Design/Slides/no4.png");

var imgPreloads = new Array();
var imgPreloadSkip = null;

var _cacheSlides = 0;
var _lastRandom = -1;

var _removedCache = false;
var _initAnim = false;

function setSelection(eng, wide) {
    var dt = new Date();
    if (eng) eng = "1"; else eng = "0";
    if (wide) wide = "1"; else wide = "0";

    var y = dt.getYear();
    if (y < 2000) y += 1900;

    setCookie("eng", eng, y + 1, 12, 31);
    setCookie("wide", wide, y + 1, 12, 31);
}

function checkCookie() {
    //var eng = getCookie("eng");
    //var wide = getCookie("wide");

    //if (eng && wide) {
        //document.location = "windex.html"
        //return false;
    //}
    //else {
        //document.getElementById("divWhole").style.display = "block";
    //}

    return true;
}

function clearCookie() {
    //deleteCookie("eng");
    //deleteCookie("wide");
}

function checkPreload() {
    if (_cacheSlides >= PicSlides.length) {
        if (_removedCache) return;
        _removedCache = true;

        //for (var i = 0; i < imgPreloads.length; i++) document.body.removeChild(imgPreloads[i]);
        imgPreloads = null;
    }
}

function initAnimation() {
    if (_initAnim) return;
    _initAnim = true;

    var imgDiv = document.getElementById("divSlide");
    if (imgDiv) {
        var imgLoad = document.getElementById("divImageLoad");
        if (imgLoad) imgDiv.removeChild(imgLoad);
    }

    var img1 = document.getElementById("imgFirst");
    var img2 = document.getElementById("imgSecond");
    
    img1.style.visibility = "visible";
    img2.style.visibility = "visible";

    var a = new ImageSwapAnimation(PicSlides, PicTitles, PicAlts, document.getElementById("divSlide"), document.getElementById("divImage1"),
			document.getElementById("divImage2"), img1, img2, 600, 260, 0, 0);

    a.Interval = 20;
    a.CountToHold = 100;
    a.AccelX = 4;
    a.AccelY = 4;
    a.BounceEnable = true;
    a.BounceCount = 3;
    a.BounceStrengh = 0.3;

    a.ChangingImageEvent = function(pAnim) {
        var rnd = -1;
        do {
            rnd = Math.floor(Math.random() * 4);
        } while (rnd == _lastRandom);

        _lastRandom = rnd;

        switch (rnd) {
            case 0:
                a.BounceG = 4;
                pAnim.DirectionX = 4;
                pAnim.DirectionY = 0;
                break;
            case 1:
                a.BounceG = 4;
                pAnim.DirectionX = -4;
                pAnim.DirectionY = 0;
                break;
            case 2:
                a.BounceG = 2;
                pAnim.DirectionX = 0;
                pAnim.DirectionY = 4;
                break;
            case 3:
                a.BounceG = 2;
                pAnim.DirectionX = 0;
                pAnim.DirectionY = -4;
                break;
        }
    }

    if (a.ChangingImageEvent) a.ChangingImageEvent(a);

    a.Start();
}

function cacheImage() {
    for (var i = 0; i < PicSlides.length; i++) {
        var img = document.createElement("img");
        img.width = "0px";
        img.height = "0px";
        img.style.width = "0px";
        img.style.height = "0px";
        img.onload = function() {
            _cacheSlides++;
            if (_cacheSlides >= PicSlides.length || _cacheSlides >= 2) initAnimation();

            checkPreload();
        }
        img.src = PicSlides[i];

        if (imgPreloads != null) imgPreloads.push(img);

        document.body.appendChild(img);
    }
    
	imgPreloadSkip = document.createElement("img");
	imgPreloadSkip.width = "0px";
	imgPreloadSkip.height = "0px";
	imgPreloadSkip.style.width = "0px";
	imgPreloadSkip.style.height = "0px";
	imgPreloadSkip.onload = function() {
		//if (imgPreloadSkip) document.body.removeChild(imgPreloadSkip);
		imgPreloadSkip = null;
	}
	imgPreloadSkip.src = '/main/splash/Design/Images/skip-hover.png';
	document.body.appendChild(imgPreloadSkip);
}

function init() {
    if (checkCookie()) {
        cacheImage();
    }
}
