// Replace every datetime pattern (abbr.published) into a
// human readable string.

/*
	Copyright (c) 2008 Yoan Blanc <yoan at dosimple dot ch>

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

if(typeof base2 !== "undefined"){
	new function(_){
		var twootr = new base2.Package(this, {
			name: "PrettyDate",
			version: "0.1",
			imports: "DOM, JavaScript",
			exports: "parseDate, diffToString, updateElements"
		});
		
		// evaluate the imported namespace
		eval(this.imports);
		
		// unities
		var nMinute=60,
			nHour=3600,
			nDay=86400,
			nWeek=604800,
			nInfinity=2419200, // 4 weeks.
			sLang= typeof LANG != "undefined" ? LANG : "en",
			i, oDate, aDates, oL10n;
		
		// translations
		oL10n = {
			"en":{
				"sec":["second","seconds"],
				"min":["minute","minutes"],
				"hour":["hour","hours"],
				"day":["day","days"],
				"week":["week","weeks"],
				"pre":"about",
				"post":"ago"
			},
			"fr":{
				"sec":["seconde","secondes"],
				"min":["minute","minutes"],
				"hour":["heure","heures"],
				"day":["jour","jours"],
				"week":["semaine","semains"],
				"pre":"il y a environ",
				"post":""
			},
			"de":{
				"sec":["Sekunde","Sekunden"],
				"min":["Minute","Minuten"],
				"hour":["Stunde","Stunden"],
				"day":["Tag","Tage"],
				"week":["Woche","Wochen"],
				"pre":"vor",
				"post":"ungefähr"
			}
		};
		
		function parseDate(sDate) {
			var m=sDate.match(/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|(?:(\+|-)([0-9]{2}):([0-9]{2})))$/);
			
			if(m) {
				// Z notation
				var oDate = new Date(Date.UTC(m[1],m[2]-1,m[3],m[4],m[5],m[6]));
				// +HH:MM notation
				if(m[7]) {
					oDate.setUTCHours(
						oDate.getUTCHours()-parseInt(m[7]+m[8], 10),
						oDate.getUTCMinutes()-parseInt(m[7]+m[8], 10));
				}
				
				return oDate;
			}
			
			return false;
		}

		function diffToString(nDiff) {
			var oLang = oL10n[sLang];
			var sKey = "";
			
			if(nDiff<nMinute) {
				sKey = "sec";
			} else if(nDiff<nHour) {
				nDiff /= nMinute;
				sKey = "min";
			} else if(nDiff<nDay) {
				nDiff /= nHour;
				sKey = "hour";
			} else if(nDiff<nWeek) {
				nDiff /= nDay;
				sKey = "day";
			} else if(nDiff<nInfinity) {
				nDiff /= nWeek;
				sKey = "week";
			} else {
				// the date is too far in the past.
				return false;
			}
			
			nDiff = parseInt(nDiff, 10);
			
			return new Array2(
				oLang.pre,
				nDiff,
				oLang[sKey][Math.min(nDiff, 2) - 1],
				oLang.post
			).filter(function(e){return !!e}).join(" ");
		}
		
		function updateElements(sSelector) {
			var aDates = Document.querySelectorAll(document, sSelector);
			var dNow = new Date();
			for(var i=0, oDate; oDate = aDates.item(i); i++) {
				var dDate = parseDate(oDate.getAttribute("title"));
				var sNewText = diffToString((dNow - dDate)/1000);
				if(sNewText) {
					if(oDate.nodeName.toLowerCase() == "abbr" && typeof oDate.firstChild !== "undefined" && !oDate.firstChild) {
						// IE don't like the ABBR tag.
						oDate.parentNode.innerHTML = oDate.parentNode.innerHTML.replace(new RegExp("(>)[^<]*(<\/"+oDate.nodeName+")", "i"), "$1"+sNewText+"$2");
					} else {
						oDate.firstChild.nodeValue = sNewText;
					}
				}
			}
		}
		
		eval(this.exports);
	} // end main closure
	
	// Example:
	base2.PrettyDate.updateElements("abbr.published");
};

