﻿/************************************************************************************************/
/* Extender Control                                                                             */
/************************************************************************************************/

Type.registerNamespace('Controls');

Controls.ExtenderControl = function(element)
{
    
    Controls.ExtenderControl.initializeBase(this, [element]);
    this._clientStateFieldId = null;
    this._clientState = null;
    this._clientStateEnabled = null;
}

Controls.ExtenderControl.prototype = {

    /// <summary>
    /// Initialise Function.
    /// </summary>
    initialize : function() {
        Controls.ExtenderControl.callBaseMethod(this, 'initialize');         
        this.LoadClientStateField();
    },
    
    /// <summary>
    /// Dispose Function.
    /// </summary>
    dispose : function() {        
        this.SaveClientStateField();
        Controls.ExtenderControl.callBaseMethod(this, 'dispose');
    },
    
    /// <summary>
    /// Get or Set Client State Field Id.
    /// </summary>
    get_ClientStateFieldId : function() {
        return this._clientStateFieldId;
    }, 
    
    set_ClientStateFieldId : function (value) { 
        this._clientStateFieldId = value;
    },
    
    /// <summary>
    /// Get or Set Client State Enabled
    /// </summary>
    get_ClientStateEnabled : function() {
        return this._clientStateEnabled;
    }, 
    
    set_ClientStateEnabled : function (value) { 
        this._clientStateEnabled = value;
    },
    
    /// <summary>
    /// Get or Set Client State Field Id.
    /// </summary>
    get_ClientState : function() {
        if(this._clientState == null)
        {
            this._clientState = new Object();
        }
        //debugger;
        return this._clientState;
    }, 
    
    /// <summary>
    /// load client state from hidden field.
    /// </summary>
    LoadClientStateField : function() { 
        if(this._clientStateEnabled)
        {
            var e = $get(this._clientStateFieldId);
            this._clientState = Sys.Serialization.JavaScriptSerializer.deserialize(e.value);
            this.LoadClientState();
        }
    },
    
    /// <summary>
    /// Save client state to hidden field.
    /// </summary>    
    SaveClientStateField : function() { 
        if(this._clientStateEnabled)
        {
            this.SaveClientState();
            var e = $get(this._clientStateFieldId);
            e.value = Sys.Serialization.JavaScriptSerializer.serialize(this.get_ClientState());
        }
    }, 
    
    /// <summary>
    /// Load client state.
    /// </summary>
    LoadClientState : function() { 
    },

    /// <summary>
    /// Save client state.
    /// </summary>    
    SaveClientState : function() { 
    }
}
Controls.ExtenderControl.registerClass('Controls.ExtenderControl', Sys.UI.Behavior);

/************************************************************************************************/
/* Blind Extender Control Behaviour                                                             */
/************************************************************************************************/

Controls.BlindExtenderControlBehaviour = function(element)
{
    Controls.BlindExtenderControlBehaviour.initializeBase(this, [element]);
   
    this._controlExpanded = null;
    this._controlIdToAnimate = null;
    this._controlIdToToggleBlind = null;
    this._toggleContractedImagePath = null;
    this._toggleExpandedImagePath = null;
    this._controlToToggleBlind = null;
    
    this._clientOnCollapse = null;
    this._clientOnCollapseComplete = null;
    this._clientOnExpand = null;
    this._clientOnExpandComplete = null;
    
    this._lastClicked = null;
    
    this.TimeBetweenClicks = 800;		// The time in milliseconds that the control will prevent quick clicks from causing an action.
}

