﻿// Class AjaxPane

// snippet: String - the path to the snippet that is being updated
// pane: Selector - the pane div that contains and updates the snippet
// params: Object Literal - set of query string params to be passed into snippet
// slug: String - to be able to refer to this object from other objects

BenefulMap.AjaxPane = Class.extend({
	init: function(snippet, pane, params, slug) {
        this.snippet = snippet;
        this.pane = pane;
        this.params = params;
        this.slug = slug;
        
        this.populateListener();
        this.populatedListener();
        this.ajaxListener();
	},
	getSnippet: function() {
	   return this.snippet;
	},
	setSnippet: function(snippet) {
	   this.snippet = snippet;
	},
	getPane: function() {
	   return this.pane;
	},
	setPane: function(pane) {
	   this.pane = pane
	},
	getParams: function() {
	   return this.params;
	},
	setParams: function(params) {
	   this.params = params;
	},
	extendParams: function(additionalParams) {
		jQuery.extend(this.params, additionalParams);
	},
	getSlug: function() {
	   return this.slug;
	},
	setSlug: function(slug) {
	   this.slug = slug;
	},
	populate: function() {
		var self = this;
		$.ajax({
			type: "GET",
			cache: false,
			url: self.getSnippet(),
			data: self.getParams(),
			success: function(data, textStatus) {
				$(self.getPane()).html(data);

				$(document).trigger( 'populated.' + self.getSlug());
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				var msg = textStatus + " loading: " + BenefulMap.Constants.Snippets.PANE_RESULTS;
			},
			dataType: "html"
		});	
	},
	populateListener: function() {
	  var self = this;
	  $(document).bind('populate.' + this.getSlug(), function(event) {
	      self.populate();
  	  });
	},
	populatedListener: function() {
	   var self = this;
	   $(document).bind('populated.' + this.getSlug(), function(event) {
	       console.info("Pane " + self.getSlug() + " populated");
   	  });
	},
	ajaxListener: function() {
    	$(this.getSnippet()).bind("ajaxSend", function() {
            // TODO BEHAVIOR
    	}).bind("ajaxComplete", function() {
    		// TODO END BEHAVIOR
    	})
	}
});


