﻿// Plugin XiTi pour JQuery

jQuery.fx.prototype.showMenuNX = function()
{
    	var startPosition = this.options.curAnim.startPosition;
		// Remember where we started, so that we can go back to it later
		this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
		this.options.show = true;

		// Begin the animation
        if(startPosition)//si startPosition est défini, on ne commence pas l'animation au début mais apres cur/startPosition
            this.custom(this.cur()/startPosition, this.cur());
        else
		    this.custom(0, this.cur());

		// Make sure that we start at a small width/height to avoid any
		// flash of content
		if ( this.prop == "width" || this.prop == "height" )
			this.elem.style[this.prop] = "1px";

		// Start by showing the element
		jQuery(this.elem).show();
}

jQuery.fx.prototype.hideMenuNX = function()
{
	    var startPosition = this.options.curAnim.startPosition;
		// Remember where we started, so that we can go back to it later
		this.options.orig[this.prop] = jQuery.attr( this.elem.style, this.prop );
		this.options.hide = true;

		// Begin the animation
		if(startPosition) //si startPosition est défini, on ne commence pas l'animation au début mais apres cur/stratPosition
		    this.custom(this.cur()/startPosition, 0);
		else
		    this.custom(this.cur(), 0);
}

jQuery.fn.animateMenuNX = function( prop, speed, easing, callback ) 
{
		var optall = jQuery.speed(speed, easing, callback);

        var startPosition;
        
		return this[ optall.queue === false ? "each" : "queue" ](function(){
			if ( this.nodeType != 1)
				return false;

			var opt = jQuery.extend({}, optall), p,
				hidden = jQuery(this).is(":hidden"), self = this;

			for ( p in prop ) {
				if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden )
					return opt.complete.call(this);

				if ( p == "height" || p == "width" ) {
					// Store display property
					opt.display = jQuery.css(this, "display");

					// Make sure that nothing sneaks out
					opt.overflow = this.style.overflow;
				}
				if(p == "startPosition")
				    startPosition = parseFloat(prop[p]);
			}

			if ( opt.overflow != null )
				this.style.overflow = "hidden";

			opt.curAnim = jQuery.extend({}, prop);

			jQuery.each( prop, function(name, val){
				var e = new jQuery.fx( self, opt, name );

				if ( /toggle|show|hide/.test(val) )
					e[ val == "toggle" ? hidden ? "show" : "hide" : val + "MenuNX" ]( prop );
				else {
					var parts = val.toString().match(/^([+-]=)?([\d+-.]+)(.*)$/),
						start = (startPosition ? e.cur(true) / startPosition : e.cur(true)) || 0;

					if ( parts ) {
						var end = parseFloat(parts[2]),
							unit = parts[3] || "px";

						// We need to compute starting value
						if ( unit != "px" ) {
							self.style[ name ] = (end || 1) + unit;
							start = ((end || 1) / e.cur(true)) * start;
							self.style[ name ] = start + unit;
						}

						// If a +=/-= token was provided, we're doing a relative animation
						if ( parts[1] )
							end = ((parts[1] == "-=" ? -1 : 1) * end) + start;

						e.custom( start, end, unit );
					} else
						e.custom( start, val, "" );
				}
			});

			// For JS strict compliance
			return true;
		});
}

	// Helper function used by the dimensions and offset modules
function getNum(elem, prop) {
	return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
}

//retourne la position relative d'un objet
jQuery.fn.xGetPosition2 = function() {
		var left = 0, top = 0, results;

		if ( this[0] ) {
			// Get correct offsets
			offset       = this.offset(),
			// Subtract the two offsets
			results = {
				top:  offset.top,
				left: offset.left
			};
		}

		return results;
	}

//retourne la position relative d'un objet
jQuery.fn.xGetPosition = function() {
		var left = 0, top = 0, results;

		if ( this[0] ) {
			// Get *real* offsetParent
			var offsetParent = this.offsetParent(),

			// Get correct offsets
			offset       = this.offset(),
			parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
						
			// Add offsetParent borders
			parentOffset.top  += getNum( offsetParent, 'borderTopWidth'  );
			parentOffset.left += getNum( offsetParent, 'borderLeftWidth' );

			// Subtract the two offsets
			results = {
				top:  offset.top - parentOffset.top,
				left: offset.left - parentOffset.left
			};
		}

		return results;
	}
	
