/**
 * Various helper functions (some reqiure jQuery)
 *
 * @author			Pat Fox
 */
Utilities= {
	/**
	 * Pad a number to a set length
	 *
	 * @param		mixed		value 		Number of numeric string
	 * @param		int			length		Desired length of output with zeroes
	 *
	 * @return		string
	 */
	ZeroPad:function( value, length ){
		if( value.toString().length < length ){
			var padding = '';
			for( var i = 0; i < length; i++ ){
				padding += '0';
			}
			value = padding + value.toString();
			value = value.substr( value.length - length );
		}
		return value;
	},

	/**
	 * Evaluate a regular expression match
	 *
	 * @param		string		field				The field to check
	 * @param		string		pattern				Regexp pattern
	 *
	 * @return		boolean							Success -- ie. did it match?
	 */
	CheckRegexp:function( value, pattern ){

		var output = true;

		var pattern = new RegExp( pattern );

		return pattern.test( value );
	},

	/**
	 * Create a deep copy of an object/array (i.e. doesn't reference any propertie of the original)
	 * Taken from http://bytes.com/forum/thread715567.html
	 *
	 * @param		mixed		obj				The object to clone
	 *
	 * @return		mixed
	 */
	Clone:function(obj) {
		var c = obj instanceof Array ? [] : {};

		for (var i in obj) {
			var prop = obj[i];

			if (typeof prop == 'object') {
			   if (prop instanceof Array) {
				   c[i] = [];

				   for (var j = 0; j < prop.length; j++) {
					   if (typeof prop[j] != 'object') {
						   c[i].push(prop[j]);
					   } else {
						   c[i].push(this.Clone(prop[j]));
					   }
				   }
			   } else {
				   c[i] = this.Clone(prop);
			   }
			} else {
			   c[i] = prop;
			}
		}

		return c;
	},

	/**
	 * Create an HTML element and populate its properties
	 *
	 * @param		string		type			What type of element to create
	 * @param		object		properties		[Optional] Options to assign to the object (text, html, css, events, id etc.)
	 *
	 * @return		HTMLElement
	 */
	Create:function( type, properties ){
		var element = jQuery( document.createElement( type ) );

		if(  typeof properties == 'object' ){
			for( var name in properties ){
				var value = properties[ name ];
				switch( name ){
					case 'css':{
						element.css( value );
						break;
					}
					case 'text':{
						element.attr
						(
							( ( jQuery.browser.mozilla ) ? 'textContent' : 'innerText' ),
							value
						);
						break;
					}
					case 'html':{
						element.append( value );
						break;
					}
					case 'events':{
						for( var event in value ){
							element.bind( event, value[ event ] );
						 }
						 break;
					}
					default:{
						element.attr( name, value )
						break;
					}
				}
			}
		}

		return element;
	},
	/**
	 * Bind a function to an object scope
	 *
	 * @param		function		fn				A (usually anonymous) function
	 * @param		object			scope			An object to bind the function to
	 * @param		array			args			[ Optional ] A list of arguments to pass to the function
	 * @param		boolean			override		[ Optional ] If args supplied, should they overwrite the normal arguments or be appended?
	 * 												- enables passing args to events without losing the event object etc.
	 *
	 * @return		function
	 */
	Bind:function( fn, scope, args, override ) {
		args = jQuery.makeArray( args );

		return function()
			{
				arguments = jQuery.makeArray( arguments );
				if( args ){
					if( override ){
						arguments = args;
					} else {
						for( var i = 0; i < args.length; i++ ){
							arguments.push( args[ i ] );
						}
					}
				}
				fn.apply( scope, arguments );
			};
	},
	GetType:function(obj){
		if (obj == undefined) return false;
		if (obj.constructor == Array) return 'array';
		if (obj.nodeName){
			switch (obj.nodeType){
				case 1: return 'element';
				case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
			}
		} else if (typeof obj.length == 'number'){
			if (obj.callee) return 'arguments';
			else if (obj.item) return 'collection';
		}
		return typeof obj;
	},

	/**
	 * Create a new 'class' by enxtending another
	 *  based on code from 	//http://javascript.crockford.com/prototypal.html and http://www.cs.rit.edu/~atk/JavaScript/manuals/jsobj/
	 *
	 * @param		function		base 			The 'class' to be extended
	 * @param		object			properties		[Optional] Any properties (or methods) to be added to the object
	 */
	Extend:function( base, properties ){
		function F(){
			// Pass through constructor
			this.base = base;
		    this.base.apply(this, arguments);
		};
		for( var prop in base.prototype ){
			F.prototype[ prop ] = base.prototype[ prop ];
		}

		for( var key in ( properties || {} )){
			F.prototype[ key ] = properties[ key ];
		}

		return F;
	},

	Truncate:function( string, length){

		  if (string.length > length) {

			trunc = trunc.substring(0, len);
			trunc = trunc.replace(/\w+$/, '');

			/* Add an ellipses to the end and make it a link that expands
			   the paragraph back to its original size */
			trunc += '<a href="#" ' +
			  'onclick="this.parentNode.innerHTML=' +
			  'unescape(\''+escape(p.innerHTML)+'\');return false;">' +
			  '...<\/a>';
			p.innerHTML = trunc;
		}
	},

	Replace:function(search, replace, subject) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Gabriel Paderni
		// +   improved by: Philip Peterson
		// +   improved by: Simon Willison (http://simonwillison.net)
		// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
		// +   bugfixed by: Anton Ongson
		// +      input by: Onno Marsman
		// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +    tweaked by: Onno Marsman
		// *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
		// *     returns 1: 'Kevin.van.Zonneveld'
		// *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
		// *     returns 2: 'hemmo, mars'

		var f = search, r = replace, s = subject;
		var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

		while (j = 0, i--) {
			if (s[i]) {
				while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
			}
		};

		return sa ? s : s[0];
	}
}

Cookie = {
	Create:function( name, value, days ) {
		if ( days ) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},

	Read:function( name ) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	Destroy:function( name ) {
		this.Create(name,"",-1);
	}
}

String.prototype.truncate = function( length )
{
	if ( this.length > length ) {

		trunc = this.substring(0, length);
		trunc = trunc.replace(/\w+$/, '');

		return trunc + '...';
  }
  return this;
}