﻿Keyboard.Key = function(control, target, src, x, y, height, width)
{
    this.radius = 300;
    this.root = target.getParent().findName("rootCanvas");
    this.xCenter = this.root["Width"]/2;
    
    this.control = control;
    this.image = target.findName("thumbHolder").findName("im");
    //this.mouseEnter = target.findName("mouseEnter");
    //this.mouseDown = target.findName("mouseLeftButtonDown");
    this.animation = target.findName("animation");
    
    this.target = target;
    this.target.cursor  = "Hand";
    this.target["Canvas.Top"] = y;
    this.target["Canvas.Left"] = x;
    
    this.image["Height"] = height;
    this.image["Width"] = width;
    this.image["Source"]= src;
        
    this.target.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
    //this.target.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
   
    this.target.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    this.target.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
}

Keyboard.Key.prototype = 
{
   setPosition: function(angle)
   {
        if(this.radius < 380) this.radius += 4;
   
        var cos = Math.cos(angle);
        var sin = Math.sin(angle);
        
        // set opacity
        this.image["Opacity"] = 1 + sin * 0.8;
        
        // set Top and Left
        var x = cos * this .radius + this.xCenter; 
        this.image["Canvas.Left"] = x;
        this.image["Canvas.Top"] = sin * 70;

        // set zindex
        this.scale = sin;
        var zindex = Math.floor(sin * 16);
        this.target["Canvas.ZIndex"] = zindex;
        
        // set x and y scale
        var yscale = 1 + sin * 0.1;
        var xamlFragment = '<ScaleTransform ScaleX="' + this.scale + '" ScaleY="' + yscale + '" CenterX="'+(this.image["Width"]/2)+'" CenterY="'+(this.image["Height"]/2)+'" />';
        var st = this.control.content.createFromXaml(xamlFragment);
        this.image["RenderTransform"] = st;
        
        /*
        // set text field value
        var tf = this.image.findName("stats");
        tf["Text"] = "ZIndex: " + zindex;
        tf["Canvas.Left"] = x;
        tf["Canvas.Top"] = this.image["Canvas.Top"] + this.image["Height"];
        
        // clear and redraw line
        var line = this.target.children.findName("indicator");
        if(line != null)
            this.target.children.remove(line);
        line = this.control.content.createFromXaml('<Path Name="indicator" Width="1" Height="52.245" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Canvas.Left="61.5" Canvas.Top="168.5" Data="M62,169 L62,220.24451"/>');
        line["Canvas.Top"] = this.image["Canvas.Top"] + this.image["Height"];
        //this.image.RenderTransformOrigin = "5 5";
        line["Canvas.Left"] = this.image["Canvas.Left"];
        this.target.children.add(line);
        */
   },
   
   handleMouseEnter: function(sender, eventArgs)
   {
        //this.image["Source"]="http://entimg.msn.com/i/gal/JessicaSimpsonRETNA/Simpson_Jes75981_75.jpg";
       // alert(this.animation.findName("test").findName("test1").Value);
       //this.animation.findName("test").findName("test1").Value = 2.3;
        //      this.animation.begin();
   },
   
   handleMouseLeave: function(sender, eventArgs)
   {
        //this.mouseEnter.stop();
   },
   
   handleMouseDown: function(sender, eventArgs)
   {
       var bigimage = this.root.findName("mainImage");
       bigimage["Opacity"] = 0;
       var secondImage = this.root.findName("secondImage");
       
       //this.root.findName("turnEffect").seek("0:0:0.501");
       //this.root.findName("turnEffect").stop();
       this.root.findName("secondTurnEffect").begin();
       
       secondImage["Source"] = bigimage["Source"];
       bigimage["Source"] = this.image["Source"];
       
       _this = this;
       window.setTimeout(function () {_this.revealNew()}, 200);
   },
   
   revealNew: function ()
   {
        var bigimage = this.root.findName("mainImage");
       bigimage["Opacity"] = 1;
        this.root.findName("turnEffect").begin();
   },
   
   handleMouseUp: function(sender, eventArgs)
   {
        // this.mouseDown.stop();
   }
 }
   
   
   