// browser detection

var agt = navigator.userAgent.toLowerCase(),
	major = parseInt(navigator.appVersion),
	// array of settings
	dhtmlSettings = new Array(),
	// cookie-related
	dhtmlTheDomain = '.delfi.ee',
	DHTML_NO_COOKIES = -1,
	DHTML_IGNORE_COOKIES = -2;

macos = (agt.indexOf('mac') != -1),
opera = (agt.indexOf('opera') != -1),
nav  = ((agt.indexOf('mozilla') != -1) && (agt.indexOf('spoofer') == -1)
	&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera') == -1)
	&& (agt.indexOf('webtv') == -1) && (agt.indexOf('hotjava') == -1)),
ie = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1)),
konq = (agt.indexOf('konqueror') != -1);
nav4 = nav && (major >= 4),
ie3 = (ie && (major < 4)),
ie4 = (ie && (major == 4) && (agt.indexOf('msie 5') == -1) && (agt.indexOf('msie 6') == -1));

function exists(a) {
	return typeof(a) != 'undefined';
}

function dhtmlSet(which, what) {
	if (!exists(dhtmlSettings[which])) {
		dhtmlSettings[which] = what;
	}
}

function dhtmlGet(what) {
	return dhtmlSettings[what];
}


function dhtmlSetCook(name, t, exp) {
	var c, d = new Date();
	d.setTime(d.getTime() + exp * 60 * 1000);

	c = name + '=t='+ t + '; expires=' + d.toGMTString() + '; domain=' + dhtmlTheDomain + '; path=/';

	document.cookie = c;

	// PUs by pu, note: no expire, so it's session cookie
	document.cookie = name + 's=1';
}

// checks if browser has cookies disabled by setting tmp cookie and querying it.
// ideas from: http://www.rgagnon.com/jsdetails/js-0092.html
function dhtmlCookiesAvail() {
	var d, t = 'dhtmlQuery=',
		c = t + '1' + '; path=/; domain=' + dhtmlTheDomain + '; path=/';
	document.cookie = c;
	if (document.cookie.indexOf(t, 0) < 0) {
		return false;
	}

	// clear that cookie
	d = new Date();
	d.setTime(d.getTime() - 1);
	document.cookie = c + '; expires=' + d.toGMTString();
	return true;
}

function dhtmlGetCook(x) {
	var a,s,e;
	a = document.cookie;

	// this assumes we always have dcid cookie
	// and our test could fail if user has rejected cookies from t.delfi.xx
	if (a == '') {
		// no cookies. disabled or just none?
		if (!dhtmlCookiesAvail()) {
			return DHTML_NO_COOKIES;
		}
	}

	s = a.indexOf(x + '=');
	if (s == -1) {
		return false;
	}
	s += x.length+1;
	e = a.indexOf(';',s);
	if (e == -1) {
		e = a.length;
	}
	return a.substring(s,e);
}

// returns value for 't' from query-string alike string
function dhtmlGetCount(c) {
	var i, d;
	c = c.split('&');
	for (i = 0; i < c.length; i++) {
		d = c[i].split('=');
		if (d == "t") {
			last;
		}
	}
	return d[1];
}

if (window.addEventListener) {
	window.dhtmlPush = function(code) {
		if (typeof(code) == 'string') {
			code = eval(code);
		}
		window.addEventListener("load", code, false);
	}
} else if (window.attachEvent)  {
	window.dhtmlPush = function(code) {
		if (typeof(code) == 'string') {
			code = eval(code);
		}
		window.attachEvent("onload", code);
	}
} else {
	(function() {
		// array events function pointers to run on page load event
		var dhtmlRunArray = new Array(),
			o = window.onload;

		// add method to run when page has loaded
		window.dhtmlPush = function(code) {
			if (typeof(code) == 'string') {
				code = eval(code);
			}
			dhtmlRunArray[dhtmlRunArray.length] = code;
		}

		window.onload = function() {
			try {
				if (o) {
					o();
				}
			} catch(e) {
			}
			for (var i = 0; i < dhtmlRunArray.length; i++) {
				dhtmlRunArray[i]();
			}
		}
	})();
}

