function getStatsPath(){
  var statsPath = window.location.toString();
  var chpos = statsPath.indexOf('livescore/');
  if( chpos > 0 ){
    return statsPath.substr(0,chpos)+'statistics/';
  }
  return 'http://www.stats.betradar.com/statistics/';
}

function getKey(){
  var statsPath = window.location.toString();
  var chpos = statsPath.indexOf('key=');
  if( chpos > 0 ){
    var endpos = statsPath.indexOf('&',chpos+1);
    if( endpos > 0 ){
      return statsPath.substr(chpos,endpos-chpos);
    } else {
      return statsPath.substr(chpos);
    }
  }
  return null;
}
/*
 * Open Functions.
 */

function openMulticast(id, date, language, url, tournament) {
  callGoogleAnalytics('MatchRow/Multicast');
  var from = new Date(date);
  var fromTS = from.getTime();
  var fullUrl = '../../?' + url + '&ts=' + fromTS + '&language=' + language;
  if (tournament == 1) fullUrl += '&tournamentid=' + id;
  else fullUrl += '&matchid=' + id;
  window.open(fullUrl, 'multicast', 'left=100,top=100,menubar=no,scrollbars=no,width=1005,height=720');
}

function openStats(folder, matchid, language, width) {
  callGoogleAnalytics('MatchRow/Statistics');
  var key = getKey();
  if (width == null) width = 1024;
  var url = getStatsPath() + folder + '/?matchid=' + matchid + (key!=null?'&key='+key:'') +'&language=' + language + '&fromlivescore=1';
  window.open(url, 'stats', 'left=100,top=100,menubar=no,scrollbars=yes,width='+width+',height=768');
}

function openTournamentStats(folder, tournamentid, language, width) {
  callGoogleAnalytics('MatchRow/TournamentStatistics');
  var key = getKey();
  if (width == null) width = 1024;
  var url = getStatsPath() + folder + '/?tournamentid=' + tournamentid + (key!=null?'&key='+key:'') +'&language=' + language;
  window.open(url, 'stats', 'left=100,top=100,menubar=no,scrollbars=yes,width='+width+',height=768');
}

function openLeagueTable(folder, tableid, language, teamidhome, teamidaway) {
  callGoogleAnalytics('MatchRow/LiveLeague');
  var key = getKey();
//  var url = '?page=leaguetable' +
  var url = getStatsPath() + folder + '/main.php?page=guth_statistics_tablecontents' +
    (key!=null?'&key='+key:'') + '&tableid=' + tableid + '&language=' + language+
    '&showtype=6&removeSelection=1&teamidhome='+teamidhome+'&teamidaway='+teamidaway;
  window.open(url, 'table_'+tableid, 'left=100,top=100,menubar=no,scrollbars=yes,width=850,height=600');
}

function openLiveStats2(folder, tableid, language, teamidhome, teamidaway) {
  callGoogleAnalytics('MatchRow/LiveStats');
  var key = getKey();
//  var url = '?page=leaguetable' +
  var url = getStatsPath() + folder + '/main.php?page=guth_statistics_tablecontents' +
    (key!=null?'&key='+key:'') + '&tableid=' + tableid + '&language=' + language+
    '&showtype=6&removeSelection=1&teamidhome='+teamidhome+'&teamidaway='+teamidaway;
  window.open(url, 'livetable_'+tableid, 'left=100,top=100,menubar=no,scrollbars=yes,width=850,height=600');
}

