// JavaScript Document
// data Games
jQuery.data_creator.player_games = function (jElement, options) {
	this.jElement = jElement ;
	this.options = jQuery.extend({}, jQuery.data_creator.player_games.settings, options) ;
	
	if (this.options.name == '') {
		this.options.name = 'games#' + Math.random() ;	
	}
	jQuery.data_creator.player_games.stack[this.options.name]=this ;
	
	this.init() ;
}

jQuery.data_creator.player_games.prototype.init = function() {
}

jQuery.data_creator.player_games.prototype.load_info = function(_id) {
	if (_id != this.options.my_id) {
		this.jElement.portlet('disable_button', 'customize') ;	
	} else {
		this.jElement.portlet('enable_button', 'customize') ;	
	}
	var self = this ;
	
	DCMS.player_games[self.options.name].scroll.getItemWrap().state({name:'refresh'}) ;

	$.post(self.options.php_file, {id: _id, player_games: ''}, function(data) {
		try{
			var text = '' ;
			var games_ = data.getElementsByTagName('games')[0] ;
			if (games_.getElementsByTagName('game').length > 0) {
				var text = '' ;
				var games = games_.getElementsByTagName('game') ;
				for (i = 0 ; i < games.length ; i++) {
					text += '<table game_id="'+games[i].getAttribute('id')+'">' ;
						text += '<tr>' ;
							text += '<td><img src="'+games[i].getAttribute('image')+'" alt="game" height="50" width="50" /></td>' ;
							text += '<td class="font-weight-bold font-size-medium">'+games[i].childNodes[0].nodeValue+'</td>' ;
					   text += '</tr>' ;
					text += '</table>' ;
				}
				var api = DCMS.player_games[self.options.name].scroll;
				api.getItemWrap().html(text) ;
				api.reload() ;
				
				api.getItems().click(function() {
					$.addHash({game_id:$(this).attr('game_id')}) ;
					var keys = $.hashURL() ;
					DCMS.player_games[self.options.name].dialog.ajaxDialog('load', {
						owner: _id,
						game_id: keys.game_id,
						rndm: Math.random()
					}).ajaxDialog('open') ;
				}) ;
			}
			else {
				var api = DCMS.player_games[self.options.name].scroll;
				api.getItemWrap().html('') ;
				api.reload() ;
			}
		}
		catch(e) {
			jQuery.data_creator.error(self.options.name+' has thrown : '+e.description) ;	
		}
	}) ;
}

jQuery.data_creator.player_games.settings = {
	my_id:0,
	id: 0,
	php_file: 'PHPtools/fetch_info.php',
	my_info:false,
	w_index: -1,
	name: '',
	dialog_name:''
} ;

jQuery.data_creator.player_games.stack = {} ;