/**
* COPYRIGHTED SOFTWARE
* PLEASE READ THE LICENSE AGREEMENT
* 
* @copyright		Copyright 2008-2010 Dilltree Inc.
* @package			tPantherPro
* @license 			http://dilltree.com/license/tPantherPro/2.0
* @author			Jeremy Dill
* 
* PANTHER PROTOTYPE
* @module panther
* @description Panther is a colleciton of js utils that go along with the tPanther abstract.
* @submodule core
* @requires jquery 
* @author Jeremy Dill (except where noted)
* @example: new panther();
* 			 _PI.console("test"); // _PI (panther instance)is always avail as the app instance of panther.  Use this for plugins, etc.
*/

var _PI=null; // PANTHER INSTANCE
(function($){
	panther=makeClass();
	panther.prototype.init=function(){
		if(!_PI) _PI=this;
		else alert('No need to call new panther() twice!. panther was already initialized.');
	};
	panther.prototype.stash={};
	panther.prototype.store={};
	panther.prototype.debug=false;
	panther.prototype.debugResp=false;
	panther.prototype.debugTypes="ALL"; // SPECIFY A COMMA SEP LIST LIKE "ERR,WRN,INFO," OR JUST SPECIFY "ALL"
	panther.prototype.stats=false;
	panther.prototype.sess="";
	/* TRACE BY JOEY DILL */
	panther.prototype.trace=function (msg) {
		if ($('#trace_wrap')[0] == undefined) {
		  $("body").append("<div id='trace_wrap' style='position:fixed;bottom:0;margin-top:75px;left:10%;width:80%;'>"+
			"<a id='trace_close' style='position:absolute;top:-22px;right:5px;padding:3px 5px;background:#fff;border:2px solid #000;color:#000;cursor:pointer;'>(close)</a>"+
			"<div id='trace_msg' style='padding:10px;border:4px ridge #F3F3EE;background:#FFF;height:100px;overflow:auto;position:relative;'>"+
			"</div></div>");
		  $('#trace_close').click(function() {
			 $('#trace_msg').slideToggle('fast');
		  });
		}
		$('#trace_msg').append(msg+'<br />');
		return msg;
	};
	panther.prototype.notice=function(sel,msg,sticky,callback){
		var jobj=$(sel);
		jobj.stop(1,1).html(msg).show();
		if ($.fn.shake) jobj.shake();
		if (!sticky) jobj.fadeLater(); //replace this with delay.
		if(typeof(callback)=="function") jobj.queue(callback);
	};
	panther.prototype.ochk=function(o){
		if (typeof(o)=="object") return true;
		return false;
	};
	panther.prototype.oEmpty=function(obj){
	  for(var i in obj){ if(obj.hasOwnProperty(i)) return false; }
	  return true;
	};
	panther.prototype.console=function (msg,func) {
		if (!this.debug) return false;
		var valid=false;
		if (typeof msg!="string") {
			var type=(typeof msg).toUpperCase();
		} else {
			msg=msg.replace(/^\s+|\s+$/g,"");
			var type=msg.toUpperCase().substring(0,msg.indexOf("-"));
		}
		var dbt=this.debugTypes.toUpperCase()+",";	
		var valid=false;
		if((dbt.indexOf(type+",")>-1 || dbt.indexOf("ALL,")>-1) && dbt.indexOf("!"+type+",")<0 ) valid=true;
		if (valid){
		if (window.console && window.console.error && window.console.dir){
			switch(type){
				case "OBJECT":
					if (msg===null) console.log('null');
					else if(msg.stack||_PI.oEmpty(msg)) console.log(msg);
					else console.dir(msg);
				break;
				case "ERR":
					console.error(msg);	
				break;
				case "WRN":
					console.warn(msg);	
				break;				
				case "INFO":
					console.info(msg);	
				break;
				default:
					msg=msg+""; //make sure its a string.
					msg=msg.replace(/^\s+|\s+$/g,"");
					console.log(msg);
				break;
			}
		} else {
			this.trace(msg);
		}
		}
		return msg;
	};
	panther.prototype.cs=panther.prototype.console;
	panther.prototype.guid = function(prefix) {
		if(typeof prefix!=="string") prefix="id";
		if(!_PI.stash[prefix]) {
			_PI.stash[prefix]={};
			_PI.stash[prefix].count=0;
			return prefix+"0";
		}
		return prefix + _PI.stash[prefix].count++;
	};
})(jQuery);
/* COUPLE HANDY UTILS */
/* MAKECLASS FUNCTION - CONCEPT BY John Resig (MIT Licensed) */
function makeClass(){
	  return function(args){
	    if ( this instanceof arguments.callee ) {
	      if ( typeof this.init == "function" )
			if (args)
	        	this.init.apply( this, args.callee ? args : arguments );
			else this.init();
	    } else
	      return new arguments.callee( arguments );
	  };
};
