﻿
var _gFullScreen = false;
var _gPlay = false;
var _gNumberPage = 0;
var _gThisNav = null;
var _gshowMenu = true;
var _gOpenPlayer = false;
var playList = new Array();
var _gNumList = 0;
InkManager = function(plugIn, navigationManager) {
    this.plugIn = plugIn;
    // need the navigation manager to obtain the number of the current ink canvas to draw on.
    this.navigationManager = navigationManager;
    _gThisNav = this.navigationManager;
    this.mouseCaptureCanvas = plugIn.content.findname("mouseCaptureCanvas");
    
    this.inkingMode = false;
    this.newStroke = null;
    this.newStroke2 = null;
    this.inkThickness = 4;
    this.inkColor = "white";
    this.inkColorContrast = "black";
    
    // Register eventhandlers
	//target.addEventListener("mouseEnter", Silverlight.createDelegate(this, this.handleMouseEnter));
	this.mouseCaptureCanvas.addEventListener("mouseLeave", Silverlight.createDelegate(this, this.handleMouseLeave));
    this.mouseCaptureCanvas.addEventListener("mouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
    this.mouseCaptureCanvas.addEventListener("mouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
 	this.mouseCaptureCanvas.addEventListener("mouseMove", Silverlight.createDelegate(this, this.handleMouseMove));    
}
//clickPage
InkManager.prototype.clickPage = function()
{
    doSend(5);
}
InkManager.prototype.closePage = function()
{
    
}
//soundPrevDown
function setOpacity(sender,name)
{
	
	sender.findName("backDown")["opacity"] = 0;
	sender.findName("playDown")["opacity"] = 0;
	sender.findName("nextDown")["opacity"] = 0;
	sender.findName("SndUpDown")["opacity"] = 0;
	sender.findName("SndDownDown")["opacity"] = 0;
	sender.findName("muteDown")["opacity"] = 0;
	
	if(name!='')
		sender.findName(name)["opacity"] = 1;

}
InkManager.prototype.toggleInkMode = function()
{
    this.inkingMode = !this.inkingMode;
    if (this.inkingMode)
    {
        this.mouseCaptureCanvas.isHitTestVisible = true;
    }
    else
    {
        this.mouseCaptureCanvas.isHitTestVisible = false;
    }
}//
var _gopenMenu = true;
function openRule(s,e)
{
	//alert(this.pageBrowserButtonCurrentState);	
	
	var normal = ii.target.findName(this.pageBrowserButtonCurrentState + "_normal");
    ii.isMouseOver = true;
	
	ii.isMouseDown = true;
    ii.target.captureMouse();
	
	
	if (ii.isMouseOver == true)
    {
      if (ii.pageBrowserButtonCurrentState == "unchecked")
      {
        ii.pageBrowserButtonCurrentState = "checked";
        
        if (ii.checkedHandler) {
            ii.checkedHandler(s, e);
        }
      }
      else
      {
        ii.pageBrowserButtonCurrentState = "unchecked";
        _gSender = sender;
        _geventArgs = eventArgs;
        if (ii.uncheckedHandler) 
		{
            ii.uncheckedHandler(s,e);
        }
      }
    }
    else
    {
    }
          
    ii.target.findName("pageBrowserWindowRect")["Rect"] = "0,-300, 550, 500";
    ii.target.findName("pageBrowserWindow")["canvas.left"] = 220;      
   
    
    ii.isMouseDown = false;
    ii.target.releaseMouseCapture();
	
}
function openMenu()
{
    if( ii.pageBrowserButtonCurrentState == "checked" && From==false)
    {
    	ii.isMouseDown = true;
    	ii.target.captureMouse();    
    	ii.isMouseOver = true;
    	if (ii.isMouseOver == true)
    	{
      		ii.pageBrowserButtonCurrentState = "unchecked";
        	if (ii.uncheckedHandler)
			{
          		ii.uncheckedHandler(_gSender, _geventArgs);
      		}
    	}
    	else
    	{
   		}
    	ii.isMouseDown = false;
    	ii.target.releaseMouseCapture();
    }
    From = false;
    var player = _gThisPage.plugIn.content.findName("PagePlayer");
    
    if( !_gopenMenu )
    {
       _gshowMenu = true;
        
        if(_gOpenPlayer)
        {
        }
        _gopenMenu = true;
    }
    else       
    {        
        _gopenMenu = false;
        _gshowMenu = false;
    }
    
}
var fullPress = false;
function fullScreen()
{
    fullPress = true;
    _gThisPage.plugIn.content.fullScreen = !_gThisPage.plugIn.content.fullScreen;    
    _gFullScreen = _gThisPage.plugIn.content.fullScreen;
}

InkManager.prototype.fullScreen = function()
{
    this.plugIn.content.fullScreen = !this.plugIn.content.fullScreen;    
    _gFullScreen = this.plugIn.content.fullScreen;    
}

InkManager.prototype.zoomScreen = function()
{
}

function  nextPage()
{
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }
  

    
    if(_gNumberPage==_gMaxNumPage)
        return;
        
    //_gNumberPage++;
    if( !_gIsZoom )
    {
        _gThisNav.oddPageNext("page" + _gThisNav.nextOddPage );
    }
    else
    {
        if( _gNumberPage/2-Math.floor(_gNumberPage/2)>0 )
        {
            _gThisNav.oddPageGo( _gNumberPage );
        }
        
        if(  _gNumberPage<10  )
          gotoZoomPage('0'+_gNumberPage);
        else
          gotoZoomPage(_gNumberPage);
        
    }    
}
function prevPage()
{
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }
   
  if(_gThisNav.nextOddPage<=3)
    return;
    
  _gNumberPage--;
 
  
  if( !_gIsZoom )
  {
      if( _gThisNav.nextOddPage!=1 )
      {
        if((_gThisNav.nextOddPage-2)<10)
        {
            _gThisNav.oddPagePrev("page00" + (_gThisNav.nextOddPage-2) );
        }
        else
        {
            _gThisNav.oddPagePrev("page0" + (_gThisNav.nextOddPage-2) );
        }
        
      }
   }
   else
   {
        if( _gNumberPage/2-Math.floor(_gNumberPage/2)==0 )
        {
            if((_gThisNav.nextOddPage-2)<10)
            {
                _gThisNav.oddPageGo( _gNumberPage );
            }
            else
            {
                _gThisNav.oddPageGo( _gNumberPage );
            }
        }
        
        if(  _gNumberPage<10  )
          gotoZoomPage('0'+_gNumberPage);
        else
          gotoZoomPage(_gNumberPage);
   }
   _gbeginAn = true;
}
function openFirst()
{
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }
  
    _gThisNav.oddPageGo( 2 );
    _gNumberPage = 1;
    _gbeginAn = true;
}
function openLast()
{
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }
  
    if(_gbeginAn)
        return;
    _gThisNav.oddPageGo( _gMaxNumPage+1 );
    _gNumberPage = _gMaxNumPage+1;
    _gbeginAn = true;
    _gThisNav.setVideo(_gNumberPage);
}

