//object available to page
var oVarManipulator = new varManipulator();

/**
 * the varManipulator class provides methods for handling variables
 *
 */
function varManipulator(){
  //properties
  //methods
  this.isDefined = isDefined;
  this.isEmpty = isEmpty;
  this.generateRandomString = generateRandomString;
  this.inArray = inArray;
  this.convertBytes = convertBytes;
  this.displayBytes = displayBytes;
  this.secondsToTime = secondsToTime;
  this.displaySecondsToTime = displaySecondsToTime;
}

/*
 * checks whether a variable is defined 
 *  
 * @param mixed variable
 * 
 * @return boolean
 */
function isDefined(variable){
	return typeof(variable) != 'undefined';
}

/*
 * checks whether an array or object has at least one element/property
 *  
 * @param mixed variable
 * 
 * @return boolean
 */
function isEmpty(variable){
	if(this.isDefined(variable) && typeof(variable) == 'object'){
		for(i in variable){
			if(this.isDefined(variable[i])) return false;
		}
	}
	return true;
}

/*
 * generate a random string of variable length 
 *  
 * @param integer length
 * 
 * @return string random string
 */
function generateRandomString(length){
  var chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
  var sRnd = "";
  for(x=0;x<length;x++){
    i = Math.floor(Math.random() * chars.length);
    sRnd += chars.charAt(i);
  }
  return sRnd;
}

/*
 * checks whether a string is content among an array elements 
 *  
 * @param string needle 
 * @param array a
 * 
 * @return boolean
 */
function inArray(needle,a){
	return ('_|_'+a.join('_|_')+'_|_').indexOf('_|_'+needle+'_|_') > -1;
}

/*
 * converts between bytes, kilobytes, megabytes and gigabites
 * based on http://javascript.internet.com/math-related/byte-converter.html   
 *  
 * @param string fromUnit: b | kb | mb | gb
 * @param string toUnit: b | kb | mb | gb
 * @param mixed value
 * @param boolean bUnitLabel: whher to append measure unit short  
 * 
 * @return string converted value
 */
function convertBytes(fromUnit,toUnit,value,bUnitLabel){
	var convertedValue,bytes;
	if(fromUnit == toUnit){
		convertedValue = value;
	}else{
		//turn into bytes
		switch(fromUnit){
			case 'b':
				bytes = value;
			break;
			case 'kb':
				bytes = Math.round(value*1024*100000)/100000;
			break;
			case 'mb':
				bytes = Math.round(value*1048576*100000)/100000;
			break;
			case 'gb':
				bytes = Math.round(value*1073741824*100000)/100000;
			break;
		}
		//turn into toUnit
		switch(toUnit){
			case 'b':
				convertedValue = Number(bytes);
			break;
			case 'kb':
				convertedValue = Math.round(bytes/1024*100000)/100000;
			break;
			case 'mb':
				convertedValue = Math.round(bytes/1048576*100000)/100000;
			break;
			case 'gb':
				convertedValue = Math.round(bytes/1073741824*100000)/100000;
			break;
		} 
	}
	convertedValue = convertedValue.toFixed(2);
	if(bUnitLabel) convertedValue += ' '+toUnit;
	return convertedValue;
}

/**  
 * displays a byte value in a measure unit appropriate to value size
 *
 * @param string val value in bytes to display    
 *
 * @return string value converted plus measure unit short
 *
 * @access public
 */

function displayBytes(value){
  var fromUnit = 'b';
  var toUnit = 'b';
  if(!value) value = '0';
  if(value >= 1024 && value < 1048576) toUnit = 'kb';
  else if(value >= 1048576 && value < 1073741824) toUnit = 'mb';
  else if(value >= 1073741824) toUnit = 'gb';
  if(toUnit != 'b') value = this.convertBytes(fromUnit,toUnit,value,true);
  else value += ' b';
  return value;
}

/**
 * Converts number of seconds into time object
 * took from http://codeaid.net/javascript/convert-seconds-to-hours-minutes-and-seconds-%28javascript%29 
 *
 * @param integer startSeconds Number of seconds to convert
 * @return object
 */
function secondsToTime(startSeconds){
  var hours = Math.floor(startSeconds / (60 * 60));
  var divisorForMinutes = startSeconds % (60 * 60);
  var minutes = Math.floor(divisorForMinutes / 60);
  var divisorForSeconds = divisorForMinutes % 60;
  var seconds = Math.ceil(divisorForSeconds);
  var oTime = {
      'h': hours,
      'm': minutes,
      's': seconds
  };
  return oTime;
}

/**
 * Displays a number of seconds into approprieate form 
 *
 * @param integer startSeconds Number of seconds to convert
 * @return string
 */
function displaySecondsToTime(startSeconds){
	var oTime = this.secondsToTime(startSeconds);
	var aTime = new Array();
	if(oTime.h) aTime.push(oTime.h+'h');
	if(oTime.m) aTime.push(oTime.m+'&prime;');
	if(oTime.s) aTime.push(oTime.s+'&Prime;');
	return aTime.join(' ');
	
}
