﻿if (!window.program) { window.program = {}; }

program.ScrollableTextBlock = function() {
    this.minimum = 0;
    this.maximum = 100;  
    this.smallStepSize = 5; 
    this.clipHeight= 100;
    this.clipWidth =435;
    
}

program.ScrollableTextBlock.prototype =
{
	initialize: function(xamlString, destination) 
	{
	    if (destination==null) {
	        return;
	    }
	    
		this.control = destination.getHost();
		
		if (xamlString == "") {
		    this.xaml = destination;
		} else {
		    this.xaml = this.control.content.CreateFromXaml(xamlString, true);
		    destination.children.add(this.xaml);
		}
		
		//Elements
		this.textTextBlock = this.xaml.findName("Text");
		this.arrowUpButton = this.xaml.findName("ArrowUp");
		this.arrowDownButton = this.xaml.findName("ArrowDown");
		
		this.thumb = this.xaml.findName("Thumb");
		this.textContainer = this.xaml.findName("TextContainer");
		this.contentPlaceHolderClip = this.xaml.findName("ContentPlaceHolderClip");		
		this.track = this.xaml.findName("Track");		
		this.scrollBar = this.xaml.findName("ScrollBar");
		
		//Initial Values
		this.scrollBarValue = 0;
		this.clip();
		
		//Animations
		
		//Event Handlers
		this.xaml.addEventListener("MouseMove",Silverlight.createDelegate(this, this.handleMouseMove));
		this.arrowUpButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleArrowUpMouseDown));
		this.arrowUpButton.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleArrowUpMouseUp));
		this.arrowDownButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleArrowDownMouseDown));
		this.arrowDownButton.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleArrowDownMouseUp));
		this.track.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleTrackMouseDown));
		this.thumb.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleThumbMouseDown));
		this.thumb.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleThumbMouseUp));
;
    },
    handleTrackMouseDown: function(sender, eventArgs) {
		y=eventArgs.getPosition(sender).y    
        this.scrollBarValue = (y/this.track.Height*100);
        this.updateScrollBar();        

    },
	set_Text: function(string) 
	{
		this.scrollBarValue = 0;
		this.textTextBlock.text = string;
		this.updateScrollBar();
		//this.smallStepSize =  this.clipHeight / this.textTextBlock.ActualHeight * 100 / 10;
	}, 	
    set_Height: function (height) {
        this.clipHeight = height;
        this.arrowDownButton["Canvas.Top"] = height - this.arrowDownButton.Height;
        var trackOffset = 2;
        this.track["Canvas.Top"] = this.arrowDownButton.height + 2;
        this.track.Height = this.arrowDownButton["Canvas.Top"] - this.track["Canvas.Top"] -trackOffset;
        this.clip();
    },
    set_Width: function (width) {
        this.clipWidth = width - 12;  
        this.textTextBlock.Width = width - 12;      
        this.scrollBar["Canvas.Left"] = width -10;        
                
        this.clip();
    },    
    clip: function() {
        this.contentPlaceHolderClip.Rect = "0, 0, " + this.clipWidth + ", " + this.clipHeight;
    },
	location: function(newX, newY) 
	{
	    if (this.xaml==null) return;	    
		this.xaml["Canvas.Left"] = newX;
		this.xaml["Canvas.Top"] = newY;
	},    

    handleArrowUpMouseDown:function (sender,args) {    
        this.arrowUpButton.Opacity = 0.5;
        this.scrollByValue = -this.smallStepSize;
        this.scrollByInterval = setInterval(Silverlight.createDelegate(this, this.scrollBy),10);        
        sender.captureMouse();
    },
    handleArrowUpMouseUp: function(sender,args) {        
	    clearInterval(this.scrollByInterval);
	    sender.releaseMouseCapture();
	    this.arrowUpButton.Opacity = 1;
    },      
    handleArrowDownMouseDown:function (sender,args) {
       this.arrowDownButton.Opacity = 0.5;
       this.scrollByValue = this.smallStepSize;
       this.scrollByInterval = setInterval(Silverlight.createDelegate(this, this.scrollBy),10);       
       sender.captureMouse();
    },    
    handleArrowDownMouseUp: function(sender,args) {        
	    clearInterval(this.scrollByInterval);  
	    sender.releaseMouseCapture();
	    this.arrowDownButton.Opacity = 1;
    },    
    scrollBy:function () {
        this.scrollBarValue += this.scrollByValue;  
        //this.control.content.findName("overlayer_title").text =new Date().getSeconds().toString() + " " + this.scrollBarValue.toString() ;
        this.updateScrollBar();
    },    
    updateScrollBar: function() {
        if (this.scrollBarValue < this.minimum) {
            this.scrollBarValue = this.minimum;
        }
        if (this.scrollBarValue > this.maximum) {
            this.scrollBarValue = this.maximum;
        }        
	    
	    var perc = this.scrollBarValue / (this.maximum - this.minimum);
	    
	    var contentTop = -(this.textTextBlock.ActualHeight * perc);
	    this.textContainer["Canvas.Top"] = contentTop;
	    
	    var thumbTop = this.track["Canvas.Top"] + ( (this.track.Height-this.thumb.Height) * perc);
	    
	    this.thumb["Canvas.Top"] = thumbTop;	    
	    
    }, 
    resetScrollBar: function() {
        this.scrollBarValue = 0;
		//this.textTextBlock.text = string;
		this.updateScrollBar();    
    },
    handleThumbMouseDown: function (sender,args) {    	
	    this.thumbIsDown=true;
	    this.click_y=args.getPosition(sender).y;	
	    this.mouseCheckInt=setInterval(Silverlight.createDelegate(this, this.checkThumbMovement),10)	
	    sender.Opacity = 0.5;
	    sender.captureMouse()
    },
    handleThumbMouseUp: function(sender,args) {
	    this.thumbIsDown=false;
	    clearInterval(this.mouseCheckInt);
	    sender.releaseMouseCapture();
	    sender.Opacity = 1;
    },
    handleMouseMove: function(sender,args) {
        this.mouseY = args.getPosition(this.track).y;
    },
    checkThumbMovement: function (sender, args) {
	    if(this.thumbIsDown == true) {
            //this.control.content.findName("overlayer_title").text =this.click_y.toString() + ";" + this.mouseY.toString();	
            this.scrollBarValue = this.mouseY / this.track.height * 100;
            this.updateScrollBar();
	    }
	    else {
		    clearInterval(this.mouseCheckInt)
		    sender.releaseMouseCapture();
	    }
    }           
    

}

