

/***** div functions *****/
function showDiv(divId) {
	if(is.dom){
		document.getElementById(divId).style.visibility = "visible";
	}else{
		eval(layerRef + '["' + divId + '"]' + styleRef + '.visibility = "visible"');
	}
}

function hideDiv(divId) {
	if(is.dom){
		document.getElementById(divId).style.visibility = "hidden";
	}else{
		eval(layerRef + '["' + divId + '"]' + styleRef + '.visibility = "hidden"');
	}
}

function moveDivTo(divId, x, y) {
	ref = ( is.dom ? document.getElementById(divId).style : document.all[divId].style );
	if (x!=null){
		if( ref.pixelLeft ){
			ref.pixelLeft = x;
		}else{
			ref.left = x;
		}
	}
	if (y!=null){
		if( ref.pixelTop ){
			ref.pixelTop = y;
		}else{
			ref.top = y;
		}
	}
}
function findDivPosition( divid ){
    var x=0; var y=0; var el,temp
    el = ie4 ? document.all[divid] : document.getElementById(divid);
	var w = el.offsetWidth;
	var h = el.offsetHeight;

	if(el.clienLeft){
		x=el.clientLeft;
		y=el.clientTop;
	}else{
		if(el.offsetParent){
		  temp = el
		  while(temp.offsetParent){ //Looping parent elements to get the offset of them as well
			temp=temp.offsetParent; 
			x+=temp.offsetLeft
			y+=temp.offsetTop;
		  }
		}
		x+=el.offsetLeft
		y+=el.offsetTop
	}
//    window.alert( x + ':' + y + ':' + w + ':' + h );

  //Returning the x and y as an array
  return [x,y,w,h]
}
/***** div functions *****/





/***** DynMenu *****/

DynMenu = function(){
	this.m = new Object;

	this.loaded = false;
//	this.over = false;
	this.on_div = false;
	this.on_img = false;
	this.on_img_away = false;

	this.divbase = 'dm_';
}


DynMenu.prototype.setBase = function( divbase ){
	this.divbase = divbase;
}


//DynMenu.prototype.addMenuToDiv = function( mid, divid, url ){
DynMenu.prototype.addMenuToDiv = function( mid, url ){
	this.m[mid] = new Object;
	this.m[mid].items = new Array;
//	this.m[mid].divid = divid;
	this.m[mid].url = url;
}

DynMenu.prototype.menuAddItem = function( mid, title, url ){
	var item = new Object;
	item.title = title;
	item.url = url;
	this.m[mid].items.push(item);
}


DynMenu.prototype.mouseOnDiv = function( e, mid ){
	if( !e ) e=window.event;
	if( !dynmenu.loaded ) return false;
	switch( e.type ){
		case "mouseover":
//			dynmenu.setMenuOn( e, mid );
			break;
		case "mouseout":
//			dynmenu.setMenuOff( e, mid );
			break;
	}

//	e.returnValue = false;
//	e.cancelBubble = true;
//	if( e.stopPropagation ){
		// dom (ie do not support this)
//		e.stopPropagation();
//		e.preventDefault();
//	}
	return false;
}


DynMenu.prototype.eventHand = function( e, mid ){
	if( !e ) e=window.event;
	if( !dynmenu.loaded ) return false;

	/*
	if( e.target ){		// dom
		var elm = e.target;
	}else{				// ie
		var elm = e.srcElement;
	}

	while ( (null!=elm) && (!elm.id) ) {
	    elm = elm.parentNode;
	}

	if( !elm ) return;
	mid = elm.id.slice( dynmenu.divbase.length );
	*/


	switch( e.type ){
		case "mousedown":
		case "click":
			window.location = dynmenu.m[mid].url;
			break;
		case "mouseover":
			dynmenu.setMenuOn( e, mid );
			break;
		case "mouseout":
//			dynmenu.setMenuOff( e, mid );
			this.on_img_away = true;
			break;
	}

//	e.returnValue = false;
//	e.cancelBubble = true;
//	if( e.stopPropagation ){
		// dom (ie do not support this)
//		e.stopPropagation();
//		e.preventDefault();
//	}
	return false;
}



DynMenu.prototype.setMenuOn = function( e, mid ) {
	if( mid && (mid!=this.on_img) ){
		if( this.on_img ){
			this.setImageOff( this.on_img );
			this.setDivOff( e, this.on_div );
		}
		this.setImageOn( mid );
		this.setDivOn( e, mid );
		this.on_img = mid;
	}
	this.on_img_away = false;
}
DynMenu.prototype.setMenuOff = function( e, mid ) {
	if (mid) {
		this.setImageOff( mid );
		this.setDivOff( e, this.on_div );
		if(this.on_img==mid) this.on_img = false;
	}
}




