var ndNewsAlert = 
{
		_alertId : null,
		
		setId : function(alertId)
		{
			this._alertId = alertId;
			return (this);
		},

		getId : function()
		{
			return (this._alertId);
		},

		deleteAlert : function()
		{
			if (this.getId() == null)
				return (false);
			var  url = "index.php?controller=NewsAlert&action=deleteAlert&alertId=" + this.getId();
			jQuery.ajax({
				url: url,
				success: function ()
				{
					ndNewsUtils.confirm(ndNewsUtils.getTranslation("Are you sure?"),
										function()
										{
											$("#alert-" + ndNewsAlert.getId()).remove();
											return (true);
										});
				},
				error: function (xhr)
				{
					if (xhr.status == 404)
					{
						ndNewsUtils.alertTranslation("alert does not exist");
					}
					ndNewsUtils.alertTranslation("An error occurs while deleting alert");
				}
			});
		},
		
		changeName : function(newName)	
		{
			var xhr = ndNewsUtils.getXHR();
			url = "index.php?controller=NewsAlert&action=changeAlertName&alertId="+encodeURIComponent(ndNewsAlert.getId())+"&newName="+encodeURIComponent(newName);
			xhr.open("GET", url, false);
			xhr.send(null);
			if (xhr.readyState == 4)
			{
				if (xhr.responseXML && $(xhr.responseXML.getElementsByTagName("status")[0]).text() == "OK")
					return (true);
				else
					ndNewsUtils.alertTranslation("An error occured");
			}
			return (false);
		},
		
		activate : function(state)
		{
			var xhr = ndNewsUtils.getXHR();
			url = "index.php?controller=NewsAlert&action=changeAlertState&alertId="+encodeURIComponent(ndNewsAlert.getId())+"&newState="+encodeURIComponent(state ? "true" : "false");
			
			jQuery.ajax({
				url : url,
				success: function()
				{
					var activated = $("#alert-" + ndNewsAlert.getId() + " .action").hasClass("activated");
					if (activated)
					{
						var translation = ndNewsUtils.getTranslation("Activate");
						var btn = document.getElementById("btnTest-" + ndNewsAlert.getId());
						$("#alert-" + ndNewsAlert.getId() + " .action .btBox").text(translation);
						$("#alert-" + ndNewsAlert.getId() + " .action").removeClass("activated");
						$("#alert-" + ndNewsAlert.getId() + " .action").addClass("desactivated");

					}
					else
					{
						var translation = ndNewsUtils.getTranslation("Desactivate");				
						var btn = document.getElementById("btnTest-" + ndNewsAlert.getId());
						$("#alert-" + ndNewsAlert.getId() + " .action .btBox").text(translation);
						$("#alert-" + ndNewsAlert.getId() + " .action").removeClass("desactivated");
						$("#alert-" + ndNewsAlert.getId() + " .action").addClass("activated");

					}
				},
				error: function()
				{
					ndNewsUtils.alertTranslation("An error occured");
				}
			});
			return (null);
		},
		
		quickCreation : function(alertSearchKey)
		{
			var xhr = ndNewsUtils.getXHR();
			url = "index.php?controller=NewsAlert&action=quickCreation&keywords="+alertSearchKey+"&" + $('#sourcelist input:checked').serialize();
			xhr.open("GET", url, false);
			xhr.send(null);
			if (xhr.readyState == 4)
			{
				if (xhr.responseXML && $(xhr.responseXML.getElementsByTagName("status")[0]).text() == "OK")
				{
					ndNewsUtils.alertTranslation("The alert has been create");
					return (true);
				}
				else
					ndNewsUtils.alertTranslation("An error occured");
			}
			return (null);
		},
		
		onHashChange : function()
		{
			var type = window.location.hash.split('-')[0];
			type = type.substr(1,type.length - 1);
			var id = window.location.hash.split('-')[1];
			if (type == "alert")
			{
				$(".alertContainer").hide();
				$("#headerNewsAlertName").fadeOut();
				$("#headerNewsAlertName").text($("#alert-"+ id +" .alertTitle").text());
				var boxDimension = UserNewsPaper.computeTextSize($('#headerNewsAlertName').text(), $('#headerNewsAlertNameBox').get(0), 20);
				if (boxDimension != null)
	            {
					$('#headerNewsAlertName').css("fontSize", boxDimension.fontSize); 
					$('#headerNewsAlertName').css("paddingTop", boxDimension.paddingTopBottom); 
					$('#headerNewsAlertName').css("paddingBottom", boxDimension.paddingTopBottom);
					$("#headerNewsAlertName").fadeIn();
	            }
					$("#alert-"+ id).show();
			}
			return (true);
		}
};
