﻿//===============================================================================
// ID		  : $Id: Codex.js 131 2008-03-31 13:56:30Z stefan.schult $
// Author     : $Author: stefan.schult $
// Description: JSClasses for Effects
//===============================================================================

MarqueeAtMouseOver = function(Canvas, TextBlock){
	this.Animation = Canvas.findName("TextMarquee");
    this.Canvas = Canvas;
    this.TextBlock = TextBlock;
	var thisClass = this;
    this.toString = function(){
        return "MarqueeAtMouseOver";
    };
	Canvas.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleButtonMouseEnter));
	Canvas.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleButtonMouseLeave));
};
MarqueeAtMouseOver.prototype = {
	handleButtonMouseEnter: function(sender, eventArgs) 
	{
		if(this.TextBlock.ActualWidth > this.TextBlock.getParent().Width) {
			this.Animation.stop();
			var DoubleAnimation = sender.findName(this.Animation.Name + "DoubleAnimation");
			DoubleAnimation["Storyboard.TargetName"] = this.TextBlock.Name + "TranslateTransform";
			var oversize = this.TextBlock.getParent().Width - this.TextBlock.ActualWidth-7;
			DoubleAnimation["To"] = oversize;
			var secs = Math.round(oversize/-7*0.2*10)/10;
			DoubleAnimation.Duration = "0:0:" + secs;
			this.Animation.begin();
		}
	},
	
	handleButtonMouseLeave: function(sender, eventArgs) 
	{
		this.Animation.stop();
	}	
}

Lasso = function(rootElement, lassoObject) {
    this.rootElement = rootElement;
    this.lasso = lassoObject;
    this.BoxesTranslateTransform = rootElement.findName("BoxesTranslateTransform");
    this.lasso.Opacity = 0;
	this.isMouseDown = false;
	this.handleEvents = [];
	this.handleCookie = {
	    handleMouseDown : rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown)),
	    handleMouseUp : rootElement.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp)),
	    handleMouseLeave : rootElement.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave)),
	    handleMouseMove : rootElement.addEventListener("MouseMove", Silverlight.createDelegate(this, this.handleMouseMove))
	}
}
Lasso.prototype = {
    toString: function(){
        return "Lasso"
    },
    mouseDelta: {
        X:0,Y:0
    },
	addEvent: function(eventHandler) 
	{
		this.handleEvents.push(eventHandler);
	},
	setVisibility: function(value) 
	{
		switch(value){
			case "Collapsed":
			case "collapsed":
				this.rootElement.Visibility = "Collapsed";
				break;
			case "Visible":
			case "visible":
				this.rootElement.Visibility = "Visible";
				break;
		}
	},
	getVisibility: function() 
	{
		return this.rootElement.Visibility;
	},
	handleMouseDown: function(sender, eventArgs) 
	{
	    this.isMouseDown = true;
        this.mouseDelta = {
            X:0,Y:0
        }   
        var currX = eventArgs.getPosition(null).x;
        var currY = eventArgs.getPosition(null).y;
        this.mouseBegin= {
            X:currX,Y:currY
        }   
        //sender.findName("debug").Text = "" + currX + ":" + currY; 
	    sender.captureMouse();
    },
	handleMouseMove: function(sender, mouseEventArgs) 
	{
        if (this.isMouseDown == true)
        {
            this.lasso.Opacity = 1;
            // Retrieve the current position of the mouse.
            var currX = mouseEventArgs.getPosition(null).x;
            var currY = mouseEventArgs.getPosition(null).y;
            this.mouseDelta.X = currX - this.mouseBegin.X;
            this.mouseDelta.Y = currY - this.mouseBegin.Y;
//            this.rootElement.findName("Debug").Text = "" + this.mouseDelta.X + ":" + this.mouseDelta.Y + " | " + this.lasso["Canvas.Top"]+ ":" + this.lasso["Canvas.Left"];
            if(this.mouseDelta.Y<0){
                this.lasso["Canvas.Top"] = this.mouseBegin.Y - this.getOffsetY(this.rootElement) + this.mouseDelta.Y;
                this.lasso["Height"] = -this.mouseDelta.Y;
            } else {
                this.lasso["Canvas.Top"] = this.mouseBegin.Y - this.getOffsetY(this.rootElement);
                this.lasso["Height"] = this.mouseDelta.Y;
            }
            if(this.mouseDelta.X<0){
                this.lasso["Canvas.Left"] = this.mouseBegin.X - this.getOffsetX(this.rootElement) + this.mouseDelta.X - this.BoxesTranslateTransform.X;
                this.lasso["Width"] = -this.mouseDelta.X;
            } else {
                this.lasso["Canvas.Left"] = this.mouseBegin.X - this.getOffsetX(this.rootElement) - this.BoxesTranslateTransform.X;
                this.lasso["Width"] = this.mouseDelta.X;
            }
            /*/// Update the beginning position of the mouse.
            beginX = currX;
            beginY = currY;*/
        }
	},
	handleMouseLeave: function(sender, eventArgs) 
	{
	    this.isMouseDown = false;
	    sender.releaseMouseCapture();
        this.lasso.Opacity = 0;
	},
	handleMouseUp: function(sender, eventArgs) 
	{
	    this.isMouseDown = false;
	    sender.releaseMouseCapture();
        this.lasso.Opacity = 0;
        for(var i=0; i<this.handleEvents.length;i++){
			this.handleEvents[i](this.rootElement, {X: this.lasso["Canvas.Left"], Y: this.lasso["Canvas.Top"], Width: this.lasso["Width"], Height: this.lasso["Height"]})
        }
	},
    getOffsetX: function(current){
		var offset = 0;
		while(current != null){
			offset+=current["Canvas.Left"];
			current = current.getParent();
		}
		return offset;
    },
    getOffsetY: function(current){
		var offset = 0;
		while(current != null){
			offset+=current["Canvas.Top"];
			current = current.getParent();
		}
		return offset;
    }
}

