// JavaScript Document
// code by Azer Manafov (azerman[at]hotmail[dot]com)


var DEF_NAV_NAME     = 'lmenuitem';
var CALC_TEXT_DIV    = 'calcPixelWidthDiv';

function clsLeftNavigation( divId, visihlp,styleSheet )
{   var m_visihlp      = (visihlp == 'undefined')?new clsViewHelper():visihlp;
    var m_base         = new clsBaseNavigate( divId, visihlp, null );
	var m_styleSheet   = (styleSheet)?styleSheet:null;

    var m_prop         =
	{
		imgactive   : 'images/sys/blank.gif',
		imgnormal   : 'images/sys/blank.gif',
		imgover     : 'images/sys/blank.gif',
		imgwidth    : 20,
		imgheight   : 20,
		imgcolwidth : 0,
		colheight   : 0,
		txtcolwidth : 0,
		bkgimgovr   : 'images/sys/blank.gif',
		bkgimgout   : 'images/sys/blank.gif',
		bkgimage    : 'images/sys/blank.gif',
		textcolorovr: '#c0c0c0',
		textcolorout: '#000000',
		tblwidth    : 0
	};

	var m_fontstyle    =  new Object( );

	this.getdivid      = function( ) { return m_base.getdivid();}
	this.setdivid      = function( divId ) { m_base.setdivid( divId );}

	this.addItem       = addItem;
	this.create        = create;
	this.fontStyle     = fontStyle;
	
	fontStyle( {'color':'#000000','font-family':'Arial','font-size':'12px','font-weight':'bold'});
	
	m_base.addEvent( 'mouseover', true );
	m_base.addEvent( 'mouseout',  false);
	
	m_visihlp.registerHelper( mnuMouseEvent );

	this.remAll        = function(){ m_base.remAll( ); }
	this.findItem      = function( sText, equalence ) { return m_base.findItem( sText, equalence ); }
    this.addObj        = function( item ) { m_base.addObj( item ); }
    function resFont  ( ) { m_fontstyle = new Object( ); }
	function fontStyle( prop )
	{   for ( var i in prop )
	    {   if ( prop[i] != undefined ) 
                 m_fontstyle[i] = prop[i];
		}
	}
	
	function calcmaxwidth( )
	{   var calcwidth = new clsCalcStringWidth( CALC_TEXT_DIV, m_fontstyle );
		if ( m_base.itemsCount() )
		{   for ( var i = 0; i < m_base.itemsCount(); i++ )
			{   var itm = m_base.getitem(i)
				calcwidth.calcStringWidth(itm.title);
			}
		}
       return (calcwidth.getMaxWidth( ) + m_prop.imgwidth + 8);
	   
	}
	
    function addItem ( sLink, sText )
    {   m_base.addItem ( sLink, sText );
    }
    this.setProperties = function ( prop )
	{   for ( var i in prop )
		      m_prop[i] = prop[i];
	}
	
	this.getProperty = function ( id ) { return m_prop[id]; }

	this.calcDimension = function ( )
	{
        if ( !m_prop.imgcolwidth )
	          m_prop.imgcolwidth = m_prop.imgwidth  + 2;
		if ( !m_prop.colheight )
		      m_prop.colheight   = m_prop.imgheight + 1;//2;

        if ( m_prop.txtcolwidth == 0 )
		     m_prop.txtcolwidth = calcmaxwidth( );
			 
		if ( m_prop.txtcolwidth )
		     m_prop.tblwidth = (m_prop.txtcolwidth + m_prop.imgcolwidth + 4);
		else 
		{   var div = m_visihlp.getElement( m_base.getdivid() );
			if ( div )
			{   var w = parseInt(m_visihlp.getStyle( div, 'width', true));
				if ( !isNaN(w))
		              m_prop.tblwidth = w;
			}
		}
		var dim = {width:m_prop.tblwidth,height:0};
		for ( var i = 0; i < m_base.itemsCount(); i++ )
		      dim.height += m_prop.colheight;
		return dim;
	}

	function outStyle( )
	{   
		
		var s = '';
		m_styleSheet = 'leftmenu';
        s += '<style type=\"text/css\">';
        s += 'table.leftmenu { width: 100%; }' ;
        s += 'table.leftmenu td, table.leftmenu td.icn ';
		s += '{ text-align: center; height:'+m_prop.colheight+'px;  background-position:center center; vertical-align: middle; }';
        s += 'table.leftmenu td';
		s += '{  background-image: url('+m_prop.bkgimgout+'); background-repeate:repeate;}';
        s += 'table.leftmenu td.icn'
		s += '{ background-image: url('+m_prop.bkgimage+');  padding-left: 0px; width:'+ m_prop.imgcolwidth +'px; }';
		
		
        s += 'table.leftmenu img    { border:0px; text-align:center; vertical-align:middle; padding-left:0px; width:'+m_prop.imgwidth+'px; height:'+m_prop.imgheight+'px;}';
			var sSpan  = 'table.leftmenu td span, table.leftmenu td a, table.leftmenu td a:link, table.leftmenu td a:visited, table.leftmenu td a:hover, table.leftmenu td a:active{ ';
			if ( m_fontstyle['color'] == undefined )
			     m_fontstyle['color'] = '#000000';
			for ( var key in m_fontstyle)
			{
				sSpan += key + ':' + m_fontstyle[key] + '; ';
			}

		sSpan +=  '} ' + "\n";
        s +=  sSpan;//'table.leftmenu span      { color:#000000; }');
        s += 'table.leftmenu td a, table.leftmenu td a:link, table.leftmenu td a:visited, table.leftmenu td a:hover, table.leftmenu td a:active { text-decoration: none; }';
        s += 'table.leftmenu td a { color:' + m_fontstyle['color'] + ';}';
		s += '</style>';
		return s;
	}
	function build   ( activeId )
	{   
	    var s='';
		if ( m_prop.tblwidth )
		{    s += '<TABLE id="'+m_base.getident()+'" width="' + m_prop.tblwidth + '" class="' + m_styleSheet + '" cellpadding="0" cellspacing="0" border="0"';
			 s += ' style="width:' + m_prop.tblwidth + 'px;"';
		}
		else s += '<TABLE id="'+m_base.getident()+'" width="100%" class="' + m_styleSheet + '" cellpadding="0" cellspacing="0" border="0"';
		s += '><TBODY>';
		for ( var i = 0; i < m_base.itemsCount(); i++ )
		{
			var id  = m_base.getident() + ( i+1 );
			var itm = m_base.getitem(i); 
			if ( itm.id != activeId )
			     s += '<TR><TD id="'+id+'_01" class="icn"><A href="' + itm.link +'"><IMG src="'+m_prop.imgnormal + '"></A></TD>\n';
			else s += '<TR><TD id="'+id+'_01" class="icn"><A href="' + itm.link +'"><IMG src="'+m_prop.imgactive + '"></A></TD>\n';
			
			if ( m_prop.txtcolwidth )
			     s += '    <TD id=\"'+id+'_02\" width="' + m_prop.txtcolwidth + '" style="width:'+ m_prop.txtcolwidth + 'px;">\n'
            else s += '    <TD id=\"'+id+'_02\">\n'
			s += '    <SPAN><A href=\"' + itm.link +'\" style="color:'+m_prop.textcolorout+';">'+itm.title+'</A></SPAN></TD></TR>\n';
		}
		s += '</TBODY></TABLE>\n';

		return s; 
	}

	function correctid( idpref )
	{   var c = "_";
	    var p = idpref.lastIndexOf( '_');
	    if ( p == (idpref.length-1))
		     idpref = idpref.substr(0,idpref.length-1);
	    var o = m_visihlp.getElement( idpref+c);
		var n = 1;
		while( o )
		{   idpref = idpref + (n++);
			o = m_visihlp.getElement( idpref + c );
		}
		return idpref;
	}
	function create  ( activeId, iHtmRet, parentid )
	{	

	    this.calcDimension( );

		var iHtmRet = (iHtmRet != 'undefined')?iHtmRet:HTM_WRT;
		
	    m_base.setident   ( correctid( DEF_NAV_NAME ));
	    m_base.setactiveid( activeId );
	    
	    var styleSheet = ( m_styleSheet )?null:outStyle( );

	    var div        = m_visihlp.getElement( m_base.getdivid() );
	    if ( !div ) return;
		switch( iHtmRet )
		{
			case HTM_RET: 
			    m_visihlp.appendStyle( styleSheet ); 
			    return build(  m_base.getactiveid() );
			case HTM_INS: 
			    m_visihlp.appendStyle( styleSheet ); 
			    div.innerHTML = build(  m_base.getactiveid() ); 
			    break;
			case HTM_WRT:
			default:
			    document.write( "<STYLE type=\"text/css\">\n" +  styleSheet + "</STYLE>\n");
			    document.write( build(  m_base.getactiveid() ));  
			    break;
		}
	}
	
	function mnuMouseEvent(e)
	{
	    var evt    = m_base.eventhandle( e );
		if ( !evt ) return;
		if ( !evt.obj ) return;
		
        var currid = m_base.getnavid( evt.obj );

        var s      = m_base.getident( ) + ( currid );
		var ob1    = m_visihlp.getElement( s +'_01').childNodes[0].childNodes[0];// go image tag
		var ob2    = m_visihlp.getElement( s +'_02');
		var ob3    = ob2.getElementsByTagName( 'A')[0];

		if ( document.all||document.getElementById )
		{   

		    if ( evt.eventid == true )//mouseover
		    {   ob1.src = m_prop.imgover;
			    m_visihlp.setStyleA( ob3,'color',m_prop.textcolorovr );
                m_visihlp.setStyleA( ob2,'background-image','url(' + m_prop.bkgimgovr + ')'); 			
			}
			else
			{   if ( currid ==  m_base.getactiveid())
			         ob1.src = m_prop.imgactive;
			    else ob1.src = m_prop.imgnormal;
			    m_visihlp.setStyleA( ob3,'color',m_prop.textcolorout);
                m_visihlp.setStyleA( ob2,'background-image','url(' + m_prop.bkgimgout + ')'); 			
			}
		}
//		m_base.setactiveid(currid);
	}
	
};
