function Seventeen(target, control) {
	this.target = target;
	this.control = control;
	
	this.contentHost = this.target.findName("ContentHost");
	this.foreground = this.target.findName("Foreground");
	this.root = this.target.findName("Root");
	this.clippingHost = this.target.findName("ClippingHost");
	this.close = this.target.findName("Close");
	
	setCallback(this.target, "MouseLeftButtonDown", delegate(this, this.handleMouseDown));
	setCallback(this.target, "MouseLeftButtonUp", delegate(this, this.handleMouseUp));
	setCallback(this.target, "MouseMove", delegate(this, this.handleMouseMove));
	setCallback(this.target.findName("Intro"), "Completed", delegate(this, this.handleIntroCompleted));
	setCallback(this.target.findName("Outro"), "Completed", delegate(this, this.handleOutroCompleted));
	setCallback(this.close, "MouseEnter", delegate(this, this.handleCloseEnter));
	setCallback(this.close, "MouseLeave", delegate(this, this.handleCloseLeave));
	setCallback(this.close, "MouseLeftButtonDown", delegate(this, this.handleCloseClick));
	
	this.wheelHelper = new WheelHelper();
	this.wheelHelper.wheelScrolled = delegate(this, this.processMouseWheel);

}

Seventeen.prototype.content = null;
Seventeen.prototype.loader = null;
Seventeen.prototype.loadingUri = null;
Seventeen.prototype.lastMouseClick = 0;
Seventeen.prototype.scaleX = 1;
Seventeen.prototype.scaleY = 1;
Seventeen.prototype.translateX = 0;
Seventeen.prototype.translateY = 0;
Seventeen.prototype.lastMouseX = 0;
Seventeen.prototype.lastMouseY = 0;
Seventeen.prototype.isMouseCaptured = false;
Seventeen.prototype.introCompleted = false;
Seventeen.prototype.closed = null;

Seventeen.prototype.load = function(uri, description) {
	this.loadingUri = uri;
	
	this.target.findName("Description").text = description;
	
	this.loader = new Loader();
	this.loader.uris.push(this.loadingUri);
	this.loader.completed = delegate(this, this.handleXamlLoadCompleted);
	this.loader.start();
}

Seventeen.prototype.handleXamlLoadCompleted = function(responses) {
	var contentXaml = responses[this.loadingUri];
	
	this.loader = null;
	this.loadingUri = null;
	
	this.content = this.control.createFromXaml(contentXaml);
	
	if (this.introCompleted) {
		this.prepareContent();
	}
}

Seventeen.prototype.handleIntroCompleted = function() {
	if (this.content != null) {
		this.prepareContent();
	}
	else {
		this.introCompleted = true;
	}
}

Seventeen.prototype.prepareContent = function() {
	this.contentHost.children.add(this.content);
	
	var contentWidth = this.getContentHeight();
	var contentHeight = this.getContentHeight();
	
	this.scaleX = 1;
	this.scaleY = 1;
	var scale = 1;
	
	var scale = this.clippingHost.width / contentWidth;
	if (this.clippingHost.height / contentHeight < scale)
		scale = this.clippingHost.height / contentHeight;
		
	this.scaleX = scale;
	this.scaleY = scale;
		
	this.translateX = this.clippingHost.width / 2 - (contentWidth * scale) / 2;
	this.translateY = this.clippingHost.height / 2 - (contentHeight * scale) / 2;
	
	this.updateTransform();
}

Seventeen.prototype.getContentWidth = function() {
	// Default to something if not set.
	var contentWidth = this.content.width;
	if (contentWidth == 0)
		return 100;
	return contentWidth;
}

Seventeen.prototype.getContentHeight = function() {
	// Default to something if not set.
	var contentHeight = this.content.height;
	if (contentHeight == 0)
		return 100;
	return contentHeight;
}

Seventeen.prototype.handleMouseDown = function(sender, eventArgs) {
	var date = new Date();
	
	if (date.getTime() - this.lastMouseClick < 500) {
		this.lastMouseClick = 0;
		this.handleMouseDoubleClick(sender, eventArgs);
		return;
	}
	this.lastMouseClick = date.getTime();
	
	this.lastMouseX = eventArgs.x;
	this.lastMouseY = eventArgs.y;
	
	this.contentHost.captureMouse();
	this.isMouseCaptured = true;
}

Seventeen.prototype.handleMouseUp = function() {
	this.isMouseCaptured = false;
	
	this.contentHost.releaseMouseCapture();
}

Seventeen.prototype.handleMouseMove = function(sender, eventArgs) {	
	if (this.isMouseCaptured) {
		this.translateX += eventArgs.x - this.lastMouseX;
		this.translateY += eventArgs.y - this.lastMouseY;
		
		this.updateTransform();
	}
	
	this.lastMouseX = eventArgs.x;
	this.lastMouseY = eventArgs.y;
}

Seventeen.prototype.handleMouseDoubleClick = function(sender, eventArgs) {
//	this.scaleX = 1;
//	this.scaleY = 1;
//	this.translateX = this.control.clientWidth / 2 - this.getContentWidth() / 2;
//	this.translateY = this.control.clientHeight / 2 - this.getContentHeight() / 2;
//	this.updateTransform();
	this.handleCloseClick();
}

Seventeen.prototype.updateTransform = function() {
	var renderTransform = "<TransformGroup>"
	renderTransform += "<ScaleTransform ScaleX='" + this.scaleX + "' ScaleY='" + this.scaleY + "'/>";
	renderTransform += "<TranslateTransform X='" + this.translateX + "' Y='" + this.translateY + "'/>";
	renderTransform += "</TransformGroup>";
		
	var transform = this.control.createFromXaml(renderTransform);
	this.contentHost.RenderTransform = transform;
}

Seventeen.prototype.updateSize = function(width, height) {
	this.foreground["Canvas.Left"] = width / 2 - this.foreground.width / 2;
	this.clippingHost["Canvas.Left"] = width / 2 - this.foreground.width / 2 + 10;
}

Seventeen.prototype.processMouseWheel = function(delta) {
	
	var zoom = Math.abs(delta) * 1.3;
	if (delta < 0)
		zoom = 1 / zoom;

	var vx = (1 - zoom) * ((this.lastMouseX - this.clippingHost["Canvas.Left"]) - this.translateX) / this.scaleX;
	var vy = (1 - zoom) * ((this.lastMouseY - this.clippingHost["Canvas.Top"]) - this.translateY) / this.scaleY;
	
	var vtx = vx * this.scaleX;
	var vty = vy * this.scaleY;

	this.translateX += vtx;
	this.translateY += vty;
	
	this.scaleX = this.scaleX * zoom;
	this.scaleY = this.scaleY * zoom;
	
	this.updateTransform();
}

Seventeen.prototype.handleCloseEnter = function() {	
	this.target.findName("CloseIn").begin();
	this.target.findName("CloseOut").stop();
}

Seventeen.prototype.handleCloseLeave = function() {
	this.target.findName("CloseOut").begin();
	this.target.findName("CloseIn").stop();
}

Seventeen.prototype.handleCloseClick = function() {
	this.target.findName("Outro").begin();
	this.target.findName("Intro").stop();
	
	this.wheelHelper.detach();
}

Seventeen.prototype.handleOutroCompleted = function() {
	if (this.closed != null) {
		this.closed();
	}
}