﻿/*--------------------------------------------------
	CORE functions, objects and utilities
--------------------------------------------------*/

// Vyhledani tagu dle jeho ID
function FindTag(id)
{
	var tag = null;
	if (document.body) {
		if (document.body.getElementById) tag = document.body.getElementById(id);
		if (tag == null && document.body.all) tag = document.all[id];
	}
	if (document.getElementById) tag = document.getElementById(id);
	if (tag == null && document.all) tag = document.all[id];
	return tag;
}

// Zjistí informace o okne prohlizece (velikosti apod.)
// * pouziva se jako objekt
function Screen(okno)
{
	var win = (okno != null ? okno : window);
	if (win.innerHeight) {
		this.pageHeight = parseInt(win.innerHeight, 10);
	} else if (win.document.documentElement && win.document.documentElement.clientHeight) {
		this.pageHeight = parseInt(win.document.documentElement.clientHeight, 10);
	} else if (win.document.body && win.document.body.clientHeight) {
		this.pageHeight = parseInt(win.document.body.clientHeight, 10);
	}
	if (win.innerWidth) {
		this.pageWidth = parseInt(win.innerWidth, 10);
	} else if (win.document.documentElement && win.document.documentElement.clientWidth) {
		this.pageWidth = parseInt(win.document.documentElement.clientWidth, 10);
	} else if (win.document.body && win.document.body.clientWidth) {
		this.pageWidth = parseInt(win.document.body.clientWidth, 10);
	}
	if (win.document.body.scrollLeft && win.document.body.scrollTop) {
		this.scrollX = parseInt(win.document.body.scrollLeft, 10);
		this.scrollY = parseInt(win.document.body.scrollTop, 10);
	} else {
		this.scrollX = parseInt(win.scrollX, 10);
		this.scrollY = parseInt(win.scrollY, 10);
	}
}

// Zobrazi vlastnosti zadaneho objektu
function ShowProperties(obj, withValues)
{
	var txt = "";
	txt = "Object properties";
	if (obj == null) {
		txt += "\r\n - object is null";
	} else {
		for (var prop in obj) {
			txt += "\r\n - " + prop;
			if (withValues != null && (withValues == true || withValues == 1)) {
				txt += ": " + eval("obj." + prop);
			}
		}
	}
	alert(txt);
}

// Zjisti obsazeny dokument v IFRAME elementu
function GetIframeDocument(iframe)
{
	if (iframe == null) return null;
	if (iframe.contentDocument) return iframe.contentDocument;
	if (iframe.contentWindow && iframe.contentWindow.document) {
		return iframe.contentWindow.document;
	}
	if (iframe.document) return iframe.document;
	return null;
}

// Zjisti okno v IFRAME elementu
function GetIframeWindow(iframe)
{
	if (iframe == null) return null;
	if (iframe.contentWindow) {
		if (iframe.contentWindow.window) return iframe.contentWindow.window;
		return iframe.contentWindow;
	}
	if (iframe.window) return iframe.window;
	return null;
}

function Point(x, y)
{
	this.x = x;
	this.y = y;
}

function GetElementPosition(obj)
{
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		while (true) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			if (!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
	} else if (obj.x && obj.y) {
		curleft += obj.x;
		curtop += obj.y;
	}
	return new Point(curleft, curtop);
}

/*--------------------------------------------------
	BROWSER detection functions and objects
--------------------------------------------------*/

var BROWSER = {
	init: function () {
		this.name = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "MSIE",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};
BROWSER.init();

/*
	Otevře okno prohlížeče požadované velikosti a nastaví jej dle parametrů
*/
function OpenBrowserWindow(AUrl, AWidth, AHeight, AMenu, AScroll, AStatus, AResize, AName)
{
	var menu = (AMenu != null && AMenu != "") ? AMenu : "no";
	var scroll = (AScroll != null && AScroll != "") ? AScroll : "no";
	var status = (AStatus != null && AStatus != "") ? AStatus : "no";
	var resize = (AResize != null && AResize != "") ? AResize : "yes";
	var name = (AName != null && AName != "") ? AName : "_blank";
	return window.open(AUrl, name, "width=" + AWidth + ",height=" + AHeight + ",directories=no,location=no,menubar=" + menu + ",personalbar=no,resizable=" + resize + ",scrollbars=" + scroll + ",status=" + status + ",toolbar=no");
}

/*
	Provede automatický klik na tlačítko buttonId
*/
function AutoClick(buttonId, useDoPostBack)
{
	var tag = FindTag(buttonId);
	if (tag == null) return;
	if (tag.focus) tag.focus();
	if (useDoPostBack === true && window.__doPostBack) {
		window.__doPostBack(tag.name, "");
	} else {
		if (tag.click) tag.click();
		else tag.form.submit();
	}
}