
var jq14 = jq14 || $.noConflict();

var MSGV = MSGV || {};
MSGV.Utils = (function() {

    var self = {
        
        dateStrToObj: function(strDate) {
            var mDate, dateObj;
            mDate = strDate.slice(0,4) + '/';
            mDate += strDate.slice(4,6) + '/';
            mDate += strDate.slice(6,9);
            dateObj = new Date(mDate);
            return dateObj
        },

        prettyDate:function(strDate) {
            var d, day, month, date;
            d = MSGV.Utils.dateStrToObj(strDate);
            day = Date.dayNames[d.getDay()];
            month = Date.abbrMonthNames[d.getMonth()];
            date = d.getDate();
            return day + ', ' + month + ' ' + date;
        },

        ellipsis : function(x, maxlen) {
            if (x.length <= maxlen) {
                return x
            } else if (maxlen < 4) {
                return x.substring(0, maxlen)
            } else {
                return x.substring(0, maxlen - 3) + "...";
            }
        },
        
        trim : function(text) {
            text = text.replace(/^\s+/, '');
            return text.replace(/\s+$/, '');
        }

    }

    return self;
    
})();

(function() { 
    var loadedFiles = {}; 
    this.$css = function(filename) { 
        if (loadedFiles[filename]) {  
            return;  
        }  
        loadedFiles[filename] = true;
        jq14.get(filename, function(data) {
            jq14('head').append('<style>' + data + '</style>');
            }
        )
    };  
})();


KenzanDebug  = new (function($jq){
 
 var localUtil = this;
 
 localUtil.logLevel = "debug";
 
 var namedLoggersBlackList = [
   "VODResultItem"
 ];
 
 this.NamedLogger = function(name){
   var levels = ["debug", "info", "warn", "error"];
   var self = {};
   
   var logIt = function(message){
     // exclude loggers who are in the black list
     if($jq.inArray(name, namedLoggersBlackList) == -1){
           KenzanDebug.logger.debug("[" + name + "] " + message);
     }
   }
   
   var atLogLevel = function(level){
     return $jq.inArray(level, levels) >= $jq.inArray(localUtil.logLevel, levels);
   }
 
    self.debug = function(message){
     if(atLogLevel("debug")){
       logIt(message)  
     }
    }
 
   self.info = function(message){
     if(atLogLevel("info")){
       logIt(message)  
     }
   }
   
   self.warn = function(message){
     if(atLogLevel("warn")){
       logIt(message)  
     }
   }
   
   self.error = function(message){
     if(atLogLevel("error")){
       logIt(message)  
     }
   }
 
   return self;
     
 };
 
 
 this.logger = new (function(){
   var forceConsole = true; // create a console if the browser doesn't have one
   this.logToServer = false;
   var enabled = false;
   if(
     window.location.toString().indexOf("http://local.") == -1 
     && window.location.toString().indexOf("http://localhost") == -1 
     && window.location.toString().indexOf("localhost:") == -1
   ){
       enabled = false;
   }
   var timers = [];
   var oThis = this;
   
   this.Timer = function(){
     this.elapsedTime = 0;
     this.startTime = null;
   };
   
   this.Timer.prototype = {
     start: function(){
       if(this.startTime !== null){
         throw new Error("Timer: \""+ this.name +"\" is already running");
       }
       this.startTime = new Date().getTime();
       return this;
     },
     stop : function(){
       if(this.startTime === null){
         throw new Error("Timer is already stopped");
       }
       this.elapsedTime += (new Date().getTime() - this.startTime);
       this.startTime = null;
       return this;
     }
   };
   
   this.enable = function(){
     enabled = true;
   };
   
   this.disable = function(){
     enabled = false;
   };
   
   this.startTimer = function(name){
     var timer = timers[name];
     if(typeof timer === "undefined"){
       timer = timers[name] = new this.Timer();
       timer.name = name;
     }
     timer.start();
   }
   
   this.stopTimer = function(name){
     var timer = timers[name];
     if(typeof timer === "undefined"){
       this.error("ERROR - there is no timer with the name: " + name); 
     }else{
       timer.stop();
     }
   }
   
   this.printTimes = function(){
     localUtil.array.forKeys(timers, function(name, timer){
       oThis.log("Timer: <b>" + name + "</b> total elapsed time is " + timer.elapsedTime + " ms.");
     });
     
     
   }
   this.clearTimers = function(){
     timers = [];
   }
   
   this.debug = function(arg){
     if(enabled){
       
       if(typeof console === "undefined" && forceConsole){
         $jq("<div id='consoleDiv' style='width: 400px; height: 200px; overflow:auto; background-color: #eeeeff; padding: 5px; position: absolute; z-index: 9999; top:0px; right:0px; color: #000000'>CONSOLE</div>").appendTo($jq("body"));
         window.console = {
           log: function(message){
                            // alert(message)
               message = message.replace(/</g, "&lt;");
               message = message.replace(/>/g, "&gt;");
                            // alert(message)
             $jq("#consoleDiv").append("<br><br>").append(message);
             var objDiv =  document.getElementById("consoleDiv");
             objDiv.scrollTop = objDiv.scrollHeight
           }
         };  
       }
       
       var message;
       if(typeof arg === "function"){ // pass a function if we're the log message is expensive to calculate
         message = arg();
       }else{
         message = arg;
       }
       var date = new Date();
       var output = date.getMinutes() + ":" + date.getSeconds() + ":" + date.getMilliseconds()  + " " + message;
       if(this.logToServer){
             $jq.get("logger.jsp",{logValue: output});
       }else if(typeof console !== "undefined"){ 
                    console.log(output);                   
       }         
     }
   };
   
   this.log = this.debug;
   this.error = this.log;
   this.warn = function(arg){
       this.debug("WARNING: " + arg);
   }
   
 })();
})(jq14)


// this is so you can register functions for document.ready even before jquery has loaded
jq14(document).ready(function(){
  if(MSGV.onloadedFns){
    jq14.each(MSGV.onloadedFns, function(index, fn){
  		fn();
  	});
  	MSGV.onloadedFns = []; // util is getting loaded twice. This is a quick check to prevent re-running of these fns.
  }
})
