/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	setup TeamTalk and other popup windows
	
	Usage:
	use URL in href attribute, and expand class attribute with the following values:
	sN_STR			_STR = name for popup
	bS_NUM			_NUM = 1 or 0, scrolling on/off
	nW_NUM			_NUM = popup width in pixels
	nH_NUM			_NUM = popup height in pixels
	
	bR2W_NUM		_NUM = 1 or 0, is ratio to width used true/false
	nRW_NUM			_NUM = 0 to 1, number to multiply width
	nRH_NUM			_NUM = 0 to 1, number to mulitply height
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function SG_SetupWnd() {
	var aTmp = xGetElementsByClassName("Wnd", document, "a");
	for (var i=0;i<aTmp.length;i++) {
		aTmp[i].popupcnt = i;
		aTmp[i].onclick = function() {
			var sHref = this.href.replace("&amp;", "&");

			aClasses = this.className.split(" ");
			if (aClasses.length == 1) {
				//	if only Wnd is specified as class (no options) then replicate target="_blank"
				window.open(sHref);
			} else {
				var nW=0, nH=0;
				var nRW=1, nRH=1;
				var bHasScrollbars = 0, bIsR2W = 0;
				var sName = "";
	
				for (var j=0;j<aClasses.length;j++) {
					if ( aClasses[j].substring(0, 2) == "nW" )
						nW = parseInt( aClasses[j].substring(2), 10 );
					else if ( aClasses[j].substring(0, 2) == "nH" )
						nH = parseInt( aClasses[j].substring(2), 10 );
					else if ( aClasses[j].substring(0, 3) == "nRW" )
						nRW = parseFloat( aClasses[j].substring(3) );
					else if ( aClasses[j].substring(0, 3) == "nRH" )
						nRH = parseFloat( aClasses[j].substring(3) );
					else if ( aClasses[j].substring(0, 4) == "bR2W" ) {
						bIsR2W = parseInt( aClasses[j].substring(4) );
						if (isNaN(bIsR2W)) bIsR2W = 0;
					} else if ( aClasses[j].substring(0, 2) == "sN" )
						sName = aClasses[j].substring(2);
					else if ( aClasses[j].substring(0, 2) == "bS" ) {
						bHasScrollbars = parseInt( aClasses[j].substring(2) );
						if (isNaN(bHasScrollbars)) bHasScrollbars = 0;
					}
				}
				
				if (sName = "") sName = "FSpopwnd" + this.popupcnt;
				
				//	it's a popup, so add this identificator (used in betslip, placement)
				if (sHref.indexOf("?") == -1) sHref += "?";
				else sHref += "&";
				sHref += "p=1";

				SG_ShowWnd(sHref, sName, nW, nH, bHasScrollbars, nRW, nRH, bIsR2W);
			}
			return false;
		};
	}
}
AE_AttachEvent("onload", "SG_SetupWnd");

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	open popup windows
	sUrl is the URL to open in popup
	sName is the name of the popup wnd
	xWidth, xHeight are dimensions (optional)
	xRatioW, xRatioH are ratios (< 1, i.e. 0.8) to screen.width and screen.height
	bIsR2W = 1/true => xRatioH is ratio to xRatioW, not to xHeight
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function SG_ShowWnd(sUrl, sName, xWidth, xHeight, bHasScrollbars, xRatioW, xRatioH, bIsR2W) {
	var xMax = 770, yMax=600;

	if (screen) {
		var xMax = screen.width, yMax = screen.height;
	}

	if (!xWidth) {
		if (xRatioW) xWidth = xMax * xRatioW;
		xWidth = Math.round(xWidth);
	}
	//	tweak for scrollbars
	if (bHasScrollbars) xWidth += 22;
	if (xWidth > xMax) xWidth = xMax;

	if (!xHeight) {
		if (xRatioH) xHeight = ((bIsR2W) ? xWidth * xRatioH : yMax * xRatioH);
		xHeight = Math.round(xHeight);
	}

	var xOffset = (xMax - xWidth)/2;  // distance from left
	var yOffset = (yMax - xHeight)/2;  // distance from top
	if (xOffset < 1) xOffset = 0;
	if (yOffset < 1) yOffset = 0;

	winUpdate = window.open(sUrl, sName, "width="+ xWidth.toString() + ",height="+ xHeight.toString() +",screenX=" + xOffset + ",screenY=" + yOffset + ",top=" + yOffset + ",left=" + xOffset + ",status=no,resizable=yes" + (bHasScrollbars ? ",scrollbars=yes" : "") );
	if (winUpdate.opener == null) winUpdate.opener = self;
	winUpdate.focus();
	
	return winUpdate;
}

var xOp7 = false, xIE = false, xUA = navigator.userAgent.toLowerCase();
if (window.opera) {
	xOp7 = ( xUA.indexOf("opera 7") != -1 || xUA.indexOf("opera/7") != -1 );
} else {
	xIE = ( xUA.indexOf("msie") != -1 );
}

function xGetElementsByClassName(clsName, parentEle, tagName, fn)
{
  var found = new Array();
  var re = new RegExp('\\b'+clsName+'\\b', 'i');
  var list = xGetElementsByTagName(tagName, parentEle);
  for (var i = 0; i < list.length; ++i) {
    if (list[i].className.search(re) != -1) {
      found[found.length] = list[i];
      if (fn) fn(list[i]);
    }
  }
  return found;
}
function xGetElementsByTagName(tagName, parentEle)
{
  var list = null;
  tagName = tagName || '*';
  parentEle = parentEle || document;
  if (xIE) {
    if (tagName == '*') list = parentEle.all;
    else list = parentEle.all.tags(tagName);
  }
  else if (parentEle.getElementsByTagName) list = parentEle.getElementsByTagName(tagName);
  return list || new Array();
}
