MBT.API.ApiId = "aaed6059969d182ff2a08e7d61458ce2d1a372c1";

var WIDGET_DEFAULT_LEAGUE = 224;

var WIDGET_DEFAULT_SEASON = 15323;
var WIDGET_DEFAULT_SEASON_DUBLERS = 8546;
var WIDGET_DEFAULT_SEASON_CUP = 8839;

var WIDGET_DEFAULT_LANG = 'uk';

var WIDGET_LEAGUE_ID = 224;
var WIDGET_LEAGUE_ID_DUBLERS = 226;
var WIDGET_LEAGUE_ID_CUP = 235;
var WIDGET_SEASON_ID = null;
var WIDGETS_HOST_ROOT_URL = "http://www.superleague.ua/frontend/handlers/bh_redirect.php?";


MBT.API.History.enabled = false;

if (MBT.API.URL.CurrentQueryString.get("season_id")) 
{
	WIDGET_SEASON_ID = MBT.API.URL.CurrentQueryString.get("season_id");
	WIDGET_SEASON_ID_DUBLERS = MBT.API.URL.CurrentQueryString.get("season_id");
	WIDGET_SEASON_ID_CUP = MBT.API.URL.CurrentQueryString.get("season_id");
}
else if(MBT.API.URL.CurrentQueryString.get("sid"))
{
	WIDGET_SEASON_ID = MBT.API.URL.CurrentQueryString.get("sid");
	WIDGET_SEASON_ID_DUBLERS = MBT.API.URL.CurrentQueryString.get("sid");
	WIDGET_SEASON_ID_CUP = MBT.API.URL.CurrentQueryString.get("sid");
} 
else
{	
	WIDGET_SEASON_ID = WIDGET_DEFAULT_SEASON;
	WIDGET_SEASON_ID_DUBLERS = WIDGET_DEFAULT_SEASON_DUBLERS;
	WIDGET_SEASON_ID_CUP = WIDGET_DEFAULT_SEASON_CUP;
}

if (MBT.API.URL.CurrentQueryString.get("lang")) {
	MBT.API.Lang = MBT.API.URL.CurrentQueryString.get("lang");
	MBT.API.Cookies.set("lang", MBT.API.Lang);
} else {	
	if (MBT.API.Cookies.get("lang")) {
		MBT.API.Lang = MBT.API.Cookies.get("lang");
	
	} else {
		MBT.API.Lang = WIDGET_DEFAULT_LANG;
		MBT.API.Cookies.set("lang", MBT.API.Lang);
	}	
}


handlePlayerClick = function (playerId, seasonId) {
	var leagueSeasonStr = "";
	
	if (typeof(seasonId) != 'undefined') {
		leagueSeasonStr += "&season_id=" + seasonId;
	} else 	if (WIDGET_SEASON_ID) {
		leagueSeasonStr += "&season_id=" + WIDGET_SEASON_ID;
	}
	
	document.location.href = WIDGETS_HOST_ROOT_URL + "player_id=" + playerId +  leagueSeasonStr;
}

handleGameClick = function (gameId, seasonId) {
	
	if (typeof(seasonId) == 'undefined'&& WIDGET_SEASON_ID) {
		seasonId =WIDGET_SEASON_ID;
	}
	
	document.location.href = WIDGETS_HOST_ROOT_URL + "game_id=" + gameId +  "&season_id=" + seasonId;;
}


handleTeamClick = function (teamId, seasonId) {	
	var leagueSeasonStr = "";
	
	if (typeof(seasonId) != 'undefined') {
		leagueSeasonStr += "&season_id=" + seasonId;
	} else if (WIDGET_SEASON_ID) {
		leagueSeasonStr += "&season_id=" + WIDGET_SEASON_ID;
	}
	
	document.location.href = WIDGETS_HOST_ROOT_URL + "team_id=" + teamId +  leagueSeasonStr;
}

handleSeasonClick = function (seasonId) {
	var link = document.location.href;
	var new_link="";
	var search_result = link.indexOf('?');
	if( search_result == -1)
		new_link = document.location.href + "?season_id=" + seasonId;
	else
	{
		var parameters_url = link.substring(search_result+1);
		new_link = link.substring(0,search_result);
		var parameters = parameters_url.split("&");
		for(i=0; i<parameters.length; i++)
		{
			var value = 0;
			var param = parameters[i].split("=");
			if(param[0]=="season_id")
			{
				value = seasonId;
			}
			else
			{
				value = param[1];
			}
			if(i != 0)
				new_link += "&";
			else
				new_link += "?";
			new_link += param[0]+"="+value;
		}
	}
	document.location.href = new_link;
}

handleLeagueClick = function (leagueId) {	
	document.location.href = WIDGETS_HOST_ROOT_URL + "?league_id=" + leagueId;
}

navigateTeamExternal = function (teamId, seasonId) {	
	var leagueSeasonStr = "";
	
	if (typeof(seasonId) != 'undefined') {
		leagueSeasonStr += "&season_id=" + seasonId;
	} else if (WIDGET_SEASON_ID) {
		leagueSeasonStr += "&season_id=" + WIDGET_SEASON_ID;
	}
	window.open(WIDGETS_HOST_ROOT_URL + "team_id=" + teamId +  leagueSeasonStr,'_newtab');
}

navigatePlayerExternal = function (playerId, seasonId) {
	var leagueSeasonStr = "";
	
	if (typeof(seasonId) != 'undefined') {
		leagueSeasonStr += "&season_id=" + seasonId;
	} else 	if (WIDGET_SEASON_ID) {
		leagueSeasonStr += "&season_id=" + WIDGET_SEASON_ID;
	}
	
	window.open(WIDGETS_HOST_ROOT_URL + "player_id=" + playerId +  leagueSeasonStr,'_newtab');
}
