function PromoInfoBox(){

	this.constructor = function(){
		this.title = 'Titolo';
		this.body = [];
		this.buttons = [{text: 'OK',callback: function(){}},{text: 'Cancel',callback: function(){}}];
		this.$box = $('<div class="promoInfoBox" />').css({display: 'none',zIndex: 1200});
		$(document.body).append(this.$box);
	};

	this.show = function(loading){
		var withOverlay = true;
		if(arguments.length > 1) withOverlay = arguments[1];
		if(loading){
			this.loadingState();
		}else{
			this.generateHTML(this.$box);
		}
		this.applyCSS();
		if(withOverlay){
			var t = this;
			var b = this.$box;
			var o = this.getOverlay();
			o.click(function(){t.close()});
			o.show();
			b.fadeIn()
		}else{
			this.$box.fadeIn();
		}
	};

	this.close = function(){
		var o = this.getOverlay();
		this.$box.fadeOut('normal',function(){
			o.hide();
		});
	}

	this.loadingState = function(){
		this.$box.empty().append('<p>&nbsp;</p>');
		this.$box.addClass('loading');
	};

	this.showMessage = function(){
		this.$box.empty().removeClass('loading');
		var d = $('<div />');
		this.generateHTML(d);
		this.$box.append(d.css('display','none'));
		var tmpH = this.$box.height();
		d.fadeIn();
		this.$box.animate({height: 'show'});
	};

	this.showMessageWithList = function(){
		this.$box.empty().removeClass('loading');
		var d = $('<div />');
		this.generateHTMLWithList(d);
		this.$box.append(d.css('display','none'));
		var tmpH = this.$box.height();
		d.fadeIn();
		this.$box.animate({height: 'show'});
	};

	this.applyCSS = function (){
		var b = this.$box;
		var scrollTop = (window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop);
		b.css({
			position: $.browser.msie && parseInt($.browser.version) <= 6 ? 'absolute' : 'fixed',
			top:	$.browser.msie && parseInt($.browser.version) <= 6 ? scrollTop+50 : 50,
			left: ($(document).width() - b.width())/2
		});
	};

	this.generateHTMLWithList =  function (obj){
		var t = this;
		obj.empty();
		var $h1 = $('<h1/>');
		$h1.html(this.title);
		obj.append($h1);
		$(this.body).each(function(i,o){
			obj.append('<p>' + o + '</p>');
		});
		var $buttons = $('<p class="actions" />');
		var $list = $('<ul />');
		$(this.buttons).each(function(i,o){
			if(o===null) return true;
			var $i = $('<li />');
			var $l = $('<a href="#" />');
			$l.text(o.text);
			$l.click(function(e){t.callback = o.callback;t.callback();e.preventDefault()});
			$i.append($l);
			$list.append($i);
		});
		$buttons.append($list);
		obj.append($buttons);
	}

	this.generateHTML =  function (obj){
		var t = this;
		obj.empty();
		var $h1 = $('<h1/>');
		$h1.html(this.title);
		obj.append($h1);
		$(this.body).each(function(i,o){
			obj.append('<p>' + o + '</p>');
		});
		var $buttons = $('<p class="actions" />');
		$(this.buttons).each(function(i,o){
			if(o===null) return true;
			var $l = $('<a href="#" />');
			$l.addClass('link' + (i+1));
			$l.text(o.text);
			$l.click(function(e){t.callback = o.callback;t.callback();e.preventDefault()});
			$buttons.append($l);
		});
		obj.append($buttons);
	}

	// Overlay
	this.getOverlay = function(){
		if(!PromoInfoBox.prototype.$overlay){
			var o = $('<div id="overlay" />');
			if($.browser.msie && parseInt($.browser.version) <= 6){
				o.css({
					position: 'absolute',
					height: $(document).height(),
					width: $(document).width()
				});
			}
			o.css('opacity',0.5);
			$(document.body).append(o.css({display: 'none',zIndex:999}));
			PromoInfoBox.prototype.$overlay = o;
		}
		return PromoInfoBox.prototype.$overlay;
	};

	this.constructor();
};

function promoMsgDisplay(box, messages){
	var continua = {
		text: 'Continua',
		callback: function(){this.close()}
	};
	/*
	var help = {
			text: 'Guida',
			callback: function(){window.location = "/"}
	};
	*/
	box.title = 'Stato promozioni';
	$(messages).each(function() {
		var text = '';
		/* caso speciale codice */
		if (this.indexOf('((code))') != -1)
			text = 'Codice promozione <strong>non valido</strong> per uno dei seguenti motivi:'
				+ '<ul><li>non esiste alcuna promozione con questo codice</li>'
				+ '<li>la promozione non è più attiva</li>'
				+ '<li>hai già usufruito di questa promozione per il numero massimo di utilizzi concessi</li>'
				+ '<li>se la promozione prevede una soglia minima di spesa, questa non è stata raggiunta</li>'
				+ '<li>se la promozione prevede uno specifico prodotto presente nel carrello, questo non è stato inserito</li></ul>';
		else {
			if (messages.length > 1)
				text += '<ul><li>';
			text += this;
			text = text.replace('((promo))', '<em>');
			text = text.replace('((/promo))', '');
			text = text.replace('((prize))', '');
			text = text.replace('((/prize))', '</em><br />');
			text = text.replace('((title))', '&quot;');
			text = text.replace('((/title))', '&quot;');
			text = text.replace('EUR', '&euro;');
			text = text.replace('((message))', '<strong>');
			text = text.replace('((/message))', '</strong>');
			if (text.indexOf('((deleted))') != -1) {
				text = text.replace('((deleted))', '');
				text = text.replace('((conditions))', ':<ul>');
				text = text.replace('((/conditions))', '</ul>');
				if (text.indexOf('((condition-lbound))') != -1) {
					text = text.replace('((condition-lbound))', '<li>spesa minima di ');
					text = text.replace('((/condition-lbound))', ' &euro;</li> ');
				}
				if (text.indexOf('((condition-lpieces))') != -1) {
					text = text.replace('((condition-lpieces))', '<li>un quantitativo di almeno ');
					text = text.replace('((/condition-lpieces))', ' pezzi</li> ');
				}
				if (text.indexOf('((condition-title))') != -1) {
					text = text.replace('((condition-title))', '<li>Il prodotto &quot;');
					text = text.replace('((/condition-title))', '&quot; deve essere presente nel carrello</li> ');
				}
			}
			if (messages.length > 1)
				text += '</li></ul>'
		}
		box.body.push(text);
	});
	
	box.buttons = [/*help*/null,continua];
	
	box.showMessage();
}
