String.prototype.trim = function(chars) {
	return this.ltrim(chars).rtrim(chars);
};

String.prototype.ltrim = function(chars) {
	return this.replace(new RegExp("^[" + chars + "]+", "g"),"");
};

String.prototype.rtrim = function(chars) {
	return this.replace(new RegExp("[" + chars + "]+$", "g"),"");
};

Number.prototype.format = function(decimals,dec_point,thousands_sep){
	var number = this;
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function(n,prec){
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };

    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    
    if(s[0].length > 3)
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    
    if((s[1] || '').length < prec){
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    
    return s.join(dec);
};

jQuery.fn.extend({
		convert_number: function() {
			return this.each( function() {
				var txt = document.getElementById(this.id).innerHTML;
				var ar_item = new Array('0','1', '2','3','4','5','6','7','8','9');				
				var ar_replace = new Array('&#1776;','&#1777;', '&#1778;','&#1779;','&#1780;','&#1781;','&#1782;','&#1783;','&#1784;','&#1785;');
				
				var result = new Array();
				for(var i = 0; i < txt.length; i++) {
					result.push(txt.charAt(i));
				}
				
				for(var i = 0; i < result.length; i++) {
					for(var j = 0; j < ar_item.length; j++) {
						if(result[i] == ar_item[j]) {
							result[i] = ar_replace[j];							
						}
					}
				}
				
				txt = "";
				for(var i = 0; i < result.length; i++) {
					txt += result[i];
				}				

				document.getElementById(this.id).innerText = txt;				
				document.getElementById(this.id).innerHTML = txt;
			});
		}
	}
); 

var farsiLanguage = true;

function myF(myField,e)
{
    e = (e) ? e : event;
	var charCode = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
	var key = charCode;
    if(key == 119)
    {farsiLanguage =(farsiLanguage==true) ? false : true ;}
}

function FarsiType(myField,e)
{
	if(myField.id == "team_name") {
	    var len = $("#team_name").val().length;
	    if(len > 13) {
	        return null;
	    }
	}
	else if(myField.id == "name") {
	    var len = $("#name").val().length;
	    if(len > 13) {
	        return null;
	    }
	}
	e = (e) ? e : event;
	var charCode = (e.charCode) ? e.charCode : ((e.which) ? e.which : e.keyCode);
	var key = charCode;	
	var FarsiType = 
	{
		farsiKey : [
			32    , 33    , 34    , 35    , 36    , 37    , 1548  , 1711  ,
			41    , 40    , 215   , 43    , 1608  , 45    , 46    , 47    ,
			48    , 49    , 50    , 51    , 52    , 53    , 54    , 55    ,
			56    , 57    , 58    , 1705  , 44    , 61    , 46    , 1567  ,
			64    , 1616  , 1584  , 125   , 1609  , 1615  , 1609  , 1604  ,
			1570  , 247   , 1600  , 1548  , 47    , 8217  , 1583  , 215   ,
			1563  , 1614  , 1569  , 1613  , 1601  , 8216  , 123   , 1611  ,
			1618  , 1573  , 126   , 1580  , 1688  , 1670  , 94    , 95    ,
			1662  , 1588  , 1584  , 1586  , 1740  , 1579  , 1576  , 1604  ,
			1575  , 1607  , 1578  , 1606  , 1605  , 1574  , 1583  , 1582  ,
			1581  , 1590  , 1602  , 1587  , 1601  , 1593  , 1585  , 1589  ,
			1591  , 1594  , 1592  , 60    , 124   , 62    , 1617
		]
	}	
	try
	{
		if(farsiLanguage)
		{
		    if (key != 46 && key < 1000 && key != 32 && key != 8 && key != 13 && key != 9 && key != 192)
		    {
				key = FarsiType.farsiKey[key-32];
				myField.value+=String.fromCharCode(key);					
				return false;
		    }
		}
		return true;
	}
	catch(error)
	{alert(error);}
	return true;
}

function help_rev(which, url)
{
	var height = "0";
	var width = "0";
	var i = 0;

	switch(which) {
	    case "league_news.jpg":
			height = "300";
			width = "415";
			i = 1;
		    break;
	    case "money.jpg":
			height = "175";
			width = "408";
			i = 2;
		    break;
		case "friendly_queue.jpg":
			height = "190";
			width = "510";
			i = 3;
		    break;
		case "team_search.gif":
			height = "220";
			width = "700";
			i = 4;
		    break;
		case "stadium.gif":
			height = "315";
			width = "300";
			i = 5;
		    break;
		case "sponsors.gif":
			height = "280";
			width = "435";
			i = 6;
		    break;
   		case "substitute.gif":
			height = "300";
			width = "500";
			i = 7;
		    break;
		case "strategy.gif":
			height = "275";
			width = "805";
			i = 8;
		    break;
		case "current_form.jpg":
			height = "110";
			width = "210";
			i = 9;
		    break;
		case "experience.gif":
			height = "200";
			width = "402";
			i = 10;
		    break;
		default:
		    break;
	}
	
	window.open(url + "how_to_play/help/" + which, i, "height=" + height + ", width=" + width + ", status=no, toolbar=no, menubar=no, location=no, background-color:#000000");
}

$( function() {
	if($(".icon") == "undefined" || !$(".icon")) {
	    return;
	}
	$(".icon").queue( function() {
		$(this).css('display', 'none');
		$(this).dequeue();
	});
	$("#body_loader_panel").queue( function() {
		$(this).slideUp('normal');
		$(this).dequeue();
	});
	setTimeout(tp, 1000);
	
	$(".ti_next").click( function() {
		if(ti_next_counter > ti_next_max) {
			ti_next_counter = 1;
		}

		$(".text_er").html($("#hint_txt_l" + ti_next_counter).html());
	    ti_next_counter++;
	});
	
	$(".button").bind("mouseover", function(){
		var alt = $(this).attr("alt");
		var src = $(this).attr("src");
		if( src.indexOf("_over") == "-1" ) {
			$(this).attr("src", src.replace(alt, alt + "_over"));
		}
	});
	$(".button").bind("mouseout", function(){
		var src = $(this).attr("src");
		if( src.indexOf("_over") != "-1" ) {
			$(this).attr("src", src.replace("_over", ""));
		}
	});

	$("ul.dashboard_menu").lavaLamp({
		fx: "easeOutCirc",
		startItem: 0,
		speed: 400
	});
//	$(".dashboard_menu").lavaLamp({
//		fx: "backout",
//		speed: 400
//	});
	
	/* Open player details popup */
		$('.player_details_popup_link').click(function(){
			var href = $(this).attr('href');
			window.open(href,null,"height=690, width=450, status=no, toolbar=no, menubar=no, location=no");
			return false;
		});
	/* ------------------------- */
		
	/* Open league table popup */
		$('.league_table_popup_link').click(function(){
			var href = $(this).attr('href');
			window.open(href,null,'height=245,width=414,scrollbars=yes, status=no, toolbar=no, menubar=no, location=no');
			return false;
		});
	/* ----------------------- */
});

function tp(){
	$("#body_loader").css('display', 'none');
}

/**
 * Checks the given value to make sure it is in Persian
 * 
 * @param string value The string to be checked
 * @author Peyman Afraz
 */
function is_farsi(value){
	if(value === '' || value === null || value === undefined || value.replace(" ", "") == "" || value.length < 2)
		return false;
	
	var pattern = /^[0-9 ي ك اآبپتثئجچحخدذرزژسشصضطظعغف قکگلمنوهی\s]{3,}$/i;
	return pattern.test(value);
}

/**
 * Friendly game notifications poll
 */
var friendly_notifications_class = function(){
	var _xhrs = new Array,
		_fetch_intval = 10,
		_container_id = 'friendly_not',
		_container = '',
		_timeouts = new Array,
		_controller = 'friendly_new';
	
	var _init = function(){
		_container = $('#' + _container_id);
		
		if(gvars.friendly_int != undefined)
			_fetch_intval = gvars.friendly_int;
		
		_fetch();
	};
	$(document).ready(_init);
	
	var _fetch = function(){
		if(gvars.controller != undefined && gvars.controller == 'friendly_new')
			return false;
		
		var excludes = new Array;
			_container.find('ul li').each(function(){
				excludes[excludes.length] = $(this).attr('rel');
			});
		excludes = excludes.join(',');
		
		if(typeof _xhrs['fetch'] == 'object')
			_xhrs['fetch'].abort();
		
		_xhrs['fetch'] = $.ajax({
			type: 'post',
			url: gvars['base_url'] + _controller +'/ajax_notification',
			data: 'ex=' + excludes,
			dataType: 'json',
			error: function(){
				_timeouts['fetch'] = setTimeout(_fetch,_fetch_intval * 1000);
			},
			success: function(result){
				_timeouts['fetch'] = setTimeout(_fetch,_fetch_intval * 1000);
				
				if(result.games != undefined && result.games.length > 0)
					_show(result.games);
				
				if(result.del != undefined && result.del.length > 0){
					for(var counter = 0; counter < result.del.length; counter++)
						_delete_request(Number(result.del[counter]));
				}
			}
		});
	};
	
	var _show = function(games){
		_container = $('#' + _container_id);
		
		/* Container */
			if(_container.length == 0){
				_container = $("<div id='" + _container_id + "'></div>").css('right','-64px').prependTo('body');
				_container.html("<span></span><div><span class='border'></span><ul></ul></div>");
			}
			
			_container.stop().animate({right: 0});
			_container.children('span').unbind('click').click(_toggle);
		/* --------- */
			
		for(var counter = 0; counter < games.length; counter++){
			var game = games[counter];
			var game_dom = $('<li></li>').attr('rel',game.id).appendTo(_container.children('div').children('ul'));
			game_dom.append("<a href='" + game.profile + "' target='_blank'>" + game.name + "</a>");
			
			/* Countdown */
				var countdown = $("<span class='countdown'>" + gvars.lang['f_l_timeout'] + " <span class='hour'>" + game.time_remain.hours + "</span><span class='minute'>" + game.time_remain.minutes + "</span>:<span class='second'>" + game.time_remain.seconds + "</span></span>").appendTo(game_dom);
				countdown.countdown({leading_zero: true,timeout_var_index: 'friendly_game_note_' + game.id},function(){
					_delete_request(Number(game.id));
				});
			/* --------- */
				
			/* Approve button */
				var approve = $("<span class='approve'></span>").appendTo(game_dom);
				approve.click(_approve);
			/* -------------- */
				
			if(_container.hasClass('open'))
				_container.children('div').stop().animate({height: _container.find('li').length * 40}).css('display','');
		}
		_container.find('ul li.odd').removeClass('odd');
		_container.find('ul li:odd').addClass('odd');
		_container.children('span').html(_container.find('ul li').length);
	};

	var _delete_request = function(request){
		if(typeof request == 'string')
			request = Number(request);
		
		if(typeof request == 'number')
			request = _container.find('ul li[rel=' + request + ']');
		
		request.remove();
		_container.children('div').stop().animate({height: _container.find('li').length * 40});
		
		if(_container.find('ul li').length == 0){
			_hide(false);
			return;
		}
		
		_container.find('ul li.odd').removeClass('odd');
		_container.find('ul li:odd').addClass('odd');
		_container.children('span').html(_container.find('ul li').length);
	};
	
	var _approve = function(){
		var self = $(this);
		if(self.hasClass('loading'))
			return false;
		
		var request = self.parents('li');
		var request_id = Number(request.attr('rel'));
		
		var loading = $('<span></span>').appendTo(self.addClass('loading'));

		$.ajax({
			type: 'post',
			url: gvars['base_url'] + _controller + '/ajax_approve_request/',
			data: 'rid=' + request_id,
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					_delete_request(request);
					return;
				}
					
				loading.remove();
				self.removeClass('loading');
			}
		});
		
		return false;
	};
	
	var _toggle = function(){
		var is_open = _container.hasClass('open');
		
		if(is_open){
			_container.children('div')	.stop()
										.animate({height: 0},function(){
											$(this).css('display','');
											_container.removeClass('open');
										});
			return false;
		}
		
		_container.addClass('open').children('div')	.stop()
													.animate({height: _container.find('li').length * 40},function(){
														$(this).css('display','');
													});
	};
	
	var _hide = function(send_request){
		if(send_request == undefined)
			send_request = true;
		
		_container.stop().animate({
			right: -64
		},function(){
			_container.remove();
		});
		
		if(send_request){
			clearTimeout(_timeouts['fetch']);
			$.ajax({
				type: 'post',
				url: gvars['base_url'] + 'friendly_new/ajax_close_notification',
				data: '',
				dataType: 'text'
			});
		}
		
		return false;
	};
};
if(gvars['coach_id'] == '198864' || gvars['coach_id'] == '50053' || gvars['coach_id'] == '642730' || gvars['coach_id'] == '198857')
	var friendly_not = new friendly_notifications_class();
