/**
 * @class				Settings
 * @author 				jose
 * @classDescription	manage setting pane
 */
var 	ndSettings = 
{
		openSettingsMenu : function(html)
		{
			var settingsDiv = null;
			var defaultTitle = settingsLocale.settings;
			if ($("#settingsPopup").length > 0)
			{
				settingsDiv = $("#settingsPopup");
				$(settingsDiv).html('');
			}
			else
			{
				settingsDiv = document.createElement("div");
				$(settingsDiv).attr("id", "settingsPopup");
				if (ndNewsUtils.windowSize().height > 600)
					$("html").css("overflow", "hidden");
				$(settingsDiv).dialog({
					height:	"auto",
					width:	800,
					draggable: false,
					resizable: false,
					modal: true,
					close: function(){$(settingsDiv).remove();if ($("html").css("overflow") != "auto") $("html").css("overflow", "auto");}
				});
			}
			$(settingsDiv).append(ndSettings.helperSettingsPane(html));
			var title = $("#settingsPopup form").length > 0 ? $("#settingsPopup form").attr("title") : defaultTitle;
			$(settingsDiv).dialog("option", "title", title);
			$(settingsDiv).dialog("option", "position", ["center", "center"]);
			ndSettings.initForms();
			ndSettings.displayTimeZoneSelect();
		},
		
		openCreditAccountPage : function(callback)
		{
			var url = "index.php?controller=Settings&action=creditAccountForm&next=settings&cancel=settings&tmpl=xml";
			var settings = 
			{
					async: false,
					dataType: "html",
					success: function(data){ndSettings.procressCreditAccountPage(data, callback);},
					error: ndNewsUtils.processXHR,
					type: 'post',
					url: url
			};
			jQuery.ajax(settings);
		},
		
		procressCreditAccountPage: function(htmlText, callback)
		{
			if (!htmlText)
				return (null);
			var defaultTitle = settingsLocale.settings;
			var containerNode =  document.createElement("div");
			containerNode.id = "settingsContainer";
			containerNode.innerHTML = htmlText;
			var formId = $(containerNode).find("form").attr("id");
			$(containerNode).find("input:submit").each(function()
				{
					if ($(this).attr("name") == "cancel")
					{
						var button = document.createElement("input");
						$(button).attr("id", $(this).attr("id"));
						$(button).attr("class", $(this).attr("class"));
						$(button).attr("type", "button");
						$(button).attr("value", $(this).attr("value"));
						$(button).attr("name", $(this).attr("name"));
						$(button).click(function()
						{
							$("#settingsPopup").dialog("close");
							return (false);
						});
						$(this).replaceWith(button);
					}
				});
			$(containerNode).find("#"+formId).bind("submit", function()
				{
					var data = $("#"+formId).serialize();
					if (!$("#"+formId).valid())
						return (false);
					var settings = 
					{
							async: false,
							data: data,
							dataType: "html",
							success: function(data)
							{
								if (data.indexOf("creditAccountForm") != -1)
									ndSettings.openCreditAccountPage(callback); 
								else 
								{
									callback();
									$("#settingsPopup").dialog("close");
								}
							},
							error: ndNewsUtils.processXHR,
							type: 'post',
							url: "index.php?controller=Settings&action=creditAccount&next=settings&cancel=settings&tmpl=xml"
					};
					jQuery.ajax(settings);
					return (false);
					
				});
			if ($("#settingsPopup").length > 0)
			{
				settingsDiv = $("#settingsPopup");
				$(settingsDiv).html('');
			}
			else
			{
				settingsDiv = document.createElement("div");
				$(settingsDiv).attr("id", "settingsPopup");
				$(settingsDiv).attr("title", settingsLocale.settings);
				$("html").css("overflow", "hidden");
				$(settingsDiv).dialog({
					height:	400,
					width:	800,
					draggable: false,
					resizable: false,
					modal: true,
					close: function(){$(settingsDiv).remove();$("html").css("overflow", "auto");}
				});
			}
			$(settingsDiv).append(containerNode);
			var title = $("#settingsPopup form").length > 0 ? $("#settingsPopup form").attr("title") : defaultTitle;
			$(settingsDiv).dialog("option", "title", title);
		},
		
		openChangeAccountPage : function(callback)
		{
			var url = "index.php?controller=Settings&action=subscribeForm&next=settings&cancel=settings&tmpl=xml";
			var settings = 
			{
					async: false,
					dataType: "html",
					success: function(data){ndSettings.procressChangeAccountPage(data, callback);},
					error: ndNewsUtils.processXHR,
					type: 'post',
					url: url
			};
			jQuery.ajax(settings);
		},
		
		procressChangeAccountPage: function(htmlText, callback)
		{
			if (!htmlText)
				return (null);
			var defaultTitle = settingsLocale.settings;
			var containerNode =  document.createElement("div");
			containerNode.id = "settingsContainer";
			containerNode.innerHTML = htmlText;
			var formId = $(containerNode).find("form").attr("id");
			$(containerNode).find("input:submit").each(function()
				{
					if ($(this).attr("name") == "cancel")
					{
						var button = document.createElement("input");
						$(button).attr("id", $(this).attr("id"));
						$(button).attr("class", $(this).attr("class"));
						$(button).attr("type", "button");
						$(button).attr("value", $(this).attr("value"));
						$(button).attr("name", $(this).attr("name"));
						$(button).click(function()
						{
							$("#settingsPopup").dialog("close");
							return (false);
						});
						$(this).replaceWith(button);
					}
				});
			$(containerNode).find("#"+formId).bind("submit", function()
				{
						var data = $("#"+formId).serialize();
						if (!$("#"+formId).valid())
							return (false);
						var settings = 
						{
								async: false,
								data: data,
								dataType: "html",
								success: function(data)
								{
									if (data.indexOf("subscribeForm") != -1)
										ndSettings.openCreditAccountPage(callback); 
									else 
									{
										callback();
										$("#settingsPopup").dialog("close");
									}
								},
								error: ndNewsUtils.processXHR,
								type: 'post',
								url: "index.php?controller=Settings&action=subscribe&next=settings&cancel=settings&tmpl=xml"
						};
						jQuery.ajax(settings);
						
					});
			if ($("#settingsPopup").length > 0)
			{
				settingsDiv = $("#settingsPopup");
				$(settingsDiv).html('');
			}
			else
			{
				settingsDiv = document.createElement("div");
				$(settingsDiv).attr("id", "settingsPopup");
				$(settingsDiv).attr("title", settingsLocale.settings);
				$("html").css("overflow", "hidden");
				$(settingsDiv).dialog({
					height:	400,
					width:	800,
					draggable: false,
					resizable: false,
					modal: true,
					close: function(){$(settingsDiv).remove();$("html").css("overflow", "auto");}
				});
			}
			$(settingsDiv).append(containerNode);
			var title = $("#settingsPopup form").length > 0 ? $("#settingsPopup form").attr("title") : defaultTitle;
			$(settingsDiv).dialog("option", "title", title);
		},
		
		/**	get html page corresponing to the action from server
		 * @param 	string	action
		 * @param	bool	getMethod
		 */
		getPageHref : function(href, method, data)
		{
			var url = href + "&next=settings&cancel=settings&tmpl=xml";
			var settings = 
			{
					async: false,
					data: data,
					dataType: "html",
					success: ndSettings.openSettingsMenu,
					error: ndNewsUtils.processXHR,
					type: method,
					url: url
			};
			jQuery.ajax(settings);
		},
		
		helperSettingsPane : function(htmlText)
		{
			if (!htmlText)
				return (null);
			var containerNode =  document.createElement("div");
			containerNode.id = "settingsContainer";
			containerNode.innerHTML = htmlText;
			var formId = $(containerNode).find("form").attr("id");
			$(containerNode).find("input:submit").each(function(){
				if ($(this).attr("name") == "cancel")
				{
					var button = document.createElement("input");
					$(button).attr("id", $(this).attr("id"));
					$(button).attr("class", $(this).attr("class"));
					$(button).attr("type", "button");
					$(button).attr("value", $(this).attr("value"));
					$(button).attr("name", $(this).attr("name"));
					$(button).click(function()
					{
							ndSettings.display("settings", "get", null);
							return (true);
					});
					$(this).replaceWith(button);
				}
			});
			$(containerNode).find("#"+formId).bind("submit", function(){ndSettings.submit(this, formId); return (false);});
				
			$(containerNode).find("a.next").each(function(){
				var url = $(this).attr("href");
				var button = document.createElement("input");
				$(button).attr("id", $(this).attr("id"));
				$(button).attr("class", "settingsbutton");
				$(button).attr("type", "button");
				$(button).attr("value", $(this).text());
				$(button).attr("name", $(this).attr("name"));
				$(button).click(function(){ndSettings.displayHref(url, "get");});
				$(this).replaceWith(button);
				});
			return (containerNode);
		},
		
		display : function(action, method, data)
		{
			var url = "index.php?controller=Settings&action=" + action;
			return (ndSettings.displayHref(url, method, data));
		},
		
		displayHref : function(href, method, data)
		{
			var page = this.getPageHref(href, method, data);
			return (true);
		},
		
		submit : function(sender, formId)
		{
			var data = $("#"+formId).serialize();
			//form is valid and user do not cancel the action ?
			if (!$("#"+formId).valid())
				return (false);
			this.displayHref($("#"+formId).attr("action"), "post", data);
			return (true);
		},
		
		getTimeZone : function(mainLand)
		{
			var xhr = ndNewsUtils.getXHR();
			if (xhr == null)
				return (false);
			var url = "index.php?controller=Settings&action=getTimeZone&mainLand="+encodeURIComponent(mainLand);
			xhr.open("GET", url, false);
			xhr.send(null);
			if (xhr.readyState == 4)
			{
				return (xhr.responseText);
			}
			return (null);
		},
		
		displayTimeZoneSelect : function()
		{
			if ( $("#timeZoneMainLand").length == 0 || $("#timeZoneCountry").length == 0)
				return (false);
			var mainLand = $("#timeZoneMainLand").val();
			if (mainLand.length == 0)
			{
				document.getElementById("timeZoneCountry").style.display = "none";
				return (false);
			}
			var timezoneList = this.getTimeZone(mainLand);
			if (timezoneList.length == 0)
			{
				document.getElementById("timeZoneCountry").style.display = "none";
				$("timeZoneCountry").val("");
				return (false);
			}
			$("#timeZoneCountry").html(timezoneList);
			document.getElementById("timeZoneCountry").style.display = "inline";
			return (true);
		},
		
		initForms	:	function()
		{
			this.initCreateAccountFormValidation();
			this.initCreateAccountFormMoreInformationValidation();
			this.initCreditAccountFormValidation();
			this.initSendInvitationMailFormValidation();
		},
		
		initLoginFormValidation : function ()
		{
			if ($("#loginForm").lenght)
				return (false);
			$("#loginForm").validate({
  			   rules: {
  			     email: 
  				   {
  			       required: true,
  			       email: true
  			     },
  			     password:
  			     {
  			    	 required: true
  			     }
  			   },
  			   messages: {
  			     email: 
  				   {
  			       required: settingsLocale.email_required,
  			       email: settingsLocale.email_incorrect_format
  			     },
  			     password: 
				   {
			       required: settingsLocale.password_required
			     }
  			   },
  			   errorPlacement: function(error, element) 
  			   {
  				 error.insertAfter(element);
  			   }});
		},
		
		initCreateAccountFormValidation : function ()
		{
			if ($("#accountForm").length == 0)
				return (false);
			$("#accountForm").validate({
  			   rules: {
  			     email: 
  				 {
  			       required: true,
  			       email: true
  			     },
  			     passwd:
  			     {
  			    	 required: true
  			     },
  			     passwdConfirm:
			     {
			    	 required: true,
			    	 equalTo : "#passwordInput"
			     }
  			   },
  			   messages: {
  			     email: 
  				   {
  			       required: settingsLocale.email_required,
  			       email: settingsLocale.email_incorrect_format
  			     },
  			     passwd:
			     {
			    	 required: settingsLocale.password_required
			     },
			     passwdConfirm:
			     {
			    	 required: settingsLocale.password_confirmation_required,
			    	 equalTo : settingsLocale.password_confirmation_incorrect
			     }
  			   },
  			   errorPlacement: function(error, element) 
  			   {
  			       error.insertAfter(element);
  			   }});
  		return (true);
		},
		
		initCreateAccountFormMoreInformationValidation : function ()
		{
			if ($("#accountFormMoreInformations").length == 0)
				return (false);
			var timezonenotcomplete = settingsLocale.incomplete_password;
			$("#accountFormMoreInformations").validate({
  			   rules: {
  				 timeZoneCountry: 
  				 {
  			       required: true
  			     }
  			   },
  			   messages: {
  				 timeZoneCountry:
			     {
			    	 required: timezonenotcomplete
			     }
  			   },
  			   errorPlacement: function(error, element) 
  			   {
  			       error.insertAfter(element);
  			   }});
  		return (true);
		},
		
		initCreditAccountFormValidation : function ()
		{
			if ($("#creditAccountForm").length == 0)
				return (false);
				$("#creditAccountForm").validate({
  			   rules: {
  				 amount: 
  				 {
  			       required: true,
  			       digits: true,
  			       min:	1
  			     }
  			   },
  			   messages: {
  				 amount:
			     {
			    	 required: settingsLocale.amount_required,
			    	 min: settingsLocale.minimum_amount,
			    	 digits: settingsLocale.incorrect_format_amount
			     }
  			   },
  			   errorPlacement: function(error, element) 
  			   {
  			       error.insertAfter(element);
  			   }});
  		return (true);
		},
		
		initSendInvitationMailFormValidation : function ()
		{
			if ($("#sendInvitationForm").length == 0)
				return (false);
				$("#sendInvitationForm").validate({
  			   rules: {
				guestemail: 
  				 {
  			       required: 	true,
  			       email:		true
  			     }
  			   },
  			   messages: {
  				 guestemail:
			     {
			    	 required: 	settingsLocale.email_required,
			    	 email:		settingsLocale.email_incorrect_format
			     }
  			   },
  			   errorPlacement: function(error, element) 
  			   {
  			       error.insertAfter(element);
  			   }});
  		return (true);
		}
};

var RecaptchaOptions = 
		{
	       theme : 'white', // 'red' | 'white' | 'blackglass' | 'clean' | 'custom'
	       tabindex : 0,
	       lang : 'fr'
	    };

