/**
 * TomatoCMS - http://www.tomatocms.com/
 * Copyright (c) 2008-2009 TIG Corporation (http://www.tig.vn)
 * 
 * @copyright	Copyright (c) 2008-2009 TIG Corporation (http://www.tig.vn)
 * @license		GNU GPL license, see http://www.tomatocms.com/license.txt or license.txt
 * @version 	$Id: widget.loader.js 1082 2009-11-26 08:13:24Z huuphuoc $
 */

/* ========== Registry namespace ============================================ */
'Tomato.Core.Widget.Loader'.namespace();

/* ========== Tomato.Core.Widget.Loader ===================================== */

/**
 * Tomato.Core.Widget.Loader
 * This class required plugins:
 * - ajaxq
 * - jquery.json
 */
Tomato.Core.Widget.Loader = function() {};
Tomato.Core.Widget.Loader.queue = function(module, name, data, containerId) {
	Tomato.Core.Widget.Loader.queueAction(module, name, 'show', data, containerId);
};
Tomato.Core.Widget.Loader.queueAction = function(module, name, act, data, containerId) {
	$('#' + containerId).addClass('t_g_loading').html('');
	$.ajaxq('widget', {
		url: '/core/widget/ajax/',
		data: { mod: module, name: name, act: act, params: data },
		success: function(response) {
			response = $.evalJSON(response);
			for (var i in response.css) {
				if ($('head').find('link[href="' + response.css[i] + '"]').length == 0) {
					$('<link rel="stylesheet" type="text/css" href="' + response.css[i] + '" />').appendTo('head');
				}
			}
			for (i in response.javascript) {
				if ($('body').find('script[src="' + response.javascript[i] + '"]').length == 0) {
					$('<script type="text/javascript" src="' + response.javascript[i] + '"></script>').prependTo('body');
				}
			}
			$('#' + containerId).removeClass('t_g_loading').html(response.content);
		}
	});
};

