/*
	Copyright James Matteson 2011. All rights reserved.
*/
var myUtils = new utilities();

function utilities(){
   var oThis = this;
   var sharedItems = null;
   
   /* String */
   function removeFromString(str, rx, replace){
      if (replace == null) replace = '';
		return str.replace(rx, replace);
	}
   
   this.stripNonAlphaNumeric = function(str, replace){
      return removeFromString(str, /[^A-Za-z0-9 ]+/g, replace);
   }
   
   /* Time */
   this.formatTime = function(time, inMilli){
      var minutes;
      var seconds;
      
      if (inMilli != null && inMilli){
         time = time / 1000; // Milliseconds to seconds
      }
      
      seconds = Math.round(time);
      minutes = Math.floor(seconds / 60);
      seconds = seconds - (60 * minutes)
      
      return minutes + ':' + (seconds < 10 ? '0' + seconds : seconds);
   }
   
   /* Keyboard */
   this.getCharCode = function(e){
      var code; //literal character code will be stored in this variable
      
      if (e && e.which){ //if which property of event object is supported (NN4)
         e = e;
         code = e.which; //character code is contained in NN4's which property
      }else{
         if (e == null) e = event;
         code = e.keyCode; //character code is contained in IE's keyCode property
      }
      
      return code;
   }
   this.isEnter = function(e){ //e is event object passed from function invocation
      return oThis.getCharCode(e) == 13 ? true : false;
   }
   this.isEsc = function(e){
      return oThis.getCharCode(e) == 27 ? true : false;
   }
   this.noSelect = function(e) {
      e.onselectstart = function() { return false; };
      e.unselectable = "on";
      e.style.MozUserSelect = "none";
      e.style.cursor = "default";
   }
   
   /* Scripts */
   this.attachScript = function(url){
      var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = url;
      $('scripts').appendChild(newScript);
   }
   this.attachSearchScript = function(url){
      var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = url;
      $('scriptSearch').innerHTML = '';
      $('scriptSearch').appendChild(newScript);
   }   
   
   /* Sharing */
   this.getSharedItems = function(){
      if (sharedItems == null){
         var temp = location.href.split('?pl=');
      
         if (temp.length == 2)
            sharedItems = temp[1].split(',');
         else
            sharedItems = [];
      }
         
      return sharedItems;
   }
}
