// LaunchE version 0.001
// Copyright 2007 by Launch21 LLC http://www.launch21.com
// licensed under the Creative Commons Attribution 2.5 License.
// http://creativecommons.org/licenses/by/2.5/

var LaunchE =
{
Controls: {}
}

// General Utility Functions

function $C(name)
{
    return LaunchE.Controls[name];
}

function LaunchE_DoMouseEnter(sender, eventArgs)
{
    var TheCtl = LaunchE_Find(sender);
    if(TheCtl != null && TheCtl.DoMouseEnter != null)
        TheCtl.DoMouseEnter(sender, eventArgs);
}

function LaunchE_DoMouseLeave(sender, eventArgs)
{
    var TheCtl = LaunchE_Find(sender);
    if(TheCtl != null && TheCtl.DoMouseLeave != null)
        TheCtl.DoMouseLeave(sender, eventArgs);
}

function LaunchE_DoMouseDown(sender, eventArgs)
{
    var TheCtl = LaunchE_Find(sender);
    if(TheCtl != null && TheCtl.DoMouseDown != null)
        TheCtl.DoMouseDown(sender, eventArgs);
}

function LaunchE_DoMouseUp(sender, eventArgs)
{
    var TheCtl = LaunchE_Find(sender);
    if(TheCtl != null && TheCtl.DoMouseUp != null)
        TheCtl.DoMouseUp(sender, eventArgs);
}

function LaunchE_Find(sender)
{
    for(var Name in LaunchE.Controls)
    {
        var TheControl = LaunchE.Controls[Name];
        if(sender.equals(TheControl.Control))
        {
            return TheControl;
        }
    }
    return null;
}

LaunchE.base = function(parent, name, x, y, width, height)
{
if(LaunchE.TheWPFE == null)
    return;
var TheXAML = this.BuildXaml(this.template(),arguments);
this.Parent = parent;
this.Args = arguments;
this.Control = LaunchE.TheWPFE.createFromXaml(TheXAML);
this.Name = name;
this.NormalTree = this.Control;
this.Width = width;
this.Height = height;
TheXAML = this.BuildXaml(this.hovertemplate(),arguments);
this.HoverTree = LaunchE.TheWPFE.createFromXaml(TheXAML);
TheXAML = this.BuildXaml(this.pressedtemplate(),arguments);
this.PressedTree = LaunchE.TheWPFE.createFromXaml(TheXAML);
this.Control.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.Control.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.Control.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.Control.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
this.HoverTree.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.HoverTree.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.HoverTree.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.HoverTree.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
this.PressedTree.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.PressedTree.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.PressedTree.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.PressedTree.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
var ParentNode = LaunchE.TheWPFE.FindName(parent);
ParentNode.Children.Add(this.Control);
LaunchE.Controls[name] = this;
this.Adjust();
}

LaunchE.base.prototype.Adjust = function()
{
}

LaunchE.base.prototype.Rebuild = function()
{
var TheXAML = this.BuildXaml(this.template(),this.Args);
this.NormalTree = LaunchE.TheWPFE.createFromXaml(TheXAML);
TheXAML = this.BuildXaml(this.hovertemplate(),this.Args);
this.HoverTree = LaunchE.TheWPFE.createFromXaml(TheXAML);
TheXAML = this.BuildXaml(this.pressedtemplate(),this.Args);
this.PressedTree = LaunchE.TheWPFE.createFromXaml(TheXAML);
this.NormalTree.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.NormalTree.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.NormalTree.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.NormalTree.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
this.HoverTree.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.HoverTree.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.HoverTree.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.HoverTree.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
this.PressedTree.MouseEnter="javascript:LaunchE_DoMouseEnter";
this.PressedTree.MouseLeave="javascript:LaunchE_DoMouseLeave";
this.PressedTree.MouseLeftButtonDown="javascript:LaunchE_DoMouseDown";
this.PressedTree.MouseLeftButtonUp="javascript:LaunchE_DoMouseUp";
var Parent = this.Control.getParent();
if(Parent == null)
{
Parent = LaunchE.TheWPFE.FindName(this.Parent);
}
Parent.Children.Remove(this.Control);
this.Control = this.NormalTree;
Parent.Children.Add(this.Control);
this.Adjust();
}

