

// Definition de la classe 'SummaryBox'
// NB: L'ordre des methodes est important
function SummaryBox(id)
{
	// Proprietes publiques et événements
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.OnNextStep = new Array();
	this.NextStepCleanUrl = eval("summaryBox_nextStepCleanUrl");
		
	// Champs et methodes privees
	var _this = this;
	var _content = _this.HtmlObject.find(".SummaryBoxContent");
	var _title = _this.HtmlObject.find(".SummaryBoxTitle");
	var _items = _this.HtmlObject.find(".SummaryBoxTitle li[class!='Separator']");	
	var _separators = _this.HtmlObject.find(".SummaryBoxTitle .Separator");	
	var _current = _this.HtmlObject.find(".SummaryBoxTitle .Selected");
	var _button = _this.HtmlObject.find("#" + id + "_nextButton");
	var _basePriceRow = _this.HtmlObject.find("tr.BasePrice");
	var _discountRow = _this.HtmlObject.find("tr.Discount");
	var _totalPriceRow = _this.HtmlObject.find("tr.TotalPrice");
	var _minWidth = 15, _maxWidth = 0;
	var _priceFormatter = new ScriptCurrencyFormatter();
	var _taxLabel = eval("taxLabel");
	
	var ContentOf = function(item)
	{
		var toFind = "#" + item.attr("id").replace("tab_", "content_");
		return _content.find(toFind);
	}
	
	
	var OnItemOver = function(item)
	{
		_current.removeClass("Selected").addClass("Deselected");
		_current.find(".Long").hide();
		_current.find(".Short").show();
		_current.animate({ width: _minWidth + "px"}, { queue: false, duration: 400 });
		var content = ContentOf(_current);
		content.removeClass("Selected").addClass("Deselected");
		content.hide();
		
		_current = item;
		_current.removeClass("Deselected").addClass("Selected");
		_current.find(".Short").hide();
		_current.find(".Long").show();
		_current.animate({ width: _maxWidth + "px"}, { queue: false, duration: 400 });
		content = ContentOf(_current);
		content.removeClass("Deselected").addClass("Selected");
		content.show();
		
		if ($(document.body).attr("class") == "IE6")
			content.find("img").each(function()
			{
				if (!$(this).attr("filter"))
				{
					var src = $(this).attr("src");
					if (src.indexOf("TextImageGenerator") > 0 || src.indexOf(".png") > 0)
						doFix($(this).get(0), true)
				}
			});
	}
	
	// Désactive les résumés des étapes ultérieures
	this.ResetUrls = function()
	{
		// Comme on va virer les résumés, on doit
		// mettre le focus sur l'étape en cours
		_this.HtmlObject.find(".Current").hover();
	
		// Ensuite, on désactive les résumés pour
		// qu'ils ne soient plus affichables au survol
		var pass = false;
		_this.HtmlObject.find(".SummaryBoxTitle .Short").each(function()
		{
			if ($(this).attr("class").indexOf("Current") != -1)
				pass = true;
			else if (pass && $(this).attr("class").indexOf("Enabled") != -1)
				$(this).removeClass("Enabled").addClass("Disabled");
		});

		// Puis on réinitialise l'URL de l'étape suivante
		_button.attr("href", _this.NextStepCleanUrl);
	}
		
	this.SetParameter = function(name, value)
	{
		var url = _button.attr("href");
		var parts = url.split('?');
		var urlRoot = parts[0];
		var queryString = parts[1];
		
		var params = Tools_GetQueryParams(queryString);
		params[name] = value;
		url = Tools_GetUrl(urlRoot, params);
		
		_button.attr("href", url);
	}

	this.SetFormattedPrices = function(totalPrice, discount, basePrice, fromLabel)
	{
		if (basePrice)
		{
			_basePriceRow.find("td").html(basePrice);
		}
		
		if (discount)	
		{
			_discountRow.find("td").html(discount);
			_discountRow.show();
		}
		else _discountRow.hide();
				
		if (fromLabel)
			_totalPriceRow.find("th").html(fromLabel);
		_totalPriceRow.find("td").html(totalPrice);
		
		// Si le prix de base est identique au prix total on masque le prix de base
		// sinon on l'affiche (à moins qu'il ne soit caché, cf. GB)
		if (_basePriceRow.find("td").html() == _totalPriceRow.find("td").html())
			_basePriceRow.hide();
		else if (_basePriceRow.attr("class").indexOf("Hidden") < 0)
			_basePriceRow.show();

	}


	// Les 3 derniers paramètres ne sont obligatoires que pour les 2 premières étapes
	// puisque le prix de base et la remise peut varier d'un choix à l'autre
	this.SetPrices = function(totalPriceValue, discountValue, basePriceValue, fromLabel)
	{
		var taxSuffix = '';
		if (Tools_IsNullOrEmpty(_taxLabel) == false)
			taxSuffix =  ' ' + _taxLabel;

		if (basePriceValue)
		{
			var basePrice = _priceFormatter.Format(basePriceValue) + taxSuffix;
		}
		
		if (discountValue)	
		{
			var discount = _priceFormatter.Format(discountValue) + taxSuffix;
		}

		var totalPrice = _priceFormatter.Format(totalPriceValue) + taxSuffix;

		
		this.SetFormattedPrices(totalPrice, discount, basePrice, fromLabel);
	}
	


		
	this.OnLoad = function()
	{
		_title.css("width", _content.width() + "px");
		_maxWidth = _title.width() - ((_items.length - 1) * _minWidth + _separators.length * 2);
		_current.css("width", _maxWidth + "px");
		_this.HtmlObject.find(".SummaryBoxTitle .Deselected").css("width", _minWidth + "px");
	    
		_items.hover(function()
		{
			if ($(this).find(".Disabled").length == 0)
			{
				var htmlContent = Tools_Trim(ContentOf($(this)).html());
				if (htmlContent != "")
					OnItemOver($(this))
			}
		});
	    
		_this.HtmlObject.find(".Selected .Short").hide();
		_this.HtmlObject.find(".Deselected .Long").hide();
		_this.HtmlObject.find(".SummaryBoxTitle ul").css("visibility", "visible");
		
		_button.click(function()
		{
			Tools_DispatchEvent(_this.OnNextStep, _this, {});
			var bodyClass = $(document.body).attr("class");
			if (bodyClass == "IE6" || bodyClass == "IE7" || bodyClass == "IE8") // ???
				window.location = $(this).attr("href");
		});
	}
}


