﻿Type.registerNamespace('AdultShop.Web.Controls.Behaviors');

AdultShop.Web.Controls.Behaviors.RotatorBehavior = function(element) {
    AdultShop.Web.Controls.Behaviors.RotatorBehavior.initializeBase(this, [element]);
    
    this._images = null;
    this._interval = 1000;
    
    this._idx = 0;
    this._tickHandler = null;
    this._timer = null;
}

AdultShop.Web.Controls.Behaviors.RotatorBehavior.prototype = {
    initialize : function() {
        AdultShop.Web.Controls.Behaviors.RotatorBehavior.callBaseMethod(this, 'initialize');
        
        if (this._images)
        {
            if (this._images.length > 1)
            {
                var img = new Image();
                
                // Preload images
                for (var i = 0; i < this._images.length; i++) {
                    img.src = this._images[i];
                }
            }
            
            $addHandlers (
                this.get_element(),
                {
                    'mouseover' : this._onMouseOver,
                    'mouseout' : this._onMouseOut
                },
                this);
                
            this.get_element().src = this._images[0];
        }
    },
    
    dispose : function() {
        $clearHandlers(this.get_element());
        
        if (this._timer) {
            this._timer.dispose();
            this._timer = null;
        }
        
        AdultShop.Web.Controls.Behaviors.RotatorBehavior.callBaseMethod(this, 'dispose');
    },
    
    get_images : function() {
        return this._images;
    },
    
    set_images : function(value) {
        if (value) {
            value = value.split(',');
        }
        
        if (this._images != value) {
            this._images = value;
            this.raisePropertyChanged('images');
        } 
    },
    
    get_interval : function() {
        return this._interval;
    },
    
    set_interval : function(value) {
        if (this._interval != value) {
            this._interval = value;
            this.raisePropertyChanged('interval');
        }
    },
    
    _onMouseOver : function(e) {
        if (this._images.length > 1) {
            if (!this._timer) {
                this._timer = new AdultShop.Web.Controls.Timer();
                this._timer.set_interval(this._interval);
                this._tickHandler = Function.createDelegate(this, this._onTick);
                this._timer.add_tick(this._tickHandler);
            }
            
            this._timer.set_enabled(true);
        }
    },

    _onMouseOut : function(e) {
        if (this._timer) {
            this._timer.set_enabled(false);
            
            this._idx = 0;
            this.get_element().src = this._images[0];
        }
    },
    
    _onTick : function(e) {
        this._idx++;
        
        if (this._idx >= this._images.length)
            this._idx = 0;
            
        this.get_element().src = this._images[this._idx];
    }
}

AdultShop.Web.Controls.Behaviors.RotatorBehavior.descriptor = {
    properties: [ {name: 'images', type: String},
                  {name: 'interval', type: Number} ]
}

AdultShop.Web.Controls.Behaviors.RotatorBehavior.registerClass('AdultShop.Web.Controls.Behaviors.RotatorBehavior', Sys.UI.Behavior);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();