DynMenu.prototype.setDivOn = function( e, mid ) {
	if(mid && this.m[mid].items.length){
		pos = findDivPosition( this.divbase+mid );
		moveDivTo( this.divbase+mid+'_div', pos[0]-1, pos[1]+pos[3]-1 );

		if( this.on_div ){
			this.setDivOff( e, this.on_div );
		}

		// showDiv
		showDiv( this.divbase+mid+'_div' );
		this.on_div = mid;
	}
}
DynMenu.prototype.setDivOff = function( e, mid ) {
	if(mid && this.m[mid].items.length && this.on_div==mid){
		hideDiv( this.divbase+mid+'_div' );
		this.on_div = false;
	}
}




DynMenu.prototype.setImageOn = function( mid ) {
	if (document.images && mid ) {
		document.images[ this.divbase+mid+'_img' ].src = this.m[mid].imgon.src;
	}
}
DynMenu.prototype.setImageOff = function( mid ) {
	if (document.images && mid ) {
		document.images[ this.divbase+mid+'_img' ].src = this.m[mid].imgoff.src;
	}
}





DynMenu.prototype.preloadImages = function( ) {
	if( !document.images ) return;
	for( var mid in this.m ){
		this.m[mid].imgoff = new Image();
		this.m[mid].imgoff.src = this.getImageUrl( mid, (this.m[mid].active?1:0) );
		this.m[mid].imgon = new Image();
		this.m[mid].imgon.src = this.getImageUrl( mid, 1 );
	}
}



DynMenu.prototype.init = function( ) {
	this.location = window.location.pathname;

	// lvk lt workaroundas... :/
	if (this.location.indexOf( '/lt' ) == 0){
		this.location = this.location.slice( 3 );
	}

	for( var mid in this.m ){
		if( this.location.indexOf( this.m[mid].url.slice(0,-1) ) == 0 ){
			this.m[mid].active = true;
			break;
		}
	}
}

DynMenu.prototype.postInit = function( ) {
	this.preloadImages();
//	this.attach();

	// attach eater
	if( document.addEventListener ){	
		document.addEventListener("mouseover", dynmenu.popupEater, true );
	}else{
		document.onmouseover = dynmenu.popupEater;
	}

	this.loaded = true;
}




DynMenu.prototype.writeImageItem = function( mid ){
	var img = this.getImageUrl( mid, (this.m[mid].active?1:0) );
	var txt = '<div id="'+this.divbase+mid+'" onmouseover="dynmenu.eventHand(event,\''+mid+'\')" onmouseout="dynmenu.eventHand(event,\''+mid+'\')"><a href="'+this.m[mid].url+'"><img name="'+this.divbase+mid+'_img" src="'+ img +'"  height="17" border="0"></a></div>';
//	window.alert( txt );
	document.write( txt );
}


// sita funkcija galima overwritint?
DynMenu.prototype.getImageUrl = function( mid, over ){
	return '/pix/lvk_' + lang + '/titulinis/t'+ (over?'1':'0') +'_'+ mid + '.gif';
}

/*
DynMenu.prototype.attach = function( ) {
//	if( !dom ) return;
	for( var mid in this.m ){
//		elm = document.getElementById( this.m[mid].divid );
		elm = document.getElementById( this.divbase + mid );
		if( elm ){
			elm.addEventListener("click", dynmenu.eventHand, false);
			elm.addEventListener("mouseover", dynmenu.eventHand, false);
			elm.addEventListener("mouseout", dynmenu.eventHand, false);
		}
	}
}
*/

DynMenu.prototype.writeItemDivs = function( ){
    if(!dom) return;

	for( mid in this.m ){
		var it = this.m[mid].items;
		if( it.length > 0 ){
			var lign = '';
			var style = 'visibility: hidden; position:absolute; ';
//			if( ie4 && menu_position[i][2] ) lign = 'style="text-align:right;"';

			o = '<DIV id="'+dynmenu.divbase+mid+'_div" class="dynMenu" onMouseover="dynmenu.mouseOnDiv(event, \'' + mid + '\');" style="'+style+'" '+lign+'>';
			for( k in it ){
				o+= '<a href="' + it[k].url + '"><nobr>' + it[k].title + '</nobr></A><BR>';
			}
			o+= '</DIV>';
			document.writeln( o );
		}
	}
}






DynMenu.prototype.checkPopup = function( e ) {
	if( !e ) e=window.event;
	pos = findDivPosition( this.divbase + this.on_div + '_div' );
	var ex = e.clientX;
	var ey = e.clientY;
	if( !this.on_img || ( this.on_img_away && ( ex<pos[0] || ex>pos[0]+pos[2] || ey>pos[1]+pos[3] ) ) ){
//	if( ex<pos[0] || ex>pos[0]+pos[2] || ey>pos[1]+pos[3] ){
		this.setMenuOff( e, this.on_div );
	}
}


// var cnt=0;
DynMenu.prototype.popupEater = function( e ) {
	if( !e ) e=window.event;
//    window.status = ++cnt;
	if( !dynmenu.on_div ) return;
//    window.status = cnt + ' 4real';
	dynmenu.checkPopup( e );
}











