/*
  BGZ, common.js
  Copyright (C) 2009 Intercon e-consulting, http://www.intercon.pl
*/

var BGZApps = new function () {
	var self = this;
	var version = "1.0.0 :: 07/08/09";
	var inited = false;

	this.getVersion = function () {
		return version;
	};

	this.linkTo = function (linkName) {
		if (!linkName) return false;
		var winName = 'BGZIncWin_' + new Date().getTime();
		Utils.popupOpen(linkName, 750, 600, winName);
	};
};

var __Utils__ = function () {
	var w = window;
	var d = document;
	var version = "1.01 :: 01/04/09";

	this.getVersion = function () {
		return version;
	};

	this.getId = function (eid, doc) {
		var el = null;
		try {
			if (doc) {
				el = doc.getElementById (eid);
			} else {
				el = d.getElementById (eid);
			}
			return el;
		} catch (e) {}
		return false;
	};
	this.getClass = function (oElm, sTagName, sClassName) {
		var aElms = (sTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName (sTagName);
		var aReturnElms = [];
		sClassName = sClassName.replace (/-/g, "\-");
		var oRegExp = new RegExp("(^|\s)" + sClassName + "(\s|$)");
		var elm;
	
		for (var i = 0; i < aElms.length; i++) {
			elm = aElms[i];
			if (oRegExp.test (elm.className)) {
				aReturnElms.push (elm);
			}
		}
		return (aReturnElms);
	};
	this.blurAnchovis = function () {
		if(d.getElementsByTagName) {
			var a = d.getElementsByTagName("A");
			for(var i = 0; i < a.length; i++) {
				a[i].onfocus = function() {this.blur()};
			}
		}
	}
	this.addEvent = function (obj, fnc, act) {
		if(obj.addEventListener)
			obj.addEventListener(act, fnc, false);
		else if(obj.attachEvent)
			obj.attachEvent('on' + act, fnc);
		return false;
	};
	this.removeEvent = function (obj, fnc, act) {
		if(obj.removeEventListener)
			obj.removeEventListener(act, fnc, false);
		else if(obj.detachEvent)
			obj.detachEvent('on' + act, fnc);
		return false;
	};
	this.saveCookie = function (name, value, days) {
		var expires = '';
		if (days) {
			var date = new Date ();
			date.setTime (date.getTime () + (days * 86400000));
			var expires = '; expires=' + date.toGMTString ();
		}
		document.cookie = name +'='+ value + expires + '; path=/';
	};
	this.readCookie = function (name) {
		var nameEq = name + '=';
		var ca = document.cookie.split (';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt (0) == ' ')
				c = c.substring (1, c.length);
			if (c.indexOf (nameEq) == 0)
				return c.substring (nameEq.length, c.length);
		}
		return null;
	};
};

var Utils = new __Utils__();

__Utils__.prototype.popupOpen = function (win, x, y, n, p) {
	if (!win) return false;
	var winName = n ? n : 'BGŻ';
	var params = p ? p : 'resizable=no,toolbar=no,status=no,location=no,menubar=no,personalbar=no,scrollbars=yes';
	var t = Math.round((Utils.getWindowY() - y) / 2);
	var l = Math.round((Utils.getWindowX() - x) / 2);
	var nw = window.open(win, winName, 'top=' + t + ',left=' + l + ',width=' + x + ',height=' + y + ',' + params);
	return false;
};

__Utils__.prototype.getWindowX = function () {
	var wW = 0;
	if (typeof window.innerWidth == 'number') {
		wW = window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		wW = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		wW = document.body.clientWidth;
	}
	return wW;
};

__Utils__.prototype.getWindowY = function () {
	var wH = 0;
	if (typeof window.innerWidth == 'number') {
		wH = window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		wH = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		wH = document.body.clientHeight;
	}
	return wH;
};
