/**
 * Title: windows.js
 * Purpose: Handles spawing and 2-way communication of popup windows from the main RW interface.
 */

function RWWindow(_n, _a, _w, _h, _s) {
	this.name	  = _n;
	this.attrs	= _a;
	this.width  = _w;
	this.height = _h;
	this.siteWidth = _s;
	this.winref = "";
	this.Open		= win_Open;
	this.Close	= win_Close;
	this.SyncWP = win_SyncWP;
}

function win_Open(_u, _force) {
	var _l = this.siteWidth + Math.floor((screen.availWidth - this.siteWidth) / 2) - (this.width / 2);
	if(true || _l + this.width > screen.availWidth) {
		_l = (this.siteWidth / 2) - (this.width / 2);
	}
	var _t = (screen.availHeight / 2) - (this.height / 2);
	var _do = true;
  this.winref = open("", this.name, this.attrs + ",left=" + _l + ",top=" + _t + ",width=" + this.width + ",height=" + this.height);      
	if(this.winref.document.getElementById("h_trackCode")) {   
		_do = _u.indexOf("trackCode=" + this.winref.document.getElementById("h_trackCode").innerHTML) == -1;
	}
	if(!_do && this.winref.document.getElementById("h_raceNum")) {
		_do = _u.indexOf("raceNum=" + this.winref.document.getElementById("h_raceNum").innerHTML) == -1;
	}
	if(_do || _force) {
		this.winref = open(_u, this.name, this.attrs + ",left=" + _l + ",top=" + _t + ",width=" + this.width + ",height=" + this.height);
  } 
  this.winref.focus();
	return _do || _force ? 1 : 0;
}

function win_Close() {
	if(this.winref) {
		this.winref.close();
		this.winref = "";
	}
}

function win_SyncWP(_u, _id) {
	if(this.Open(_u, 0)) 
		return;
	if(this.winref.oWagerObject) {
		if(this.winref.oWagerObject.GetUniqueRaceCount() == 1) {
			_p = _id.split("-");
			//alert("runner-" + _p[2] + "-" + oWagerObject.GetSelectedTab() + "-" + _p[3]);
    	var _rb = this.winref.document.getElementById("runner-" + _p[2] + "-" + this.winref.oWagerObject.GetSelectedTab() + "-" + _p[3]);    
     	if(_rb) {
      	this.winref.RunnerButtonClicked(_rb);
			}
		}
	}
}





