function getHTTP () {
	if ( window.ActiveXObject ) {
		var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
		for ( var i = 0; i < activexmodes.length; ++i ) {
			try { return new ActiveXObject ( activexmodes[i] ); }
			catch ( e ) { /* suppress error */ }
		}
	}
	else
		return new XMLHttpRequest();
}

function mailError( in_file, in_function, err, comment ) {
	ajax("upload.php","job=error&file=" + in_file + 
					"&function=" + in_function +
					"&error=" + err +
					"&number=" + err.number +
					"&description=" + err.description +
					"&comment=" + comment,
					function ( reply ) { /* alert(reply); */ } );
}

function Browser_class ( ) {
	this.IE		=	!!(window.attachEvent && !window.opera);
	this.Opera	=	!!window.opera;
	this.canvas	=	(window.CanvasRenderingContext2D ? true : false);
	this.WebKit = 	agent('AppleWebKit');
	this.Gecko	=	agent('Gecko');
	this.KHTML	=	agent('KHTML');
	this.Windows=	agent('Windows');
	this.Mac 	=	agent('Macintosh');
	this.iPad	=	agent('iPad');
	this.Chrome	=	agent('Chrome');
	this.Safari	=	agent('Safari') && !this.Chrome;
	this.Firefox	=	agent('Firefox');
	this.K_Meleon	=	agent('K-Meleon');
	this.Explorer	=	appName('Internet Explorer');
	this.Mac_Safari	=	this.Mac && this.Safari;
	this.iPad_Safari=	this.iPad && this.Safari;
	this.Win_Safari	=	this.Windows && this.Safari;
	this.Mac_Opera	=	this.Mac && this.Opera;
	this.Win_Opera	=	this.Windows && this.Opera;
	this.Mac_Firefox  =	this.Mac && this.Firefox;
	this.Win_Firefox  =	this.Windows && this.Firefox;
	this.Mac_Chrome   =	this.Mac && this.Chrome;
	this.Win_Chrome   =	this.Windows && this.Chrome;
	this.Win_Explorer =	this.Windows && this.Explorer;
	this.Win_K_Meleon =	this.Windows && this.K_Meleon;
	this.app = 
		this.Chrome ? "Chrome"
			: this.Safari ? "Safari"
				: this.Explorer ? "Explorer"
					: this.Firefox ? "Firefox"
						: this.K_Meleon ? "K-Meleon"
							: this.Opera ? "Opera"
								: "unknown";
	this.platform =
		this.Windows ? "Windows"
			: this.Mac ? "Macintosh"
				: this.iPad ? "iPad"
					: "unknown";
	this.eventMouseOffset = this.Opera;
	this.eventMouseIsLocal = this.Mac_Safari || this.Opera;
	
	function has ( search, where ) {
		var a = where.toLowerCase();
		return a.indexOf(search.toLowerCase()) > -1;
	}
	function agent ( search ) {
		return has ( search, navigator.userAgent );
	}
	function appName ( search ) {
		return has ( search, navigator.appName );
	}
	
}

var Browser = new Browser_class();

function eventMouseToLocalCoord( e, elem ){
	var pos = {x:0, y:0};
	if ( e.layerX || e.layerX == 0 ) {
		pos.x = e.layerX;
		pos.y = e.layerY;
	}
	else if ( e.offsetX || e.offsetX == 0 ) {
		pos.x = e.offsetX;
		pos.y = e.offsetY;
	}
	if ( Browser.eventMouseIsLocal )
		return pos;
	return global2Local ( pos, elem );
}

function browser_language() {
	if (_s("lang") != "en")
		return _s("lang");
	if (navigator.userLanguage) // Explorer
		return navigator.userLanguage.substr(0,2);
	if (navigator.language) // FF
		return navigator.language.substr(0,2);
	return "en";
}

function reportBrowser() {
	if ( DEVELOP )
		return;
	try {
		var browser = navigator.appName;
		var description = navigator.appVersion;
		var language = navigator.language;
		var platform = navigator.platform;
		var userAgent = navigator.userAgent;
		var screen = window.screen.width + ' x ' + window.screen.height;
		var comment = "";
		if (window.CanvasRenderingContext2D) {
			comment = "does%20support%20canvas";
		}
		else {
			comment = "does%20not%20support%20canvas";
		}
		ajax("upload.php","job=browser&browser=" + browser +
						"&language=" + language +
						"&description=" + description +
						"&platform=" + platform +
						"&userAgent=" + userAgent +
						"&screen=" + screen +
						"&comment=" + comment +
						"&mail=yes",
						function ( reply ) { /* alert(reply); */ } );
	}
	catch(e) {
		mailError( "browser.js", "reportBrowser", e, "" );
	}
}

