﻿Type.registerNamespace('AdultShop.Web.Controls.Behaviors');

AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior = function(element) {
    AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.initializeBase(this, [element]);
    
    this._imageID = null;
    this._image = null;
  
    this._hyperLinks = null;
    this._interval = 1000;
        
    this._idx = 0;
    this._tickHandler = null;
    this._timer = null;
}

AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.prototype = {
    initialize : function() {
        AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.callBaseMethod(this, 'initialize');
                
        if (this._imageID) {
           this._image = document.getElementById(this._imageID);
        }
                
        if (this._hyperLinks)
        {
            if (this._hyperLinks[0].length > 1)
            {
                /*
                Issue in IE8
                var img = new Image();

                // Preload images
                for (var i = 0; i < this._hyperLinks[0].length; i++) {
                    img.src = this._hyperLinks[0][i];
                }*/
                
                // set timer event
                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);
                }
                       
                if (this._image)
                {
                    // set image
                    this._image.src = this._hyperLinks[0][0];
                    // set alt text
                    this._image.title = this._hyperLinks[2][0];
                    // set width
                    if(this._hyperLinks[4][0] != "")
                        this._image.width = this._hyperLinks[4][0]
                    // set height
                    if(this._hyperLinks[5][0] != "")
                        this._image.height = this._hyperLinks[5][0]
                }
                // set navigation
                this.get_element().href = this._hyperLinks[1][0];
                this.get_element().target = this._hyperLinks[3][0];
            }
        }
    },
    
    dispose : function() {
        $clearHandlers(this.get_element());
        
        if (this._timer) {
            this._timer.dispose();
            this._timer = null;
        }
        
        AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.callBaseMethod(this, 'dispose');
    },
    
    
    get_imageID : function() {       
        return this._imageID;
    },
    set_imageID : function(value) {
        if (this._imageID != value) {
            this._imageID = value;
            this.raisePropertyChanged('imageID');
        }
    },
             
    get_hyperLinks : function() {
        return this._hyperLinks;
    },
    
    set_hyperLinks : function(value) {
        if (this._hyperLinks != value) {
            this._hyperLinks = value;
            this.raisePropertyChanged('hyperLinks');
        } 
    },
    
    get_interval : function() {
        return this._interval;
    },
    
    set_interval : function(value) {
        if (this._interval != value) {
            this._interval = value;
            this.raisePropertyChanged('interval');
        }
    },
    
    _onTick : function(e) {
        this._idx++;
        
        if (this._idx >= this._hyperLinks[0].length)
            this._idx = 0;
            
        if (this._image)
        {
            // set image
            this._image.src = this._hyperLinks[0][this._idx];
            // set alt text
            this._image.title = this._hyperLinks[2][this._idx];
            // set width
            if(this._hyperLinks[4][0] != "")
                this._image.width = this._hyperLinks[4][this._idx]
            // set height
            if(this._hyperLinks[5][0] != "")
                this._image.height = this._hyperLinks[5][this._idx]
        }
        if(this.get_element())
        {
            // set navigation
            this.get_element().href = this._hyperLinks[1][this._idx];
            this.get_element().target = this._hyperLinks[3][this._idx];
        }
    }
}

AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.descriptor = {
    properties: [ {name: 'interval', type: Number},
                  {name: 'hyperLinks', type: Object},
                  {name: 'imageID', type: String} ]
}

AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior.registerClass('AdultShop.Web.Controls.Behaviors.HyperLinkRotatorBehavior', Sys.UI.Behavior);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();