Vetstoria.notifications = new(new Class({
	__construct: function () {

		this.notifications = [];
		this.unread_notification = [];
		this.owner_type = "";
		this.owner_id = "";

	},
	init: function () {

		console.log("Vetstoria.notifications : init");
		//this.getNotification();

	},
	/**
	 * Grab notifications from the backend and cache them
	 */
	get: function () {
		console.log("Vetstoria.notifications : get");

		var ptr = this;

		json  = Vetstoria.json_command('notification/get');

		//$.get('notification/get', async: false, function (json) {

			if (json.state != 0) return;

			if (json.data == "null") {
				ptr.notifications = [];
				//ptr.notificationsLength = 0;
				//ptr.displayNotification();
				return;
			}

			for (var i = 0, length = json.data.length; i < length; i++) {

				if (json.data[i].read == 0) {
					ptr.unread_notification.push(json.data[i]);
				}

			}

			ptr.notifications = json.data;


			//ptr.displayNotification();
			//ptr.displayJgrowl();
			//ptr.check_new_msg();

		//});
		
		return this.notifications;
	},
	/**
	 * @deprecated : bad name 
	 */
	getNotification: function () {
		return this.get();
	},
	/**
	 * Mark all notifications as read
	 */
	markAllNotificationsAsRead: function () {
		console.log("Vetstoria.notifications : read");
		var prt = this;
		$.post("notification/read", function () {
			prt.unread_notification = [];
			
		});
	},
        /**
	 * Mark all notifications as unread
	 */
	markAllNotificationsAsUnread: function () {
		console.log("Vetstoria.notifications : read");
		var prt = this;
		$.post("notification/unread", function () {
			prt.unread_notification = [];
			
		});
	},
	/**
	 * Mark one notification as read
	 */
	markSingleNotificationsAsRead: function (id) {
		console.log("Vetstoria.notifications : read");
		var prt = this;
		$.post("notification/read/"+id, function () {
			
		});
	},
        /**
	 * Mark one notification as unread
	 */
	markSingleNotificationsAsUnread: function (id) {
		console.log("Vetstoria.notifications : unread");
		var prt = this;
		$.post("notification/unread/"+id, function () {
			
		});
	},
	/**
	 * Remove notification
	 */
	removeNotification: function (id) {
		console.log("Vetstoria.notifications : remove");
		var prt = this;
		$.post("notification/remove/"+id, function () {
			$('div#notificationfb div.cm_content[rel="'+id+'"]').hide();
		});
	},
	/**
	 * Remove notifications
	 */
	removeNotifications: function (id) {
		console.log("Vetstoria.notifications : remove");
		var prt = this;
		$.post("notification/remove", function () {
			$('div#notificationfb div.cm_content[name!="FR"]').hide();
		});
	},
	/**
	 * @deprecated : bad name 
	 */
	readNotification: function () {
		return this.read();
	},
	/**
	 * Display in the contextual menu
	 * @todo : move to vetstoria.contextualmenu !!!
	 * @deprecated : should use Vetstoria.contextualmenu instead
	 */
	display: function () {

		var $ul = $("<ul/>");
		var prt = this;
		var divWidth = 420;

		if (this.notifications.length == 0) {
			var $span = $("<span />").text("No new notifications.");
			var $read = $("<a/>").text("See all").css({
				"cursor": "pointer",
				"list-style": "none"
			}).click(function () {
				prt.readNotification();
			});

			$("div.notificationss").empty().append($span).append(
			$("<hr/>")).append(
			$("<div style='padding:0 0 5px 35px;'/>").append(
			$read));

			return;
		}

		for (var i = 0, length = this.notifications.length; i < length; i++) {

			var $li = $("<li class='clearfix'/>");
			var $div = $("<div/>").addClass("sc_menu_outer").css({
				overflow: 'hidden'
			});
			var $p = $("<p/>").addClass("sc_menu_inner");
			var $span = $("<span/>").html(this.notifications[i].content);
			var $date = $("<em/>").attr("rel", this.notifications[i].date).addClass("timestamp");
			$span.append($date);
			$p.append($span);
			$div.append($p);
			$li.append($div);

					/*
					 * if (this.notifications[i].read==0) {
					 * $li.addClass("unread"); };
					 */

			$ul.append($li);

			$div.mousemove(function (e) {
				var span = $(this).find('.sc_menu_inner > span');
				var pWidth = span[0].offsetLeft + span.outerWidth() + 100;
				var left = (e.pageX - $(this).offset().left) * (pWidth - divWidth) / divWidth;
				$(this).scrollLeft(left);

			});

		}

		if (this.notificationsLength != 0) {

			var $read = $("<a/>").text("See all").css({
				"cursor": "pointer",
				"list-style": "none"
			}).click(function () {
				prt.readNotification();
			});

			$("div.notificationss").empty().append($ul).append(
			$("<hr/>")).append(
			$("<div style='padding:0 0 5px 35px;'/>").append(
			$read));

			$("div.user_info li.notificationss").find("a").text("Notifications (" + prt.unread_notification.length + ")");
		}

		// Vetstoria.setDate();
	},
	/**
	 * @deprecated : bad name 
	 */
	displayNotification: function () {
		return this.display();
	},
	/**
	 * Popup new notifications
	 */
	popup: function () {

		$.jGrowl.defaults.pool = 1;

		var prt = this;

		for (var i = 0; i < prt.unread_notification.length; i++) {

			$("#notification").jGrowl(
			prt.unread_notification[i].content, {

				header: "notification",
				sticky: false,
				life: 3000,
				log: function () {

				},
				beforeOpen: function () {

				},
				position: "bottom-right",
				speed: "slow"

			});

		}

	},
	/**
	 * @deprecated : bad name 
	 */
	displayJgrowl: function () {
		return this.popup();
	},
	timer_check_new: function () {

		var prt = this;

		console.log("Vetstoria.notifications.timer_check_new : launched");

		setInterval(

		function () {

			console.log("Vetstoria.notifications.timer_check_new : getting new notifications");

			$.get('notification/get', function (json) {

				if (json.state != 0) return;

				if (json.data == "null") {
					prt.notifications = [];
					prt.notificationsLength = 0;
					console.log(
					prt.notifications, prt.notificationsLength);
					return;
				}

				var temp = [];

				for (var i = 0, length = json.data.length; i < length; i++) {

					if (json.data[i].read == 0) {
						temp.push(json.data[i]);
					}

				}

				if (prt.unread_notification.length != temp.length && temp.length != 0) {

					prt.notifications = json.data;
					prt.unread_notification = temp;

					$("#notification").jGrowl(
					temp[0].content, {

						header: "new notification",
						sticky: false,
						life: 3000,
						log: function () {

						},
						beforeOpen: function () {

						},
						position: "bottom-right",
						speed: "slow"

					});

					prt.displayNotification();
				}

			});

		}, 30000);
	},
	check_new_msg: function () {
		return this.timer_check_new();
	}

}));
