/* ############################################################################
/* ############################################################################
* © sanofi-aventis library
*/
(function($){
/* ############################################################################ */
/* ############################################################################ */
/******************************************************************************
* Public jQuery API
*/
$.fn.extend(
{
	//+ jQueryObject $("a.modalPopup").modalPopup();
	/*+ jQueryObject*/modalPopup : function()
	{
		return this.each(function(){new ModalPopup(this);});
	}
});
$.extend(
{
	//+ jQuery $.log("X");
	/*+ jQuery*/log : function(/*string*/msg)
	{
		if(window.console)console.debug(msg);return this; // Just for FX
	},
	//+ jQuery $.modal(true);
	/*+ jQuery*/modal : function(/*bool*/visible)
	{
		Modal.Instance().setVisible(visible);return this;
	},
	//+ jQuery $.resizeModal();
	/*+ jQuery*/resizeModal : function()
	{
		Modal.Instance().onResize();return this;
	}
});
/******************************************************************************
/******************************************************************************
* # util
*/
var util = 
{
	/*+ static function*/proxy : function(object, object_method)
	{
		return function(){return object_method.apply(object, arguments);}
	},
	/*+ static string*/documentHeight : function()
	{
		if($.browser.msie && $.browser.version < 7){
			// handle IE 6
			var scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
			var offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
		
			if(scrollHeight < offsetHeight){
				return $(window).height() + 'px';
			}else{
				return scrollHeight + 'px';
			}
		}else{
			// handle "good" browsers
			return $(document).height() + 'px';
		}
	}
};
/******************************************************************************
* # Modal (Syngleton)
*/
var Modal = function(){this.init.apply(this, arguments);}
Modal._Instance = null;Modal.Instance = function(){if(null == Modal._Instance)Modal._Instance = new Modal();return Modal._Instance;};
Modal.prototype = 
{
	/*- void*/init : function()
	{
		this.currentModalNumber = 0;
		
		this.domElt = $("<div id='sa-modal' style='position:absolute;z-index:1000;top:0;left:0;width:100%;height:100%;background-color:#999;'></div>")
						.hide().fadeTo(0, 0).appendTo(document.body);
		
		this.onResize();
		$(window).bind("resize", util.proxy(this, this.onResize));
	},
	/*+ void*/setVisible : function(/*bool*/visible)
	{
		if(visible)
			 this.show();
		else
			this.hide();
		
		$.log("Modal.currentModalNumber : " + this.currentModalNumber);
	},
	/*- void*/show : function()
	{
		if(this.currentModalNumber==0)
		{
			$('#sa-modal').queue(util.proxy(this, this.queueShow)).fadeTo("normal", 0.43, this.onFadeToFinishProxy);
		}
		this.currentModalNumber++;
	},
	/*- void*/hide : function()
	{
		if(this.currentModalNumber>1)
		{
			this.currentModalNumber--;
		}			
		else if(this.currentModalNumber==1)
		{
			this.currentModalNumber--;
			$('#sa-modal').fadeTo("normal", 0, this.onFadeToFinishProxy).queue(util.proxy(this, this.queueHide));
		}
		// else if(this.currentModalNumber==0) return;
	},
	/*- void*/queueShow : function()
	{
		$(this.domElt).show().dequeue();
	},
	/*- void*/queueHide : function()
	{
		$(this.domElt).hide().dequeue();
	},
	/*+ void*/onResize : function()
	{
		$(this.domElt).height(1).height(util.documentHeight());
	}
};
/******************************************************************************
* # ModalPopup
*/
var ModalPopup = function(){this.init.apply(this, arguments);}
ModalPopup.prototype = 
{
	/*- void*/init : function(link)
	{
		var params = {};try{eval("params = " + $(link).attr("rel"));}catch(e){}
		
		this.opts = $.extend({
			openLink : link,
			domElt : null,
			closeTxt : "Close",
			width : 430
		}, params );
		
		$(this.opts.openLink).bind("click", util.proxy(this, this.onOpenLinkClick));
	},
	/*- void*/show : function()
	{
		$.modal(true);
		
		this.opts.domElt = $("<div id='modalPopup'><div id='modalPopup-bg2'><a class='closeRight' href='#'><span>" + this.opts.closeTxt + "</span></a><div class='contener'></div><a class='closeRight' href='#'><span>" + this.opts.closeTxt + "</span></a><span class='clear'>&nbsp;</span></div></div>")
			.css({
				"position" : "absolute",
				"zIndex" : 2000,
				"width" : this.opts.width + "px",
				"top" : $(this.opts.openLink).position().top + $(this.opts.openLink).height(),
				"left" : $(this.opts.openLink).position().left})
			.hide()
			.appendTo(document.body);
		$(this.opts.domElt).find("a.closeRight").bind("click", util.proxy(this, this.onCloselinkClick));
		
		this.loadLink();
	},
	/*- void*/hide : function()
	{
		$(this.opts.domElt).find("a.closeRight").unbind("click");
		$(this.opts.domElt).find("form:first").unbind("submit");
		$(this.opts.domElt).remove();
		$.modal(false);
	},
	/*- void*/onOpenLinkClick : function(e)
	{
		e.preventDefault();
		this.show();
	},
	/*- void*/onCloselinkClick : function(e)
	{
		e.preventDefault();
		this.hide();
		$(this.opts.openLink).focus();
	},
	/*- void*/loadLink : function()
	{
		$.ajax
		({
			type: "GET",
			url: this.opts.openLink.href + "&time=" + (new Date().getTime() / 1000),
			success: util.proxy(this, this.onLoad),
			error: function(XHR, textStatus, errorThrown){$.log("ModalPopup : " + textStatus);}
		});
	},
	/*- void*/loadForm : function(sUrl)
	{
		$.ajax
		({
			type: "POST",
			url: sUrl,
			data: $(this.opts.domElt).find("form:first").serialize(),
			success: util.proxy(this, this.onLoad),
			error: function(XHR, textStatus, errorThrown){$.log("ModalPopup : " + textStatus);}
		});
	},
	/*- void*/onLoad : function(html, textStatus)
	{
		$(this.opts.domElt).find("div.contener").html(html);
		
		// init multiMail module if exist
		$(this.opts.domElt).find("div.multiMail a.delMail, div.multiMail a.addMail").bind("click", util.proxy(this, this.onAddDelMailLinkClick));
		
		// init form if exist
		$(this.opts.domElt).find("form:first").bind("submit", util.proxy(this, this.onSubmitClick));
		
		$(this.opts.domElt).show();
		$(this.opts.domElt).find("form input[type=text]:first").focus();
		
		$.resizeModal();
	},
	/*- void*/onSubmitClick : function(e)
	{
		e.preventDefault();
		$(this.opts.domElt).hide();
		$(this.opts.domElt).find("form:first").unbind("submit");
		this.loadForm($(this.opts.domElt).find("form:first").attr("action"));
	},
	/*- void*/onAddDelMailLinkClick : function(e)
	{
		e.preventDefault();
		
		if($(e.currentTarget).hasClass("delMail"))
		{
			if(10==$(e.currentTarget).parent().parent().find("div.inputText").size())
			{
				$(this.opts.domElt).find("div.multiMail a.addMail").show();
			}
			if(2==$(e.currentTarget).parent().parent().find("div.inputText").size())
			{
				$(this.opts.domElt).find("div.multiMail a.delMail").hide();
			}
			$(e.currentTarget).parent().remove();
		}
		else // $(e.currentTarget).hasClass("addMail")
		{
			var inputName = $(this.opts.domElt).find("div.multiMail input:first").attr("name");
			$(e.currentTarget).parent().before("<div class='inputText' id='inputText_multi_recipient_email'><input id='"+inputName+"' size='40' name='"+inputName+"' value='' type='text' /> <a href='#' class='delMail'><span>" + $(this.opts.domElt).find("div.multiMail a.delMail:first").text() + "</span></a></div>");
			$(this.opts.domElt).find("div.multiMail a.delMail, div.multiMail a.addMail").unbind("click");
			$(this.opts.domElt).find("div.multiMail a.delMail, div.multiMail a.addMail").bind("click", util.proxy(this, this.onAddDelMailLinkClick));
			$(e.currentTarget).parent().parent().find("input[type=text]:last").focus();
			
			if(10==$(e.currentTarget).parent().parent().find("div.inputText").size())
			{
				$(this.opts.domElt).find("div.multiMail a.addMail").hide();
			}
			if(2==$(e.currentTarget).parent().parent().find("div.inputText").size())
			{
				$(this.opts.domElt).find("div.multiMail a.delMail").show();
			}
		}
		
		$.resizeModal();
	}
};
/* ############################################################################ */
/* ############################################################################ */
})(jQuery);
/* ############################################################################ */
/* ############################################################################ */