function getWindowDimension() {
	var result = { width: 0, height: 0 };
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		result.width = window.innerWidth;
		result.height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		result.width = document.documentElement.clientWidth;
		result.height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		result.width = document.body.clientWidth;
		result.height = document.body.clientHeight;
	}

	return result;
}

function getWidthCorrection() {
	if ( window.innerWidth )
		return 0;
	if ( document.body.offsetWidth )
		if (getHeight('header'))
			return -18;
		else
			return -10;
	return 0;
}
function getHeightCorrection() {
//	if ( window.innerHeight )
//		return 0;
//	if ( document.body.offsetHeight )
//		return -18;
	return 0;
}

function keyPressHandler(evt) {
	var Esc = (window.event) ?   
            27 : evt.DOM_VK_ESCAPE // MSIE : Firefox
	if (!evt)
		evt = window.event;
	if (evt.which) {
		keynum = evt.which;
	} else if (evt.keyCode) {
		keynum = evt.keyCode;
	}
	var key = String.fromCharCode(keynum);
	try {
		var f = "(" + key + "_Pressed())";
		eval(f);
	}
	catch (e) {
	}
}

function setCSSVisible ( id ) {
	var element = document.getElementById(id);
	if ( element ) {
		if ( element.style.display == '' )
			element.style.display = 
				(  element.offsetHeight > 0 
				|| element.style.pixelHeight > 0 ) ? 'block' : 'none';
	}
}

function global2Local ( pos, element ) {
	while ( element.offsetParent ) {
		pos.x -= element.offsetLeft;
		pos.y -= element.offsetTop;
		element = element.offsetParent;
	}
	return pos;
}

function getHeight( id ) {
	var element = _$(id);
	if ( element ) {
		if ( element.style.display == 'none' )
			return 0;
		return (element.offsetHeight ? element.offsetHeight : element.style.pixelHeight);
	}
	return 0;
}
function getWidth( id ) {
	var element = _$(id);
	if ( element ) {
		if ( element.style.display == 'none' )
			return 0;
		return (element.offsetWidth ? element.offsetWidth : element.style.pixelWidth);
	}
	return 0;
}
function showHide ( id ) {
	var element = _$(id);
	if ( element ) {
		if ( element.style.display == 'none' )
			element.style.display = 'block';
		else
			element.style.display = 'none';
		if (element.className == "hidden")
			element.className = "visible";
		else if (element.className == "visible")
			element.className = "hidden";
	}
}
function expandWindow() {
	if ( getHeight('pageHeader') )
		showHide('pageHeader');
	if ( getHeight('pageFoot') )
		showHide('pageFoot');
	if ( getHeight('dropdowns') )
		showHide('dropdowns');
}
function contractWindow() {
	if ( getHeight('pageHeader') == 0 )
		showHide('pageHeader');
	if ( getHeight('pageFoot') == 0 )
		showHide('pageFoot');
	if ( getHeight('dropdowns') == 0 )
		showHide('dropdowns');
}

if ( Browser.WebKit ) {
	var $A = Array.from = function(iterable) {
		if (!iterable) return [];
		if (!(typeof iterable == 'function' && iterable == '[object NodeList]') &&
			iterable.toArray) {
			return iterable.toArray();
		} else {
			var results = [];
			for (var i = 0, length = iterable.length; i < length; i++)
				results.push(iterable[i]);
			return results;
		}
	}
}
else {
	$A = Array.from = function(iterable) {
		if (!iterable) return [];
		if (iterable.toArray) {
			return iterable.toArray();
		} else {
			var results = [];
			for (var i = 0, length = iterable.length; i < length; i++)
				results.push(iterable[i]);
			return results;
		}
	}
}

Function.prototype.bind = function() {
	var __method = this, args = $A(arguments), object = args.shift();
	return function() {
		return __method.apply(object, args.concat($A(arguments)));
	}
}

