SAWUtils = function(){alert("Trying to create instance of static class SAWUtils");}

SAWUtils.getElementAbsolutePosition = function(vEl){
	var vLeft = 0,  vTop = 0;
	do{
		vLeft += vEl.offsetLeft;
		vTop += vEl.offsetTop;
		vEl=vEl.offsetParent;
	}while(vEl.tagName!='BODY');
	vRet = new Object();
	vRet.left = vLeft;
	vRet.top = vTop;
	
	return(vRet);
}

/**
Sets the width of an html element by adjusting its style - width attribute, until the offsetWidth property gets the desired value
*/
SAWUtils.setElementWidth = function(vHtmlElement, iWidth){
	for(vRepeat=0; vRepeat<2; vRepeat++){
		//document.getElementById('debug').innerHTML+="<br/>";
		var iWidth1=iWidth; var nRep;
		if(iWidth>0){
			nRep = 0; while(vHtmlElement.offsetWidth>iWidth && iWidth1>0 && nRep<10){
				iWidth1--;
				vHtmlElement.style.width = iWidth1+'px';
				nRep++;
			}
			iWidth1=iWidth;
			//document.getElementById('debug').innerHTML+="&nbsp;&nbsp;&nbsp;&nbsp;<strong>nRep="+nRep+"   width="+vHtmlElement.offsetWidth+"    iWidth="+iWidth+"    iWidth1="+iWidth1+"</strong>";
			nRep = 0; while(vHtmlElement.offsetWidth<iWidth && nRep<10){
				iWidth1++;
				vHtmlElement.style.width = iWidth1+'px';
				nRep++;
			}
			//document.getElementById('debug').innerHTML+="||&nbsp;&nbsp;&nbsp;&nbsp;<strong>nRep="+nRep+"   width="+vHtmlElement.offsetWidth+"    iWidth="+iWidth+"    iWidth1="+iWidth1+"</strong>";
		}
	}
}

/**
Sets the height of an html element by adjusting its style - height attribute, until the offsetHeight property gets the desired value
*/
SAWUtils.setElementHeight = function(vHtmlElement, iHeight){
	for(vRepeat=0; vRepeat<2; vRepeat++){
		var iHeight1 = iHeight;
		nRep = 0; while(vHtmlElement.offsetHeight>iHeight && iHeight1>1 && nRep<10){
			iHeight1--;
			vHtmlElement.style.height = iHeight1+'px';
			nRep++;
		}
		iHeight1 = iHeight;
		nRep = 0; while(vHtmlElement.offsetHeight<iHeight && nRep<10){
			iHeight1++;
			vHtmlElement.style.height = iHeight1+'px';
			nRep++;
		}
	}
}

SAWUtils.getMousePosition = function(e){
	var iMouseX = 0, iMouseY = 0;
	if(!e) e = window.event;
	if(e.clientX || e.clientY) { // IE
		iMouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		iMouseY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	else{
		iMouseX = e.pageX;
		iMouseY = e.pageY;
	}
	
	var so = new Object();
	so.left = Math.max(iMouseX, 0);
	so.top = Math.max(iMouseY, 0);
	return so;
}

/**
 transforms 32 bit decimal value to hex string; used for colors, called by SAWUtils.darkenColor
*/
SAWUtils.toHex32bit = function(iCol){
	var i1 = Math.floor(iCol/16),
		i2 = iCol - i1*16,
		s1 =''+i1,
		s2 =''+i2;
	if(s1=='10') s1='A'; else if(s1=='11') s1='B'; else if(s1=='12') s1='C'; else if(s1=='13') s1='D'; else if(s1=='14') s1='E'; else if(s1=='15') s1='F';
	if(s2=='10') s2='A'; else if(s2=='11') s2='B'; else if(s2=='12') s2='C'; else if(s2=='13') s2='D'; else if(s2=='14') s2='E'; else if(s2=='15') s2='F';
	
	return s1+s2;
}

/**
	reduces all color channels by 3*16 (0x3F)
*/
SAWUtils.darkenColor = function(sColor){
	if(sColor.length!=7 && sColor.substring(0,1)!='#'){
		alert('cannot parse color:'+sColor+', expecting color format `#A8F965`');
		return sColor;
	}
	iR = parseInt('0X'+sColor.substr(1,2));
	iG = parseInt('0X'+sColor.substr(3,2));
	iB = parseInt('0X'+sColor.substr(5,2));
	if(isNaN(iR) || isNaN(iG) || isNaN(iB)){
		alert('cannot parse color:'+sColor+', expecting hexagesimal color format `#A8F965`');
		return sColor;
	}

	iR -= 4*16; if(iR<0) iR = 0;
	iG -= 4*16; if(iG<0) iG = 0;
	iB -= 4*16; if(iB<0) iB = 0;
	
	
	return '#'+SAWUtils.toHex32bit(iR)+SAWUtils.toHex32bit(iG)+SAWUtils.toHex32bit(iB);
}


/**
blurs an html element - makes it semitransparent
**/
SAWUtils.blurElement = function(vEl){
	var vPos = SAWUtils.getElementAbsolutePosition(vEl),
		ix = vPos.left,
		iy = vPos.top,
		ih = vEl.offsetHeight,
		iw = vEl.offsetWidth,
		sId = vEl.id+"_blur",
		vElBlur = document.getElementById(sId);
	if(!vElBlur){
		vElBlur = document.createElement('span');
		vElBlur.id = sId;
		vEl.appendChild(vElBlur);
	}
	vElBlur.style.position='absolute';
	vElBlur.style.left = ix;
	vElBlur.style.top = iy;
	vElBlur.innerHTML = '<img width="'+iw+'" height="'+ih+'" src="images/blur.png"></img>';
}

/**
'unblurs' an html element - made semitransparent by a call to SAWUtils.blurElement()
**/
SAWUtils.unblurElement = function(vEl){
	vElBlur = document.getElementById(vEl.id+"_blur");
	if(vElBlur) vElBlur.innerHTML = '';
}