/**
 * jquery.datemaker.js: Date Maker plugin
 *
 * Copyright (c) 2008 Doug Sparling
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 *
 * Create a Date object using the given date format string.
 *
 * Date formats like Perl CGI::Cookie:
 * Date formats like Rails ActiveSupport::CoreExtension::Numeric::Time
 *
 * @option String s number of seconds
 * @option String m number of minutes
 * @option String h number of hours
 * @option String d number of days
 * @option String M number of months
 * @option String y number of years
 * @option String s x.second.ago
 * @option String s x.second.from_now
 * @option String s x.seconds.ago
 * @option String s x.seconds.from_now
 * @option String s x.minute.ago
 * @option String s x.minute.from_now
 * @option String s x.minutes.ago
 * @option String s x.minutes.from_now
 * @option String s x.hour.ago
 * @option String s x.hour.from_now
 * @option String s x.hours.ago
 * @option String s x.hours.from_now
 * @option String s x.day.ago
 * @option String s x.day.from_now
 * @option String s x.days.ago
 * @option String s x.days.from_now
 * @option String s x.month.ago
 * @option String s x.month.from_now
 * @option String s x.months.ago
 * @option String s x.months.from_now
 * @option String s x.year.ago
 * @option String s x.year.from_now
 * @option String s x.years.ago
 * @option String s x.years.from_now
 * @option String midnight 
 * @option String now 
 *
 * @example 
 * $.datemaker('+30s');                // 30 seconds from now
 * $.datemaker('+5m');                 // 5 minutes from now
 * $.datemaker('+1h');                 // 1 hour from now
 * $.datemaker('-1d');                 // 1 day ago 
 * $.datemaker('+10d');                // 10 days from now 
 * $.datemaker('+2M');                 // 2 months from now      
 * $.datemaker('-6M');                 // 6 months ago      
 * $.datemaker('+1y');                 // 1 year from now      
 * $.datemaker('30.seconds.ago');      // 30 seconds ago     
 * $.datemaker('30.seconds.from_now'); // 30 seconds from now      
 * $.datemaker('5.minutes.ago');       // 5 minutes ago     
 * $.datemaker('5.minutes.from_now');  // 5 minutes from now      
 * $.datemaker('1.hour.ago');          // 1 hour ago     
 * $.datemaker('1.hour.from_now');     // 1 hour from now      
 * $.datemaker('10.days.ago');         // 10 days ago     
 * $.datemaker('10.days.from_now');    // 10 days from now      
 * $.datemaker('6.months.ago');        // 6 months ago     
 * $.datemaker('6.months.from_now');   // 6 months from now      
 * $.datemaker('1.year.ago');          // 1 year ago     
 * $.datemaker('1.year.from_now');     // 1 year from now      
 * $.datemaker('midnight');            // midnight
 * $.datemaker('now');                 // now
 * @desc Examples...
 * @example $.cookie('cookie_name', 'cookie_value', { expires: $.datemaker('+1h') });
 * @desc Using with jQuery cookie plugin
 *
 * @param String s Date format identifier.
 * @return Date object
 * @type Date object
 *
 * @name $.datemaker
 * @cat Plugins/Date
 * @author Doug Sparling/doug.sparling@gmail.com
 * @version 0.1.1
 */
(function($) {

  $.datemaker = function(expires) {

    var d1 = new Date();

    if ( ar = expires.match(/^([+|-])?(\d+)(\w)$/) ) {

      var action;
      RegExp.$1 == '+' || RegExp.$1 == ''  ? action = '+' : action = '-';

      if (RegExp.$3 == 's') {
        action  == '-' ? $.datemaker.advanceSeconds(d1, '-', RegExp.$2) : $.datemaker.advanceSeconds(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'm') {
        action  == '-' ? $.datemaker.advanceMinutes(d1, '-', RegExp.$2) : $.datemaker.advanceMinutes(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'h') {
        action  == '-' ? $.datemaker.advanceHours(d1, '-', RegExp.$2) : $.datemaker.advanceHours(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'd') {
        action  == '-' ? $.datemaker.advanceDays(d1, '-', RegExp.$2) : $.datemaker.advanceDays(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'M') {
        action  == '-' ? $.datemaker.advanceMonths(d1, '-', RegExp.$2) : $.datemaker.advanceMonths(d1, '+', RegExp.$2);
      } else if (RegExp.$3 == 'y') {
        action  == '-' ? $.datemaker.advanceYears(d1, '-', RegExp.$2) : $.datemaker.advanceYears(d1, '+', RegExp.$2);
      }
    } else if ( ar = expires.match(/^(\d+)\.seconds?\.ago$/) ) {
      $.datemaker.advanceSeconds(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.seconds?\.from_now$/) ) {
      $.datemaker.advanceSeconds(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.minutes?\.ago$/) ) {
      $.datemaker.advanceMinutes(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.minutes?\.from_now$/) ) {
      $.datemaker.advanceMinutes(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.hours?\.ago$/) ) {
      $.datemaker.advanceHours(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.hours?\.from_now$/) ) {
      $.datemaker.advanceHours(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.days?\.ago$/) ) {
      $.datemaker.advanceDays(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.days?\.from_now$/) ) {
      $.datemaker.advanceDays(d1, '+', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.years?\.ago$/) ) {
      $.datemaker.advanceYears(d1, '-', RegExp.$1);
    } else if ( ar = expires.match(/^(\d+)\.years?\.from_now$/) ) {
      $.datemaker.advanceYears(d1, '+', RegExp.$1);
    } else if (ar = expires.match(/^now$/)) {
      // just return d1
    } else if (ar = expires.match(/^midnight$/)) {
       d1.setDate(d1.getDate()+1);
       d1.setHours(0);
       d1.setMinutes(0);
       d1.setSeconds(0);
    }
    return d1;
  };

  // Advance functions
  $.datemaker.advanceSeconds = function(d1, direction, seconds) {
      direction == '-' ? d1.setSeconds( d1.getSeconds() - parseInt(seconds) ) : d1.setSeconds( d1.getSeconds() + parseInt(seconds) );
  }
  $.datemaker.advanceMinutes = function(d1, direction, minutes) {
      direction == '-' ? d1.setMinutes( d1.getMinutes() - parseInt(minutes) ) : d1.setMinutes( d1.getMinutes() + parseInt(minutes) );
  }
  $.datemaker.advanceHours = function(d1, direction, hours) {
      direction == '-' ? d1.setHours( d1.getHours() - parseInt(hours) ) : d1.setHours( d1.getHours() + parseInt(hours) );
  }
  $.datemaker.advanceDays = function(d1, direction, days) {
      direction == '-' ? d1.setDate( d1.getDate() - parseInt(days) ) : d1.setDate( d1.getDate() + parseInt(days) );
  }
  $.datemaker.advanceMonths = function(d1, direction, months) {
      direction == '-' ? d1.setMonth( d1.getMonth() - parseInt(months) ) : d1.setMonth( d1.getMonth() + parseInt(months) );
  }
  $.datemaker.advanceYears = function(d1, direction, years) {
      direction == '-' ? d1.setFullYear( d1.getFullYear() - parseInt(years) ) : d1.setFullYear( d1.getFullYear() + parseInt(years) );
  };
}) (jQuery)

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            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
        }
        // CAUTION: 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;
    }
};