/**
  * Hashtable
  */
var Hashtable = Class.create();
Hashtable.prototype = {
	/**
	 * Инициализация класса
	 */ 
 	initialize : function() {
 		this.keys = [];
		this.values = [];
 	},
	
	/**
 	* Очистить хэш-таблицу
 	*/
	Clear : function() {
		this.keys = [];
		this.values = [];
	},
	
	/**
	 * Добавить значение
 	*/
	Get : function(key) {
		if (key == null) return null;
		for (var i = this.keys.length - 1; i >= 0; i--) {
			if (this.keys[i] == key) return this.values[i];
		}
		return null;
	}, 

	/**
 	* Извлечь значение
 	*/
	Put : function(key, value) {
		if (key == null || value == null) return;
		for (var i = this.keys.length; i >= 0; i--) {
			if (this.keys[i] == key) { 
				this.values[i] = value;
				return;
			}
		}
		this.values.push(value);
		this.keys.push(key);
		if (this.keys.length > 500) {
			this.keys.splice(0, 10);
			this.values.splice(0, 10);
		}
	}	
}

function popwindow(aurl, aw, ah, aname, aresize, ascroll) { 
	var params = "width=" + String(aw) + ",height=" + String(ah) + ",toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=";
	if (aresize) { params += "yes,"; } else { params += "no,"; }
	params += "scrollbars=";
	if (ascroll) { params += "yes"; } else { params += "no"; }
    var nwin = window.open(aurl, aname, params); 
    if (nwin) {
		nwin.focus();
    } else {
		window.alert('Pop-up windows appear to be blocked on your browser. Unable to proceed...');
    }
}

function initPopUp(intWidth, intHeight) {
	if (!opener) return;
	var newW = parseInt(intWidth); if (isNaN(newW)) newW = 800;  
	var newH = parseInt(intHeight); if (isNaN(newH)) newH = 600;
  
	if (newW > screen.availWidth) newW = screen.availWidth;
	if (newH > screen.availHeight) newH = screen.availHeight;
  
	window.resizeTo(newW, newH); // изменить размер окна на предустановленные значения
	var body = document.body;
	if (!document.all) {
		var x = opener.screenX + (opener.outerWidth - window.outerWidth) / 2;
		var y = opener.screenY + (opener.outerHeight - window.outerHeight) / 2;
		window.moveTo(x, y);
	} else {
		var W = body.offsetWidth;
		var H = body.clientHeight;
		var x = (screen.availWidth - W) / 2;
		var y = (screen.availHeight - H) / 2;
		window.moveTo(x, y);
	}
}

function getElm(name) {
	if (document.getElementById) {
		return document.getElementById(name);
  	} else if (document.all) {
		return document.all[name];
	} else if (document.layers) {
		return document.layers[name];
  	}
}

function showPGN(gid) {
	popwindow('/chess/pgn/' + gid + '.html', 600, 520, '_gmpgn', 1, 1);
}

function showBoard(gid) {
	popwindow("/chess/analyze/" + gid + ".html?popup=1", 790, 540, "_viewbd" + gid, 1, 1);
}

function showMoveHelp() {
	popwindow('/modules.php?name=Chess&file=game&op=movehelp&popup=1', 430, 260, '_movehelp');
}

function helpTime() {
	popwindow('/modules.php?name=Chess&file=game&op=timehelp&popup=1', 550, 460, '_timehelp');
}

function helpRating() {
	popwindow('/modules.php?name=Chess&file=game&op=ratinghelp&popup=1', 550, 460, '_ratinghelp');
}

function helpRules(url) {
	popwindow(url, 550, 460, '_rule');
}

var GlobalPage = {};
GlobalPage.unloadStart = false;
GlobalPage.updaters = [];

window.WindowUnloader = function(callback) {
	var oldEvent = window.onunload;
	window.onunload = function(e) {
		if (oldEvent) oldEvent(e);
		callback(e);
	}
};

function add_widget_frame(name, width, height) {
	document.write('<iframe width="' + width + '" height="' + height + '" scrolling="no" frameborder="0" src="' + Config.WidgetsURL + name + '.html"></iframe>');
}