var TOT_Carousel = function() {
    this._intNumberOfImages = 0;
    this._objCurrent = { Big1:0, Big2:0, Small1:0, Small2:0 };
    this._objNew = { Big1:0, Big2:0, Small1:0, Small2:0 };
    this._aryPic1 = [];
    this._aryPic2 = [];
    this._blnOnPic2 = false;
    this.Animate = {
        ChangeTime: 8000
        , FadeSpeed: 2000
    };
};

TOT_Carousel.prototype = {

    init: function() {
        this.startTimer();
    },

    dispose: function() {
        this._intNumberOfImages = null;
        this._objCurrent = null;
        this._objNew = null;
        this._aryPic1 = null;
        this._aryPic2 = null;
        this.Animate = null;
    },

    startTimer: function() {
        setTimeout($.proxy(this, "changeItems"), this.Animate.ChangeTime);
    },

    changeItems: function() {
        this._objNew = { Big1: 0, Big2: 0, Small1: 0, Small2: 0 };
        this._objNew.Big1 = this.getRandomNumberNotYetChosen();
        this._objNew.Big2 = this.getRandomNumberNotYetChosen();
        this._objNew.Small1 = this.getRandomNumberNotYetChosen();
        this._objNew.Small2 = this.getRandomNumberNotYetChosen();
        var aryThisPic = (this._blnOnPic2) ? this._aryPic2 : this._aryPic1;
        var aryNextPic = (this._blnOnPic2) ? this._aryPic1 : this._aryPic2;
        $("#" + aryNextPic[0]).css("background-image", this.getBackgroundImage(this._objNew.Big1, true));
        $("#" + aryNextPic[1]).css("background-image", this.getBackgroundImage(this._objNew.Big2, true));
        $("#" + aryNextPic[2]).css("background-image", this.getBackgroundImage(this._objNew.Small1, false));
        $("#" + aryNextPic[3]).css("background-image", this.getBackgroundImage(this._objNew.Small2, false));
        for (var i = 0; i < 4; i++) {
            var intRandom = Math.round(Math.random() * 500);
            $("#" + aryThisPic[i]).delay(intRandom).fadeOut(this.Animate.FadeSpeed);
            $("#" + aryNextPic[i]).removeClass("invisible");
            $("#" + aryNextPic[i]).fadeOut(0);
            var fn = null;
            if (i == 3) fn = $.proxy(this, "startTimer");
            $("#" + aryNextPic[i]).delay(intRandom).fadeIn(this.Animate.FadeSpeed, fn);
        }
        this._objCurrent = this._objNew;
        this._blnOnPic2 = !this._blnOnPic2;
    },

    getBackgroundImage: function(intPic, blnLarge) {
        var strOut = "url(images/carousel/";
        if (intPic < 10) strOut += "0";
        strOut += intPic.toString() + "_" + ((blnLarge) ? "b" : "s") + ".jpg)";
        return strOut;
    },

    getRandomNumberNotYetChosen: function() {
        var intRandom;
        var blnChosen = false;
        var intTry = 0;
        while (!blnChosen && intTry < 50) {
            intRandom = Math.round(Math.random() * 12);
            if (intRandom > 0
            && intRandom != this._objNew.Big1 && intRandom != this._objNew.Big2
            && intRandom != this._objNew.Small1 && intRandom != this._objNew.Small2
            && intRandom != this._objCurrent.Big1 && intRandom != this._objCurrent.Big2
            && intRandom != this._objCurrent.Small1 && intRandom != this._objCurrent.Small2) {
                blnChosen = true;
            }
            intTry += 1;
        }
        return intRandom;
    }

};
