

// Definition de la classe 'FinancingBox'
// NB: L'ordre des methodes est important
function FinancingBox(id)
{

	// Proprietes et événements publiques
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.IsDisabled = false;
	this.OnRequest = new Array();
	this.OnResponse = new Array();
	this.OnCustom = new Array();
	this.SessionId = null;
	this.ButtonUrl = '';
			
	if (this.HtmlObject.length > 0)
		this.DisplayMode = (this.HtmlObject.attr("class")
			.indexOf("Unboxed") < 0 ? "Boxed" : "Unboxed");
			
	// Champs et methodes privees
	var _this = this;
	var _title = _this.HtmlObject.find(".FinancingBoxTitle");
	var _innerContent = _this.HtmlObject.find(".FinancingBoxInnerContent");
	var _content = _this.HtmlObject.find(".FinancingBoxText");
	var _footer = _this.HtmlObject.find(".FinancingBoxFooter");
	var _loading = _this.HtmlObject.find(".FinancingBoxHeader img");
	var _button = _this.HtmlObject.find(".FinancingButton");
	var _buttonPart = _this.HtmlObject.find(".ButtonPart");
	var _currentRequest = null;
	var _pendingRequest = null;
	var _isOpened = false;
	var _lastParams = null;
	var _noCache = false;

	var SetLoadingVisible = function(visible)
	{
		if (visible)
		{
			_loading.fadeIn();
			if (_this.DisplayMode == "Unboxed")
				_title.fadeIn();
		}
		else
		{
			if (_this.DisplayMode == "Boxed")
				_loading.hide();
			else
			{
				_title.hide();
				_loading.hide();
			}
		}
	}
		
	// Retrouve les liens modaux
	var GetModalLinks = function(element)
	{
		var links = new Array();
		if (element)
			element.find("a").each(function()
			{
				var script = $(this).attr("onclick");
				if (script && script.toString().toLowerCase().indexOf('showmodaldialog') > 0)
					links.push($(this));
				else
				{
					var href = $(this).attr("href");
					if (href && href.toString().toLowerCase().indexOf('showmodaldialog') > 0)
						links.push($(this));
				}
			});
		return links;
	}
	
	var CustomRefresh = function()
	{				
		if (_lastParams)
		{
			_this.UpdateContent(_lastParams, true);
			Tools_DispatchEvent(_this.OnCustom, _this, {});
		}
	}
	
	// Provoque un rafraichissement à chaque clic sur un lien modal
	this.SetModalEvents = function(element)
	{
		jQuery.each(GetModalLinks(element),
			function (i, val) { val.click(CustomRefresh) });
	}
	
	var OnUpdate_Success = function(response)
	{
		if (_this.IsDisabled)
			return ;
			
		var sessionId = response["financingSessionId"];
		if (Tools_IsNullOrEmpty(sessionId) == false)
			_this.SessionId = sessionId;

		_currentRequest = null;
		
		if (_pendingRequest)
		{
			ExecuteRequest(_pendingRequest.Params, _pendingRequest.NoCache);
			_pendingRequest = null;
			return ;
		}
		
		SetLoadingVisible(false);
		
		var contentHtml = response["content"];
		var footerHtml =  response["footer"];
		
		_content.html(contentHtml);
		_footer.html(footerHtml);
		_this.ButtonUrl = response["buttonUrl"];
		_buttonPart.css("display", Tools_IsNullOrEmpty(_this.ButtonUrl) ? "none" : "block");

		_this.SetModalEvents(_innerContent);	
		
		var args = 
		{
			SessionId: _this.SessionId,
			Min: response["mmin"],
			MinLabel: response["mmin_label"],
			Max: response["mmax"],
			MonthlyBudget: response["mref"]
		};
		
		_content.slideDown("slow", function()
		{
			Tools_DispatchEvent(_this.OnResponse, _this, args);
		});
	}
	
	var OnUpdate_Failed = function(response)
	{
		_currentRequest = null;
		_pendingRequest = null;
		_content.html("Error !").slideDown(function()
		{
			Tools_DispatchEvent(_this.OnResponse, _this, { Failed: true });
		});
		_this.Disable();
	}
	
	var ExecuteRequest = function(params, noCache)
	{
		if (_this.IsDisabled)
			return null;

		var sessionId = params["FinancingSessionId"];
		if (Tools_IsNullOrEmpty(sessionId) == false)
			_this.SessionId = sessionId;
		
		params["FinancingSessionId"] = _this.SessionId;

		if (!params["isSummary"])
			params["isSummary"] = false;

		_lastParams = params;

		_buttonPart.hide();
		SetLoadingVisible(true);
		Tools_DispatchEvent(_this.OnRequest, _this, {});
		_content.slideUp("normal", function()
		{
			_currentRequest = Ajax_Execute(_financingServiceUrl, "GetMonthlyContent",
				params, OnUpdate_Success, OnUpdate_Failed, noCache);
		});
	}

	this.Abort = function()
	{
		if (!_currentRequest)
			return true;
		if (Tools_IsNullOrEmpty(_this.SessionId))
			return false;
	
		SetLoadingVisible(false);
		if (_currentRequest)
		{
			_currentRequest.abort();
			_currentRequest = null;
		}
		return true;
	}
	
	this.Disable = function()
	{
		_this.IsDisabled = true;
		_this.HtmlObject.fadeOut();
		_this.Abort();
	}
	
	this.UpdateContent = function(params, noCache)
	{
		if (_this.HtmlObject.length == 0 || !_financingServiceUrl)
			_this.Disable();

		if (_this.IsDisabled)
			return ;

		if (_this.Abort() == false)
		{
			_pendingRequest = { Params: params, NoCache: noCache };
			return ;
		}
		
		var queryParams = Tools_GetQueryParams();
		var isCustom = (queryParams && queryParams["FinancingCustom"] == '1');
		_noCache = (_noCache || noCache || isCustom) ? true : false;
		
		ExecuteRequest(params, _noCache);
	}
	
	this.OnButtonClick = function()
	{
		if (Tools_IsNullOrEmpty(_this.ButtonUrl) == false)
		{
			var s = _this.ButtonUrl.toLowerCase();
			if (s.indexOf('http:') == 0)
				window.open(_this.ButtonUrl, '_new');
			else if (s.indexOf('javascript:') == 0)
				eval(_this.ButtonUrl.substring(11));
			if (s.indexOf('showmodaldialog') > 0)
				CustomRefresh();
		}
	}
	
	this.OnLoad = function()
	{
		if (_this.HtmlObject.length == 0 || !_financingServiceUrl)
			_this.Disable();

		_button.click(_this.OnButtonClick);
		
		if (typeof(_financingStartSessionId) != 'undefined')
			_this.SessionId = _financingStartSessionId;
	}
}


