if (!Shop) {
	var Shop = {};
}

Logger = {
	debugIsOn : false,
	setDebug : function(isOn) {
		this.debugIsOn = isOn;
	},
	error : function(msg) {
		if (this.debugIsOn) {
			alert(msg);
		}
	}
};

Shop.Util = {
	getProtocol : function() {
		return document.location.protocol;
	},
	render : function(template, params) {
		return template.replace(/\#\{([^{}]*)\}/g, function(a, b) {
			var r = params[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		});
	},
	toQueryString : function(params) {
		var pairs = [];
		for (key in params) {
			if (params[key] != null && params[key] != ''
					&& typeof params[key] != 'function') {
				pairs.push( [ key, params[key] ].join('='));
			}
		}
		return pairs.join('&');
	},
	isIE : function(test) {
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
			if (typeof test === "function") {
				return test(new Number(RegExp.$1));
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
};

Shop.Page = {
	getDimensions : function() {
		var de = document.documentElement;
		var width = window.innerWidth || self.innerWidth
				|| (de && de.clientWidth) || document.body.clientWidth;
		var height = window.innerHeight || self.innerHeight
				|| (de && de.clientHeight) || document.body.clientHeight;
		return {
			width : width,
			height : height
		};
	}
};

Shop.Frame = {
	content_template : '<iframe id="hotmart_shop_iframe" src="http://pagead.hotmart.net.br/ads/#{key}/pgad1.html" width="#{width}" height="#{height}" frameborder="0" scrolling="no" allowtransparency="true" ></iframe>',	

	getBox : function(params) {
		return document.getElementById(params.id_box_shop);
	},
	getQuery : function(params) {
		var urlParams = Shop.Util.toQueryString( {
			key : params.key
		});
		return urlParams;
	},
	getFrameParams : function(params) {
		return {
			protocol : Shop.Util.getProtocol(),
			query : this.getQuery(params),
			key : params.key,
			width : params.width,
			height : params.height
		};
	},
	show : function(params) {
		var iframeParams = this.getFrameParams(params);
		var iframeHtml = Shop.Util.render(this.content_template, iframeParams);
		var box = this.getBox(params);
		box.innerHTML = iframeHtml;
	}
};

Shop.Widget = {

	setupOptions : function(params, optDebug) {
		if (optDebug != null && typeof (optDebug) !== 'undefined') {
			Logger.setDebug(optDebug);
		}
		if (typeof (params) === 'undefined' || params == null) {
			Logger
					.error("Nenhum parametro informado ao Widget do Hotmart. Verifique se a variavel 'opts' se ela esta preenchida ou com algum erro de sintaxe.")
			return;
		}
		if (params.key == null) {
			Logger
					.error("A chave ('key') do widget deve ser informada. Verifique na variavel 'opts' e adicione a propriedade 'key' com o valor retornado pelo Hotmart");
			return;
		}
		this.params = params;
	},
	show : function(options, optDebug) {
		this.setupOptions(options, optDebug);
		Shop.Frame.show(this.params);
	}
};

