﻿//
// Expander
//

EventType.ExpanderCommand = "evExpanderCommand";

function ExpanderCommandType() {}
ExpanderCommandType.Expand = "Expand";
ExpanderCommandType.Collapse = "Collapse";

// extend EventArgs
ExpanderCommandArgs.prototype = new EventArgs();
ExpanderCommandArgs.prototype.constructor= ExpanderCommandArgs;
function ExpanderCommandArgs(command)
{
    this.Type = EventType.ExpanderCommand;
	this.Command = null;
	
	if (arguments.length > 0)
	{
		this.Command = command;
	}
  
	this.toString = function()
	{
		return "[EVENT: "+this.Type+" SENDER: "+this.Source+" COMMAND: "+this.Command+"]";
	}
}


function ExpanderBehavior() {}
ExpanderBehavior.None = 0;
ExpanderBehavior.Click = 1;
ExpanderBehavior.Over = 2;

function Expander()
{
    this.Container = null;
    this.Window = null;
    this.LinkID = null;
    this.Behavior = ExpanderBehavior.Click;
    this.IsExpand = false;

    this.Initialize = function(container, containerWindow, linkID)
    {
		this.Container = container;
		this.Window = containerWindow;
		this.LinkID = linkID;
		
        var linkDiv = this.GetDiv(this.LinkID);
        switch(this.Behavior) {
            case ExpanderBehavior.Click:
                this.OnLinkClickFunction = new Function('', this.Container + '.OnLinkClick_BehaviorClick();');
                BrowserEvent.Attach(linkDiv, "onclick", this.OnLinkClickFunction);
                break;
            case ExpanderBehavior.Over:
                this.OnLinkMouseOverFunction = new Function('', this.Container + '.OnLinkMouseOver_BehaviorOver();');
                this.OnLinkMouseOutFunction = new Function('', this.Container + '.OnLinkMouseOut_BehaviorOver();');
                BrowserEvent.Attach(linkDiv, "onmouseover", this.OnLinkMouseOverFunction);
                BrowserEvent.Attach(linkDiv, "onmouseout", this.OnLinkMouseOutFunction);
                break;
        }
    }

    this.OnLinkClick_BehaviorClick = function ()
    {
        if(! this.IsExpand) {
            var args = new ExpanderCommandArgs(ExpanderCommandType.Expand);
            args.Container = this.Container;
            Expander.Event.Send(args);
            this.IsExpand = true;
        } else {
            var args = new ExpanderCommandArgs(ExpanderCommandType.Collapse);
            args.Container = this.Container;
            Expander.Event.Send(args);
            this.IsExpand = false;
        }
    }

    this.OnLinkMouseOver_BehaviorOver = function()
    {
        if(! this.IsExpand) {
            var args = new ExpanderCommandArgs(ExpanderCommandType.Expand);
            args.Container = this.Container;
            Expander.Event.Send(args);
            this.IsExpand = true;
        }
    }
    this.OnLinkMouseOut_BehaviorOver = function()
    {
        if(this.IsExpand) {
            var args = new ExpanderCommandArgs(ExpanderCommandType.Collapse);
            args.Container = this.Container;
            Expander.Event.Send(args);
            this.IsExpand = false;
        }
    }
    
    this.GetDiv = function(id)
    {
        var div = this.Window.document.getElementById(id);
        return div;
    }
	this.ParsePx = function(arg)
	{
		var retVal;
		var re = /\d+px/i;
		if (arg.match(re))
		{
			retVal = arg.substr(0, arg.length-2);
		}
		else
		{
			retVal = arg;
		}
		return Number(retVal);
	}
}
Expander.Event = new Event(EventType.ExpanderCommand);


function AnimationType() {}
AnimationType.None = 0;
AnimationType.NoAnimate = 1;
AnimationType.Linear = 2;

function AnimationState() {}
AnimationState.None = 0;
AnimationState.Expand = 1;
AnimationState.Collapse = 2;

