/*
 * Depends:
 *	ui.core.js
 *	ui.draggable.js
 *	ui.resizable.js
 */
(function($) {

$.widget("dcms.teams_games", {
	_init:function() {
		var self = this ;
		self._getData('scrollable_api').getItems().click(function() {
			self._trigger('thumb_click', 0, $(this).attr('game_id')) ;
		}) ;
	},
	load:function(team_id) {
		var self = this ;
		$.ajax({
			type:'POST',
			url:self._getData('data_dir')+'teams/games/fetch.php',
			data: {team_id:team_id},
			success:function(data) {
				var games = data.getElementsByTagName('input') ;
				var text = '' ;
				for (i = 0 ; i < games.length ; i++) {
					text += '<table game_id="'+games[i].getAttribute('id')+'">' ;
						text += '<tr>' ;
							text += '<td title="'+games[i].childNodes[0].nodeValue+'"><img src="'+self._getData('js_root')+games[i].getAttribute('image')+'" alt="" height="'+self._getData('image_height')+'" width="'+self._getData('image_width')+'" /></td>' ;
						text += '</tr>' ;
					text += '</table>' ;
				}
				var api = $('tr > td > div.scrollable', self.element).scrollable() ;
				if (api.getItems().length > 0) {
					api.getItemWrap().fadeOut(250, function() {
						var self_ = self ;
						api.getItemWrap()
							.html(text)
							.fadeIn(250, function() {
								api.reload() ;
								api.getItems().click(function() {
									self_._trigger('thumb_click', 0, $(this).attr('game_id')) ;
								}) ;
							});
					}) ;
				} else {
					api.getItemWrap().html(text);
					api.getItemWrap().fadeIn(250, function() {
						var self_ = self ;
						api.reload() ;
						api.getItems().click(function() {
							self_._trigger('thumb_click', 0, $(this).attr('game_id')) ;
						}) ;
					}) ;
				}
			}
		}) ;
	}
}) ;

//$.dcms.teams_games.getter = "getUrl";

$.extend($.dcms.teams_games, {
	defaults: {
		image_width:40,
		image_height:40,
		data_dir:'',
		js_root:'./',
		scrollable_api:null
	},
});

})(jQuery);
