/*json2.2 */
(function($){$.toJSON=function(o)
{if(typeof(JSON)=='object'&&JSON.stringify)
return JSON.stringify(o);var type=typeof(o);if(o===null)
return"null";if(type=="undefined")
return undefined;if(type=="number"||type=="boolean")
return o+"";if(type=="string")
return $.quoteString(o);if(type=='object')
{if(typeof o.toJSON=="function")
return $.toJSON(o.toJSON());if(o.constructor===Date)
{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
if(o.constructor===Array)
{var ret=[];for(var i=0;i<o.length;i++)
ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
name='"'+k+'"';else if(type=="string")
name=$.quoteString(k);else
continue;if(typeof o[k]=="function")
continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
return eval("("+src+")");else
throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
{if(string.match(_escapeable))
{return'"'+string.replace(_escapeable,function(a)
{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);


/*
 * jQuery Extended JSon Cookie Plugin
 * relese 2010-01-11
 *
 * It is licensed as free software under the terms of the GNU General Public License (GPL)
 * http://www.gnu.org/licenses/gpl.html
 *
 * This plugin is based on Jquery Cookie plugin http://plugins.jquery.com/project/cookie
 * 
 * Work by Rodolphe Franceschi
 */
 
 // anic
// deux modif
// on force le path a la racine l.78
// on empeche la creation si cookie vide, le controle se fait plus bas l. 217

// Class definition 
function jQueryextendedjsoncookieUtils()
{

}

/* Fonction de compatibilite avec la librairie initiale */
jQueryextendedjsoncookieUtils.corewritefunction = function(argss)
{
  var name = argss[0];
  var value = argss[1];
  var options = argss[2];


  options = options || {};
  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
  }
//  var path = options.path ? '; path=' + (options.path) : '';
  path='; path=/';
  var domain = options.domain ? '; domain=' + (options.domain) : '';
  var secure = options.secure ? '; secure' : '';
  var cookievalue = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  document.cookie = cookievalue;
}






/* Simple deletion of the cookie */
jQueryextendedjsoncookieUtils.removeCookie = function(argss)
{
  var cookiename = argss[0];

  varcookievalue = jQueryextendedjsoncookieUtils.getCookieValueDecoded( argss )
  if ( varcookievalue != undefined )
  {
    document.cookie = cookiename +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
  } 
}

/* Simple write of all the cookie content with an empty value */
jQueryextendedjsoncookieUtils.writeEmptyCookie = function(argss)
{
  var cookiename = argss[0];

  document.cookie = cookiename + '=';
}

/* Simple write of all the cookie content */
jQueryextendedjsoncookieUtils.writeCookie = function(argss)
{
  jQueryextendedjsoncookieUtils.corewritefunction(argss);
}


/* Get the cookie value encoded */
jQueryextendedjsoncookieUtils.getCookieValue = function(argss)
{
  var cookiename =  argss[0];

  var fullcookievalue = jQueryextendedjsoncookieUtils.getFullCookie(argss);
  if (fullcookievalue == undefined)
  {
    return undefined;
  }
  return fullcookievalue.substring(cookiename.length + 1);
}

/* get The cookie valude decoded */
jQueryextendedjsoncookieUtils.getCookieValueDecoded = function(argss)
{
  var cookievalue = jQueryextendedjsoncookieUtils.getCookieValue(argss);
  if (cookievalue == undefined) {return undefined; };
  return decodeURIComponent(cookievalue);
}


/* Get the value of a cookie by cookie name*/
jQueryextendedjsoncookieUtils.getFullCookie = function(argss)
{
  var cookiename = argss[0];

  var cookieValue = undefined;
  var componentValueOutput = undefined;

  if (document.cookie && document.cookie != '')
  {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++)
    {
      var cookie = jQuery.trim(cookies[i]);
   
      if (cookie.substring(0, cookiename.length + 1) == (cookiename + '='))
      {
        componentValueOutput = cookie;
        
        break;
      }
    }
  }
  return componentValueOutput;
}

/*
 * Advanced function thar stores Extended attributes (not value).
 * Warning, This function must be called AT THE END OF THE CALLS
 */
jQueryextendedjsoncookieUtils.setExtendedAttributes = function(argss)
{
  var cookiename = argss[0];
  var extendedattributesarray = argss[1];

  var cookvalue = jQueryextendedjsoncookieUtils.getCookieValueDecoded(argss);
  var argstopass = Array(cookiename, cookvalue, extendedattributesarray);

  jQueryextendedjsoncookieUtils.corewritefunction(argstopass);
}

/*
 * Function that gets a variale Value from a cookie
 */
jQueryextendedjsoncookieUtils.getCookieVariable = function(argss)
{
  var cookiename = argss[0];
  var variablename = argss[1];

  var cookvalue = jQueryextendedjsoncookieUtils.getCookieValueDecoded(argss);
  if (cookvalue != '' && cookvalue != undefined)
  {
    jsonoutput_eval = $.evalJSON(cookvalue);
    return jsonoutput_eval[variablename];
  }

  return undefined;
}


/*
 * Advanced function that stores a variable in the value of the cookie in Json data exchange format
 */
jQueryextendedjsoncookieUtils.setCookieVariable = function(argss)
{
  var cookiename = argss[0];
  var variablename = argss[1];
  var variablevalue = argss[2];

  var jsonoutput = undefined;

  // First, get the cookie value
  var cookvalue = jQueryextendedjsoncookieUtils.getCookieValueDecoded(Array(cookiename));

  // if cookie value is undefined, write empty cookie then set the variable
  if ( (cookvalue == undefined) || (cookvalue == '') )
  {
  // anic
 //   jQueryextendedjsoncookieUtils.writeEmptyCookie(Array(cookiename));

    var variableunique = new Object();
    variableunique[variablename] = variablevalue;

    var chainejsonencoded = $.toJSON(variableunique);

    var argstopass = new Array();
    argstopass.push(cookiename);
    argstopass.push(chainejsonencoded);

    jQueryextendedjsoncookieUtils.writeCookie(argstopass);
  }
  else
  {
    // else, add the variable to the cookie
    jsonoutput_eval = $.evalJSON(cookvalue);
    jsonoutput_eval[variablename] = variablevalue;
    var chainejsonencoded = $.toJSON(jsonoutput_eval);

    var argstopass = new Array();
    argstopass.push(cookiename);
    argstopass.push(chainejsonencoded);

    jQueryextendedjsoncookieUtils.writeCookie(argstopass);
  }
}





// JQuery Interface
// We assume that first argument is method name....
jQuery.extendedjsoncookie = function()
{
  // We forge dynamic call chain
  var chain = "jQueryextendedjsoncookieUtils.";
  var internalargs = new Array();
  for (var i=0; i < arguments.length; i++)
  {
     var thisarg = arguments[i];
     if (i == 0)
     {
       chain = chain + thisarg;
     }
     else
     {
       internalargs.push(thisarg);
     }
  }
  
  // We make an eval on it
  return eval(chain) ( internalargs );
};

// anic



