﻿//===============================================================================
// ID		  : $Id: Codex.js 131 2008-03-31 13:56:30Z stefan.schult $
// Author     : $Author: stefan.schult $
// Description: JSClasses for Manipulate Scan of Codex
//===============================================================================

ScaleScan = function(ScaleControl, ScaleTransform, ScaleAnimation){
    this.toString = function(){
        return "ScaleScan";
    };
    this.ScaleSlider			= ScaleControl.Children.getItem(0);
    this.ScaleLower				= ScaleControl.Children.getItem(1);
    this.ScaleHigher			= ScaleControl.Children.getItem(2);
    this.ScaleTransform			= ScaleTransform;
    this.ScaleAnimation			= ScaleAnimation;
    this.ScaleAnimationX		= ScaleAnimation.findName(ScaleAnimation.name + "X");
    this.ScaleAnimationY		= ScaleAnimation.findName(ScaleAnimation.name + "Y");
    this.ScaleAnimationY["Storyboard.TargetName"] = this.ScaleAnimationX["Storyboard.TargetName"] = ScaleTransform.Name;
    this.ScaleAnimationSlider	= ScaleAnimation.findName(ScaleAnimation.name + "Slider");
    this.ScaleAnimationSlider["Storyboard.TargetName"]	= this.ScaleSlider.Children.getItem(0).Name;
    this.ButtonScaleLower = new ScaleButton(this.ScaleLower);
    this.ButtonScaleHigher = new ScaleButton(this.ScaleHigher);
    this.factor = 6;
    this.step = 8;
    this.hasAnimation = false;
    this.scaleWidth = this.ScaleSlider.Children.getItem(1).Width;
	this.handleCookie = {
	    SliderHandleMouseDown:	this.ScaleSlider.Children.getItem(0).addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.SliderHandleMouseDown)),
	    SliderHandleMouseUp:	this.ScaleSlider.Children.getItem(0).addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.SliderHandleMouseUp)),
	    SliderHandleMouseMove:	this.ScaleSlider.Children.getItem(0).addEventListener("MouseMove", Silverlight.createDelegate(this, this.SliderHandleMouseMove)),
	    SliderBarHandleMouseDown:	this.ScaleSlider.Children.getItem(1).addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.SliderBarHandleMouseDown)),
	    LowerHandleMouseDown:	this.ScaleLower.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.LowerHandleMouseDown)),
	    HigherHandleMouseDown:	this.ScaleHigher.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.HigherHandleMouseDown)),
	    handleAnimationCompleted:this.ScaleAnimation.addEventListener("Completed", Silverlight.createDelegate(this, this.handleAnimationCompleted))
	}
}
ScaleScan.prototype = {
    SliderHandleMouseMove: function(sender, eventArgs) 
    {
        if(this.mouseDown != null){
			var delta = eventArgs.getPosition(null).x - this.mouseDown.X - this.getOffsetX(sender.getParent());
			if(delta >= 0 && delta <=sender.getParent().width-10){
				sender["Canvas.Left"] = delta;
				var scale = delta/this.scaleWidth * this.factor + 1;
				this._setScale(scale);
			}
        }
    },
	
    _setScale: function(scale) 
    {
		this.ScaleTransform.ScaleX = scale;
		this.ScaleTransform.ScaleY = scale;
    },
	
    resetScale: function() 
    {
		this._setScale(1);
		this.ScaleSlider.Children.getItem(0)["Canvas.Left"] = 0;
    },
	
    rectangleScale: function(stepScale) 
    {
		var scaleWidth = this.scaleWidth-6;
		var stepScale = (this.factor+1 < this.ScaleTransform.ScaleX + stepScale) ? this.factor+1-this.ScaleTransform.ScaleX : stepScale-1;
		this._animateScale(1, (scaleWidth/this.factor*stepScale), stepScale);
    },
	
    _animateScale: function(scaleDirection, stepSlider, stepScale) 
    {
		var scaleWidth = this.scaleWidth-6;
		var stepSlider = stepSlider || (scaleWidth)/this.step*scaleDirection;
		var stepScale = stepScale || this.step/scaleWidth*this.factor*scaleDirection;
		if(	(this.ScaleSlider.Children.getItem(0)["Canvas.Left"]+stepSlider <= scaleWidth) &&
			(this.ScaleSlider.Children.getItem(0)["Canvas.Left"]+stepSlider >= 0) && 
			!this.hasAnimation ){
			this._startAnimation(stepSlider, stepScale);
			return;
		} else if(!this.hasAnimation){
			var normalStep = (scaleWidth)/this.step*scaleDirection;
			if(this.ScaleSlider.Children.getItem(0)["Canvas.Left"]+stepSlider > scaleWidth){
				stepSlider = scaleWidth - this.ScaleSlider.Children.getItem(0)["Canvas.Left"];
			}
			if(	(this.ScaleSlider.Children.getItem(0)["Canvas.Left"]+stepSlider < 0) ){
				stepSlider = this.ScaleSlider.Children.getItem(0)["Canvas.Left"]*-1;
			}
 			stepScale = this.step/scaleWidth*this.factor*scaleDirection*stepSlider/normalStep;
 			this._startAnimation(stepSlider, stepScale);
 		}
 	},
	
    _startAnimation: function(stepSlider, stepScale) 
    {
		this.ScaleAnimationX.By = this.ScaleAnimationY.By = stepScale;
		this.ScaleAnimationSlider.By = stepSlider;
		this.hasAnimation = true;
		this.ScaleAnimation.Begin();
 	},
	
    SliderBarHandleMouseDown: function(sender, eventArgs) 
    {
		var scaleWidth = this.scaleWidth-6;
		var stepSlider	= eventArgs.getPosition(null).x - this.getOffsetX(sender) - this.ScaleSlider.Children.getItem(0)["Canvas.Left"];
		var stepScale	= stepSlider/this.scaleWidth * this.factor;
 		this._startAnimation(stepSlider, stepScale);
 	},
	
    LowerHandleMouseDown: function(scale) 
    {
		this._animateScale(-1);
    },
	
    HigherHandleMouseDown: function(sender, eventArgs) 
    {
		this._animateScale(1);
    },
	
    handleAnimationCompleted: function(sender, eventArgs) 
    {
		this.hasAnimation = false;
    },
	
    SliderHandleMouseDown: function(sender, eventArgs) 
    {
		this.mouseDown = {
			X:eventArgs.getPosition(null).x - this.getOffsetX(sender),
			Y:eventArgs.getPosition(null).y - this.getOffsetY(sender)
		};
		sender.CaptureMouse();
    },
	
    SliderHandleMouseUp: function(sender, eventArgs) 
    {
		this.mouseDown = null;
		sender.ReleaseMouseCapture();  
    },
    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;
    }
}


