(function(jQuery) {                                          // Compliant with jquery.noConflict()
jQuery.fn.mcChannelList = function(o) {

// Uncomment line below if you do not use jquery.mediacenter.js
//	jQuery.require("js/v2/_src/jquery.ram.js"); 
	jQuery.require("js/v2/template/jquery.template-min.js");

	// Default values
	o = jQuery.extend(jQuery.fn.mcChannelList.defaults, o || {});

	// Local data
	var channel_list = [];

	var context = {};
	// Templates
	var t_channel = (o.tplSelector) ? jQuery.template( jQuery(o.tplSelector).html ) : jQuery.template( o.template );

	var mcCont;

	return this.each(function() {		// Returns the element collection. Chainable.
		mcCont = this;

		jQuery(document).unbind("load/channelList").bind("load/channelList", function(event, data) {

//DP_Debug.dump(data, '-->load/channelList: data', true, 3);
			// Update context
			context = jQuery.extend(context, data || {});
//DP_Debug.logWarning("mcChannelList: ON load");
			loadData();
		});

    });	// close each


	// Private functions

	function loadData() {

//DP_Debug.logInfo('channelList: loadData');

//		if (jquery.ram && jquery.ram.get('channelList')) {
//DP_Debug.dump(jQuery.ram.get('channelList'), 'CHANNEL DATA in RAM', true, 3);
		if (jQuery.ram.get('channelList')) {
//DP_Debug.logInfo('channelList: jQuery.ram.get');
			// Use Data in cache
			ramData = eval(jQuery.ram.get('channelList'));

//			context.channelName = ramData.title;
			channel_list = ramData.items;
			context.channelCount = channel_list.length;
			delete ramData;

			makeList();

		} else {
//DP_Debug.logWarning('channelList: NO $.ram.get');
			// No data in cache: AJAX Request
			if (o.onLoading) o.onLoading(mcCont);

//	DP_Debug.logger("Loading Ajax data", "Info");

			// Display Spinning Wheel
			jQuery(mcCont).html('<div class="spbDistance"></div><div class="spbContainer"></div>');

			// Load JSON Data as String
			jQuery.ajax({
				type		: "GET",
				url			: o.listUrl,
				dataType	: "text",
				timeout		: 10000,
				success		: function (data) {
//alert('titi');
					// Save Data in session
					jQuery.ram.set('channelList', data);

					// USE RAM function
					channel_list = eval(data);
					context.channel_name = channel_list.title;
					channel_list = channel_list.items;
					context.channelCount = channel_list.length;

					makeList();

				}, //close Success

				error: function (XMLHttpRequest, textStatus, errorThrown) {
					jQuery(mcCont).empty();
					if (o.onError) {
						o.onError(XMLHttpRequest, textStatus, errorThrown);
					} else {
						jQuery(mcCont).html('Error loading channel list: '+textStatus + '\n' + errorThrown + '\n'
							+ unescape( o.listUrl)
						);
					}
				}			//close Error
			});				//close $.ajax
		}
	}						//close loadData


	function makeList() {
//DP_Debug.dump(context, 'channelList::makeList: context', true, 3);
		jQuery(mcCont).empty();
		// Add every channel to the list
		jQuery.each(channel_list, function(i, item) {
			jQuery(mcCont).append( t_channel , item);
			// Get Current Channel Name
			if (context.cid == item.id) {
				context.channelName = item.title;
			}
		}); //close each
		jQuery(document).trigger("channelList.loaded", context);

		if (o.onComplete && typeof(o.onComplete == "function")) {
			o.onComplete(mcCont, context);
		}
	}						// close makeList()


};							// close mcChannelList

jQuery.fn.mcChannelList.defaults = {

// Dynamic cache url
//	listUrl				: "mc_getListJson.php?id=54",
// Static cache url
	listUrl				: "cache/json/ch__54.json.txt",

	channel_id			: null,
	onLoading			: null,
	onComplete			: null,
	template			: '<div id="mc_channel_${id}" class="mcChannelName">${title} (${id})</div>',
	tplSelector			: null
};


})(jQuery);