﻿//Reusable slider class.
//Build for all canvas manager objects
//Attaches to canvas manager holder canvas in xaml

function slider(control, UP, DOWN, Handle, Track, Target, Holder, Top, Window_Height, Content_Height, Tab)
{
    //x:Names
    this.control = control;
    this.up = this.control.content.findname(UP);
    this.down = this.control.content.findname(DOWN);
    this.handle = this.control.content.findname(Handle);
    this.track = this.control.content.findname(Track);
    this.target = this.control.content.findname(Target);
    this.slider = this.control.content.findname(Holder);
    this.window = Window_Height;
    this.content = Content_Height;
    this.offset = Top;
    this.tab = Tab;
    
    //Curser
    this.up.cursor = "Hand";
    this.down.cursor = "Hand";
    this.handle.cursor = "Hand";
    this.track.cursor = "Hand";
    
    //variables
    this.hPress = false;
    this.diff = 0;
    this.targetHeight;
    
    //EventHandlers
    this.up.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.pressUp));
    this.down.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.pressDown));
    this.handle.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.pressHandle));
	this.handle.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.releaseHandle));
	this.handle.addEventListener("MouseMove", Silverlight.createDelegate(this, this.moveHandle));
	this.track.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.pressTrack));
	this.track.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.releaseTrack));
	this.track.addEventListener("MouseMove", Silverlight.createDelegate(this, this.moveHandle));
}

slider.prototype = {

    pressUp: function(sender, args)//Up button
    {
        var myHeight = this.content-this.window;
        if(this.target["Canvas.Top"] <= -this.tab)
        {
            this.target["Canvas.Top"] += this.tab;
            this.handle["Canvas.Top"] = (this.target["Canvas.Top"]/(-myHeight))*(this.track.Height-this.handle.Height-2)+ this.track["Canvas.Top"]+1;
        }
        else{
            this.target["Canvas.Top"] = 0;
            this.handle["Canvas.Top"] = this.track["Canvas.Top"]+1;
        }
        
        
    },
    pressDown: function(sender, args)//Down Button
    {
        var myHeight = this.content-this.window;
        if(this.target["Canvas.Top"] > (-1*myHeight)+this.tab)
        {
            this.target["Canvas.Top"] -= this.tab;
            this.handle["Canvas.Top"] = (this.target["Canvas.Top"]/(-myHeight))*(this.track.Height-this.handle.Height-2)+ this.track["Canvas.Top"]+1;
        }
        else
        {
            this.target["Canvas.Top"] = (-1*myHeight);
            this.handle["Canvas.Top"] = this.track["Canvas.Top"]+this.track.Height-this.handle.Height-1;
        }
    },
    pressHandle: function(sender, args)//Attaches handle to mouse movements
    {
        sender.CaptureMouse()
        this.hPress = true;
        this.diff = args.getPosition(null).y - (this.handle["Canvas.Top"]+this.slider["Canvas.Top"]);
    },
    releaseHandle: function(sender, args)//Releases handle from mouse control
    {
        sender.ReleaseMouseCapture();
        this.hPress = false;
    },
    moveHandle: function(sender, args)//Called onMousemove
    {
        if(this.hPress)
        {
            var place = args.getPosition(null).y-this.diff-this.slider["Canvas.Top"];
            if(place < this.track["Canvas.Top"]+1)
            {
                this.handle["Canvas.Top"] = this.track["Canvas.Top"]+1;
                this.moveTarget(0);
            }
            else if(place > this.track["Canvas.Top"]+this.track.Height-this.handle.Height-1)
            {
                this.handle["Canvas.Top"] = this.track["Canvas.Top"]+this.track.Height-this.handle.Height-1;
                this.moveTarget(1);
            }
            else
            {
                this.handle["Canvas.Top"] = place;
                var Percent = (this.handle["Canvas.Top"]-this.track["Canvas.Top"])/(this.track.Height-this.handle.Height)
                this.moveTarget(Percent);
            }
        }
    },
    
    pressTrack: function(sender, args)//updates handle position and starts dragging of handle
    {
        sender.CaptureMouse()
        this.hPress = true;
        this.diff = (this.handle.Height/2); 
        var place = args.getPosition(null).y-this.diff-this.slider["Canvas.Top"];
            if(place < this.track["Canvas.Top"]+1)
            {
                this.handle["Canvas.Top"] = this.track["Canvas.Top"]+1;
                this.moveTarget(0);
            }
            else if(place > this.track["Canvas.Top"]+this.track.Height-this.handle.Height-1)
            {
                this.handle["Canvas.Top"] = this.track["Canvas.Top"]+this.track.Height-this.handle.Height-1;
                this.moveTarget(1);
            }
            else
            {
                this.handle["Canvas.Top"] = place;
                var Percent = (this.handle["Canvas.Top"]-this.track["Canvas.Top"])/(this.track.Height-this.handle.Height);
                this.moveTarget(Percent);
            }
    },
    
    releaseTrack: function(sender, args)//Stops dragging of handle when pressing the track first
    {
        sender.ReleaseMouseCapture();
        this.hPress = false;
    },
    
    moveTarget: function(Percent)//Moves content.  Called from an on mouse mouse
    {
        var dist = this.content-this.window;
        this.target["Canvas.Top"] = -1*dist*Percent;
    }, 
    
    windowHeight: function(key)//Changes the window height and updates slider handle position
    {
        this.window = key;
        if(this.checkVisible())
        {
        //alert(this.content)
        //alert(this.window)
            //alert(Math.abs(this.target["Canvas.Top"]/(this.content-this.window)))
            var Percent = Math.abs(this.target["Canvas.Top"]/(this.content-this.window));
            this.handle["Canvas.Top"] = (this.track.Height-this.handle.height)*Percent+this.track["Canvas.Top"];
        }
    },
    
    contentHeight: function(key)//Changes the contentHeight
    {
        this.content = key;
        this.checkVisible()
    },
    
    checkVisible: function()//Checks to see if slider should be visible based on content height and window height
    {
        if(this.content > this.window)
        {
            this.slider.Visibility = "Visible";
            return true;
        }
        else
        {
            this.slider.Visibility = "Collapsed";
            this.reset();
            return false;
        }
    },
    
    reset: function()//Resets slider handle and contentHolder to starting position
    {
        this.target["Canvas.Top"] = 0;
        this.handle["Canvas.Top"] = this.track["Canvas.Top"]+1;
    }
}
