/*
 * PHP's strtotime function ported to Javascript.
 * 
 * strtotime(string time, int now)
 * 
 * Copyright (c) 2008, Caio Ariede
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice, this
 *		 list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice, this
 *		 list of conditions and the following disclaimer in the documentation and/or
 *		 other materials provided with the distribution.
 *     * Neither the name of the Caio Ariede nor the names of its contributors may
 *		 be used to endorse or promote products derived from this software without
 * 		 specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

function strtotime(str, now)
{

	str = str.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
	str = str.replace(/[\t\r\n]/g, ''); // unecessary chars 

	if (str == 'now') return (new Date()).getTime();
	else if (!isNaN(parse = Date.parse(str))) return parse;
	else if (now) now = new Date(now);
	else now = new Date();

	str = str.toLowerCase();

	var __is =
	{
		day:
		{
			'sun': 0, 'mon': 1, 'tue': 2, 'wed': 3,
			'thu': 4, 'fri': 5, 'sat': 6
		},
		mon:
		{
			'jan': 0, 'feb': 1, 'mar': 2, 'may': 3, 'apr': 4,  'jun': 5,
			'jul': 6, 'aug': 7, 'sep': 8, 'oct': 9, 'nov': 10, 'dec': 11
		}
	}

	var match = str.match(/^(\d{2,4}-\d{2}-\d{2})(\s\d{1,2}:\d{1,2}(:\d{1,2})?)?$/);

	if (match != null)
	{
		if (!match[2])
			match[2] = '00:00:00';
		else if (!match[3])
			match[2] += ':00';

		var s = match[1].split(/-/g);

		for (var i in __is.mon)
			if (__is.mon[i] == s[1] - 1)
				s[1] = i;

		return strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2]);
	}

	var regex = '([+-]?\\d+\\s'
			  + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
			  + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
			  + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday)'
			  + '|(last|next)\\s'
			  + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
			  + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
			  + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday))'
			  + '(\\sago)?';

	match = str.match(new RegExp(regex, 'g'));

	if (match == null)
	{
		return false;
	}

	for (var i in match)
	{
		if (!process(match[i].split(' ')))
		{
			return false;
		}
	}

	return now;

	function process(m)
	{
		var ago = (m[2] && m[2] == 'ago');
		var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);

		switch (m[0])
		{
			case 'last':
			case 'next':

				switch (m[1].substring(0, 3))
				{
					case 'yea':
						now.setFullYear(now.getFullYear() + num);
						break;
					case 'mon':
						now.setMonth(now.getMonth() + num);
						break;
					case 'wee':
						now.setDate(now.getDate() + (num * 7));
						break;
					case 'day':
						now.setDate(now.getDate() + num);
						break;
					case 'hou':
						now.setHours(now.getHours() + num);
						break;
					case 'min':
						now.setMinutes(now.getMinutes() + num);
						break;
					case 'sec':
						now.setSeconds(now.getSeconds() + num);
						break;
					default:

						var day;

						if (typeof (day = __is_day[m[1].substring(0, 3)]) != 'undefined')
						{
							var diff = day - now.getDay();

							if (diff == 0)
							{
								diff = 7 * num;
							}
							else if (diff > 0)
							{
								if (m[0] == 'last') diff -= 7;
							}
							else
							{
								if (m[0] == 'next') diff += 7;
							}

							now.setDate(now.getDate() + diff);
						}
				}

				break;

			default:

				if (/\d+/.test(m[0]))
				{
					num *= parseInt(m[0]);

					switch (m[1].substring(0, 3))
					{
						case 'yea':
							now.setFullYear(now.getFullYear() + num);
							break;
						case 'mon':
							now.setMonth(now.getMonth() + num);
							break;
						case 'wee':
							now.setDate(now.getDate() + (num * 7));
							break;
						case 'day':
							now.setDate(now.getDate() + num);
							break;
						case 'hou':
							now.setHours(now.getHours() + num);
							break;
						case 'min':
							now.setMinutes(now.getMinutes() + num);
							break;
						case 'sec':
							now.setSeconds(now.getSeconds() + num);
							break;
					}
				}
				else
				{
					return false;
				}

				break;

		}

		return true;
	}

}
