Type.registerNamespace('McCannErickson.Silverlight.Controls')

McCannErickson.Silverlight.Controls.Button = function(element, name, canvasLeft, canvasTop)
{
	this.element = element;
	if(canvasLeft)
		this.element["Canvas.Left"] = canvasLeft;
	
	if(canvasTop)
		this.element["Canvas.Top"] = canvasTop;
	
	this.name = name;
	
	this.isSelected = false;

	this.buttonMouseEnter = this.element.findName(this.name + "_MouseEnter");
	this.buttonMouseLeave = this.element.findName(this.name +"_MouseLeave");
	
	this.element.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
	this.element.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
	this.element.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
	this.element.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
}

McCannErickson.Silverlight.Controls.Button.prototype =
{
	  addMouseOverHandler: function(handler) { this.getEvents().addHandler("MouseOver", handler); },
    removeMouseOverHandler: function(handler) { this.getEvents().removeHandler("MouseOver", handler); },
		
		addMouseOutHandler: function(handler) { this.getEvents().addHandler("MouseOut", handler); },
    removeMouseOutHandler: function(handler) { this.getEvents().removeHandler("MouseOut", handler); },
		
		addMouseDownHandler: function(handler) { this.getEvents().addHandler("MouseDown", handler); },
    removeMouseDownHandler: function(handler) { this.getEvents().removeHandler("MouseDown", handler); },
		
		addMouseUpHandler: function(handler) { this.getEvents().addHandler("MouseUp", handler); },
    removeMouseUpHandler: function(handler) { this.getEvents().removeHandler("MouseUp", handler); },
		
		getName: function() { return this.name; },
		
		getSelected: function() { return this.isSelected; },
		setSelected: function(value) { this.isSelected = value; },
		
		getEvents: function()
    {
        if(!this.events)
        {
            this.events = new Sys.EventHandlerList();
        }
        return this.events;
    },
    
    raiseEvent: function(eventName, eventArgs)
    {
      var handler = this.getEvents().getHandler(eventName);
        
      if(handler)
      {
        if(!eventArgs) eventArgs = Sys.EventArgs.Empty;
					handler(this, eventArgs);
      }
    },
		
		handleMouseEnter: function(sender, eventArgs)
		{
			this.buttonMouseEnter.Begin();			
			this.raiseEvent("MouseOver", this);
		},
		
		handleMouseLeave: function(sender, eventArgs)
		{
			if(!this.isSelected)
			{
				this.buttonMouseLeave.Begin();
			}
			
			this.raiseEvent("MouseOut", this);
		},
		
		handleMouseDown: function(sender, eventArgs)
		{
			this.isSelected = true;
			this.raiseEvent("MouseDown", this);
		},
		
		handleMouseUp: function(sender, eventArgs)
		{
			this.raiseEvent("MouseUp", this)
		}
}

McCannErickson.Silverlight.Controls.Button.registerClass('McCannErickson.Silverlight.Controls.Button');