function getElById(id) {
	var tere = (typeof(id) == 'object'?id:document.getElementById(id));
	return (tere?tere:false);
}

function addEvent(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};


function removeEvent(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};

function getScrollXY() {
	var scrOfX = typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
	var scrOfY = typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
	return [ scrOfX, scrOfY ];
}

function getWinSize() {
	var windowHeight = window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
	var windowWidth = window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
	return [ windowWidth, windowHeight ];
}	

function getPageSize() {
	var xScroll,yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	return [ xScroll, yScroll ];
}

function getMouseXY(e,panull) {
	var tempX = 0;
	var tempY = 0;
	var scrollxy = getScrollXY();
 
	if (!e) var e = window.event;
	if (e.clientX || e.clientY) {
		tempX = e.clientX + scrollxy[0];
		tempY = e.clientY + scrollxy[1];
	} else if (e.pageX || e.pageY) {
		tempX = e.pageX;
		tempY = e.pageY;
	}
	if (panull==undefined) {
		if (tempX < 0){tempX = 0}
		if (tempY < 0){tempY = 0}
	}
	
	return [ tempX, tempY ];
}

function findPosXY(obj) {
	var curleft = 0;
	var curtop = 0;	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop	
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
		curtop += obj.y;
	}
	var res = new Array(curleft,curtop);
	return res;
}


var isDrag = false;
var drx,dry;
var dobj;
function startDrag(e,w,opcust) {
 if (!e) e = window.event;
 var mXY = getMouseXY(e);
 if (!isDrag) {
  dobj = getElById(w);
  var scrl = getScrollXY();
  var winS = getWinSize();
  var pgSize = getPageSize();
  dobj.setAttribute('winsizew',winS[0]-(pgSize[1]>winS[1]?20:2));
  dobj.setAttribute('winsizeh',winS[1]-(pgSize[0]>winS[0]?20:2));
  opcust = opcust!=undefined?opcust:70;
  dobj.setAttribute('opaa',opcust)
  if (opcust < 100) setOpacity(dobj,opcust);
  addEvent(document,'mousemove',startDrag);
  addEvent(document,'mouseup',endDrag);
  isDrag = true;  
  var oXY = findPosXY(dobj);
  drx = mXY[0] - oXY[0];
  dry = mXY[1] - oXY[1];
  
 }
 else {
  var scrollxy = getScrollXY();
  var winSize = [ dobj.getAttribute('winsizew'), dobj.getAttribute('winsizeh') ];
  var newLeft = mXY[0] - drx;
  var newTop = mXY[1] - dry;
  if (newLeft > (winSize[0]-dobj.offsetWidth+parseInt(scrollxy[0]))) newLeft = winSize[0]-dobj.offsetWidth+parseInt(scrollxy[0]);
  if (newTop > (winSize[1]-dobj.offsetHeight+parseInt(scrollxy[1]))) newTop = winSize[1]-dobj.offsetHeight+parseInt(scrollxy[1]);
  if (newLeft < scrollxy[0]) newLeft = scrollxy[0];
  if (newTop < scrollxy[1]) newTop = scrollxy[1];
  dobj.style.left = newLeft+'px';
  dobj.style.top = newTop+'px';
  // noņemam selectus
  document.body.focus();
  if (document.selection) document.selection.empty();
  return false;
 }
}

function endDrag() {
 removeEvent(document,'mousemove',startDrag);
 removeEvent(document,'mouseup',endDrag);
 var opcust = dobj.getAttribute('opaa');
 if (opcust < 100) setOpacity(dobj,100);
 dobj.removeAttribute('opaa');
 isDrag=false;
}

function winHeight() {
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}
function winWidth() {
	return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}
		
function posTop() {
	return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}

function hidePop(el) {
 var b = document.getElementById(el);
 if (b) b.style.visibility='hidden';
}

function blinkBack(el,col,bg,stay) {
	if (el) {
		col = col || '#D7FFD7'; //#FEFBC0
		bg = bg || '';
		if (el.blinkMemTim) { clearTimeout(el.blinkMemTim); }
		if (el.blinkMemInt) { clearInterval(el.blinkMemInt); }
		var blstep = 1;
		el.blinkMemInt = setInterval(
			function () {
				if ((blstep%2)==1) { el.style.backgroundColor=col; }
				else { el.style.backgroundColor=bg; }
				blstep++;
				if (blstep > 7) {
					clearInterval(el.blinkMemInt);
					if (stay == undefined) { el.blinkMemTim = setTimeout(function() { el.style.backgroundColor=bg; },3000); }
				}
			},
		150);
	}
}