ScanDragging = function(rootElement){
    this.toString = function(){
        return "ScanDragging";
    };
    this.rootElement = rootElement;
    this.draggingEnabled = false;
	this.setOuterBarrier();
	this.setPosFactor();
	this.handleCookie = {
	    handleMouseDown : rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown)),
	    handleMouseUp : rootElement.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp)),
	    handleMouseEnter : rootElement.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter)),
	    handleMouseMove : rootElement.addEventListener("MouseMove", Silverlight.createDelegate(this, this.handleMouseMove))
	}
}
ScanDragging.prototype = {
	handleMouseDown: function(sender, eventArgs) 
	{
		if(this.draggingEnabled){
			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
			}
			this.currPosition= {
				Left:sender["Canvas.Left"],Top:sender["Canvas.Top"]
			}
			this.setOuterBarrier();
			sender.Cursor = "Hand";
			sender.captureMouse();
	    }
    },
	handleMouseEnter: function(sender, mouseEventArgs) 
	{
    },
	handleMouseMove: function(sender, mouseEventArgs) 
	{
        if (this.isMouseDown == true)
        {
            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;
            var delta = {
				Left: this.currPosition.Left + this.mouseDelta.X/sender.findName(sender.getParent().Name + "ScaleTransform").ScaleX,
				Top: this.currPosition.Top + this.mouseDelta.Y/sender.findName(sender.getParent().Name + "ScaleTransform").ScaleY
            };
			sender["Canvas.Left"] = (delta.Left>this.minDelta.Left && delta.Left<this.maxDelta.Left) ? delta.Left : sender["Canvas.Left"];
			sender["Canvas.Top"] = (delta.Top>this.minDelta.Top && delta.Top<this.maxDelta.Top) ? delta.Top : sender["Canvas.Top"];
//			sender.findName("ScanCanvas")
        }
	},
	handleMouseUp: function(sender, eventArgs) 
	{
        if (this.isMouseDown == true)
        {
			this.isMouseDown = false;
			sender.Cursor = "Arrow";
			this.setPosFactor();
			sender.releaseMouseCapture();
	    }
    },
	setDraggingEnabled: function(isEnabled) 
	{
		this.draggingEnabled = isEnabled;
	},
	getDraggingEnabled: function() 
	{
		return this.draggingEnabled;
	},
	
    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;
    },
    setOuterBarrier: function(){
		this.minDelta = {
			Left: -(this.rootElement.getParent().getParent().Width/2),
			Top: -(this.rootElement.getParent().getParent().Height/2)
		};
		this.maxDelta = {
			Left: this.rootElement.getParent().getParent().Width/2,
			Top: this.rootElement.getParent().getParent().Height/2
		}
    },
    setPosFactor: function(){
		this.PosFactor = {
			Left : (this.rootElement["Canvas.Left"]/this.rootElement.getParent().getParent().Width),
			Top : (this.rootElement["Canvas.Top"]/this.rootElement.getParent().getParent().Height)
		};
//		this.rootElement.findName("Debug").Text = "Left: " + (this.rootElement["Canvas.Left"]/this.rootElement.getParent().getParent().Width);
    },
	updateLayout: function() 
	{
		this.rootElement["Canvas.Left"]= this.rootElement.getParent().getParent().Width * this.PosFactor.Left;
		this.rootElement["Canvas.Top"]= this.rootElement.getParent().getParent().Height * this.PosFactor.Top;
    }
}

