(function($) {
  // Shell for your plugin code
  $.ajaxModalWindow = function(options) {
	  options = $.extend($.ajaxModalWindow.defaults,options);
	// Functions to manipulate the modal window
		var fx = {

		        // Checks for a modal window and returns it, or
		        // else creates a new one and returns that
		        "initModal" : function() {
		                // If no elements are matched, the length
		                // property will return 0
		                if ( $(".modal-window").length==0 )
		                {
		                    // Creates a div, adds a class, and
		                    // appends it to the body tag
		                    return $("<div>")
		                            .hide()
		                    		.addClass("modal-window")
		                            .appendTo("body");
		                }
		                else
		                {

		                    // Returns the modal window if one
		                    // already exists in the DOM
		                    return $(".modal-window");
		                }
		            },

		            // Adds the window to the markup and fades it in
		            "boxin" : function(data, modal) {
		                    // Creates an overlay for the site, adds
		                    // a class and a click event handler, then
		                    // appends it to the body element
		                    $("<div>")
		                        .hide()
		                        .addClass("modal-overlay")
		                        .click(function(event){
		                                // Removes event
		                                fx.boxout(event);
		                            })
		                        .appendTo("body");
		                    // Loads data into the modal window and
		                    // appends it to the body element
		                    modal
		                        .hide()
		                        .append(data)
		                        .appendTo("body");

		                    // Fades in the modal window and overlay
		                    $(".modal-window,.modal-overlay")
		                        .fadeIn("slow");
		                },

				// Fades out the window and removes it from the DOM
			    "boxout" : function(event) {
			            // If an event was triggered by the element
			            // that called this function, prevents the
			            // default action from firing
			            if ( event!=undefined )
			            {
			                event.preventDefault();
			            }
					
			            // Fades out the modal window, then removes
			            // it from the DOM entirely
			            $(".modal-window,.modal-overlay").fadeOut("slow", function() {
			                        $(this).remove();
			                    }
			                );
			        },
		            "buildModalWindow" : function(options) {
		                	// Pulls up department in a modal window
		                	$("form :input" + options.clickBindSelect).live("click", function(event){
		                		
		                	    // Stops the form from loading
		                		event.preventDefault();
		                		    //    console.log( $(this).attr('name') );
		                		        // get action/id string off the name attr
		                		       var action = $(this).attr('name');
		                		       var processFile = $("form" + options.formActionSelect).attr('action');
		                		        
		                		       modal = fx.initModal();
		                		        // Creates a button to close the window
		                		        $("<a>")
		                		            .attr("href", "#")
		                		            .addClass("modal-close-btn")
		                		            .button({
		                		                icons: {
		                		                    primary: "ui-icon-circle-close"
		                		                },
		                		                text: false
		                		            })
		                		            .html("&times;")
		                		            .click(function(event){
		                		                        // Removes modal window
		                		                        fx.boxout(event);
		                		                    })
		                		            .appendTo(modal);
		                		        
		                		     // Loads the department data from the DB
		                		     $.ajax({
		                		            type: "GET",
		                		            url: processFile,
		                		            data: "AjaxRequest&action=" + action,
		                		            success: function(data){
		                	                    fx.boxin(data, modal);
		                		            },
		                		            error: function(msg) {
		                		                console.log( msg );
		                	                    modal.append(msg);
		                	                }
		                	       });

		                	});
		                }

		    };
		if($(".modal-close-btn").length==0) {
			fx.buildModalWindow(options);
		}
  }
  $.ajaxModalWindow.defaults = {
		  "clickBindSelect" : null,
		  "formActionSelect" : null
  };
})(jQuery);
