Module_Matches = function(preset_id) {
	this.init(preset_id);
}

//static get main item
Module_Matches.__dict = {};
Module_Matches.gm = function(preset_id) {
	if (Module_Matches.__dict[preset_id]) {
		return Module_Matches.__dict[preset_id];
	}
	
	var match = new Module_Matches(preset_id);
	
	Module_Matches.__dict[preset_id] = match;
	return match;
}

Module_Matches.prototype = {
	preset_id: null,
	
	element_list: null,
	
	current_page: null,
	total_pages: null,
	
	init: function(preset_id) {
		var main;
		
		this.preset_id = preset_id;
		main = this.getMain();
		
		this.element_list = main.find('.list');
		this.element_detail = main.find('.block-detail');
		
		this.current_page = main.find('input[name=current_page]').val();
		this.total_pages = main.find('input[name=total_pages]').val();
		
		//enable or disable
		this.updatePager(true);
	},
	
	showList: function() {
		this.element_list.css('display', 'block');
		this.element_detail.css('display', 'none');
	},
	
	showMatch: function(match_id, index) {
		var self = this;
		var data = { 
				ajax: 'true',  
				op: 'match', 
				m: this.preset_id, 
				match: match_id,
				index: index
		};
		
		this.element_list.css('display', 'none');
		this.element_detail.css('display', 'block');
		this.element_detail.html('Loading...');
		
		//real load of match
		$.post(location.href, data,
				function(data){
					self.element_detail.html(data.content);
				}
			, 'json');		
	},
	
	getMain: function() {
		return $('.m_matches_'+this.preset_id);
	},
	
	nextPage: function() {
		if (this.current_page < this.total_pages) {
			this.current_page++;
			
			this.loadPage();
			this.updatePager();
		}
	},
	
	previousPage: function() {
		if (this.current_page > 1) {
			this.current_page--;
			
			this.loadPage();
			this.updatePager();
		}		
	},
	
	getPage: function(page) {
		page = Math.max(1, page);
		
		this.current_page = page;
		this.loadPage();
		this.updatePager();
	},
	
	goItemPage: function(element) {
		var pd = $(element).closest('.pager').find('.cp input').val();
		pd = parseInt(pd);
		
		if (!isNaN(pd)) {
			pd = Math.max(1, pd);
			pd = Math.min(pd, this.total_pages);
			
			this.current_page = pd;
			this.loadPage();
		}
		
		this.updatePager();
	},
	
	loadPage: function() {
		var main = this.getMain();
		
		//do through ajax
		$.post(location.href, { ajax: 'true',  op: 'page', m: this.preset_id, page: this.current_page },
				function(data){
					main.find('.list .block-wrapper').html(data.content);
				}
			, 'json');
	},
	
	updatePager: function(from_init) {
		var main = this.getMain();
		
		if (!from_init)
			this.element_list.find('.pager .cp input').val(this.current_page);		
		
		if (this.current_page < this.total_pages) {
			this.element_list.find('.button-next').removeClass('button-next-disabled');			
		} else {
			this.element_list.find('.button-next').addClass('button-next-disabled');
		}
		
		if (this.current_page > 1) { 
			this.element_list.find('.button-previous').removeClass('button-previous-disabled');			
		} else {
			this.element_list.find('.button-previous').addClass('button-previous-disabled');
		}
	},
	
	
	postComment: function(match_id, element) {
		var self = this;
		var main_area = $(element).closest('.block-container');
		var textarea = main_area.find('textarea');
		var comment = jQuery.trim(textarea.val());
		
		if (comment == '') {
			alert('You must write a comment');
		} else {
			var data = { 
				ajax: 'true',  
				op: 'addcomment', 
				m: this.preset_id, 
				match: match_id,
				comment: comment,
				guest: 'Dummy Guest'
			};
			
			$.post(location.href, data, 
				function(response) {
					if (response.error.length > 0) {
						alert(response.error.join("\n"));
					} else {
						//add comment
						var content_area = $('.match-body-'+match_id+' .block-comments');
						
						var total_comments = response.total_comments;
						var cid = response.cid;						
						var comment = $(response)
						var element = $(response.comment);
						
						content_area.find('.number-comments').html(response.total_comments);						
						content_area.find('.items').append(element);
						element.hide();
						element.slideDown(300);
						
						//empty area
						textarea.val('');
					}
				}, 
			'json');
		}
	}
}



