/* 
	kello-lite js library 1.0
	update: 18.09.2007 (functions -> object) back-compatibility
	
	this script is under GNU GPL licence http://www.gnu.org/licenses/gpl.txt
	it contains some free scripts so you can also feel free using it
	but except for commercial use which needs permission of the author
*/


	/* 
		getPosById: returns array with position of given object:
		position[0] - left; position[1] - top;
		obj = id of destination object
	*/
	
	function $ (id) {
		return document.getElementById (id);
	}
	
	function getPosById (obj) {
		return getPos (obj, 1);
	}
	
	function getPosByRef (obj) {
		return getPos (obj, 0);
	}
	
	function getPos (obj, how) {
		if (how) {
			var el = $(obj);
		} else {
			var el = obj;
		}
		var el = $(obj);
		var pleft = 0;
		var ptop = 0;
		if (el.offsetParent) {
			while (el.offsetParent) {
				pleft += el.offsetLeft
            ptop += el.offsetTop
				el = el.offsetParent;
			}
		} else if (el.x) {
			pleft += el.x;
			ptop += el.y;
		}
      var position = Array (pleft, ptop);
		return position;
	}
	
	/* 
		showHide:
		obj - id of destination object
		act='' - acts alternately
		act=1  - always show object
		act=0  - always hide object
	*/	 
	function showHide (obj, act) {
      var element = $(obj);
		if (act == null) act = 1;
		if (act == 'toggle') {
			if ($(obj).style.display=='none') act = 'show';
			else act = 'hide';
		}
		with (element.style) {
			if (!act || act=='hide') {
				visibility = 'hidden';
				display = 'none';
			} else {
				visibility = 'visible';
				display = 'block';
			}
		}
	}

	/* 
		setPosByEl: set position of edest element relatively to esrc element
		edest, esrc - id's of elements
		xx - shift to the right
		yy - shift to the bottom
	*/
	function setPosByEl (edest,esrc,xx,yy) {
		var obj = $(edest);
		var position = getPosById (esrc);
		var pX = position[0];
		var pY = position[1];
		with (obj.style) {
			position = 'absolute';
			left = (pX+xx)+'px';
			top = (pY+yy)+'px';		
		}
	}

	/*
		getTagsByRef: return an array with references to elements with tags `stag` and rel=`srel`
		stag - tag's name
		srel - rel in tags
	*/
	function getTagsByRef (stag, srel) {
		var element = Array ();
		var j = 0;
		var i = 0;
		while (ab=document.getElementsByTagName(stag)[i]) {
	   	if (rel=ab.getAttribute('rel')) {
				if (rel.indexOf(srel) != -1) element[j++] = ab;
	   	}
         i++;
	   }
		return element;
	}

	/* 
		getTagsById: returns an array with id's of tags `stag` and rel=`srel`
		stag - tag's name
		srel - rel in tags
	*/
	function getTagsById (stag, srel) {
		var element = Array ();
		var els = getTagsByRef (stag, srel);
		var els_ile = els.length;
		for (i=0; i<els_ile; i++)
			element[i] = els[i].getAttribute('id');
		return element;
	}
	
	/*
		windowSize: returns an array containing browser window size
	*/
	function windowSize () {
		if (document.all) {
			bwys = document.body.clientHeight;
			bszer = document.body.clientWidth;
		} else {
			bwys = document.documentElement.clientHeight;
			bszer = document.body.clientWidth;
		}
      var size = Array (bszer, bwys);
		return size;
	}
	
	var Cookies = {
		setCookie: function (name, value, days) {
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";		
		},
		getCookie: 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;		
		},
		eraseCookie: function (name) {
			createCookie(name,"",-1);		
		}
	}

	function setStyle (obj, styl, wartosc) {
		$(obj).style.styl = wartosc;
	}

	/*
		centreElement: makes element `obj` centered, but it doesn't take a window scroll
		obj - element to be centre
		ww - width of obj
		hh - height of obj
	*/
	function centreElement (obj,ww,hh) {
		wsize = windowSize ();
		winW = wsize[0];
  		winH = wsize[1];
		with ($(obj).style) {
			position = 'absolute';
			left = Math.round(winW/2)-Math.round(ww/2)+'px';
			top = Math.round(winH/2)-Math.round(hh/2)+'px';
			zIndex = '999';
		}
	}
	
	/*
		centreWithScroll: makes element `obj` centered, but the window scroll is taken
		obj - element to be centred
		ww - width of obj
		hh - height of obj
	*/
	function centreWithScroll (obj, ww, hh) {
		var przesuniecie = document.documentElement.scrollTop | document.body.scrollTop;
		size = windowSize ();
		bwys = size[1];
		bszer = size[0];
		var wspY = Math.round(bwys/2) - Math.round(hh/2);
		var wspX = Math.round(bszer/2) - Math.round(ww/2);
		with ($(obj).style) {
			position = 'absolute';
			left = wspX + 'px';
			top = wspY + przesuniecie + 'px';
			zIndex = '999';
		}
	}

	/*
		fullScreen: resizes `obj` to full screen
		obj - element to be resized
	*/
	function fullScreen (obj) {
		size = windowSize ();
		bwys = size[1];
		bszer = size[0];
		pagesize = getPageSizeWithScroll();
		bwys = pagesize[1];
		bszer = document.body.clientWidth;		
		with ($(obj).style) {		
			position = 'absolute';
			width = bszer + 'px';
			height = bwys + 'px';
			top = '0px';
			left = '0px';
			zIndex = '998';
		}
		showHide (obj, 1);
	}

	function getPageSizeWithScroll () {
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
	  	}
		arrayPageSizeWithScroll = new Array (xWithScroll,yWithScroll);
		//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
		return arrayPageSizeWithScroll;
	}
	
/*
	things below should stay to preserve back compatibility with elder kelite.js
*/

	function createCookie (name,value,days) {
		Cookies.setCookie (name, value, days);
	}	

	function readCookie (name) {
		Cookies.getCookie (name);
	}

	function eraseCookie (name) {
		Cookies.eraseCookie (name);
	}	

