/*
 * jQuery Gigi EI Slide Menu
 * version: 0.2 (10/01/2009)
 * @requires jQuery v1.2.6 or later
 *
 * No use and no modification whitout express authorisation
 *
 * Revision: $Id$
 */
(
function($)
{
 $.GigiEISlideMenu_busy = false;
 
 $.fn.GigiEISlideMenu = function( options )
 {
  if( $(this).length > 0 )
  {
   options = $.extend({
                       divNormalClass: 'input_156_close',
                       divTopClass: 'input_156_open_top',
                       divBottomClass: 'input_156_open_bottom',
                       css_Zindex_open: '2',
                       css_Zindex_close: '1',
                       title: '',
                       forceEventOnSelectedElement: false,
                       developOnClick: true,
                       developOnMouseOver: false,
                       envelopOnMouseOut: false,
                       envelopAfter: 0
                      }, options || {} );
   
   var input = $(this);
   var divNormal = document.createElement( "div" );
   var divBottom = document.createElement( "div" );
   var divTop = document.createElement( "div" );
   var list = document.createElement( "ul" );
   
   var mouseHoverMenu = false;
   var mouseOutTimeout = null;
   
   // Properties
   $(divNormal).addClass( options.divNormalClass );
   $(divNormal).addClass( "GigiEISlideMenu_closed" );
   $(divNormal).css( { position: 'relative', zIndex: options.css_Zindex_close } );
   $(divTop).addClass( options.divTopClass );
   $(divBottom).addClass( options.divBottomClass );
   $(divBottom).addClass( "GigiEISlideMenu_slide" );
   $(divBottom).hide();
   $(this).hide();
   
   // Creation
   $(divBottom).append( divTop );
   $(this).before( divNormal );
   $(divNormal).html( "<span><span></span></span>" );
   $(divNormal).append( divBottom );
   $(divTop).append( list );
   
   // Events
   $(divNormal).mouseover( function()
                           {
                            mouseHoverMenu = true;
                           
                           if( options.envelopOnMouseOut == true )
                            window.clearTimeout( mouseOutTimeout );
                           } );
   $(divNormal).mouseout( function()
                          {
                           mouseHoverMenu = false;
                           
                           if( options.envelopOnMouseOut == true )
                            mouseOutTimeout = window.setTimeout( function(){ closeMenu(); }, options.envelopAfter );
                          } );
   
   $("label[for="+$(this).attr("id")+"]").click( function(){ openMenu(); } );
   if( options.developOnClick === true )
    $(divNormal).click( function(){ openMenu(); } );
   if( options.developOnMouseOver === true )
    $(divNormal).mouseover( function(){ openMenu(); } );
   
   if( options.title !== false )
   {
    if( options.title != "" )
    {
     element = document.createElement( "li" );
     $(element).addClass( "title" );
     $(element).html( options.title );
     
     $(list).append( element );
    }
    else if( $("label[for="+$(this).attr("id")+"]").length == 1 )
    {
     element = document.createElement( "li" );
     $(element).addClass( "title" );
     $(element).html( $("label[for="+$(this).attr("id")+"]").html() );
     
     $(list).append( element );
    }
   }
   $.each( $(this)[0].options,
           function( i, v )
           {
            element = document.createElement( "li" );
            $(element).html( v.text );
            $(element).attr( "class", $(v).attr("class") );
            
            if( v.selected == true )
            {
             $(input).val( v.value );
             $(divNormal).children("span:eq(0)").children("span:eq(0)").html( v.text );
            }
            
            $(element).click( function()
                              {
                               if( v.selected == false || options.forceEventOnSelectedElement === true )
                               {
                                v.selected = true;
                                $(input).change();
                               }
                               $(divNormal).children("span:eq(0)").children("span:eq(0)").html( v.text );
                               closeMenu();
                              } );
            
            $(list).append( element );
           } );
   
   $("body").click( function()
                    {
                     if( $.GigiEISlideMenu_busy == false && mouseHoverMenu == false )
                      closeMenu();
                    } );
   
   $(input).change( function()
                    {
                     $(divNormal).children("span:eq(0)").children("span:eq(0)").html( $(input)[0].options[$(input)[0].selectedIndex].text );
                    } );
   
   // Function animations
   function openMenu()
   {
    if( $(divBottom).filter(':visible').length == 0 )
    {
     $.GigiEISlideMenu_busy = true;
     $(divBottom).stop();
     $("div.GigiEISlideMenu_slide, div.GigiEISlideMenu_closed").css( { "z-index": options.css_Zindex_close, "top": 0, "left": 0 } );
     $("div.GigiEISlideMenu_slide").not(divBottom).slideUp( 'fast', function()
                                                                    {
                                                                     $("div.GigiEISlideMenu_closed").not(divNormal).children("span").show();
                                                                    } );
     $(divNormal).children("span").hide();
     $(divNormal).css( "z-index", options.css_Zindex_open );
     $(divBottom).css( "z-index", options.css_Zindex_open );
     $(divBottom).slideDown( 'fast', function(){ $.GigiEISlideMenu_busy = false; } );
    }
   }
   
   function closeMenu()
   {
    if( $(divBottom).filter(':visible').length == 1 )
    {
     $.GigiEISlideMenu_busy = true;
     $(divBottom).stop();
     $(divBottom).slideUp( 'fast', function()
                                   {
                                    $(divNormal).css( "z-index", options.css_Zindex_close );
                                    $(divBottom).css( "z-index", options.css_Zindex_close );
                                    $(divNormal).children("span").show();
                                    
                                    $.GigiEISlideMenu_busy = false;
                                   } );
    }
   }
  }
 };
}
)(jQuery);