//retourne la position absolue d'un objet html
jQuery.fn.xGetAbsolutePosition = function(checkFrame) 
    {
        var parent = this[0];
        var result = new Object(),tmp;
        var first = true;
        
        result.top = 0;
        result.left = 0;        
        
        var getGoodParent = function(elem)
        {
            var parentWindow = elem.ownerDocument ? (elem.ownerDocument.parentWindow || elem.ownerDocument.defaultView) : elem;
            elem = parentWindow.frameElement;
            return elem;
        }
        
		while(parent && parent.tagName != "BODY")
		{
		    if((parent.style && parent.style.top) || first)  
		    {
		        tmp = jQuery(parent).xGetPosition();
    		    
		        result.top += tmp.top;
		        result.left += tmp.left;	      		    
		       
		       first = false;
		    }
		    
		   parent = !parent.frames ? parent.parentNode : (parent.frameElement ? parent.frameElement.parentNode : "");	
		   if(checkFrame && parent && parent.tagName == "BODY")
		    parent = getGoodParent(parent);		    	
		}
		
		return result;
	}
	
//Calcule la hauteur d'un objet html en prenant en compte le padding et le border
jQuery.fn.xHeight = function(checkStyleValueFirst)
{
    var height,paddingTop,paddingBottom,borderBottom,borderTop;
    
     var getStyleValue = function(style)
       {
            var res = 0;
            if(style)
            {
                res = parseInt(style.substr(0,style.length-2),10);
            }
            return res;
       }
   
   height = checkStyleValueFirst ? (getStyleValue(this.css("height")) || this.height()) : (this.height() || getStyleValue(this.css("height")));
   if(isNaN(height))
    height = 0;
   paddingTop = getStyleValue(this.css("paddingTop")) || 0;
   borderTop = getStyleValue(this.css("borderTopWidth")) || 0;
   paddingBottom = getStyleValue(this.css("paddingBottom")) || 0;
   borderBottom = getStyleValue(this.css("borderBottomWidth")) || 0;
   
   height += paddingTop + paddingBottom + borderTop + borderBottom;
   
   return height;
}

//Calcule la largeur d'un objet html en prenant en compte le padding et le border
jQuery.fn.xWidth = function()
{
    var width,paddingLeft,paddingRight,borderLeft,borderRight;
    
     var getStyleValue = function(style)
       {
            var res = 0;
            if(style)
            {
                res = parseInt(style.substr(0,style.length-2),10);
            }
            return res;
       }
   
   width = this.width() || getStyleValue(this.css("width"));
   if(isNaN(width))
    width = 0;
   paddingLeft = getStyleValue(this.css("paddingLeft")) || 0;
   paddingRight = getStyleValue(this.css("paddingRight")) || 0;
   borderLeft = getStyleValue(this.css("borderLeftWidth")) || 0;
   borderRight = getStyleValue(this.css("borderRightWidth")) || 0;
   
   width += paddingLeft + paddingRight + borderRight + borderLeft;
   
   return width;
}

jQuery.fn.xFindPreviousNode = function(elemToFind) 
{
    var child = this[0].childNodes || this[0].document.body.childNodes;
    var node = null,nodeTmp = null;
    
    for(var i=0;i<child.length;i++)
    {
            
        if(child[i].sourceIndex == (elemToFind.frameElement.sourceIndex || elemToFind.sourceIndex))
        {
           node = child[i].parentNode;
           break;
        }
        else
        {
            nodeTmp = jQuery(child[i]).xFindPreviousNode(elemToFind);
            if(nodeTmp != null)
                node = nodeTmp;
        }
    }
    
    return node;
}

// sélection par expressions régulières
jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

//Gestion des cookies
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = jQuery.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery.xBrowser = function()
{
    var brow = new Object();
    for(elem in jQuery.browser)
        brow[elem] = jQuery.browser[elem];
	brow.chrome = /chrome/.test(navigator.userAgent.toLowerCase())
	return brow;
};



	