LaunchE.base.prototype.BuildXaml = function(template, args)
{
    var TheText = template;
    for(var i=0;i<args.length;i++)
    {
        TheText = TheText.replace("%"+i, args[i]);
        TheText = TheText.replace("%"+i, args[i]);
    }
    return(TheText);
}

LaunchE.base.prototype.DoMouseEnter = function(sender, eventArgs)
{
this.MouseIn = true;
var Parent = this.Control.getParent();
Parent.Children.Remove(this.Control);
if(this.MouseDown)
    this.Control = this.PressedTree;
else
    this.Control = this.HoverTree;
Parent.Children.Add(this.Control);
this.Adjust();
}

LaunchE.base.prototype.DoMouseLeave = function(sender, eventArgs)
{
this.MouseIn = false;
var Parent = this.Control.getParent();
Parent.Children.Remove(this.Control);
this.Control = this.NormalTree;
Parent.Children.Add(this.Control);
this.Adjust();
}

LaunchE.base.prototype.DoMouseDown = function(sender, eventArgs)
{
this.MouseDown = true;
var Parent = this.Control.getParent();
Parent.Children.Remove(this.Control);
this.Control = this.PressedTree;
Parent.Children.Add(this.Control);
this.Adjust();
}

LaunchE.base.prototype.DoMouseUp = function(sender, eventArgs)
{
this.MouseDown = false;
var Parent = this.Control.getParent();
Parent.Children.Remove(this.Control);
if(this.MouseIn)
    this.Control = this.HoverTree;
else
    this.Control = this.NormalTree;
Parent.Children.Add(this.Control);
this.Adjust();
if(this.MouseIn && this.Click != null)
{
    this.Click(sender, eventArgs);
}
}

// Base Templates
LaunchE.base.prototype.template = function() { return "<Canvas Name='%1' Canvas.Left='%2' Canvas.Top='%3' Width='%4' Height='%5' >%%%</Canvas>"; };

//////////// BUTTON
LaunchE.button = function(parent, name, x, y, width, height, text)
{
LaunchE.base.call(this, parent, name, x, y, width, height, text);
}
LaunchE.button.prototype = new LaunchE.base();
LaunchE.button.prototype.constructor = LaunchE.button;
LaunchE.button.prototype.superclass = LaunchE.base;

LaunchE.button.prototype.Adjust = function()
{
//    var TextBlock = this.Control.Children.GetItem(1);
    var TextBlock = this.Control.FindName(this.Name+"-ButtonText");
    if(TextBlock != null)
    {
        TextBlock.SetValue("Canvas.Left", (this.Width-TextBlock.ActualWidth)/2);
        TextBlock.SetValue("Canvas.Top", (this.Height-TextBlock.ActualHeight)/2);
    }
}

// Button Templates
LaunchE.button.prototype.template = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle RadiusX='4' RadiusY='4' Width='%4' Height='%5' Stroke='#FF5CA4BA'><Rectangle.Fill><LinearGradientBrush StartPoint='0.5,0' EndPoint='0.5,1' ><GradientStop Color='#FFF6FFFF' Offset='0.0' /><GradientStop Color='#FFDFF4E3' Offset='1.0' /></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock Name='%1-ButtonText' Canvas.Top='0' Canvas.Left='0' Width='100' Height='20' FontFamily='Verdana' FontSize='12'>%6</TextBlock>");};

LaunchE.button.prototype.hovertemplate = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle RadiusX='4' RadiusY='4' Width='%4' Height='%5' Stroke='#FF5CA4BA'><Rectangle.Fill><LinearGradientBrush StartPoint='0.5,0' EndPoint='0.5,1' ><GradientStop Color='#FFE6EFEF' Offset='0.0' /><GradientStop Color='#FFCFE4D3' Offset='1.0' /></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock Name='%1-ButtonText' Canvas.Top='0' Canvas.Left='0' Width='100' Height='20' FontFamily='Verdana' FontSize='12'>%6</TextBlock>");};

