/** 
 * @fileoverview
 * @author			Michael Ord <michael.ord@think.eu>
 * @version			0.1
 * @class
 * @requires		YAHOO.util.Dom
 * @requires		YAHOO.util.Event
 * 
 * @file			init.js
 * @description		
 */

/* Add extra functionality for lower versions of IE */
if (!Array.prototype.push){
	Array.prototype.push	= function () {
		for (var i = 0; i < arguments.length; i++) {
			this [ this.length ] = arguments [ i ];
		};
		return this.length;
	};
};

/**
 * TODO Description
 * @method normalize
*/
if (!String.prototype.trim) {
	String.prototype.trim	= function () {
		return this.replace (/^\s*/,'').replace(/\s*$/, '');
	};
};

/**
 * TODO Description
 * @method normalize
*/
String.prototype.reverse = function () {
	var s	= '';
	for ( var i = this.length - 1; i >= 0; i-- ) {
		s	+= this.charAt ( i );
	};
	return s;
}

/**
 * TODO Description
 * @method normalize
*/
String.prototype.normalize = function () {
	return this.replace (/\s+/g, " ");
};

/**
 * TODO Description
 * @method isLeapYear
*/
Date.prototype.isLeapYear = function () {
	var year	= this.getFullYear ();
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
};

/**
 * TODO Description
 * @method getDaysInMonth
*/
Date.prototype.getDaysInMonth = function () {

	var days	= new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	if (this.isLeapYear ()){
		days [ 1 ]	= 29;
	};

	return days [ this.getMonth () ];
};

/**
 *
 * @param {Object} YYYY
 * @param {Object} MM
 * @param {Object} DD
 */
Date.prototype.isValid = function (YYYY, MM, DD) {

	DD	= Number (DD);
	MM	= Number (MM);
	YYYY	= Number (YYYY);

	if (MM >= 0 && MM < 12 && String (YYYY).length == 4 && DD > 0) {

		var tmp	= new Date (YYYY, MM);
		var days	= tmp.getDaysInMonth ();

		if (DD <= tmp.getDaysInMonth ())	{
			return true;
		};
	};
	return false;
};

/**
 *
 * @param {Object} to
 */
Date.prototype.getDiffMonths = function (to) {
	return this.dateDiff ("m", to);
};

/**
 *
 * @param {Object} to
 */
Date.prototype.getDiffYears = function (to) {
	return this.dateDiff ("y", to);
};

/**
 *
 * @param {Object} dp
 * @param {Object} v_date
 */
Date.prototype.dateDiff = function (dp, v_date) {
	var ms	= v_date - this;
	var ss	= Math.floor (ms / 1000);
	var mm	= Math.floor (ss / 60);
	var hh	= Math.floor (mm / 60);
	var dd	= Math.floor (hh / 24);
	var yy	= Math.floor (dd / 365.25);
	var mn	= Math.floor (dd / 365.25 * 12);
	return (dp == "y") ? yy : (dp == "m") ? mn : (dp == "d") ? dd : (dp == "h") ? hh : (dp == "n") ? mm : (dp == "s") ? ss : ms;
};