var activeArray = new Array();

function previewImage( file, target )
{
	var iLen = String( file ).length;
	var cLast = String( file ).substring( iLen-1 );
	if( cLast == '/' )
		file = file + "1px.gif";

	var a = document.getElementById( target );
	if( a )
		a.src=file;
		
	return true;	
}

function switchImage( file, id )
{
	var a = document.getElementById( id );
	if( a )
		a.src=file;
	return true;
}

function getAbsPos(elt,which)
{
	var iPos = 0;
	while (elt != null)
	{
		iPos += parseInt( elt["offset" + which] );
		elt = elt.offsetParent;
	}
 
 	return iPos;
}

function activateSubMenu( id, parent, pos )
{
	if( addRef( id ) == 1 )
	{
		var a = document.getElementById( id );
		if( a )
		{
			a.style.display='inline';
			if( pos == "left" )
			{
				//alert( parent.currentStyle.marginLeft );
				a.style.left = getAbsPos( parent, "Left" ) + parent.offsetWidth + "px";
				a.style.top = getAbsPos( parent, "Top" ) + "px";
			}
			else if( pos == "right" )
			{
				a.style.left = 	getAbsPos( parent, "Left" ) - a.offsetWidth + "px";
				a.style.top = getAbsPos( parent, "Top" ) + parent.offsetHeight - a.offsetHeight + "px";
			}
			else if( pos == "top" )
			{
				a.style.left = 	getAbsPos( parent, "Left" ) + "px";
				a.style.top = getAbsPos( parent, "Top" ) + parent.offsetHeight + "px";
			}
			else if( pos == "bottom" )
			{
				a.style.left = 	getAbsPos( parent, "Left" ) + "px";
				a.style.top = getAbsPos( parent, "Top" ) - a.offsetHeight + "px";
			}
			
		}
	}
}

function deActivateSubMenu( id )
{
	setTimeout( "releaseRef('"+ id  + "')", 200 );
}

function addRefs()
{
//	alert( "addRefs" );
//	alert( arguments.length );
//	alert( arguments[0] );
	for( var i=0; i<arguments.length; i++ )
		addRef(arguments[i]);
}

function releaseRefs()
{
	for( var i=0; i<arguments.length; i++ )
		deActivateSubMenu( arguments[i] );
		//releaseRef(arguments[i]);
		
//	alert( "releaseRefs" );
//	alert( arguments.length );
//	alert( arguments[0] );
	
}

function addRef( id )
{
	if( !activeArray[id])
		activeArray[id]=0;
	activeArray[id]++;

	return activeArray[id];
}

function releaseRef( id )
{
	if( !activeArray[id])
		activeArray[id]=0;
	activeArray[id]--;
	
	if( activeArray[id] == 0 )
	{
		var a = document.getElementById( id );
		if( a )
			a.style.display='none';
	}
}

function change_class( obj, className )
{
   obj.className=className;
}

//Skaalautuva popup (kuvat)
  function PopupPic(sPicURL) {
     newwindow=window.open( "popup.htm?"+sPicURL, "",  
     "resizable=1,HEIGHT=200,WIDTH=200");
	 if (window.focus) {newwindow.focus();
	newwindow.moveTo(150,150);}
   }