/*! (C) 2008 PRIME Computer Kft. www.prime.hu */
//-------------------------------------------------------------------------------------------
// PS-Intra Java MenuClass
// Based on: jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
// - rewrite / reconstructure better jQuery support
// - extended functionality:
//   - Dynamic item width measuring
//   - Different state for open container root items
//   - using of bgiframe for IE select z-order issues
//-------------------------------------------------------------------------------------------

jQuery.psmenu = function( mnuid, opt )
{
	// Re-format menu id
	mnuid = mnuid.substr(0,1) == '#' ? mnuid : '#' + mnuid;
	
	var 
	  // Options
		opts = $.extend({
			animover:   100, 		//duration of slide in animation, in milliseconds
			animout:     80, 		//duration of slide in animation, in milliseconds
			minsubmenuw: 50, 		// minimum sub-menu width
			// 'open-sign' arrow definitions
			arrows:   {
									down:{classnam: 'jqpsmarrdown', img: 'java/images/psm_down.gif',  rpadd: 23 },
									side:{classnam: 'jqpsmarrside', img: 'java/images/psm_right.gif', rpadd: 23 }
								}
	  	}, opt || {} ),
	  // Main menu points		
	  $mainmenu=$(mnuid+'>ul');

	// Switch on bgiframe for all 'ul' tag in menu
	$('ul',mnuid).bgiframe();

	// Bind over / out functions, add open-sign images	
	$mainmenu.find('ul').parent().each(			
	  function(i)
    {
    	// Determine current object + submenu and dimensions
	    var $curobj=$(this), $subul=$curobj.find('ul:first');
	    this._dims={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth()};
	    
	    // Set max with for each subitem
			var mxw=opts.minsubmenuw; $($subul.children('li')).each( function() { var aw=$(this).width(); if(aw>mxw) mxw=aw; } );
			this._dims.subulw = mxw + 25; $($subul.children('li').children('a').width(this._dims.subulw));
	    
	    // Set top for submenu + border-top for first submenu-item						    
	    this.istoph=$curobj.parents('ul').length==1;
		  $subul.css({top:this.istoph ? this._dims.h+'px' : 0}).children('li:first').children('a:first').css( { borderTop: '1px solid #7DA8C5' } );
		  
		  // Insert open-sign image
		  $curobj.children('a:first').css(this.istoph ? {paddingRight: opts.arrows.down.rpadd} : {}).append( 
		    '<img src="'+ $.skinspecific( (this.istoph ? opts.arrows.down.img : opts.arrows.side.img) ) 
		    +'" class="' + (this.istoph ? opts.arrows.down.classnam : opts.arrows.side.classnam) + '" />' );
		   
		  // Define hover in-out functions (submenu show/hide)
	    $curobj.hover(
	    	// Hover-in
		    function(e)
		    {
				  var $targetul=$(this).children('ul:first');
			    if($targetul.queue().length<=1) //if 1 or less queued animations
			    {
			    	var menuleft=this.istoph ? 0 : this._dims.w;
			    	menuleft=($(this).offset().left+menuleft+this._dims.subulw>$(window).width()) ? (this.istoph ? -this._dims.subulw+this._dims.w : -this._dims.w) : menuleft;
				    $targetul.css({left:menuleft+'px', width:this._dims.subulw+'px'}).slideDown(opts.animover).parent().children('a:first').addClass('slactive');
				  }
			  },
			
				// Hover-out
			  function(e)
			  {
				  $(this).children('ul:first').slideUp(opts.animout).parent().children('a:first').removeClass('slactive');
			  }
		  ); //end hover funcs
	  } // end each-function
  ); //end each()
  
  // Hide all submenus
  $mainmenu.find('ul').css({display:'none', visibility:'visible'});
};
