//Namespace
if (typeof EnergyApps == "undefined"){EnergyApps = {Concert: {}};}
if (typeof EnergyApps.Concert == "undefined"){EnergyApps.Concert= {
	Manager : {}, 
	MyProductsBox: {}, 
	ConfiguratorBox: {}};
}
/**
 * Zentral Manager for Concert Layers
 * @class EnergyApps.Concert.Manager
 * @namespace EnergyApps.Concert
 * @extends Publicis.Observable
 * @author Sebastian Sauer
 * @constructor
 * @param {Object} cfg
 */
EnergyApps.Concert.Manager = function () { 
	// call parent constructor
	EnergyApps.Concert.Manager.superclass.constructor.call(this);
	/**
	 * @configuratorConfig {Object}  
	 */
	/**
	 * @myProductsConfig {Object}  
	 */
	this.configuratorConfig = {
		layerType : "content",
		classPrefix : "contentlayer_2",
		idContainerPrefix : "contentlayer_2_content_container_",
		idButtonPrefix : "contentlayer_2_content_closebutton_",
		width : 936,
		height : 500
	};
	this.myProductsConfig = Object.extend ({}, this.configuratorConfig);
	this.myProductsConfig = Object.extend (
		this.myProductsConfig, 
		{
			width: 913,
			height : 400
	});
	
	this.MYPRODUCTS_REGEX = /^#jump-to-myproducts-(.+)$/;
	this.CONFIGURE_REGEX = /^#jump-to-configure-(.+)$/;
	
	this._registerConfigureBox();
	this._registerMyProductsBox();
	this._openOnLocationHash();
	
};

Publicis.extend(EnergyApps.Concert.Manager, Publicis.Observable, {
	/**
	 * register Configure Box link
	 * @private
	 */
	_registerConfigureBox : function () {
		var $openConfigure = $('openConfigureLayer');
		if(null === $openConfigure) {
			return;
		}
		this.configuratorConfig = Object.extend(
			this.configuratorConfig,
			{
				trigger : $openConfigure
			}
		);
		this.configuratorBox = new EnergyApps.Concert.ConfiguratorBox(this.configuratorConfig);
	},
	/**
	 * register My Products Box link
	 * @private
	 */
	_registerMyProductsBox : function () {
		var $openMyProducts = $('openMyProductsLayer');
		if(null === $openMyProducts) {
			return;
		}
		try {
			this.myProductsConfig = Object.extend(
				this.myProductsConfig,
				{
					trigger : $openMyProducts
				}
			);
			this.myProductsBox = new EnergyApps.Concert.MyProductsBox(this.myProductsConfig);
		} catch (e) {}
	},
	/**
	 * open on certain Location Hashes
	 * @private
	 */
	_openOnLocationHash : function () {
		var hash = window.location.hash,
			data,
			urlParams,
			box,
			origUrl;
		if(this.MYPRODUCTS_REGEX.test(hash) && (this.getMyProductsBox() instanceof EnergyApps.Concert.MyProductsBox)) {
			this.getMyProductsBox().open();
			data = hash.replace(this.MYPRODUCTS_REGEX, "$1");
			urlParams = decodeURIComponent(data);
			box = this.getMyProductsBox();
			origUrl;
			origUrl = box.getUrl();
			box.setUrl(origUrl + urlParams);
			
		}
		if(this.CONFIGURE_REGEX.test(hash) && (this.getConfiguratorBox() instanceof EnergyApps.Concert.ConfiguratorBox)) {
			this.getConfiguratorBox().open();
			data = hash.replace(this.CONFIGURE_REGEX, "$1");
			urlParams = decodeURIComponent(data);
			box = this.getConfiguratorBox();
			if(false === (box instanceof EnergyApps.Concert.ConfiguratorBox)) {
				return;
			}
			origUrl = box.getUrl();
			box.setUrl(origUrl + urlParams);
			// remove the location hash params onclose from the iframe loaded if it got a jump in hash
			box.addListener("close", function (){
				var tmp = this.getUrl(),
					parts = tmp.split(this.CONFIGURE_REGEX),
					url = parts[0];
				this.setUrl(url);
			}, box);
		}
	},
	/**
	 * @return false | {Object} EnergyApps.Concert.MyProductsBox
	 */
	getMyProductsBox : function () {
		if(typeof this.myProductsBox === "undefined") {
			return false;
		} 
		return this.myProductsBox;
	},
	/**
	 * @return false | {Object} EnergyApps.Concert.ConfiguratorBox
	 */
	getConfiguratorBox : function () {
		if(typeof this.configuratorBox === "undefined") {
			return false;
		}
		return this.configuratorBox;
	}
});

/**
 * @class EnergyApps.Concert.MyProductsBox
 * @namespace EnergyApps.Concert
 * @extends Publicis.IFrameBox
 * @constructor
 * @author Sebastian Sauer
 */

EnergyApps.Concert.MyProductsBox = function (cfg) { 
	EnergyApps.Concert.MyProductsBox.superclass.constructor.call(this, cfg);
	if(typeof TrackOpenMyProducts !== "function") {
		return this;
	}
	if(false === Publicis.isArray (this.trigger)) {
		this.trigger.observe('click', function () {
			TrackOpenMyProducts(); 
		});
	} else {
		for (var i in this.trigger) {
			this.trigger[i].observe('click', function () {
				TrackOpenMyProducts(); 
			});
		}
	}
};
Publicis.extend(EnergyApps.Concert.MyProductsBox, Publicis.IFrameBox, {});

/**
 * @class EnergyApps.Concert.ConfiguratorBox
 * @namespace EnergyApps.Concert
 * @extends Publicis.IFrameBox
 * @constructor
 * @author Sebastian Sauer
 */

EnergyApps.Concert.ConfiguratorBox = function (cfg) { 
	EnergyApps.Concert.ConfiguratorBox.superclass.constructor.call(this, cfg);
	if(typeof TrackOpenConfigurator !== "function") {
		return this;
	}
	if(false === Publicis.isArray (this.trigger)) {
		this.trigger.observe('click', function () {
			TrackOpenConfigurator(); 
		});
	} else {
		for (var i in this.trigger) {
			this.trigger[i].observe('click', function () {
				TrackOpenConfigurator(); 
			});
		}
	}
};
Publicis.extend(EnergyApps.Concert.ConfiguratorBox, Publicis.IFrameBox, {});




