// Copyright (c) 2004 Pachollini [pch@nahory.net]

function c_hset()
	{
	var $i;

	this.$elements=find_class(document.body,"middle|(height[0-9]{1,3})");

	this.update=c_hset_update;
	this.rset=c_hset_reset;

	document.body.onresize=update_page;
	window.onresize=update_page;

	this.update(true);
	}

	function c_hset_reset()
		{
		var $i,$test_exp;
		for($i=0;$i<this.$elements.length;$i++)
			{
			$test_exp=new RegExp("(^| )(height([0-9]{1,3}))($| )");
			if($test_exp.test(this.$elements[$i].className)) this.$elements[$i].style.height="auto";
			$test_exp=new RegExp("(^| )(middle)($| )");
			if($test_exp.test(this.$elements[$i].className))this.$elements[$i].style.marginTop=0;;
			}
		}

	function c_hset_update($check_mod)
		{
		var $i,$amod,$modified;
		$modified=false;
		for($i=0;$i<this.$elements.length;$i++)
			{
			$amod=update_style(this.$elements[$i]);
			if($amod)$modified=true;
			}
		if($modified)setTimeout("$hset.update("+($check_mod?1:0)+")",10);
		else setTimeout("$hset.update(0)",1000);
		}

function update_style($element)
	{
	var $aheight,$val,$modified,$oldval,$test_exp,use_middle;
	var $parent_height=get_current_height($element.parentNode);
	$test_exp=new RegExp("(^| )(middle)($| )");
	$use_middle=$test_exp.test($element.className);
	$test_exp=new RegExp("(^| )(height([0-9]{1,3}))($| )");
	var $hclass=$test_exp.exec($element.className);
	if($hclass)
		{
		$oldval=$element.style.height;
		$aheight=$hclass[3];
		if($aheight<0)$aheight=0;
		if($aheight>100)$aheight=100;
		$val=$parent_height*$aheight/100;
		switch(navigator.appName)
			{
			case "Opera":
				$val-=(style2px(get_current_style($element.parentNode,"padding-top"))+style2px(get_current_style($element.parentNode,"padding-bottom")));
				break;
			case "Microsoft Internet Explorer":
					if(document.compatMode=="CSS1Compat")
						{
						$val-=(style2px(get_current_style($element.parentNode,"padding-top"))+style2px(get_current_style($element.parentNode,"padding-bottom")));
						}
					else
						{
						$val-=(style2px(get_current_style($element.parentNode,"padding-top"))+style2px(get_current_style($element.parentNode,"padding-bottom")));
						$val+=(style2px(get_current_style($element,"padding-top"))+style2px(get_current_style($element,"padding-bottom")));
						}
				break;
			default:
				break;
			}
		$val-=(style2px(get_current_style($element,"padding-top"))+style2px(get_current_style($element,"padding-bottom")));
		if(!$use_middle) $val-=style2px(get_current_style($element,"margin-top"));
		$val-=style2px(get_current_style($element,"margin-bottom"));
		$val-=(style2px(get_current_style($element,"border-top-width"))+style2px(get_current_style($element,"border-bottom-width")));
		if($val<0)$val=0;
		$element.style.height=$val+"px";
		if($oldval!=$element.style.height)$modified=true;
		}
	if($use_middle)
		{
		$oldval=$element.style.marginTop;
		$aheight=$element.style.height?style2px($element.style.height):get_current_height($element);
		$val=(($parent_height-$aheight)/2)-(style2px(get_current_style($element,"padding-top"))+style2px(get_current_style($element,"border-top-width")));
		switch(navigator.appName)
			{
			case "Opera":
				$val-=style2px(get_current_style($element.parentNode,"padding-top"));
				break;
			}
		$element.style.marginTop=$val+"px";
		if($oldval!=$element.style.marginTop)$modified=true;
		}
	return $modified;
	}

function get_current_style($element,$property)
	{
	var ee,$i;
	try
		{
		var $cs=document.defaultView.getComputedStyle($element,'');
		$val=$cs.getPropertyValue($property);
		}
	catch(ee)
		{
		$property=$property.split("-");
		for($i=1;$i<$property.length;$i++) $property[$i]=$property[$i].toUpperCase();
		$property=$property.join("");
		$val=$element.currentStyle.getAttribute($property);
		}
	return $val;
	}

function get_current_height($element)
	{
	var ee;
	try
		{
		var $cs=document.defaultView.getComputedStyle($element,'');
		$val=style2px($cs.getPropertyValue("height"));
		}
	catch(ee)
		{
		$val=($element.offsetHeight);
		if($val<0)$val=0;
		}
	return $val;
	}

function update_page(e)
	{
	if(!e)e=window.event;
	$hset.rset();
	setTimeout("$hset.update(true)",10);
	try{e.stopPropagation();}
	catch(ee){}
	}

function find_class($element,$classnames,$result,$first)
	{
	if(!$first)$first=$element;
	if(!$result)$result=new Array();
	if ($element.nodeType==1)
		{
		var $test_exp=new RegExp("(^| )("+$classnames+")( |$)");
		if($test_exp.test($element.className)) $result[$result.length]=$element;
		}
	if ($element.hasChildNodes()) $result=find_class($element.firstChild,$classnames,$result,$first);
	if ($element.nextSibling && $element!=$first) $result=find_class($element.nextSibling,$classnames,$result,$first);
	return $result;
	}

function style2px(hodnota)
	{
	if (hodnota) return Number(hodnota.substr(0,hodnota.indexOf("px")));
	else return 0
	}