Controls.BlindExtenderControlBehaviour.prototype = {
    /// <summary>
    /// Initialise Function
    /// </summary>
    initialize : function() {
       Controls.BlindExtenderControlBehaviour.callBaseMethod(this, 'initialize');  
       $addHandlers(this.get_element(), { 'click' : this._blindToggleClientSideClick}, this); 
       this._controlToToggleBlind  = $get( this._controlIdToToggleBlind);
       this._initBlind();
    },
    
    /// <summary>
    /// Dispose Function
    /// </summary>
    dispose : function() {
        Controls.BlindExtenderControlBehaviour.callBaseMethod(this, 'dispose');
    },
    
    _raiseCancellableEvent: function(methodName) {
		var cancel = false;
		if (methodName) {
			var args = { cancel: false };
			eval(methodName + "(this, args)");
			cancel = args.cancel;
		}
		return cancel;
    },
    
    /// <summary>Raises the ClientCollapse client-side event.</summary>
    /// <returns>True if the client event has cancelled the collapse</returns>
    _raiseOnClientCollapse: function() {
		return this._raiseCancellableEvent(this._clientOnCollapse);
    },
    
    /// <summary>Raises the ClientExpand client-side event.</summary>
    /// <returns>True if the client event has cancelled the expansion.</returns>
    _raiseOnClientExpand: function() {
		return this._raiseCancellableEvent(this._clientOnExpand);
    },
    
    /// <summary>Raises the ClientCollapseComplete client-side event.</summary>
    _raiseOnClientCollapseComplete: function() {
		if (this._clientOnCollapseComplete) {
			eval(this._clientOnCollapseComplete + "(this)");
		}
    },
    
    /// <summary>Raises the ClientExpandComplete client-side event.</summary>
    _raiseOnClientExpandComplete: function() {
		if (this._clientOnExpandComplete) {
			eval(this._clientOnExpandComplete + "(this)");
		}
    },
    
    /// <summary>
    /// Toggle blind up/down Function
    /// </summary>
    _toggleBlind : function() {
		var controlExpanded = this.get_ClientState().ControlExpanded;
		var effectCancelled = false;
		
        if (controlExpanded) {
			effectCancelled = this._raiseOnClientCollapse();
			if (!effectCancelled) {
				Effect.BlindUp(this._controlIdToAnimate, { duration: 0.5, afterFinish: BlindAnimation_AfterCollapse, __client: this });
				// Only if we're dealing with an image...
				if (this._controlToToggleBlind && this._controlToToggleBlind.src)
					this._controlToToggleBlind.src = this._toggleContractedImagePath;
			}
        }
        else {
			effectCancelled = this._raiseOnClientExpand();
			if (!effectCancelled) {
				Effect.BlindDown(this._controlIdToAnimate, { duration: 0.5, afterFinish: BlindAnimation_AfterExpand, __client: this });
				// Only if we're dealing with an image...
				if (this._controlToToggleBlind && this._controlToToggleBlind.src)
					this._controlToToggleBlind.src = this._toggleExpandedImagePath;
			}
        }
        
        if (!effectCancelled)
			this._setToggleState(!controlExpanded);
    },
    
    /// <summary>
    /// Set the initial state of the blind
    /// </summary>
    _initBlind : function () {
        this.set_ControlExpanded = this._controlExpanded;
        if(!this.get_ClientState().ControlExpanded)
        {
          Effect.Fade(this._controlIdToAnimate,{duration:0});
          this._setToggleState(false);
          
          if (this._controlToToggleBlind && this._controlToToggleBlind.src)
			this._controlToToggleBlind.src = this._toggleContractedImagePath; 
        }
    },
    
    /// <summary>
    /// Set Control To Expand's State - Open or Closed
    /// </summary>
    _setToggleState : function (expanded) {
        this.get_ClientState().ControlExpanded = expanded;
        this.SaveClientStateField();
    },    
        
    /// <summary>
    /// Client side click event
    /// </summary>
    _blindToggleClientSideClick : function(e) {
		var shouldToggle = true;
		
		if (this._lastClicked != null) {
			var millisFromLastClick = (new Date()).getTime() - this._lastClicked.getTime();
			if (millisFromLastClick <= this.TimeBetweenClicks) {
				shouldToggle = false;
			}
		}
		
		if (shouldToggle) {
			this._toggleBlind();
			this._lastClicked = new Date();
		}
    },
    
    /// <summary>
    /// Control Expanded Flag Accessors.
    /// </summary>            
    get_ControlExpanded : function() {
        return this.get_ClientState().ControlExpanded;
    },
    set_ControlExpanded : function(value) {
        this._controlExpanded = value;
    },

    /// <summary>
    /// Control To Be Animated Accessors.
    /// </summary> 
    get_ControlIdToAnimate : function() {
        return this._controlIdToAnimate;
    },
    set_ControlIdToAnimate : function(value) {
        this._controlIdToAnimate = value;
    },
      
    /// <summary>
    /// Control To Toggle Blind Accessors.
    /// </summary>       
    get_ControlIdToToggleBlind : function() {
        return this._controlIdToToggleBlind;
    },
    set_ControlIdToToggleBlind : function(value) {
        this._controlIdToToggleBlind = value;
    },   
    
    /// <summary>
    /// Control To Toggle Blind Accessors.
    /// </summary>       
    get_ToggleContractedImagePath : function() {
        return this._toggleContractedImagePath;
    },
    set_ToggleContractedImagePath : function(value) {
        this._toggleContractedImagePath = value;
    },
    
    /// <summary>
    /// Control To Toggle Blind Accessors.
    /// </summary>       
    get_ToggleExpandedImagePath : function() {
        return this._toggleExpandedImagePath;
    },
    set_ToggleExpandedImagePath : function(value) {
        this._toggleExpandedImagePath = value;
    },
    
    /// <summary>Name of the method to call prior to collapsing the blind control</summary>
    get_ClientOnCollapse: function() {
		return this._clientOnCollapse;
    },
    set_ClientOnCollapse: function(value) {
		this._clientOnCollapse = value;
    },
    
    /// <summary>Name of the method to call after collapsing the blind control</summary>
    get_ClientOnCollapseComplete: function() {
		return this._clientOnCollapseComplete;
    },
    set_ClientOnCollapseComplete: function(value) {
		this._clientOnCollapseComplete = value;
    },
    
    /// <summary>Name of the method to call prior to expanding the blind control</summary>
    get_ClientOnExpand: function() {
		return this._clientOnExpand;
    },
    set_ClientOnExpand: function(value) {
		this._clientOnExpand = value;
    },
    
    /// <summary>Name of the method to call after expanding the blind control</summary>
    get_ClientOnExpandComplete: function() {
		return this._clientOnExpandComplete;
    },
    set_ClientOnExpandComplete: function(value) {
		this._clientOnExpandComplete = value;
    }
}

