﻿//===============================================================================
// ID		  : $Id: Codex.js 131 2008-03-31 13:56:30Z stefan.schult $
// Author     : $Author: stefan.schult $
// Description: JSClass for ResizePlugin
//===============================================================================

PluginResize = function(rootElement){
	var thisClass = this;
    this.toString = function(){
        return "PluginResize";
    };
    this.rootElement = rootElement;
    this.elements = {};
    this.functionsAfter = {};
    this.timer = null;
    rootElement.getHost().content.onResize = Silverlight.createDelegate(this, function(){
		this.updateLayout();
		window.clearTimeout(this.timer);
		this.timer = window.setTimeout(Silverlight.createDelegate(this, this.afterUpdateLayout), 250);
	});
    rootElement.getHost().content.onFullScreenChange = Silverlight.createDelegate(this, function(){
		this.updateLayout();
		window.clearTimeout(this.timer);
		this.timer = window.setTimeout(Silverlight.createDelegate(this, this.afterUpdateLayout), 250);
	});
}
PluginResize.prototype = {
	add : function(Canvas, Resize){
		this.elements[Canvas.Name] = [Canvas, Resize];
	},
	addAfter : function(name, functionAfter){
		this.functionsAfter[name] = [functionAfter];
	},
	updateLayout : function(){
		var plugIn = this.rootElement.getHost();
		var Dimension = {
			Current : {
				width: ((plugIn.content.ActualWidth != 0) ? plugIn.content.ActualWidth : plugIn.offsetWidth),
				height: ((plugIn.content.ActualHeight != 0) ? plugIn.content.ActualHeight : plugIn.offsetHeight)
			},
			Previous : {
				width: this.rootElement.Width,
				height: this.rootElement.Height
			}
		}
		this.rootElement.Width = Dimension.Current.width;
		this.rootElement.Height = Dimension.Current.height;
		for(canvas in this.elements){
			this.elements[canvas][1](this.elements[canvas][0], Dimension);
		}
	},
	afterUpdateLayout : function(){
		window.clearTimeout(this.timer);
		for(canvas in this.functionsAfter){
			this.functionsAfter[canvas][0]();
		}
	}
}