function openDetails(matchid, language) {
  callGoogleAnalytics('MatchRow/Details');
  window.open('?page=details&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openJsonDetails(matchid, language) {
  callGoogleAnalytics('MatchRow/Details');
  window.open('?page=json_details&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openDetailsTennis(matchid, language) {
  callGoogleAnalytics('MatchRow/DetailsTennis');
  window.open('?page=tennisdetails&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openLineups(matchid, language) {
  callGoogleAnalytics('MatchRow/Lineups');
  window.open('?page=lineups&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openJsonLineups(matchid, language) {
  callGoogleAnalytics('MatchRow/Lineups');
  window.open('?page=json_lineups&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openInjuries(matchid, language) {
  callGoogleAnalytics('MatchRow/Injuries');
  window.open('?page=injuries&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openTV(matchid, language) {
  callGoogleAnalytics('MatchRow/TV');
  window.open('?page=tv&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openJsonTV(matchid, language) {
  callGoogleAnalytics('MatchRow/TV');
  window.open('?page=json_tv&matchid=' + matchid+ '&language=' + language, 'details', 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600');
}

function openLiveLeagueTableFlash(tableid, language) {
  callGoogleAnalytics('MatchRow/Lineups');
  window.open('?page=liveleaguetable_flash&tableid=' + tableid + '&language=' + language, 'lltf'+tableid, 'left=100,top=100,menubar=no,scrollbars=yes,width=600,height=600,resizable=1');
}


function openLivetableFlash(flashurl, tableid, language) {
  callGoogleAnalytics('MatchRow/LivetableFlash');
  var url=flashurl+"&tableid="+tableid+"&versionid=1&language="+language;
  window.open(url, 'multicast', 'left=100,top=100,menubar=no,scrollbars=no,width=600,height=700');
}


var current = null;

function checkBrowser() {
  this.ver=navigator.appVersion; this.agent=navigator.userAgent
  this.dom=document.getElementById?1:0;
  this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
  this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
  this.ie4=(document.all && !this.dom)?1:0;
  this.ie=this.ie4||this.ie5||this.ie6
  this.mac=this.agent.indexOf("Mac")>-1
  this.opera5=this.agent.indexOf("Opera 5")>-1
  this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
  this.ns4=(document.layers && !this.dom)?1:0;
  this.brwsr=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5 || this.dom)
  return this
}

var brwsr = new checkBrowser();
ns4 = (document.layers) ? true:false;
ie4 = (document.all) ? true:false;
if (ie4) {
  if (navigator.userAgent.indexOf('MSIE 5') > 0) {
    ie5 = true;
  } else {
    ie5 = false;
  }
} else {
  ie5 = false;
}
if (brwsr.ns4 == 1) ns4 = true;
else if (brwsr.ns6==1) ns6 = true;
else if (brwsr.ie4==1) ie4 = true;
else ie5 = true;
var ns6;

function matchDetails(matchid, e) {
  cleanUp();
  var details = document.getElementById('details_' + matchid);
  if (details != null) {
    current = details;
    mouseMove(e);
    details.style.display="block";
  }
}

function mouseMove(e) {
  var x;
  var y;
  if (ns4) {x=e.pageX; y=e.pageY;}
  if (ns6) {x=e.pageX; y=e.pageY;}
  if (ie4) {x=event.x; y=event.y;}
  if (ie5) {x=event.x+document.body.scrollLeft; y=event.y+document.body.scrollTop;}
  if (current != null) {
    current.style.top = y + 6;
    current.style.left = x;
  }
}

function cleanUp() {
  if (current != null) {
    current.style.display="none";
    current = null;
  }
}

function sms(shopid, sportid, sportname, homeid, homename, awayid, awayname, matchid) {
	callGoogleAnalytics('MatchRow/SMS');
	window.open('http://smslivescores.com/betradar/smslivescore.php?shopid='+shopid+'&sportid='+sportid+'&sportname='+sportname+'&participanthomeid='+homeid+'&participanthomename='+escape(homename)+'&participantawayid='+awayid+'&participantawayname='+escape(awayname)+'&eventid='+matchid,"sms","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=1,width=516");
}

function toggleHidden() {
	if (!document.styleSheets) return;
	var theRules = new Array();
	if (document.styleSheets[1].cssRules)
		theRules = document.styleSheets[1].cssRules
	else if (document.styleSheets[1].rules)
		theRules = document.styleSheets[1].rules
	else return;
	var image = document.getElementById("hideshowbutton");
	if (theRules[theRules.length-1].style.display == 'none') {
		theRules[theRules.length-1].style.display = '';
		image.src = "img/showhide.png";
	}
	else {
		theRules[theRules.length-1].style.display = 'none';
		image.src = "img/showhide.png";
	}
}