LaunchE.button.prototype.pressedtemplate = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle RadiusX='4' RadiusY='4' Width='%4' Height='%5' Stroke='#FF5CA4BA'><Rectangle.Fill><LinearGradientBrush StartPoint='0.5,0' EndPoint='0.5,1' ><GradientStop Color='#FFE6EFEF' Offset='0.0' /><GradientStop Color='#FFAFD4C3' Offset='1.0' /></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock Name='%1-ButtonText' Canvas.Top='0' Canvas.Left='0' Width='100' Height='20' FontFamily='Verdana' FontWeight='Bold' FontSize='12'>%6</TextBlock>");};


//////////// CHECKBOX
LaunchE.checkbox = function(parent, name, x, y, width, height, text, checked)
{
this.Checked = checked;
this.Click = function () { this.DoCheckboxClick();};
LaunchE.base.call(this, parent, name, x, y, width, height, text);
}
LaunchE.checkbox.prototype = new LaunchE.base();
LaunchE.checkbox.prototype.constructor = LaunchE.button;
LaunchE.checkbox.prototype.superclass = LaunchE.base;

LaunchE.checkbox.prototype.Adjust = function()
{
    var TheCheck = this.Control.FindName(this.Name+"-Check");
    if(TheCheck != null)
    {
        if(this.Checked)
        {
            TheCheck.Stroke = "Blue";
            TheCheck.Fill = "Blue";
        }
        else
        {
            TheCheck.Stroke = "Transparent";
            TheCheck.Fill = "Transparent";
        }
    }
}

LaunchE.checkbox.prototype.DoCheckboxClick = function()
{
    this.SetChecked(!this.Checked);
}

LaunchE.checkbox.prototype.SetChecked = function(NewChecked)
{
    if(this.Checked == NewChecked)
        return;
    this.Checked = NewChecked;
    this.Adjust();
}

// Checkbox Templates
LaunchE.checkbox.prototype.template = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle Width='16' Height='16' Stroke='#FF5CA4BA' Fill='#FFDDDDDD'/><Rectangle Canvas.Top='3' Canvas.Left='3' Width='10' Height='10' Fill='#FFDDDDDD' /><Path Name='%1-Check' Data='M 4,8 L 8,12 14,6 13,4 8,10 5,6 4,8' Stroke='Blue' Fill='Blue' /><TextBlock Canvas.Top='0' Canvas.Left='20' Width='100' Height='20' FontFamily='Verdana' FontSize='12'>%6</TextBlock>");};

LaunchE.checkbox.prototype.hovertemplate = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle Width='16' Height='16' Stroke='#FF5CA4BA' Fill='#FFDDDDDD'/><Rectangle Canvas.Top='3' Canvas.Left='3' Width='10' Height='10' Fill='#FFBBBBDD' /><Path Name='%1-Check' Data='M 4,8 L 8,12 14,6 13,4 8,10 5,6 4,8' Stroke='Blue' Fill='Blue' /><TextBlock Canvas.Top='0' Canvas.Left='20' Width='100' Height='20' FontFamily='Verdana' FontSize='12'>%6</TextBlock>");};

LaunchE.checkbox.prototype.pressedtemplate = function() { 
return LaunchE.base.prototype.template.apply(this).replace("%%%", 
"<Rectangle Width='16' Height='16' Stroke='#FF5CA4BA' Fill='#FFDDDDDD'/><Rectangle Canvas.Top='3' Canvas.Left='3' Width='10' Height='10' Fill='#FFBBBBDD' /><Path Name='%1-Check' Data='M 4,8 L 8,12 14,6 13,4 8,10 5,6 4,8' Stroke='Blue' Fill='Blue' /><TextBlock Canvas.Top='0' Canvas.Left='20' Width='100' Height='20' FontFamily='Verdana' FontSize='12'>%6</TextBlock>");};


