if (!window.basscut)
	window.basscut = {};

basscut.Page = function() 
{
}

basscut.Page.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		// Sample event hookup:	
		rootElement.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
		// Hook the plug-in's resize event
        this.resize(rootElement, null);
        this.control.content.onResize = Silverlight.createDelegate(this, this.resize);
        //this.setCallback(this.control.content, "onResize", this.resize);
	},
	// Sample event handler
	handleMouseDown: function(sender, eventArgs) 
	{
		// The following line of code shows how to find an element by name and call a method on it.
		// this.control.content.findName("scratch1").Stop();
		// this.control.content.findName("scratch1").Begin();
		// this.control.content.findName("scream").Stop();
		// this.control.content.findName("scream").Play();
	}
}
basscut.Page.prototype.resize = function(sender, eventArgs) {
   var sourceWidth = sender.findName("Canvas").width;
   var targetWidth = this.control.content.actualWidth;
   var sourceHeight = sender.findName("Canvas").height;
   var targetHeight = this.control.content.actualHeight;
   
   var scale = 0;
   if (sourceHeight > 0 && sourceWidth > 0) {
       var aspectRatio = sourceWidth/sourceHeight;
       scale = Math.min(targetHeight / sourceHeight, targetWidth / sourceWidth);
   }
   if(scale!=0){
       var scaleTransform = sender.findName("scaleTransform");
       scaleTransform.scaleX = scale;
       scaleTransform.scaleY = scale;
       var translateTransform = sender.findName("translateTransform");
       translateTransform.X = (targetWidth - (sourceWidth * scale)) / 2 / scale;
       translateTransform.Y = (targetHeight - (sourceHeight * scale)) / 2 / scale;
   }
}

function scratch(sender, eventArgs){
	if(sender.name == "right_eye"){
		sender.findName("scratch1").Stop();
		sender.findName("scratch1").Begin();
		sender.findName("scream").Stop();
		sender.findName("scream").Play();
	}
}

function poke(sender, eventArgs){
	if(sender.name == "left_eye"){
		sender.findName("wimp").Stop();
		sender.findName("wimp").Begin();
	}
}
