/* -------------------------------------------------------------------------- */
/** 
 *    @fileoverview
 *       Flash -> JS interface
 *
 *    @version rev001.2008-12-08
 *    @requires swfobject.js
 *    @requires common.js
 */
/* -------------------------------------------------------------------------- */



/* -------------------- Settings for CitroenJSInterface -------------------- */

var __CITROEN_FLASH_MODE_CSS__ = "css/flash.css";
var __CITROEN_FLASH_NODE_ID__  = (BA.ua.isWinIE && BA.ua.revision < 7) ? "mainFlash" : "wrapper";
var __CITROEN_FLASH_MIN_SIZE__ = {
	width : 1000,
	height: 730
};



/* --------------- CitroenJSInterface --------------- */
/**
 * Flash -> JS Interface.
 */
function CitroenJSInterface(/* name, arg1, arg2, ... */) {
	var _this = arguments.callee;
	return _this.doCallBack.apply(_this, arguments);
}
CitroenJSInterface.callBack = {};
CitroenJSInterface.addCallBack = function(name, func, aThis) {
	if (!this.callBack[name]) {
		this.callBack[name] = [];
	}
	this.callBack[name].push(BACreateDelegate(func, aThis));
}
CitroenJSInterface.doCallBack = function(/* name, arg1, arg2, ... */) {
	var name = arguments[0];
	if (!name || !this.callBack[name]) return;
	var args = [];
	for (var i = 1, n = arguments.length; i < n; i++) {
		args.push(arguments[i]);
	}
	var ret = [];
	this.callBack[name].forEach(function(func){
		ret.push(func.apply(this, args));
	}, this);
	if (this.callBack[name].length == 1) {
		ret = ret[0];
	}
	return ret;
}



/* --------------- Constructor : CitroenMinSizeObserver --------------- */
/**
 * observer of target node size.
 * @class common settings and environment variables set.
 * @param {String} id         id in target node.
 * @param {Object} minSize    Object contain width/height property.
 * @constructor
 */
function CitroenMinSizeObserver(id, minSize) {
	/** target node.
	    @type BAElement @private */
	this.node      = null;
	/** container node or window-object.
	    @type Object @private */
	this.container = null;
	/** minimum size for target node.
	    @type Object @private */
	this.minSize   = {
		width : 0,
		height: 0
	}
	/** timer for search target node.
	    @type BATimer @private */
	this.timer     = null;

	this.init(id, minSize);
}

/** 
 * @param {String} id         id in target node.
 * @param {Object} minSize    Object contain width/height property.
 */
CitroenMinSizeObserver.prototype.init = function(id, minSize) {
	this.setMinSize(minSize);
	BAAddOnload(function(){
		if (!this.isEnabled()) {
			new BASetTimeout(arguments.callee, 100, this);
			return;
		}
		var node = document.getElementByIdBA(id);
		if (!node) {
			new BASetTimeout(arguments.callee, 100, this);
			return;
		}
		this.setNode(node);
		if (this.isCSSMode()) {
			this.setContainer(window);
		} else {
			this.setContainer(this.getNode().getParentNodeBA());
		}
		this.onContainerResize();
	}, this);
	if (this.isCSSMode()) {
		window.addEventListenerBA("resize", this.onContainerResize, this);
	}
}

/** 
 * get target node.
 * @return target node
 * @type BAElement
 */
CitroenMinSizeObserver.prototype.getNode = function() {
	return this.node;
}

/** 
 * set target node.
 * @param {BAElement} node    target node.
 */
CitroenMinSizeObserver.prototype.setNode = function(node) {
	this.node = node;
	this.setMinSize(this.getMinSize());
}

/** 
 * get container node or window-object.
 * @return container
 * @type Object
 */
CitroenMinSizeObserver.prototype.getContainer = function() {
	return this.container || window;
}

/** 
 * set container node or window-object.
 * @param {Object} container    node or window-object.
 */
CitroenMinSizeObserver.prototype.setContainer = function(container) {
	this.container = container;
}

/** 
 * get minimum size.
 * @return minimum size
 * @type Object
 */
CitroenMinSizeObserver.prototype.getMinSize = function() {
	return this.minSize;
}

/** 
 * set minimum size.
 * @param {Object} minSize    minimum size.
 */
CitroenMinSizeObserver.prototype.setMinSize = function(minSize) {
	if (minSize && (!isNaN(minSize.width) || !isNaN(minSize.height))) {
		if (!isNaN(minSize.width) && minSize.width >= 0) {
			this.minSize.width = parseInt(minSize.width, 10);
		}
		if (!isNaN(minSize.height) && minSize.height >= 0) {
			this.minSize.height = parseInt(minSize.height, 10);
		}
	}
	this.onContainerResize();
}

/** 
 * is enabled.
 * @return true/false
 * @type Boolean
 */
CitroenMinSizeObserver.prototype.isCSSMode = function() {
	return (BA.ua.isSafari || (BA.ua.isWinIE && BA.ua.revision < 7));
}

/** 
 * is enabled.
 * @return true/false
 * @type Boolean
 */
CitroenMinSizeObserver.prototype.isEnabled = function() {
	return BA.env.isDOMReady;
}

/** 
 * clear timer.
 * @private
 */
CitroenMinSizeObserver.prototype.clearTimer = function() {
	if (this.timer) {
		this.timer.clearTimer();
		this.timer = null;
	}
}

/** 
 * resize event handler (for WinIE lte 6.)
 * @private
 */
CitroenMinSizeObserver.prototype.onContainerResize = function(e) {
	this.clearTimer();
	if (this.isEnabled() && this.node) {
		if (this.isCSSMode()) {
			if (this.container) {
				var containerSize = {
					width  : this.container.offsetWidth,
					height : this.container.offsetHeight
				}
				if (this.container === window) {
					var geom = BAGetGeometry();
					containerSize.width  = geom.windowW;
					containerSize.height = geom.windowH;
				}
				if (containerSize.width < this.minSize.width) {
					this.node.style.width = this.minSize.width + "px";
				} else {
					this.node.style.width = (this.container === window) ? "100%" : containerSize.width + "px";
				}
				if (containerSize.height < this.minSize.height) {
					this.node.style.height = this.minSize.height + "px";
				} else {
					this.node.style.height = (this.container === window) ? "100%" : containerSize.height + "px";
				}
			} else {
				this.timer = new BASetTimeout(arguments.callee, 100, this);
			}
		} else {
			this.node.style.minWidth  = this.minSize.width  + "px";
			this.node.style.minHeight = this.minSize.height + "px";
		}
	}
}



/* -------------------- Main : register start-up -------------------- */

if (__CITROEN_FLASH_ENABLED__) {
	BAAppendCSS(__CITROEN_FLASH_MODE_CSS__);
	var __CITROEN_FLASH_SIZE_OBSERVER__ = new CitroenMinSizeObserver(__CITROEN_FLASH_NODE_ID__, __CITROEN_FLASH_MIN_SIZE__);
	CitroenJSInterface.addCallBack("resize", __CITROEN_FLASH_SIZE_OBSERVER__.setMinSize, __CITROEN_FLASH_SIZE_OBSERVER__);
}
