/**
 * all around js functions go here
 * 
 * 
 */

//Auto-Fill Plugin
//Written by Joe Sak http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click/

(function($){$.fn.autofill=function(options){var defaults={value:'Search',defaultTextColor:"#ccc",activeTextColor:"#000"};var options=$.extend(defaults,options);return this.each(function(){var obj=$(this);obj.css({color:options.defaultTextColor}).val(options.value).focus(function(){if(obj.val()==options.value){obj.val("").css({color:options.activeTextColor});}}).blur(function(){if(obj.val()==""){obj.css({color:options.defaultTextColor}).val(options.value);}});});};})(jQuery);



/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
/**
 * Function : debug()
 * Arguments: The data - array,hash(associative array),object
 * DependsOn: the dump function
 * Returns  : a js alert of the js dump
 */

function debug(value){
	alert(dump(value));
}


/**
 * This function takes a single argument (a number of seconds) 
 * and returns a formatted string in the format "hh:mm:ss". 
 * Hours will not show if the total is less than an hour, 
 * minutes will always show.
 * 
 * Examples: 
 * 1:15:05 (4505 seconds), 
 * 36:01 (2161 seconds), 
 * 00:45 (45 seconds)
 * 
 * @param 
 * @return
 */
function secondsToHms(d) {
	d = Number(d);
	var h = Math.floor(d / 3600);
	var m = Math.floor(d % 3600 / 60);
	var s = Math.floor(d % 3600 % 60);
	return ((h > 0 ? h + ":" : "") + (m > 0 ? (m < 10 ? "0" : "") + m + ":" : "00:") + (s < 10 ? "0" : "") + s);
}

/**
 * This function takes a single argument (a number of seconds) 
 * and returns a formatted string in the format "mm:ss". 
 * Hours will not show, if the argument is more then an hour it will convert to minutes
 * minutes will always show.
 * 
 * Examples: 
 * 1:15:05 (4505 seconds), 
 * 36:01 (2161 seconds), 
 * 00:45 (45 seconds)
 * 
 * @param 
 * @return jwSecondsToms(d)
 */
function jwSecondsToMs(d) {
	d = Number(d);
	var m = Math.floor(d  / 60);
	var s = Math.floor(d % 3600 % 60);
	return ((m > 0 ? (m < 10 ? "0" : "") + m + ":" : "00:") + (s < 10 ? "0" : "") + s);
}

/**
 * Convert Filesize Bytes to Readable String
 * 
 * @param bytes
 * @return
 */

function readableBytes(bytes) {
	var s = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
	var e = Math.floor(Math.log(bytes)/Math.log(1024));
	return (bytes/Math.pow(1024, Math.floor(e))).toFixed(2)+" "+s[e];
}