InkManager.prototype.nextPage = function()
{
    
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }    

    if(_gNumberPage==_gMaxNumPage)
        return;
        
    _gNumberPage++;
    if( !_gIsZoom )
    {
        this.navigationManager.oddPageNext("page" + this.navigationManager.nextOddPage );
    }
    else
    {
        if( _gNumberPage/2-Math.floor(_gNumberPage/2)>0 )
        {
            this.navigationManager.oddPageGo( _gNumberPage );
        }
        
        if(  _gNumberPage<10  )
          gotoZoomPage('0'+_gNumberPage);
        else
          gotoZoomPage(_gNumberPage);
        
    }    
}
InkManager.prototype.goPage = function()
{
    var textInput = document.getElementById("textInput");
    this.navigationManager.oddPageGo( textInput.value );
}
function goPage()
{
    var textInput = document.getElementById("textInput");
    if(textInput.value<2)
    {
        _gNumberPage = 2;
    }
    else
    {
        if (textInput.value > 3)
        {
          _gNumberPage = parseInt(textInput.value)+2;
        }
        else
        {
          _gNumberPage = textInput.value;
        }
    }
    
    
    _gNumberPage++;
    _gThisNav.oddPageGo1( _gNumberPage );
    if( _gNumberPage > 43 )
    {
        _gThisNav.setVideo(43);
    }
}
InkManager.prototype.prevPage = function()
{
  if (_gVideoOpen)
  {
     _gThisPage.plugIn.content.findName("movie").stop();
     _gVideoOpen = false;
  }
  if(_gNumberPage==0)
    return;
    
  _gNumberPage--;
  
  if( !_gIsZoom )
  {
      if( this.navigationManager.nextOddPage!=1 )
      {
        if((this.navigationManager.nextOddPage-2)<10)
        {
            this.navigationManager.oddPagePrev("page00" + (this.navigationManager.nextOddPage-2) );
        }
        else
        {
            this.navigationManager.oddPagePrev("page0" + (this.navigationManager.nextOddPage-2) );
        }
        
      }
   }
   else
   {
        if( _gNumberPage/2-Math.floor(_gNumberPage/2)==0 )
        {
            if((this.navigationManager.nextOddPage-2)<10)
            {
                this.navigationManager.oddPageGo( _gNumberPage );
            }
            else
            {
                this.navigationManager.oddPageGo( _gNumberPage );
            }
        }
   }
}
InkManager.prototype.handleMouseLeave = function(sender, args)
{
    this.newStroke = null;   // Stop inking the current stroke if the mouse/stylus leaves the control area
    this.newStroke2 = null;
}

