if (!window.PictureDownloader)
	window.PictureDownloader = {};

PictureDownloader.Page = function() 
{
}

var glbPictures;
var glbsender;
var glbPictureIDX = 0;
var glbPictureIDY = 0;

var mouseDownPosition = 0;
var mouseDownValue = -1;
var thumbCenterX;
var thumbCenterY;
var thumbWidth;	
var totalFrames = 36;
var startFrame = 117;
var lastFrame = "image0";
var nextFrameCompleted = -1;
var scalingFactor;

PictureDownloader.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		glbsender = rootElement;		
				
   		// calculate global variables
   		thumbWidth = rootElement.findName("sliderThumbX").width;
   		thumbCenterX = thumbWidth / 2;
   		
   		thumbWidth = rootElement.findName("sliderThumbY").width;
   		thumbCenterY = thumbWidth / 2;

		// set the initial position for the slider
		var thumbX = rootElement.findName("sliderThumbX");
		thumbX["Canvas.Left"] = startFrame;
		newValueX = startFrame;
		
		var thumbY = rootElement.findName("sliderThumbY");
		thumbY["Canvas.Top"] = startFrame;
		newValueY = startFrame;

		var plugin = rootElement.getHost();
		var downloader = plugin.createObject("downloader");

		downloader.addEventListener("downloadProgressChanged", onDownloadProgressChanged);
		downloader.addEventListener("completed", onCompleted);

		downloader.open("GET", "Pictures.zip");
		downloader.send();

	}

}

function doScale(sender, newValueX, newValueY) 
{
	glbPictureIDX = parseInt(newValueX / scalingFactor);	
	
	if (glbPictureIDX > totalFrames) 
	{
	  glbPictureIDX = totalFrames;
	}
	
	glbPictureIDY = parseInt(newValueY / scalingFactor);	
	
	if (glbPictureIDY > totalFrames) 
	{
	  glbPictureIDY = totalFrames;
	}

	SHOWPICTURE();
}

// MOUSE MOVEMENT

function clickSelectedMouseDown(sender, mouseEventArgs) 
{	
	// mouse was clicked somewhere on the slider - move
	// the slider thumb appropriately.
	var coordinate = mouseEventArgs.getPosition(null);
	var coordinateX = coordinate.x;
	var coordinateY = coordinate.y;
	
    var sliderX = sender.findName("sliderCanvasX");
    coordinateX -= sliderX["Canvas.Left"];
    var sliderY = sender.findName("sliderCanvasY");
    coordinateY -= sliderY["Canvas.Top"];
    
	mouseDownValue = 1;
	
	slider_SetValue(sliderX, coordinateX - thumbCenterX, coordinateY - thumbCenterY);
}


function clickSelectedMouseMove(sender, mouseEventArgs) 
{
   	if (mouseDownValue != -1) 
	{
	  sender.captureMouse();
	  	var coordinate = mouseEventArgs.getPosition(null);
	    var coordinateX = coordinate.x;
	    var coordinateY = coordinate.y; 
	    
        var sliderX = sender.findName("sliderCanvasX");
	    coordinateX -= sliderX["Canvas.Left"];	
	    var sliderY = sender.findName("sliderCanvasY");
	    coordinateY -= sliderY["Canvas.Top"];	
	    
	    slider_SetValue(sliderX, coordinateX - thumbCenterX, coordinateY - thumbCenterY);
	}
}

function clickSelectedMouseUp(sender) 
{
    sender.releaseMouseCapture();
	mouseDownValue = -1;
}


// SLIDER

function slider_SetValue(sender, newValueX, newValueY) 
{    
	//Check if slider is at the maximum to the right
	if (newValueX > sender.findName("sliderX").width) 
	{
	  newValueX = sender.findName("sliderX").width;
    }
    
    //Check is slider is at the maximum to the left
    if (newValueX <= 0) 
	{
      newValueX = 0;
    }
    
    //Check if slider is at the maximum to the bottom
	if (newValueY > sender.findName("sliderY").height) 
	{
	  newValueY = sender.findName("sliderY").height;
    }
    
    //Check is slider is at the maximum to the top
    if (newValueY <= 0) 
	{
      newValueY = 0;
    }
		
	// calculate scaling values
	doScale(sender, newValueX, newValueY);
}




//SHOW PICTURE

function SHOWPICTURE()
{

	if(glbPictureIDX == totalFrames)
	{
	  glbPictureIDX = (totalFrames - 1);
	} 
	
	if(glbPictureIDY == totalFrames)
	{
	  glbPictureIDY = (totalFrames - 1);
	} 

	if(glbPictures != null)
	{
	  var DisplayPicture = glbsender.findName("DisplayPicture");
	  var strPicture = PadDigits(glbPictureIDY,2) + "_" + PadDigits(glbPictureIDX,2) + ".jpg"; 
	  	
	  	//Note: This try catch is poor programming!
	  	//We should find a way to simlpy check glbPictures
	  	//and see if the image is in the .zip file  
	    try
        {
            DisplayPicture.setSource(glbPictures, strPicture); 
        }
        catch(errorObj)
        {
            //alert(errorObj.message);
            DisplayPicture.Source = "NoImageFound.jpg"; 
        }
	  
	}


}


//DOWNLOADER

function onDownloadProgressChanged(sender, eventArgs)
{
    var plugin = sender.getHost();

    var percentage = Math.floor(sender.downloadProgress * 100);

    var progressText = plugin.content.findName("progressText");
    var progressBar = plugin.content.findName("progressBar");

    progressText.text = percentage + "%";
    progressBar.width = percentage * 4; 
}


function onCompleted(sender, eventArgs)
{
    var plugin = sender.getHost();
    var myProgressBar = plugin.content.findName("myProgressBar");
    myProgressBar.Opacity = 0;

    glbPictures = sender;

    scalingFactor = sender.findName("sliderX").width / totalFrames;	
    doScale(sender, newValueX, newValueY);
}


//Utility

    function PadDigits(n, totalDigits) 
    { 
        n = n.toString(); 
        var pd = ''; 
        if (totalDigits > n.length) 
        { 
            for (i=0; i < (totalDigits-n.length); i++) 
            { 
                pd += '0'; 
            } 
        } 
        return pd + n.toString(); 
    } 
 