/*
 * menuBox plugin pour jQuery développé par Mandchou
 * http://www.mandchou.com/
 *
 * Copyright (c) 2009 Charly BELLE
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2010-01-13 13:45:21 -0500 (Wed, 13 Jan 2010)
 * Revision: 1
 */

(function($) {  
$.fn.menuBox = function (options){ // réglages par défaut
	options = jQuery.extend({
		speedIn:200, 
		speedOut:100, 
		menuWi:200,
		align:'horizontal'
},options);	
$.fn.findPos = function() {
       obj = jQuery(this).get(0);
       var curleft = obj.offsetLeft || 0;
       var curtop = obj.offsetTop || 0;
       while (obj = obj.offsetParent) {
                curleft += obj.offsetLeft
                curtop += obj.offsetTop
       }
       return {x:curleft,y:curtop};
   }
this.each(function(){
var _self = $(this);
//var globalWi = parseInt($('html').width());
_self.find('ul').css({width:options.menuWi+'px',position:'absolute'});
_self.find('ul').addClass('ulFirstChild');
_self.find('ul').find('ul').css({marginLeft:options.menuWi+'px'});
_self.find('ul').find('ul').removeClass('ulFirstChild');

if(options.align=='vertical'){
	var firstAlign = parseInt(_self.width());
	var debugAlign = 10;
}

else

{
	var firstAlign = 0;
	var debugAlign = 5;
}
var cmpt="fermer";

$(this).find('.ulFirstChild').css({marginLeft:firstAlign+'px'});

$(this).find('ul').hide();
$(this).find('li').bind('mouseenter',function()
{

									  
var curObj = $(this).find('ul:first');
var nbItem;

/*test*/
if(this.id != "")
{
	$(this).removeClass('rub_menu').addClass('rub_on');
	nbItem = window.document.getElementById("nbItem_"+this.id).value;
}
/*test*/


var globalWi = parseInt($('html').width());
var pos = $(this).findPos();
if((globalWi - pos.x)-options.menuWi < options.menuWi)
{
	var curMargin = parseInt(curObj.css('marginLeft'));
	if(curMargin !=0){
	curObj.css({marginLeft:'-'+options.menuWi+'px'});	
	}
	
	var diffMargin = (globalWi - pos.x)-options.menuWi;
	
	if((globalWi - pos.x) < options.menuWi){
	$(this).find('.ulFirstChild').css({marginLeft:diffMargin-firstAlign-debugAlign+'px'});
	}	
}
else
{
	//ici le roll Over
	var curMargin = parseInt(curObj.css('marginLeft'));
	if(curMargin !=0){
	curObj.css({marginLeft:options.menuWi+'px'});
	$(this).find('.ulFirstChild').css({marginLeft:firstAlign+'px'});
	}
}
curObj.stop();
//curObj.css({opacity:1});
curObj.css({height : nbItem*38 +'px'});

//curObj.fadeIn(options.speedIn);
//curObj.slideDown(options.speedIn);
curObj.animate({
	height: 'toggle'
  }, {
    duration: options.speedIn, 
    specialEasing: {
      height: 'easeOutBounce'
    }
  });

});
$(this).find('li').bind('mouseleave',function(){
											  

var curObj = $(this).find('ul:first');
/*test*/
if(this.id != "")
{
	$(this).removeClass('rub_on').addClass('rub_menu');
}
/*test*/
curObj.stop();

//curObj.fadeOut(options.speedOut);
//curObj.slideUp(options.speedOut);
curObj.animate({
    height: 'toggle'
  }, {
    duration: options.speedOut, 
    specialEasing: {
      height: 'easeOutBounce'
    }
  });

//curObj.hide();
});
});
}})(jQuery);

	

