function findPosX(obj) {
	var curleft = 0;
	if (document.getElementById || document.all)
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	return curleft;
}


function findPosY(obj) {
	var curtop = 0;
	if (document.getElementById || document.all)
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	return curtop;
}


function snapToObj(type,objMove,objRef,offset,otherActions)
{
	if(!(objRef=findObj(objRef)) || !(objMove=findObj(objMove))) return
	if (type=='height') 
	{
		objMove.style.height = objRef.offsetHeight + offset + 'px'
		if (otherActions)
		{
			if (arrSearch(otherActions,'modifyClip')) cssModify_clip(objMove,'','',objRef.offsetHeight-offset,'')
		}
	}
	if (type=='width') 
	{
		objMove.style.width = objRef.offsetWidth + offset + 'px'
		if (arrSearch(otherActions,'modifyClip')) cssModify_clip(objMove,'',objRef.offsetWidth + offset,'','')
	}
	if (type=='topLeftCorner') 
	{
	}
	if (type=='topRightCorner') 
	{
		var ref_right = findPosX(objRef) + objRef.offsetWidth
		objMove.style.left = ref_right + offset[0] + 'px'
		objMove.style.top = findPosY(objRef) + offset[1] + 'px'
	}
	if (type=='bottomLeftCorner')
	{
		objMove.style.left = findPosX(objRef) + offset[0] + 'px'
		objMove.style.top = findPosY(objRef) + objRef.offsetHeight + offset[1] + 'px'
	}
	if (type=='bottomRightCorner')
	{
		var ref_right = parseInt(objRef.style.left) + parseInt(objRef.style.width)
		objMove.style.left = ref_right + offset[0] + 'px'
		objMove.style.top = parseInt(objRef.style.top) + objRef.offsetHeight + offset[1] + 'px'
	}
	if (type=='top')
	{
		objMove.style.top = findPosY(objRef) + offset + 'px'
	}
	if (!otherActions) otherActions = ['visibility','display']
	if (otherActions)
	{
		if (arrSearch(otherActions,'visibility'))
		{
			objMove.style.visibility = 'visible'
		}
		if (arrSearch(otherActions,'display'))
		{
			objMove.style.display = 'block'
		}
	}
}


function snapToViewport(type,obj,objRef,offset,otherActions)
{	
	if (typeof(obj)=='string') obj = findObj(obj)
	if (typeof(objRef)=='string') objRef = findObj(objRef)
	var viewPort = viewportDimensions()
	if (type=='height') 
	{
		obj.style.height = viewPort.height - findPosY(objRef) + offset + 'px'
		if (arrSearch(otherActions,'modifyClip'))
		{
			cssModify_clip(obj,'','',viewPort.height - findPosY(objRef) + offset,'')
		}
	}
}


function cssModify_clip(obj,top,right,bottom,left)
{
	if (typeof(obj)=='string') obj = findObj(obj)
	var clipStr = obj.style.clip
	var clipNum = clipStr.match(/\((.+)\)/)
	var clipArr = clipNum[1].split(' ');
	if (top!=='')
	{}
	if (right!=='')
	{
		clipArr[1] = right+'px';
	}
	if (bottom!=='')
	{
		clipArr[2] = bottom+'px';
	}
	if (left!=='')
	{}
	obj.style.clip = 'rect('+clipArr.join(' ')+')'
}


function viewportDimensions()
{
	var res = new Object()
	if (self.innerHeight)
	{
		res.width = self.innerWidth;
		res.height = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		res.width = document.documentElement.clientWidth;
		res.height = document.documentElement.clientHeight;
	}
	else if (document.body) 
	{
		res.width = document.body.clientWidth;
		res.height = document.body.clientHeight;
	}
	return res
}
