/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, brackets, $ */

define(function (require, exports, module)
{
    "use strict";
	
	// Load brackets modules
	var ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
		CommandManager = brackets.getModule("command/CommandManager");

	// Load stylesheet
	ExtensionUtils.loadStyleSheet(module, "brackets-notifications.css");

	// All notifications are appended to this container
	$(".main-view .content #editor-holder").append("<div id='notifications-container'></div>");
	
	// Adds the notification to the notifications container
	function constructNotification(input)
	{
		console.log("Construct Notification");
		
		// ReturnData contains the notification element and the action elements for binding events to
		var returnData = {};
		
		// Define defaults
        var defaults = 
        {
            title:"",
            message:"",
            time:2000
        }
        // Merge missing defaults into input
        for(var prop in defaults)
        {
            input[prop] = input[prop] !== undefined? input[prop] : defaults[prop];
        }
    
		// Contruct and append notification
		var notification = $("<div class='notification'><h3>"+input.title+"</h3><p>"+input.message+"</p></div>");
        if(input.actions !== undefined)
		{
			var actions = $("<div class='actions'></div>");
			returnData.actions = [];
			for(var i=0; i<input.actions.length; i++)
			{
				var action = $("<div class='action'>"+input.actions[i].label+"</div>");
				if(input.actions[i].callback !== undefined)
				{
					action.click(input.actions[i].callback);
				}
				actions.append(action);
				returnData.actions[input.actions[i]] = action;
			}
			notification.append(actions);
		}
		notification.click(clickHandler);
		$("#notifications-container").append(notification);
        returnData.notification = notification;
		
		// Fade if time is set
		if(input.time !== 0)
        {
            notification.delay(input.time).fadeOut();
        }
		
		return returnData;
	}
    
    // First, register a command - a UI-less object associating an id to a handler
    var NOTIFICATION_COMMAND_ID = "notifications.notification";   // package-style naming to avoid collisions      TODO: Check whether this is not crazy naming..
    CommandManager.register("Send Notification notification", NOTIFICATION_COMMAND_ID, constructNotification);

    // Handle clicks.
    function clickHandler(e)
    {
        $(this).fadeOut();
    }
});

@@ -1213,16 +1213,56 @@
 ication%0A
+%09%09// Save options to the element's data%0A
 %09%09var no
@@ -1359,16 +1359,28 @@
 %3C/div%3E%22)
+.data(input)
 ;%0A      
@@ -1472,36 +1472,8 @@
 %22);%0A
-%09%09%09returnData.actions = %5B%5D;%0A
 %09%09%09f
@@ -1644,24 +1644,57 @@
 ined)%0A%09%09%09%09%7B%0A
+%09%09%09%09%09action.click(clickHandler);%0A
 %09%09%09%09%09action.
@@ -1765,59 +1765,8 @@
 n);%0A
-%09%09%09%09returnData.actions%5Binput.actions%5Bi%5D%5D = action;%0A
 %09%09%09%7D
@@ -1839,16 +1839,99 @@
 ndler);%0A
+%09%09if(input.clicked !== undefined)%0A%09%09%7B%0A%09%09%09notification.click(input.clicked);%0A%09%09%7D%0A%09%09%0A
 %09%09$(%22#no
@@ -1980,56 +1980,8 @@
 n);%0A
-        returnData.notification = notification;%0A
 %09%09%0A%09
@@ -2113,26 +2113,28 @@
 %09return 
-returnData
+notification
 ;%0A%09%7D%0A   
@@ -2512,16 +2512,43 @@
 e clicks
+ fired by notif and actions
 .%0A    fu
@@ -2571,17 +2571,16 @@
 ler(
-e
 )%0A    %7B%0A
     
@@ -2575,24 +2575,169 @@
 )%0A    %7B%0A
-        
+%09%09if($(this).hasClass('action'))%0A%09%09%7B%0A%09%09%09$(this).parents('.notification').fadeOut();%09%09// TODO: prevent parents() from going to far up the DOM%0A%09%09%7Delse%7B%0A%09%09%09
 $(this).
@@ -2743,22 +2743,26 @@
 .fadeOut();%0A
+%09%09%7D%0A
     %7D%0A%7D);%0A

Handle clicks fired by both actions and notif correctly