// JavaScript Document
// menuline2
// code by Azer Manafov (azerman[at]hotmail[dot]com)
// .js encoding - "utf-8"

function clsLineMenu( divId, visihlp )
{   var m_divId   = (divId == 'undefined')?null:divId;
    var m_prop    =
    {   imgactive: new Array( 'images/sys/menus/mnu_ac_l.png','images/sys/menus/mnu_ac_c.png','images/sys/menus/mnu_ac_r.png'),
		imgnormal: new Array( 'images/sys/menus/mnu_of_l.png','images/sys/menus/mnu_of_c.png','images/sys/menus/mnu_of_r.png'),
		imghover:  new Array( 'images/sys/menus/mnu_on_l.png','images/sys/menus/mnu_on_c.png','images/sys/menus/mnu_on_r.png'),
		clractive: '#ffffff',
		clrnorm:   '#808080',
		clrhover:  '#000000',
		tdheight:  24
	};
	var m_menuprefix = "mnuitem";
    var m_this     = this;
	var m_drop_tm    = null;
    var m_drop_hmouse = null;

    var m_arItems    = new Array( );
	var m_visihlp    = (visihlp == 'undefined')?new clsViewHelper():visihlp;
	var m_fontstyle  = 
	{
		'font-size'  : '12px', 
		'font-family': 'Arial',
		'font-weight': 'normal',
		'color'      : '#000000'
	};
	
	var m_menuarray  = new Array( );
	var m_menulevel  = 0;
	
    this.getItems = function( ) { return m_arItems; }; 
	this.addItem  = addItem;
	this.addSubitem = addSubitem;
	this.create   = create;
	this.activeId = 1;

	this.setProperties  = setProperties;
	this.clickmenuitem  = clickmenuitem;

    function setProperties( prop )
	{	for ( var i in prop )
		{   if ( typeof m_prop[i] != 'undefined' )
			     m_prop[i] = prop[i];
		}
	    for ( var i in prop )
		{   if ( typeof m_fontstyle[i] != 'undefined' && m_fontstyle[i] != undefined )
			     m_fontstyle[i] = prop[i];
		}
	}
    function itemById  ( id )
	{   for ( var  i = 0; i < m_arItems.length; i++ )
		{    
		      if ( m_arItems[i].id == id ) return m_arItems[i];
		}
		return null;	  
	}
    function itemByName( name )
	{   for ( var  i = 0; i < m_arItems.length; i++ )
		      if ( m_arItems[i].title == name ) return m_arItems[i];
		return null;	  
	}
    function addSubitem( ident, objLnav )
	{   var itm = null;
		if ( typeof ident == 'string')
		     itm = itemByName( ident );
		else itm = itemById  ( ident );

		if ( itm )
		{
		    itm.submenu = objLnav;
	        var drop_name = "subitm_"+itm.id;
			
			var dim       = objLnav.calcDimension( );
            if ( dim.width == 0 ) dim.width = 180;//'background-color':'#FFFFFF',
            var styles    =  { 'z-index':100, 'position':'absolute', 'left':'392px', 'top':'199px', 
				               'width':(dim.width+'px'), 'border':'0px solid red', 'visibility':'hidden'};
			var drop_div  = m_visihlp.createDivLayer ( document.getElementsByTagName("body")[0], drop_name, styles );
			objLnav.setdivid( drop_name );

            if ( !m_drop_tm )			
			{   m_drop_tm = new timerAction( dropexec, dropexec,dropexec);
				m_drop_tm.callstoponkill( true );
                m_drop_tm.setBreakTime( 1000 );
			}
		}
	}
	
    function addItem ( sLink, sText, sId )
	{   for ( var i = 0; i < m_arItems.length; i++ )
		{   if ( m_arItems[i].title == sText )
		    {
				alert( "Dublicate item:"+sText);
			     return;
			}
		}
		var sId = (sId == 'undefined' )?(m_arItems.length+1):sId;  
	    m_arItems.push( {id:(m_arItems.length+1),link:sLink,title:sText,numid:sId, btn:null,submenu:null});
	    
	}
	function build   ( activeId )
	{   var s = '';
        s += '<table id="menutable" cellpadding="0" cellspacing="2" border="0" >';
        s += '<tbody><tr>';
		for ( var i = 0; i < m_arItems.length; i++ )
		{   var lnk   = m_arItems[i].link;
		    var id    = m_arItems[i].id;
		    var title = m_arItems[i].title;
			var itm   = m_menuprefix + (i+1);
			var btn   = 'mnubtn'  + (i+1);

			m_arItems[i].btn  = new clsTblButton( itm,btn, title,m_visihlp);
			m_arItems[i].btn.setproperties( m_fontstyle );

		    if ( id == activeId )
			{   m_arItems[i].btn.setproperties( { 'imgnormal': m_prop.imgactive,'imghover': m_prop.imgactive });
			    m_arItems[i].btn.setproperties( { 'clrnorm'  : m_prop.clractive,'clrhover': m_prop.clractive });
		        m_arItems[i].btn.setproperties( { color:m_prop.clractive});
			    s += '<td id="' + itm +'" style="border-right: 0px solid #ffffff; text-align: center;">';
			}
			else
            {   m_arItems[i].btn.setproperties( { 'imgnormal': m_prop.imgnormal,'imghover': m_prop.imghover  });
			    m_arItems[i].btn.setproperties( { 'clrnorm'  : m_prop.clrnorm,  'clrhover': m_prop.clrhover  });
		        m_arItems[i].btn.setproperties({color:m_prop.clrnorm});
			    s += '<td id="' + itm +'" style="border-right: 0px solid #ffffff; text-align: center;">';
			}
		    s += '</td>';
		}
        s += '</tr></tbody></table>';
		
		return s; 
		
	}
					
	this.initbuttons = function ( )
	{   for ( var i = 0; i < m_arItems.length; i++ )
		{   m_arItems[i].btn.setproperties( { evthandler : this} );
			m_arItems[i].btn.create  ( HTM_INS );
		}

	}
	function clickmenuitem( e, itemid )
	{
		var id   = parseInt(itemid.substr( m_menuprefix.length,10));
		var type = e.type;
		switch( type )
		{   case 'mouseover':
			case 'mouseout':
			var itm = m_visihlp.getElement( itemid );
		    itm = itm.parentNode;//tr
		    itm = itm.parentNode;//tbody
		    itm = itm.parentNode;//table
			itm = itm.parentNode;//td
			if ( !m_arItems[id-1].submenu ) return;			
			if ( type == 'mouseover' )
			{   m_drop_tm.killtm    ( );
				m_drop_tm.starttm   ( m_arItems[id-1]);
			}
			break;
			case 'click':
		    m_visihlp.goToUrl( m_arItems[id-1].link);
            break;
		}
	}
	function create  ( activeId, iHtmRet, parentid )
	{	
		var iHtmRet = (iHtmRet != 'undefined')?iHtmRet:HTM_WRT;
/*		
		if ( activeId == null )
		{   
		    var ref  = self.location.href;
			ref = ref.substr(ref.lastIndexOf("/")+1);

            if ( ref.length )
			{   if ( ref.indexOf('?') == 0 )
			         ref = '';
			}

            if ( ref.length )
			{   for ( var i = 0; i <  m_arItems.length; i++ )
				{   if ( m_arItems[i].link.indexOf(ref) != -1 )
						 activeId = (i+1);
				}
			}
			
			if ( activeId == null )
			     activeId = m_this.activeId;
		}
*/		
		if ( activeId == null )
			 activeId = m_this.activeId;
		if ( (typeof activeId) == 'string')
		{   var i = 0;
		    for ( i = 0; i < m_arItems.length; i++ )
			{   if ( m_visihlp.trim(m_arItems[i].title) == m_visihlp.trim(activeId))
				{    activeId = i+1;
					 break;
				}
			}
			if ( i == m_arItems.length ) activeId = 1;
		}
	    this.activeId = activeId;

        var div = m_visihlp.getElement( m_divId );
		if ( !div ) // if placeholder not found
		{    if ( !parentid ) return;
			 var parent = m_visihlp.getElement( parentid );
			 div        = m_visihlp.createDivLayer( parent, m_divId,divsheme );
			 iHtmRet    = HTM_INS;
		}
		if ( !div ) return;
		
		switch( iHtmRet )
		{
			case HTM_RET: 
			    return build( activeId );
			case HTM_INS: 
			    div.innerHTML = build( activeId ); 
			    break;
			case HTM_WRT:
			default:
			    document.write( build( activeId ));  
			    break;
		}
		for ( var i = 0; i <  m_arItems.length; i++ )
		{   if ( m_arItems[i].submenu )
			     m_arItems[i].submenu.create( (i+1),iHtmRet);
		}
	}

	function colorshift( colorval, shiftval, dir )
	{   var sig = colorval.indexOf('#');
	    if ( sig != -1 )
	         colorval = colorval.substr( sig+1 );
	    while( colorval.length < 6 ) colorval += '0';
		
		var rgb = new Array( parseInt('0x'+colorval.substr( 0,2)),
							 parseInt('0x'+colorval.substr( 2,2)),
							 parseInt('0x'+colorval.substr( 4,2))
							);
        var str = '';
		for ( var i = 0; i < 3; i++ )
		{  	if ( dir == true )
				 rgb[i] *= shiftval;
		    else rgb[i] /= shiftval;
			var s = rgb[i].toString(16);
			if ( s.length == 1 ) s = '0'+s;
			if ( s.length > 2 ) s = s.substr( s.length-2,2);
			str += s;
		}
		str = '#'+str;
		return str;
	}
    function dropexec( state,param )
    {   if ( !param ) return;
	    var drop_div = m_visihlp.getElement(param.submenu.getdivid( ));
		switch( state )
		{
			case TM_STATE_START:
			var ritm = m_visihlp.getObjectRect(m_menuprefix+param.id);
			drop_div.style.left = ( ritm.left + ritm.width/4 )    + 'px';
			drop_div.style.top  = ( ritm.top  + ritm.height + 2 ) + 'px';
			m_visihlp.setVisible( drop_div,true);
			break;
			case TM_STATE_RUN:
            var r = m_visihlp.getObjectRect( drop_div );
            if ( m_visihlp.pointInObject( drop_div,r.width, r.height )) 
            {   m_drop_tm.cleartm( );
                return;
            }
            if ( m_drop_tm.gettm() > 500 )
                m_drop_tm.stoptm( )
			break;
			case TM_STATE_STOP:
			m_visihlp.setVisible( drop_div, false );

			break;
		}
    }
	this.prepareMenuFromArray = function ( lmenuprop)
	{
		if ( m_menuarray.length )
		{   for ( var i = 0; i < m_menuarray.length; i++ )
			{   addItem( m_menuarray[i].link, m_menuarray[i].name);
				if ( m_menuarray[i].submenu != null )
				{   
					var lmenu = new clsLeftNavigation(  null, m_visihlp, null );
	
					lmenu.setProperties( lmenuprop );
	
					for ( var j = 0; j < m_menuarray[i].submenu.length; j++ )
					{
						  lmenu.addItem ( m_menuarray[i].submenu[j].link, m_menuarray[i].submenu[j].name );
					}
					addSubitem( (i+1), lmenu );
				}
			}
		}
	
	}
	this.xmlParseMenu  = function( xmldomitems )
	{   var arr = new Array( );
	    var n   = 0;
		for ( var j = 0; j < xmldomitems.length; j++ )
		{   if ( xmldomitems[j].nodeType == 1)
			{   switch( xmldomitems[j].nodeName )
				{   case 'item':
						arr[n++]    = xmlParseMenuItem( xmldomitems[j] );
					break;
				}
			}
		}
        if ( m_menulevel == 0 ) m_menuarray = arr;// end recursion 
		return arr;
	}
	function xmlParseMenuItem ( field )
	{
		var prop = { id:      '1', article:'', background:'', bgcolor:'', name:    'none', link:    '#', submenu: null }; 
		try { prop.id         = visi.trim(field.attributes.getNamedItem( "id").nodeValue); } catch(e){}
		try { prop.article    = visi.trim(field.attributes.getNamedItem( "article").nodeValue); } catch(e) {}
		
		try { prop.background = visi.trim(field.attributes.getNamedItem( "background").nodeValue); } catch(e) {}
		try { prop.bgcolor    = visi.trim(field.attributes.getNamedItem( "bgcolor").nodeValue); } catch(e) {}
	
		var chl  = field.childNodes;
	
		for ( var i = 0; i < chl.length; i++ )
		{   if ( chl[i].nodeType != 1 ) continue;
			switch( chl[i].nodeName )
			{   case 'name':    prop.name = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\""); break;
				case 'link':    prop.link = visi.trim(chl[i].childNodes[0].nodeValue," \t\n\r\f\""); break;
				case 'submenu':
					var subfields = chl[i].childNodes;

                    m_menulevel++; //increment recursion level
					prop.submenu = m_this.xmlParseMenu( subfields );
                    m_menulevel--;
					break;
			}
		}
		return prop;
	}
	this.getActivePageFromLinks = function( article )
	{   

	    if ( article != undefined )// find active menu item from links
		{   for ( var i = 0; i < m_menuarray.length; i++ )
			{   if ( m_menuarray[i].article.indexOf( article) == 0 || article.indexOf( m_menuarray[i].article) == 0)
				  	 return (m_this.activeId = i+1);

				if ( m_menuarray[i].submenu)
				{   if ( getActiveSubpageFromLinks( m_menuarray[i].submenu, article ))
					     return (m_this.activeId = i+1);
				}
			}
		}
		return m_this.activeId;
	}
	function getActiveSubpageFromLinks( arr, active )
	{   
	    for ( var i = 0; i < arr.length; i++)
		{   if ( arr[i].article.indexOf( active) == 0 )
				 return true;
		}
		return false;
	}
	
};
// end  menu line
