///////////////////////////////////////////////////////////////////////////////
//
//  inkButtons.js
//
// 
// © 2007 Microsoft Corporation. All Rights Reserved.
//
// This file is licensed as part of the Silverlight 1.0 SDK, for details look here: http://go.microsoft.com/fwlink/?LinkID=89144&clcid=0x409
//
///////////////////////////////////////////////////////////////////////////////// =========================

// InkToggleButton and InkButton Classes

// Controls the Annotations for the PageTurn app
InkToggleButton = function(plugIn, text, checkedHandler, uncheckedHandler) {
    // create XAML string
    var _str =    '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">';
    _str = _str + '    <TextBlock Height="19" Width="64" Canvas.Top="2" Canvas.Left="4" x:Name="toggleButtonText" Foreground="gray" FontFamily="Verdana" FontSize="12" TextWrapping="Wrap"/>';
    _str = _str + '    <Rectangle Height="19" Width="0" x:Name="toggleButtonRectangle" Stroke="gray" Fill="transparent" StrokeThickness="1.5"  RadiusX="4" RadiusY="4" Opacity="1"/>';
    _str = _str + '</Canvas>';
    
    // create XAML thumbnail using createFromXaml
    this.xamlElement = plugIn.content.createFromXaml(_str, true);
    
    this.rectangle = this.xamlElement.findName("toggleButtonRectangle");
    this.textBlock = this.xamlElement.findName("toggleButtonText");
    this.textBlock.text = text;
    this.rectangle.width = this.textBlock.actualWidth + 8;
  
    this.checkedHandler = checkedHandler;
    this.uncheckedHandler = uncheckedHandler;
    
    this.currentState = "unchecked";
    this.isMouseOver = false;
    this.isMouseDown = false;
    
    // Register eventhandlers
    this.xamlElement.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    this.xamlElement.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    this.xamlElement.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
}

InkToggleButton.prototype.handleMouseEnter = function(s,e) {
    this.rectangle.StrokeThickness = 2.5;
    this.isMouseOver = true;
}

InkToggleButton.prototype.handleMouseLeave = function(s,e) {
    this.rectangle.StrokeThickness = 1.5;
    this.isMouseOver = false;
}

InkToggleButton.prototype.handleMouseDown = function(s,e) {
    // toggle button state
    if (this.currentState == "unchecked") {
        this.currentState = "checked";
        this.textBlock.Foreground = "white";
        this.rectangle.Stroke = "white";

        if (this.checkedHandler) {
            this.checkedHandler();
        }
    } else {
        this.currentState = "unchecked";
        this.textBlock.Foreground = "gray";
        this.rectangle.Stroke = "gray";
        
        if (this.uncheckedHandler) {
            this.uncheckedHandler();
        }
    }
}

InkButton = function(plugIn, text, clickedHandler) {
    // create XAML string
    var _str =    '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">';
    _str = _str + '    <TextBlock Height="19" Canvas.Top="2" Canvas.Left="4" x:Name="buttonText" Foreground="White" FontFamily="Verdana" FontSize="12" TextWrapping="Wrap"/>';
    _str = _str + '    <Rectangle Height="19" Width="0" x:Name="buttonRectangle" Stroke="White" Fill="transparent" StrokeThickness="1.5"  RadiusX="4" RadiusY="4" Opacity="1"/>';
    _str = _str + '</Canvas>';
    
    // create XAML thumbnail using createFromXaml
    this.xamlElement = plugIn.content.createFromXaml(_str, true);
    
    this.rectangle = this.xamlElement.findName("buttonRectangle");
    this.textBlock = this.xamlElement.findName("buttonText");
    this.textBlock.text = text;
    this.rectangle.width = this.textBlock.actualWidth + 8;
  
    this.clickedHandler = clickedHandler;
        
    // Register eventhandlers
    this.xamlElement.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    this.xamlElement.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    this.xamlElement.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
}

InkButton.prototype.handleMouseEnter = function(s,e) {
    this.rectangle.StrokeThickness = 2.5;
}

InkButton.prototype.handleMouseLeave = function(s,e) {
    this.rectangle.StrokeThickness = 1.5;
}

InkButton.prototype.handleMouseDown = function(s,e) {
    this.textBlock.Foreground = "white";
    this.rectangle.Stroke = "white";

    if (this.clickedHandler) {
        this.clickedHandler();
    }
}

