/**
 * jQuery DisplayConfig
 * Version 1.0.0 (18/11/2009)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2009 Michel Meyer
 */
var DisplayConfig = function(displayer, url, options) {
	this.displayer = displayer;
	this.url       = url;

	this.initializeOptions(options);
};

DisplayConfig.prototype.initializeOptions = function(options) {
	var displayer = this.displayer;

	var defaultOptions = {
		ajax : {
			complete: function(XMLHttpRequest, message) {
				if (message == 'success') {
					var oldBsElts = displayer.children('*[class*="dispcfg"]');
					var newBsElts = jQuery('<div>' + XMLHttpRequest.responseText + '</div>').children('*[class*="dispcfg"]');
					if (oldBsElts.length > 0 && newBsElts.length > 0) {
						displayer.append(XMLHttpRequest.responseText);
					}
					else {
						// FIXME Removed replaced player only
						// displayer.children('*[class*="dispcfg"]').remove();
						if (oldBsElts.length > 0)
							players.removePlayer();
						displayer.html(XMLHttpRequest.responseText);
					}
				}
			},
			type:         'POST',
			cache:        true,
			contentType:  'application/x-www-form-urlencoded',
			dataFilter:   function(data, type){},
			dataType:     'html'
		},
		displayConfig:    {},
		post:             {}
	};

	this.options = jQuery.extend(defaultOptions, options);
	this.addDisplayConfig(this.options.displayConfig);
};

DisplayConfig.prototype.execute = function() {
	var displayer = this.displayer;
	var options   = this.options;
	var data      = jQuery.extend({'query' : this.displayConfig}, this.options.post);
	var parent    = this;

	jQuery.ajax({
		url: this.url,
		data: jQuery.urlSerialized(data).join('&'),
		type: this.options.ajax.type,
		beforeSend: function() {
			displayer.trigger('beforeExecute', { displayConfig: parent, displayer: this.displayer });
		},
		error: function() {
			displayer.trigger('error', { displayConfig: parent, displayer: this.displayer });
		},
		complete: function(XMLHttpRequest, message, a, b){
			options.ajax.complete(XMLHttpRequest, message, displayer);
			displayer.trigger('afterExecute', { displayConfig: parent, displayer: this.displayer });
		},
		cache:       this.options.ajax.cache,
		contentType: this.options.ajax.contentType,
		dataFilter:  this.options.ajax.dataFilter,
		dataType:    this.options.ajax.dataType
	});

	jQuery.urlSerialized(data);

	return this;
};


DisplayConfig.prototype.addPost = function(post) {
	this.options.post = jQuery.extend(this.options.post, post);
	return this;
};

DisplayConfig.prototype.addDisplayConfig = function(displayConfig) {
	this.displayConfig = jQuery.extend(this.displayConfig, displayConfig);
	return this;
};

DisplayConfig.prototype.removeDisplayConfigParameter = function(name) {
	for (parameterName in this.displayConfig) {
		if (parameterName == name) {
			delete this.displayConfig[parameterName];
		}
	}

	return this;
};

DisplayConfig.prototype.removePostParameter = function(name) {
	for (parameterName in this.options.post) {
		if (parameterName == name) {
			delete this.options.post[parameterName];
		}
	}

	return this;
};



DisplayConfig.prototype.getDisplayConfigParameter = function(name) {
	return this.displayConfig[name] ? this.displayConfig[name] : null;
};

DisplayConfig.prototype.getPostParameter = function(name) {
	return this.options.post[name] ? this.options.post[name] : null;
};

DisplayConfig.prototype.hasDisplayConfigParameter = function(name) {
	return this.displayConfig[name] ? true : false;
};

DisplayConfig.prototype.hasPostParameter = function(name) {
	return this.options.post[name] ? true : false;
};

DisplayConfig.prototype.getDisplayConfig = function() {
	return this.displayConfig;
};

DisplayConfig.prototype.setDisplayConfig = function(displayConfig) {
	this.displayConfig = displayConfig;
	return this;
};

DisplayConfig.prototype.getPost = function() {
	return this.options.post;
};

DisplayConfig.prototype.setPost = function(post) {
	this.options.post = post;
	return this;
};


/**
 * extend jQuery Methods
 */
jQuery.fn.extend({
	displayConfig : function (url, options) {
		var target = this;
		var generatedDisplayConfig = jQuery(target).children('.display-config-handler');

		if (generatedDisplayConfig.length > 0 && !jQuery(target).hasBsData('displayConfig')) {
			target = generatedDisplayConfig;
		}

		if (!jQuery(target).hasBsData('displayConfig')) {
			jQuery(target).setBsData('displayConfig', new DisplayConfig(this, url, options));
		}
		return jQuery(target).getBsData('displayConfig');
	}
});

