(function(jQuery) {																	// Compliant with jquery.noConflict()

jQuery.fn.mcPopularList = function(o) {

	jQuery.require("js/v2/template/jquery.template-min.js");

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

	// Local data
	var result = {};
	var context = {};
	var channel_list = [];
	var mcCont;
	var cid;
//	var url			=  "cache/json/content__" + data.cid + ".json.txt";

	var t_channel = (o.tplSelector) ? 
				jQuery.template(jQuery(o.tplSelector).html) : 
				jQuery.template(o.tpl);


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

		mcCont = this;

																			// close Bind
    });																			// close Each

/* ------------------------------- loadData --------------------------------- */
	function loadData(params) {
		callbackOK		= params.success || null;
		callbackError	= params.error || null;

		if (o.onLoading) o.onLoading();

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

		if (context.cid==null) cid=0;
		else cid = context.cid;
		// Load JSON Data as String
		jQuery.ajax({
			type: "GET",
			url: o.url + "?id=" + cid + "&nb=" + o.ipp,
			dataType: "json",
			timeout: 30000,
			success: function (data) {
//DP_Debug.dump(data, 'Popular List: loadData: data', true, 3);
				o.items = data.items;
				if (callbackOK) callbackOK(o.items);
			},																	// close Success

			error: function (XMLHttpRequest, textStatus, errorThrown) {
				if (callbackError) callbackError(textStatus, errorThrown);
			}																	// close Error
		});																		// close $.ajax
	}

/* ------------------------------- makeList --------------------------------- */
	function makeList() {
		jQuery(mcCont).empty();

		// Add every channel to the list
		jQuery.each(o.items, function(i, item) {
//DP_Debug.dump(item, 'Popular List: makeList: item', true, 3);
			item.index = i;
			var toto = jQuery(t_channel.apply(item));
			jQuery('a', toto).click(function(e) {
				context.vid = o.items[i].guid;
				context.video = o.items[i];
				jQuery(document).trigger('video.selected', context);
				if (o.onClick) 
					return o.onClick(context);
				else
					return true;
			});

			jQuery(mcCont).append(toto);
		});																		// close each
		jQuery('.mcVideo', mcCont)
			.mouseover(function (){
				jQuery(this).addClass("selected");
			})
			.mouseout(function () {
				jQuery(this).removeClass("selected");
			});

	}																			// close makeList()

};

/* ============================= Default Values ============================= */
jQuery.fn.mcPopularList.defaults = {
	url			: "getPopularJson.php",

	tpl			: '<div class="mcPopItem"><table><tr><td class="mcPopImg"><a href="?vid=${guid}"><img src="http://www.sportweek.fr/commun/n80x60/${img}" class="mcVideoThumb" width="80" height="60"></a></td><td class="mcPopInfos"><a href="?vid=${guid}"><div class="title">${title}</div></a><div class="rating rank${rank}"></div><a href="?vid=${guid}" class="more">Voir &raquo;</a></td></tr></table></div>',

	ipp			: 6,															// Items per page
	items		: {},

	tplSelector	: null,

	onLoading	: null,
	onComplete	: null,
	onError		: null,
	onClick		: null,

	thumbPath	: "http://www.sportweek.fr/commun/n80x60/"
};

})(jQuery);