function ExpanderViewport()
{
    this.Container = null;
    this.Window = null;
    this.ViewportID = null;
    this.ViewID = null;
    this.ViewHeight = 0;
    this.ListenTo = null;
    this.AnimationType = AnimationType.NoAnimate;
    
    this.Initialize = function(container, containerWindow, viewportID)
    {
		this.Container = container;
		this.Window = containerWindow;
		this.ViewportID = viewportID;

		Loader.AddLoadHandler("" + this.Container + ".OnLoad()");
		Loader.AddUnloadHandler("" + this.Container + ".OnUnload()");
    }
	this.OnLoad = function()
	{
		this.AddEventHandlers();
		this.CalcurateDimensions();
	}
	this.OnUnload = function()
	{
	    this.RemoveEventHandlers();
	}
	
    this.CalcurateDimensions = function()
    {
		if(this.ViewHeight == 0)
		{
		    var viewportDiv = this.GetDiv(this.ViewportID);
		    var viewDiv = this.GetDiv(this.ViewID);
		    
		    var tempHeight = viewportDiv.style.height;
		    var tempDisplay = viewportDiv.style.display;
		    viewportDiv.style.height = '0px';
		    viewportDiv.style.display = 'block';
		    
		    // this.ViewHeight = this.ParsePx(viewDiv.style.height);
		    this.ViewHeight = viewDiv.scrollHeight;
		    
		    viewportDiv.style.height = tempHeight;
		    viewportDiv.style.display = tempDisplay;
		}
	}
	
	this.AddEventHandlers = function()
	{
        switch(this.AnimationType) {
            case AnimationType.NoAnimate:
        		this.ExpanderCommandHandler = new EventHandler(this.Container);
		        this.ExpanderCommandHandler.Container = this.Container;
        		this.ExpanderCommandHandler.MethodName = "OnExpanderCommand_NoAnimate";
		        Expander.Event.AddHandler(this.ExpanderCommandHandler);
                break;
            case AnimationType.Linear:
        		this.ExpanderCommandHandler = new EventHandler(this.Container);
		        this.ExpanderCommandHandler.Container = this.Container;
        		this.ExpanderCommandHandler.MethodName = "OnExpanderCommand_Linear";
		        Expander.Event.AddHandler(this.ExpanderCommandHandler);
                break;
        }
	}
	this.RemoveEventHandlers = function()
	{
        switch(this.AnimationType) {
            case AnimationType.NoAnimate:
            case AnimationType.Linear:
	            Expander.Event.RemoveHandler(this.ExpanderCommandHandler);
	            break;
	    }
	}
    
    this.OnExpanderCommand_NoAnimate = function (args)
    {
        if(args.Container != this.ListenTo)
            return;
            
        var viewportDiv = this.GetDiv(this.ViewportID);
        switch(args.Command) {
            case ExpanderCommandType.Expand:
                viewportDiv.style.display = 'block';
                break;
            case ExpanderCommandType.Collapse:
                viewportDiv.style.display = 'none';
                break;
        }
    }

    this.IsAnimating = false;
    this.OnExpanderCommand_Linear = function (args)
    {
        if(args.Container != this.ListenTo)
            return;
        if(this.IsAnimating)
            return;
        
        this.IsAnimating = true;
        switch(args.Command) {
            case ExpanderCommandType.Expand:
                this.AnimateLinear(AnimationState.Expand);
                break;
            case ExpanderCommandType.Collapse:
                this.AnimateLinear(AnimationState.Collapse);
                break;
        }
    }
    
    this.AnimationPercent = 0;
    this.AnimationTimer = null;
    this.AnimationInterval = 50;
    this.AnimateLinear = function(initialState)
    {
        if(this.AnimationTimer == null) {
            this.AnimationState = initialState;
            
            switch(this.AnimationState) {
                case AnimationState.Expand:
                    this.AnimationPercent = 0;
                    break;
                case AnimationState.Collapse:
                    this.AnimationPercent = 100;
                    break;
            }
            
            var viewportDiv = this.GetDiv(this.ViewportID);
            viewportDiv.style.height = '' + (this.AnimationPercent*this.ViewHeight/100) + 'px';
            viewportDiv.style.display = 'block';
            this.AnimationTimer = setTimeout(this.Container + ".TimedUpdateLinearAnimation()", this.AnimationInterval);
        }
    }
    this.TimedUpdateLinearAnimation = function()
    {
        switch(this.AnimationState) {
            case AnimationState.Expand:
                this.AnimationState = this.UpdateLinearAnimation_Expand();
                break;
            case AnimationState.Collapse:
                this.AnimationState = this.UpdateLinearAnimation_Collapse();
                break;
        }
        if(this.AnimationState > 0) {
    		this.AnimationTimer = setTimeout(this.Container + ".TimedUpdateLinearAnimation()", this.AnimationInterval);
    	} else {
    	    this.AnimationTimer = null;
        }
    }
    this.UpdateLinearAnimation_Expand = function()
    {
        var viewportDiv = this.GetDiv(this.ViewportID);
        this.AnimationPercent = this.AnimationPercent + 25;
        
        if(viewportDiv != null) {
            viewportDiv.style.height = '' + (this.AnimationPercent*this.ViewHeight/100) + 'px';
        }
        
        if(this.AnimationPercent < 100) {
            return AnimationState.Expand;
        } else {
            viewportDiv.style.height = 'auto';
            this.IsAnimating = false;
            this.IsExpand = true;
            return AnimationState.None;
        }
    }
    this.UpdateLinearAnimation_Collapse = function()
    {
        var viewportDiv = this.GetDiv(this.ViewportID);
        this.AnimationPercent = this.AnimationPercent - 20;
        
        if(viewportDiv != null) {
            viewportDiv.style.height = '' + (this.AnimationPercent*this.ViewHeight/100) + 'px';
        }
        
        if(this.AnimationPercent > 0) {
            return AnimationState.Collapse;
        } else {
            viewportDiv.style.display = 'none';
            this.IsAnimating = false;
            this.IsExpand = false;
            return AnimationState.None;
        }
    }
    
    this.GetDiv = function(id)
    {
        var div = this.Window.document.getElementById(id);
        return div;
    }
	this.ParsePx = function(arg)
	{
		var retVal;
		var re = /\d+px/i;
		if (arg.match(re))
		{
			retVal = arg.substr(0, arg.length-2);
		}
		else
		{
			retVal = arg;
		}
		return Number(retVal);
	}
}

