framework.declareClass("org.iit.FlashProxy", null, 
    function(flash) {
        this.flash = flash;
        this._timer = null;
        this._dfd = null;
    },
    {
        SetVariable: function(name, value) {
            if ( typeof this.flash.SetVariable != "undefined" ) {
                setTimeout(framework.runInScope(this, function(){this._set(name, value)}), 500);
            } else {
                if ( !this._dfd ) {
                    this._dfd = new framework.Deferred();

                    this._dfd.addCallbacks(framework.runInScope(this, function() {
                        //if ( window.console ) console.log("Setting variable " + name + " = " + value);
                        this._set(name, value);
                    }), function(res) {
                        if ( window.console ) {
                            console.log(res);
                        }
                    });

                    this._timer = setInterval(framework.runInScope(this, this._check), 50);

                    if ( window.console ) console.log("starting timer");
                } else {
                    this._dfd.addCallback(this, function() {
                        this._set(name, value);
                    });
                }
            }
        },
        _check: function() {
            //if ( window.console ) console.log("_check SetVariable", this.flash.SetVariable);
            if ( this.flash.SetVariable ) {
                clearInterval(this._timer);
                this._timer = null;
                setTimeout(framework.runInScope(this, function() {
                    this._dfd.callback();
                    this._dfd = null;
                }), 500);
            }
        },
        _set: function(name, value) {
            if ( window.console ) console.log("Setting variable " + name + " = " + value);
            this.flash.SetVariable(name, value);
        },
        SetVariableImmediate: function(name, value) {
            this._set(name, value);
        }
    }
);


framework.declareClass("org.iit.BackgroundRotator", null,
    function(config) {
        this.changer = config.changer;
        this.images = config.images;
        this._imgElements = [];
        this.properties = config.properties;
        this.seazon = null;
        this._index = -1;
        this._toIndex = -1;
        this._loadedImagesCount = 0;
        this._r = /^\s*url\((.+?)\)\s*$/;
        this.changer.onEnd.add(framework.runInScope(this, this._rotate));
        this.onStart = new framework.event.Event();
        this.onEnd = new framework.event.Event();
        this.onInit = new framework.event.OnLoadEvent();
        this.initialize();
    },
    {
        initialize: function() {
            //if ( window.console ) console.log("initializing images");
            this._loadImages();
        },
        _loadImages: function() {
            var img, imgSrc;
            for ( var i = 0; i < this.images.length; i++ ) {
                //img = new Image();
                img = this._imgElements[i] = document.createElement("IMG");
                imgSrc = this.images[i].match(this._r)[1];
                img.onload = framework.runInScope(this, this._imageLoaded, imgSrc, i);
                img.src = imgSrc;
                //if ( window.console ) console.log("loading image '" + img.src + "'");
            }
        },
        _imageLoaded: function() {
            var img, index;
            if ( arguments.length == 3 ) {
                img = arguments[1];
                index = arguments[2];
            } else {
                img = arguments[0];
                index = arguments[1];
            }
            this._loadedImagesCount++;
            //if ( window.console ) console.log("loaded image[" + index + "] '" + img + "'; loaded images count: " + this._loadedImagesCount);
            if ( this.images.length == this._loadedImagesCount ) {
                this.onInit.fire();
            }
        },
        _currentIndex: function() {
            var img = framework.getComputedStyle(this.changer.getElement()).backgroundImage.match(this._r)[1];
            for ( var i = 0, l = this.images.length; i < l; i++ ) {
                if ( img.indexOf(this.images[i].match(this._r)[1]) != -1 ) {
                    return i;
                }
            }
            return -1;
        },
        rotateTo: function(index) {
            this._index = this._currentIndex();
            this._toIndex = index;
            this.seazon = index;
            this.onStart.fire();
            this._rotate();
        },
        _rotate: function() {
            //console.log(this.changer.getElement().style.backgroundImage);
            if ( this._index != -1 && this._toIndex != -1 && this._index != this._toIndex ) {
                this._index++;
                if ( this._index >= this.images.length ) {
                    this._index = 0;
                }
                this.changer.change(framework._mixin({
                    backgroundImage: this.images[this._index]
                }, this.properties));
            } else {
                if ( this._toIndex == this._index ) {
                    this.onEnd.fire();
                }
                this._toIndex = -1;
            }
        }
    }
);

framework.onLoad.add(function() {

    if ( !window.console && String(window.location.href).indexOf("debug") != -1 ) {
        var d = document.body.appendChild(document.createElement("div"));
        d.style.cssText = "background: #FFFFFF; border: 1px solid black; text-align: left; height: 300px; overflow: auto; padding: 2px;";
        window.console = {};
        window.console.log = function() {
            for ( var i = 0, l = arguments.length; i < l; i++ ) {
                d.innerHTML += i!=0?";":"" + String(arguments[i]);
            }
            d.innerHTML += "<br />\n";
        }
    }

    var ch = org.iit.Widget.get("header");
    var fl = framework.query("div#flash")[0];
    fl.style.display = "none";

    var flashProxy = new org.iit.FlashProxy(getMovie());

    window.rotator = new org.iit.BackgroundRotator({
        changer: ch,
        images: [
            "url(/tpl/img1/top_collage_vesna.jpg)",
            "url(/tpl/img1/top_collage_leto.jpg)",
            "url(/tpl/img1/top_collage_osen.jpg)",
            "url(/tpl/img1/top_collage_zima.jpg)"
        ],
        properties: { backgroundPosition: "center top" }
    });
    rotator.onStart.add(function() {
        fl.style.display = "none";
        var seazonArr = ["vesna","leto","osen","zima"];
        flashProxy.SetVariable("seazon", seazonArr[rotator.seazon]);
    });
    rotator.onEnd.add(function() {
        //rotator.onEnd.removeAll();
        fl.style.display = "block";
        var seazonArr = ["vesna","leto","osen","zima"];
        flashProxy.SetVariable("seazon", seazonArr[rotator.seazon]);
        /*
        if ( check != null )
            setTimeout("flashVarSet("+rotator.seazon+")", 1000);
        */
	//alert(rotator.seazon);
    });
    rotator.onInit.add(function() {
        //if ( window.console ) console.log("initialization completed");
        fl.style.display = "block";
	flashProxy.SetVariable("seazon","leto");
	
        setTimeout("flashVarSet(0)", 6000);
    });
});


function flashVarSet(j) {
    var seazonArr = ["vesna","leto","osen","zima"];
    getMovie().SetVariable("seazon", seazonArr[j]);
    if (check) {
       check = null;
    }
}


function loadFlash(param) {
    check = 1;
    if ( param == "vesna" ) {
        rotator.rotateTo(0);
    } else if ( param == "leto" ) {
        rotator.rotateTo(1);
    } else if ( param == "osen" ) {
        rotator.rotateTo(2);
    } else if ( param == "zima" ) {
        rotator.rotateTo(3);
    }
}
