function CountLeft(field, count, max) {
	// if the length of the string in the input field is greater than the max value, trim it 
	if (field.value.length > max)
		field.value = field.value.substring(0, max);
	else // calculate the remaining characters  
		count.value = max - field.value.length;
}
var blogBox = Class.create({
  initialize: function(params) {
    this.params = params || {};    
		this.wrapperEl = $(params['wrapper']);
		this.template = '';
		if (params['template'])
			this.template = params['template'];
		if (!this.params['skipLoad'])
			this.load();
		else
			this.initElements();
  },
  initElements: function() {
  	try {
	  	this.leagueSelectEl = null;
	  	this.wrapperEl.select('#blog_league_id').each(function(el) { 
	  		this.leagueSelectEl = el;
	  		this.leagueSelectEl.observe('change',this.onLeagueChanged.bind(this));
	  	}.bind(this));
	  	this.blogPostsHolderEl = null;
	  	this.wrapperEl.select('#blogPostsHolder').each(function(el) {this.blogPostsHolderEl = el}.bind(this));
	  	this.messageEl = null;
	  	this.wrapperEl.select('#message').each(function(el) { this.messageEl = el}.bind(this));
	  	this.postButtonEl = null;
	  	this.wrapperEl.select('#postButton').each(function(el) { this.postButtonEl = el}.bind(this));
	  	if (this.postButtonEl) {
	  		this.postButtonEl.stopObserving('click');
			if (this.params['observePostButtonClick']) {
				this.postButtonEl.observe('click',this.onPostButtonClick.bind(this));
				this.postButtonEl.observe('keyup',function(ev) {
					var unicode = ev.keyCode? ev.keyCode : ev.charCode
					if (unicode == 13) { // Enter
						this.onPostButtonClick.bind(this);
					}

				}.bind(this));
			}
		  }
		  this.wrapperEl.select('[action-name=report]').each(function(el) {
		  	el.stopObserving('click');
		  	el.observe('click',this.onReportClick.bind(this,el));
		  }.bind(this));
	  	this.wrapperEl.select('form').each(function(el) {
	  		this.formEl = el;
	  		//this.formEl.observe('submit',this.onFormSubmit.bind(this));
	  		this.formEl.onsubmit = this.onFormSubmit.bind(this);
	  	}.bind(this));
		 } catch(e) {}
  },
  load: function(params) {
  	var params = params || {};
  	
  	clearTimeout(this.newerLoadingTS);
  	
  	var parameters = {
  		action: !params['newer'] ? 'ajax_box' : 'ajax_newer'
  	};
  	if (this.leagueSelectEl && this.leagueSelectEl.value)
  		parameters['data[league_id]'] = this.leagueSelectEl.value;
  	else if (this.params['league_id'])
  		parameters['data[league_id]'] = this.params['league_id'];
  	if (this.params['hideLeagueSelect'])
  		parameters['data[hide_league_select]'] = 1;
  	if (this.params['hideSeasonSelect'])
  		parameters['data[hide_season_select]'] = 1;
	if (this.params['loginNewWindow'])
  		parameters['data[login_new_window]'] = 1;
  	parameters['data[template]'] = this.template;
  	if (params['newer'])
  		parameters['data[max_id]'] = this.maxId;

  	var url = affiliate_web_root+'blog.php';
	  new Ajax.Request(url, {
			parameters: parameters,
			method: 'get',
		  asynchronous: true,
		  onComplete:function(request) {
		  	var json, i;
		    eval('json = ' + request.responseText);
		    
		    if (params['newer']) {
		    	if (json['html'] && json['html'] != '') {
			    	this.blogPostsHolderEl.insert({
			    		top: json['html']
			    	});
			    }
		    } else {
			    this.wrapperEl.innerHTML = json['html'];
			  }
		    if (json['lastId']) this.lastId = json['lastId'];
		    if (json['maxId']) this.maxId = json['maxId'];
		    this.initElements();
		    
		    this.newerLoadingTS = this.newer.bind(this).delay(5);
			}.bind(this)
		});
  },
  newer: function(params) {
  	var params = params || {};
  	this.load({
  		maxId: this.maxId,
  		newer: true
  	});
  },
  post: function(params) {
  	var params = params || {};
  	
  	var parameters = {
  		action: 'ajax_box_post'
  	};
  	if (this.leagueSelectEl && this.leagueSelectEl.value)
  		parameters['data[league_id]'] = this.leagueSelectEl.value;
  	else if (this.params['league_id'])
  		parameters['data[league_id]'] = this.params['league_id'];
  	if (this.params['hideLeagueSelect'])
  		parameters['data[hide_league_select]'] = 1;
  	if (this.params['hideSeasonSelect'])
  		parameters['data[hide_season_select]'] = 1;
  	if (this.params['loginNewWindow'])
  		parameters['data[login_new_window]'] = 1;
  	if (this.messageEl) {
  		parameters['data[message]'] = this.messageEl.value;
  		if (parameters['data[message]'] == 'Type your message here...') parameters['data[message]'] = '';
  	}
  	parameters['data[template]'] = this.template;

  	var url = affiliate_web_root+'blog.php';
	  new Ajax.Request(url, {
			parameters: parameters,
			method: 'get',
		  asynchronous: true,
		  onComplete:function(request) {
		  	var json, i;
		    eval('json = ' + request.responseText);

				if (!json['logged']) {
					if (this.params['loginNewWindow']) {
						location = 'http://www.totelfootball.com/affiliate/totelfootball/user.php?action=register';
					} else {
						alert('You must log in to post message.');
					}
					return;
				}
				
				if (json['blocked']) {
					alert('Your account is blocked');
					return;
				}
				
				if (!json['havebets']) {
					alert('You must place a bet first');
					return;
				}
				
		    if (json['html']) {
			    this.wrapperEl.innerHTML = json['html'];
			    if (json['lastId']) this.lastId = json['lastId'];
			    if (json['maxId']) this.maxId = json['maxId'];
			    this.initElements();
				}
				
			}.bind(this)
		});
  },
  report: function(postId,login) {
  	if (confirm('Are you sure you want to report '+login+' for profanity?')) {

			var allowed,blocked,havebets,logged;

			// User pre-check
			var url = web_root+'blog.php?action=ajax_report_precheck';
			var params = {};
		  new Ajax.Request(url, {
				parameters: params,
			  asynchronous: false,
			  onComplete:function(request) {
			  	var json, i;
			    eval('json = ' + request.responseText);
			    
			    allowed = json['allowed'];
			    blocked = json['blocked'];
			    havebets = json['havebets'];
			    logged = json['logged'];
				}
			});
 
			if (!logged) {
				alert('You must log in to make a report.');
				return;
			}
			
			if (blocked) {
					alert('Your account is blocked');	
			} else {
				if (isNaN(allowed))
					var allowed = true;
				if (allowed) {
						
					if (havebets) {

						var url = web_root+'blog.php?action=ajax_report';
						var x = new Ajax.Request(url,{
						method: 'post',
							parameters: {
								post_id: postId,
							},
							onCreate: function (){
							},
							onComplete: function(r) {
								var res = eval('(' + r.responseText + ')');
								//$('post_list').innerHTML = res['posts'];
								
								if (res['alert']) {
									alert(res['alert']);
								}

								if (res['success'])
									this.load();
							}.bind(this)
						});

					} else {
						alert('You must place a bet first');			
					}
				} else {
					alert('You must log in.');	
				}
			}

  	}
  },
  onPostButtonClick: function(ev) {
  	this.post();
  },
  onFormSubmit: function(ev) {
  	this.post();
  	return false;
  },
  onLeagueChanged: function(ev) {
  	this.load();
  },
  onReportClick: function(el,ev) {
  	var postId = el.readAttribute('action-value');
  	var login = el.readAttribute('post-login');
  	this.report(postId,login);
  }
});