/// <summary>Callback method for the collapse animation - used to raise the blind animation's CollapseComplete event.</summary>
function BlindAnimation_AfterCollapse(effectInstance) {
	if (effectInstance && effectInstance.options && effectInstance.options.__client)
		effectInstance.options.__client._raiseOnClientCollapseComplete();
}

/// <summary>Callback method for the expand animation - used to raise the blind animation's ExpandComplete event.</summary>
function BlindAnimation_AfterExpand(effectInstance) {
	if (effectInstance && effectInstance.options && effectInstance.options.__client)
		effectInstance.options.__client._raiseOnClientExpandComplete();
}

Controls.BlindExtenderControlBehaviour.registerClass('Controls.BlindExtenderControlBehaviour', Controls.ExtenderControl);

/************************************************************************************************/
/* Rotator Control Behaviour                                                             */
/************************************************************************************************/

Controls.RotatorExtenderControlBehaviour = function(element)
{
    Controls.RotatorExtenderControlBehaviour.initializeBase(this, [element]);
   
    this._panelsIdToRotate = new Array();
    this._controlToRotateNext = null;
    this._controlIdToRotateNext = null;
    this._controlToRotatePrevious = null;
    this._controlIdToRotatePrevious = null;
	this._displayItemIndex = 0;
	this._nextItemIndex = 0;
	this._previousItemIndex = 0;
	this._itemCount = 0;
	this._rotateNextControlEnabled = true;
	this._rotatePreviousControlEnabled = true;
}

Controls.RotatorExtenderControlBehaviour.prototype = {
    /// <summary>
    /// Initialise Function
    /// </summary>
    initialize : function() {
    
        Controls.RotatorExtenderControlBehaviour.callBaseMethod(this, 'initialize');  
       
        this._showNextHandler = Function.createDelegate(this, this._showNext);
        this._showPreviousHandler = Function.createDelegate(this, this._showPrevious);
        this._controlToRotateNext = $get( this._controlIdToRotateNext);
        this._controlToRotatePrevious = $get( this._controlIdToRotatePrevious);
       
        $addHandler($get(this._controlIdToRotateNext), "click", this._showNextHandler);
        $addHandler($get(this._controlIdToRotatePrevious), "click", this._showPreviousHandler);

        this._itemCount = this._panelsIdToRotate.length;
    },
    
	/// <summary>
	///	showNext()
	///	Display the next tag in a group
	/// </summary>
	_showNext : function(){
	
    $(this._panelsIdToRotate[this._displayItemIndex]).hide();
    
    this._displayItemIndex = this.get_NextItemIndex();
    
    $(this._panelsIdToRotate[this._displayItemIndex]).show();
	},

    /// <summary>
    ///	showPrev()
	///	Display the next tag in a group
    /// </summary>
	_showPrevious : function(){
	
	$(this._panelsIdToRotate[this._displayItemIndex]).hide();
	
	this._displayItemIndex = this.get_PreviousItemIndex();
	
    $(this._panelsIdToRotate[this._displayItemIndex]).show();	
	},
	
	/// <summary>
    /// Control To Be Rotated.
    /// </summary> 
    get_PanelsIdToRotate : function() {
        return this._panelsIdToRotate;
    },
    set_PanelsIdToRotate : function(value) {
        this._panelsIdToRotate = value;
    },
    
    /// <summary>
    ///Current Index of Tag being Displayed.
    /// </summary>       
    get_DisplayItemIndex : function() {
        return this._displayItemIndex;
    },
    set_DisplayItemIndex : function(value) {
        this._displayItemIndex = value;
    }, 
    
    /// <summary>
    /// Next Index of Tag being Displayed.
    /// </summary>       
    get_PreviousItemIndex : function() {
        
        previousItemIndex = this._displayItemIndex - 1;
        
        if( previousItemIndex < 0 ) {
        
                previousItemIndex = this._itemCount -1;
        }
        
        return previousItemIndex;
    },
    
    /// <summary>
    /// Previous Index of Tag being Displayed.
    /// </summary>       
    get_NextItemIndex : function() {
    
        nextItemIndex = this._displayItemIndex + 1;
        
        if( nextItemIndex >= this._itemCount ) {
        
                nextItemIndex = 0;
        }
        
        return nextItemIndex;
    },
    
    /// <summary>
    /// Control To Show Next
    /// </summary>       
    get_ControlIdToRotateNext : function() {
        return this._controlIdToRotateNext;
    },
    set_ControlIdToRotateNext : function(value) {
        this._controlIdToRotateNext = value;
    }, 
    /// <summary>
    /// Control To Show Previous.
    /// </summary>       
    get_ControlIdToRotatePrevious : function() {
        return this._controlIdToRotatePrevious;
    },
    set_ControlIdToRotatePrevious : function(value) {
        this._controlIdToRotatePrevious = value;
    }
 }
 
 Controls.RotatorExtenderControlBehaviour.registerClass('Controls.RotatorExtenderControlBehaviour', Controls.ExtenderControl);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();