

// Definition de la classe 'Ribbon'
// NB: L'ordre des methodes est important
function Ribbon(id)
{
	// Proprietes et evenements publics
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.BodyGroupChanged = new Array();	// Arguments: string BodyGroup
	this.FinancingOverBudget = new FinancingOverBudget(id + "_financingOverBudget");
	this.ActionBar = new ActionBar(id + "_actionBar");
	
	// Champs et methodes privees
	var _this = this;
	var _combo = new ComboBox(id + "_combo");
	var _shadow = $("#" + id + "Shadow");
	var _isShadowVisible = false;
	var _startUrl = eval("startUrl");
	
	// Corrige le pb du chemin affiché par
	// CPP qui n'est pas synchrone avec l'iframe
	var GetTopUrl = function(itemId)
	{
		var metas = _combo.GetMetadatas(itemId);
		if (metas)
		{
			var currentUrl = window.location.href.toLowerCase().replace('\\', '/');
			if (metas.GradesUrl && currentUrl.indexOf('/range/grades') != -1)
				return metas.GradesUrl;
			if (metas.EnginesUrl && currentUrl.indexOf('/range/engines') != -1)
				return metas.EnginesUrl;
			if (metas.CompareUrl && currentUrl.indexOf('/range/compareversions') != -1)
				return metas.CompareUrl;
			if (metas.ConfigureUrl && currentUrl.indexOf('/configurator/') != -1)
				return metas.ConfigureUrl;
			return metas.ShowRoomUrl;
		}
		return null;
	}
	
	var OnComboChanged = function(sender, args)
	{
		var parts = _startUrl.split('?');
		var urlRoot = parts[0];
		var queryString = parts[1];

		var params = Tools_GetQueryParams(queryString);
		params["GrBodyStyle"] = args.Value;
		
		_this.SetShadow(false);
		Tools_DispatchEvent(_this.BodyGroupChanged, _this, {});
		
		var url = GetTopUrl(args.ItemId);
		if (Tools_IsNullOrEmpty(url) == false)
			window.open(url, "_top");
		else
			window.location = Tools_GetUrl(urlRoot, params);
	}
	
	var OnComboOpened = function(sender, args)
	{
		_this.SetShadow(true);
	}
	
	var OnComboClosed = function(sender, args)
	{
		if (!args.ValueChanged)
			_this.SetShadow(false);
	}
	
	var OnActionsEnter = function(sender, args)
	{
		_combo.Close();
		_this.SetShadow(true);
	}
	
	var OnActionsOut = function(sender, args)
	{
		_this.SetShadow(false);
	}
	
	this.SetParameter = function(name, value)
	{
		_this.ActionBar.SetParameter(name, value);
		
		// Ca met à jour les liens de la comboBox
		var parts = _startUrl.split('?');
		var params = Tools_GetQueryParams(parts[1]);
		if (params[name])
			params[name] = value;
		_startUrl = Tools_GetUrl(parts[0], params);
	}
		
	this.SetShadow = function(isVisible)
	{
		if (_isShadowVisible == isVisible)
			return ;
	
		if (isVisible)
		{
			_isShadowVisible = true;
			_shadow.stop().show().css("opacity", "0").fadeTo(1000, 0.6);
		}
		else
		{
			_isShadowVisible = false;
			_shadow.stop().css("opacity", "0.6").fadeOut(1000);
		}
	}

	var OverBudget_ComboBoxItem_OnClick = function()
	{
		var itemId = $(this).attr("id");
		var params = Tools_GetQueryParams(_startUrl);
		params["GrBodyStyle"] = _combo.GetMetadatas(itemId).Value;
		_this.FinancingOverBudget.ShowAlert(params, $(this));
	}
	
	this.UpdateOverBudgetIcons = function()
	{
		var budget = _this.FinancingOverBudget.GetCurrentBudget();
		if (_this.FinancingOverBudget.IsDisabled || !budget)
			return ;

		$(".ComboBoxItem").each(function()
		{
			var metas = _combo.GetMetadatas($(this).attr("id"));
			if (metas && metas.PriceValue > budget.Max)
			{
				$(this).addClass("OverBudget");
				$(this).bind("click", OverBudget_ComboBoxItem_OnClick);
			}
			else
			{
				$(this).removeClass("OverBudget");
				$(this).unbind("click", OverBudget_ComboBoxItem_OnClick);
			}
		});
	}

	// Methodes publiques
	this.OnLoad = function()
	{
		_this.FinancingOverBudget.OnLoad(_this);
		_this.FinancingOverBudget.OnResponse.push(function() { _this.UpdateOverBudgetIcons() });
		_this.UpdateOverBudgetIcons();

		_combo.OnLoad();
		_combo.Opened.push(OnComboOpened);
		_combo.Closed.push(OnComboClosed);	
		_combo.SelectedValueChanged.push(OnComboChanged);

		_shadow.click(function() { _combo.Close() });

		_this.ActionBar.OnLoad();
		_this.ActionBar.MouseOut.push(OnActionsOut);
		_this.ActionBar.MouseEnter.push(OnActionsEnter);
	}
}

