﻿///////////////////////////////////////////////////////////////////////////////////
// Scroll bar for Nav Window
////////////////////////////////////////////////////////////////////////////////////

contentmaster.scrollBarNav = function(parentWindow, areaToScroll, contentHeight, shapeHeight, scrollSizeRef, topY, windowNum, scrollNum, areaToScroll2, isWindowType)
{
    this.control = document.getElementById("silverlightControl1");
    this.parentWindow = parentWindow;
    this.areaToScroll = areaToScroll;
    this.areaToScroll2 = areaToScroll2
    this.isWindowType = isWindowType;

    this.contentHeight = contentHeight;
    this.shapeHeight = shapeHeight;
    this.scrollSizeRef = scrollSizeRef;
    this.topY = topY;
    this.windowNum = windowNum;
    this.scrollNum = scrollNum;

    
    this.scrollAmount = 25;
    this.isScrolling = false;
    
    
	var xaml = "";
	xaml += "<Canvas Canvas.Top='0' Canvas.Left='0' Name='ScrollBar_WindowID_ScrollID'>";
	xaml += "  <Canvas Width='10' Height='25'>";
	xaml += "    <Rectangle Width='10' Height='25' Fill='LightGray' Cursor='Hand' RadiusX='4' RadiusY='4' />";
	xaml += "  </Canvas>";
	xaml += "  <Canvas Canvas.Top='25' Width='10' Height='300'>";
	xaml += "    <Rectangle Width='10' Height='300' Fill='GhostWhite' RadiusX='4' RadiusY='4' />";
	xaml += "    <Canvas Width='10' Height='50'>";
	xaml += "      <Rectangle Width='10' Height='50' Fill='Gray' Cursor='Hand' RadiusX='4' RadiusY='4' />";
	xaml += "    </Canvas>";
	xaml += "  </Canvas>";
	xaml += "  <Canvas Canvas.Top='325' Width='10' Height='25'>";
	xaml += "    <Rectangle Width='10' Height='25' Fill='LightGray' Cursor='Hand' RadiusX='4' RadiusY='4' />";
	xaml += "  </Canvas>";
	xaml += "</Canvas>";
 
	xaml = xaml.replace(/WindowID/, this.windowNum); 
	xaml = xaml.replace(/ScrollId/, this.scrollNum);
    this.scrollBar = this.control.content.createFromXaml(xaml);
    this.parentWindow.children.add(this.scrollBar);


    this.upButton = this.scrollBar.children.getItem(0);
    this.bar = this.scrollBar.children.getItem(1);
    this.downButton = this.scrollBar.children.getItem(2);

    this.node = this.bar.children.getItem(1);
    
    if (this.isWindowType)
    {
		this.upButton.Width = 15;
		this.upButton.children.getItem(0).Width = this.upButton.Width;
		
		this.downButton.Width = 15;
		this.downButton.children.getItem(0).Width = this.downButton.Width;
		
		this.bar.Width = 15;
		this.bar.children.getItem(0).Width = this.bar.Width;
		this.bar.children.getItem(1).children.getItem(0).Width = this.bar.Width;
    }
    
    this.upButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.UpButton_MouseDown));
    this.downButton.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.DownButton_MouseDown));
    this.node.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.Node_MouseDown));
    this.node.addEventListener("MouseMove", Silverlight.createDelegate(this, this.Node_MouseMove));
    this.node.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.Node_MouseUp));
    
    this.configureScrollBar(this.contentHeight, this.shapeHeight, true);    
}



contentmaster.scrollBarNav.prototype.configureScrollBar = function(contentHeight, shapeHeight, initial)
{
    this.contentHeight = contentHeight;
    this.shapeHeight = shapeHeight;
    
    
    var xMod = this.isWindowType ? -7 : 0;
    var yMod = this.isWindowType ? 40 : 47;
    
    this.scrollBar["Canvas.Top"] = this.scrollSizeRef ? this.scrollSizeRef["Canvas.Top"] + yMod : 0;
    this.scrollBar["Canvas.Left"] = this.scrollSizeRef ? this.scrollSizeRef.Width + xMod : 260;
    
    this.upButton["Canvas.Top"] = 0;
    
    this.bar["Canvas.Top"] = this.upButton["Canvas.Top"] + this.upButton.Height;
    this.bar.Height = this.scrollSizeRef.Height - 104;
    this.bar.children.getItem(0).Height = this.bar.Height;
    
    this.downButton["Canvas.Top"] = this.bar["Canvas.Top"] + this.bar.Height;
    
    
    this.scrollScale = (this.bar.Height) / contentHeight;
	if (this.scrollScale < 0 || this.scrollScale > 1) this.scrollScale = 1;

    this.node["Canvas.Top"] = (-this.areaToScroll["Canvas.Top"] * this.scrollScale);
	this.node.Height = (this.scrollScale * (this.bar.Height));
	this.node.Children.getItem(0).Height = this.node.Height;
    
}


contentmaster.scrollBarNav.prototype.removeScrollBar = function()
{
	try
	{
		var scrollR = this.control.content.findName("ScrollBar_" + this.windowNum + "_" + this.scrollNum);
		this.parentWindow.children.remove(scrollR);
	}
	catch(e)
	{
		this.parentWindow.children.removeAt(this.parentWindow.children.count-1);
	}
}



contentmaster.scrollBarNav.prototype.UpButton_MouseDown = function(sender)
{
    var newY = this.node["Canvas.Top"] - this.scrollAmount * this.scrollScale;
    this.DoScroll(newY);
}

contentmaster.scrollBarNav.prototype.DownButton_MouseDown = function(sender)
{
    var newY = this.node["Canvas.Top"] + this.scrollAmount * this.scrollScale;
    this.DoScroll(newY);
}



contentmaster.scrollBarNav.prototype.Node_MouseDown = function(sender, mouseEventArgs)
{
    this.mouseNodeOffset = mouseEventArgs.getPosition(sender).Y;
    this.isScrolling = true;
    sender.captureMouse();
}

contentmaster.scrollBarNav.prototype.Node_MouseUp = function(sender)
{
    this.isScrolling = false;
    sender.releaseMouseCapture();
}

contentmaster.scrollBarNav.prototype.Node_MouseMove = function(sender, mouseEventArgs)
{
    if (this.isScrolling == true)
    {
        var y = mouseEventArgs.getPosition(this.bar).Y;
		var newY = y - this.mouseNodeOffset;
        
		this.DoScroll(newY);
    }
}

contentmaster.scrollBarNav.prototype.DoScroll =  function (newY)
{
	var barToUse = this.bar;
	var node = this.node;

	if (newY < 0) newY = 0;
	if (newY + node.Children.getItem(0).Height > barToUse.Height) newY = barToUse.Height - node.Children.getItem(0).Height;
	  
	node["Canvas.Top"] = newY;
	this.areaToScroll["Canvas.Top"] = -newY * (1 / this.scrollScale);
	
	if (this.areaToScroll2 != null)
	{
		this.areaToScroll2["Canvas.Top"] = this.areaToScroll["Canvas.Top"];
	}
}

contentmaster.scrollBarNav.prototype.DoContentScroll =  function (ContentY)
{

	var newY = (ContentY / (1 / this.scrollScale));
	this.DoScroll(newY);
}



