﻿// Javascript class that handles events to simulate a button
function button(target, backgroundElement, overAnimation, outAnimation, overAnimationTarget, outAnimationTarget, downState, clickHandler, downHandler, index, idleAnimation) {
    this.target = target;
    this.backgroundElement = backgroundElement;
    this.overAnimation = overAnimation; //button's mouseover animation, equals "none" if there is none
    this.outAnimation = outAnimation; //button's mouseout animation, equals "none" if there is none
    this.overAnimationTarget = overAnimationTarget; //button's mouseover handler, equals "none" if there is none
    this.outAnimationTarget = outAnimationTarget; //button's mouseout handler, equals "none" if there is none
    this.downState = downState;
    //this.selectedElement = selectedElement;
    // this.hoverFill = hoverFill;
   // this.mouseDownFill = mouseDownFill;
    this.clickHandler = clickHandler;
    this.downHandler = downHandler;
    
    this.index = index;  //string of the button's name/identifier
    this.idleAnimation = idleAnimation;  //the button's idle animation, equals "none" if there is none
    
    // Register eventhandlers
    if(this.overAnimation != "none")
    {
        target.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    }
    if(this.outAnimation != "none")
    {
        target.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    }
    if(this.clickHandler != "none")
    {
        target.addEventListener("mouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
    }
    if(this.downHandler != "none")
    {
        target.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    }
    if(idleAnimation != "none")
    {
        idleAnimation.addEventListener("completed", Silverlight.createDelegate(this, this.animationLoop));
        idleAnimation.begin();
    }
}

button.prototype.animationLoop = function(sender, eventArgs)
{
    this.idleAnimation.stop();
    this.idleAnimation.begin();
}

button.prototype.handleMouseEnter = function(sender, eventArgs) 
{
    //this.oldFill = this.backgroundElement.fill;
    //this.backgroundElement.fill = this.hoverFill;
    this.target.Cursor = "Hand";
    this.overAnimation.stop();
    this.overAnimationTarget["Storyboard.TargetName"] =  this.backgroundElement.name;
    this.overAnimation.begin();
    
}

button.prototype.handleMouseLeave = function(sender, eventArgs) {
     //this.backgroundElement.fill = this.oldFill;
     this.outAnimation.stop();
     this.outAnimationTarget["Storyboard.TargetName"] =  this.backgroundElement.name;
     this.outAnimation.begin();
}

button.prototype.handleMouseUp = function(sender, eventArgs) {
    if (this.clickHandler) {
        this.clickHandler(sender, eventArgs);
    }
    this.downState["Opacity"] = 0;
    //this.backgroundElement.fill = this.hoverFill;
    //this.selectedElement.fill = this.selectedFill;
}

button.prototype.handleMouseDown = function(sender, eventArgs) {
    
    if (this.downHandler) {
        this.downHandler(sender, eventArgs);
    }
    this.downState["Opacity"] = 100;
    //this.backgroundElement.fill = this.mouseDownFill;
    
}