﻿
var Proxy;
function serviceProxy(serviceURL) {
	var _I = this;
	this.serviceURL = serviceURL;

	this.invoke = function(method, data, callback, error, bare) {
		var json = JSON2.stringify(data);
		var url = _I.serviceURL + method;

		$.ajax({
			url: url,
			data: json,
			type: "POST",
			processData: false,
			contentType: "application/json",
			timeout: 10000,
			dataType: "text",
			success: function(res) {
				if (!callback) return;

				var result = JSON2.parse(res);

				if (bare)
				{ callback(result); return; }

				for (var property in result) {
					callback(result[property]);
					break;
				}
			},
			error: function(xhr) {
				if (!error) {
					if (xhr.responseText) {
						onProxyError(JSON2.parse(xhr.responseText));
					}
					else
						onProxyError({Message: xhr}); return;
				}

				if (xhr.responseText) {
					var err = JSON2.parse(xhr.responseText);
					if (err)
						error(err);
					else
						error({ Message: "Unknown server error" })
				}
				return;
			}
		});
	}
}

function onProxyError(msg) {
	//alert("Error: " + obj.Message);
	$('#debug').append("Error: " + msg.Message + "<br />");
}

function newWindow(l,w,h,o,s) {

	if (s == null || s == undefined)
		s = true;

	s = (s) ? true : false;
	
	if (w == null || w == undefined)
		w = 400;
	else if (isNaN(w)) {
		v = parseInt(w.replace('%', '').replace(' ', ''));
		v = (isNaN(v) || (!isNaN(v) && (v < 1 || v > 100))) ? 50 : v;
		w = (window.screen.availWidth * (v / 100));
	}

	if (h == null || h == undefined)
		h = 600;
	else if (isNaN(h)) {
		v = parseInt(h.replace('%', '').replace(' ', ''));
		v = (isNaN(v) || (!isNaN(v) && (v < 1 || v > 100))) ? 50 : v;
		h = (window.screen.availHeight * (v / 100));
	}


	o = (o != null && o != undefined) ? o : '';
	t = ((window.screen.availHeight / 2) - (h / 2));
	lf = ((window.screen.availWidth / 2) - (w / 2));

	p = o.split(',');
	o = '';
	for (x = 0; x <= p.length - 1; x++) {
		v = p[x].split('=');
		if (v[0] != 'height' && v[0] != 'width' && v[0] != 'top' && v[0] != 'left')
			o += ',' + v[0] + '=' + v[1];
	}

	o = (o + ',height=' + h + ',width=' + w + ',top=' + t + ',left=' + lf).substring(1);

	var w = window.open(l, '', o);
	if (!w && s) {
		if (confirm("Popup blocker prevented the window from opening. Do you want to navigate away from this page?"))
			window.location = l;
	}

	return w;
}

function slideToggle(x, q, y) { $('#' + q).slideToggle(); if (y != null && typeof (y) == "function") { y(x); } }

function base() {
	if ($.browser == "msie") {
		document.onfocusin = onWindowFocus;
		document.onfocusout = onWindowBlur;
	}
	else {
		window.onfocus = onWindowFocus;
		window.onblur = onWindowBlur;
	}

	function onWindowFocus() {
		chess.isFocused = true;
	}

	function onWindowBlur() {
		chess.isFocused = false;
	}
}

base.prototype.isFocused = false;

chess = new base();