InkManager.prototype.handleMouseDown = function(sender, args)
{
  if (this.inkingMode)
  {
    sender.CaptureMouse();
    var inkPresenter = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-2) + "ip");
     
    if (inkPresenter != null)
    { 
      this.newStroke = this.plugIn.content.createFromXAML("<Stroke/>");
      inkPresenter.Strokes.Add(this.newStroke);
      this.newStroke.DrawingAttributes.Width = this.inkThickness;
      this.newStroke.DrawingAttributes.Height = this.inkThickness;
      this.newStroke.DrawingAttributes.Color = this.inkColor;
      this.newStroke.DrawingAttributes.OutlineColor = this.inkColorContrast;
      this.newStroke.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter));
    }

    // Inking Page 2 (copy to other page so you can ink crossing pages)
    var inkPresenter2 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-1) + "ip");
    if (inkPresenter2 != null)
    {
      this.newStroke2 = this.plugIn.content.createFromXAML("<Stroke/>");
      inkPresenter2.Strokes.Add(this.newStroke2);
      this.newStroke2.DrawingAttributes.Width = this.inkThickness;
      this.newStroke2.DrawingAttributes.Height = this.inkThickness;
      this.newStroke2.DrawingAttributes.Color = this.inkColor;
      this.newStroke2.DrawingAttributes.OutlineColor = this.inkColorContrast;
      this.newStroke2.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter2));
    }
  }
}

InkManager.prototype.handleMouseUp = function(sender, args)
{
    this.newStroke = null;
    this.newStroke2 = null;
}

InkManager.prototype.handleMouseMove = function(sender, args)
{
  if (this.inkingMode)
  {
    if (this.newStroke != null)
    {
      var inkPresenter = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-2) + "ip");
      this.newStroke.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter));
    }
     
    // Inking Page 2
    if (this.newStroke2 != null)
    {
      var inkPresenter2 = this.plugIn.content.findName("page" + getTwoDigitInt(this.navigationManager.nextOddPage-1) + "ip");
      if (inkPresenter2 != null)
      {
        this.newStroke2.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter2));
      }
    }
  }
}
