﻿//+++++++++++++++++++++++++++++++++++++++++++++++++
// 	2009, CommonTools, Kee js framework
//	Andrey [Kee] Peskov, warhuman[at]mail.ru
// 	You free to use this code, but 
//	please leave this copy-info intact
//+++++++++++++++++++++++++++++++++++++++++++++++++

if(!kee){ var kee = {}; }
if(!kee.tool) { kee.tool = {}; }

kee.tool.enable_log = true;


kee.tool.inlabel = function(obj, str, isfocus) {var v = obj.value;
if (isfocus) { if (v == str) { obj.value = ''; obj.className = ''; } }
else { if (v == '') { obj.value = str; obj.className = 'shadow'; } } }

kee.tool.reimage = function(img, imgsrc){
	if(!img) return;
	if (!kee.tool.swaps) { kee.tool.swaps = 0; }
	if(!img.orig) { img.orig = img.src.toString(); }
	if(!img.count) { img.count = 0; }
	img.setAttribute('src',img.orig +"&rnd=" + img.count);
	img.count++;
}

kee.tool.o = function(nm){
	var obj = document.getElementById(""+nm);
	return (!obj || obj == 'undefined' || obj == null)?{}:obj;
}
kee.tool.parents = function(ob,num) {
	num = new Number(num)||1;
	for (var i=0;i<num;i++){ if(ob!=null)ob = ob.parentNode; }
	return ob;
}
kee.tool.position = function(obj){
	var parent = obj;	
	var pose = {x:0,y:kee.tool.dig(obj.offsetHeight),w:kee.tool.dig(obj.offsetWidth),h:kee.tool.dig(obj.offsetHeight)};
	while(parent.tagName.toUpperCase()!="BODY"){ 
		pose.x += parent.offsetLeft; 
		pose.y += parent.offsetTop; 
		if(parent.style.position.toUpperCase()=="ABSOLUTE") break;
		parent=parent.offsetParent; 
	}
	return pose;
}


kee.tool.dig = function(str,prezero){
	str = (str||"").toString();
	var n = new Number(str.replace(/[^0-9]/ig,""));
	if(prezero == true){ n = (n<10)?"0"+n:n; }
	return n;
}
kee.tool.addHandler = function(object, event, handler){
  if (typeof object.addEventListener != 'undefined'){ object.addEventListener(event, handler, false); }
  else if (typeof object.attachEvent != 'undefined') { object.attachEvent('on' + event, handler); }
  else { throw "Events NOT supported"; }
}
kee.tool.removeHandler = function(object, event, handler){
  if (typeof object.removeEventListener != 'undefined'){ object.removeEventListener(event, handler, false); }
  else if (typeof object.detachEvent != 'undefined') { object.detachEvent('on' + event, handler); }
  else { throw "Events NOT supported"; }
}
kee.tool.clone  = function(obj,withfunc,withsubobj) {    
	withfunc = (!withfunc)?false:true;
	withsubobj = (!withsubobj)?false:true;
	
	if (!obj ||('object' !== typeof(obj)))  { return obj; }
	var clon = ('function' === typeof(obj.pop)) ? [] : {};    
		var prop;
		var subobj;     
		for (prop in obj) { 
			if (typeof(obj[prop]) === 'function') { 
					if(withfunc == true) { clon[prop] = obj[prop]; } 
			}
			else {
			//else if (obj.hasOwnProperty(prop)) { //IE7 WTF??
				subobj = obj[prop];        
				if (subobj && 'object' === typeof(subobj)) { 
					if(withsubobj == true) { clon[prop] = kee.tool.clone(subobj); }
				}         
				else { clon[prop] = subobj; }      
			}
		}
	return clon;  
}
kee.tool.get_style = function(elm) {
	return elm.currentStyle||window.getComputedStyle(elm, null)
}


kee.tool.filterinput = function(obj, mark) {
	if(!obj) { return; }
	//kee.tool.addHandler(obj,"blur",this);
	mark = mark||"";
	mark = mark.toString().toLowerCase();
	var text=obj.value;
	switch (mark) {
		case "iterator": text=text.replace(/[^\d:,]/gi,'');	break;
		default: break;
	}
	obj.value=text;
	
}



kee.tool.DEBUG = function(obj, tab){
	tab = tab||1;
	var counter =0;
	var str="";
	for (a in obj){

		if(a.indexOf("_")>-1) {
			str += a + ": " + obj[a]+"\n";
		}else{
		
			if (typeof(obj[a]) === 'function') {str += "\n<F " + a + ">\n" + obj[a] + ""; counter = 0;} 
			else if (typeof(obj[a].pop) === 'function') {	
					var aa =obj[a]; 
					str += "\n<A " + a +">\n"; 
					for (var i=0;i<aa.length;i++) {	
						if(typeof(aa[i]) === 'object') { str += "[" + i +"] " + kee.tool.DEBUG(aa[i],tab) + ""; }
						else { str += "[" + i +"] " + aa[i]  + "\n"; }
					}
			counter = 0;	
			} 
			else if (typeof(obj[a]) === 'object') { var aa=obj[a];  str += "\n<O " + a + ">\n" + kee.tool.DEBUG(aa,tab) + ""; counter = 0; } 
			else { str += a + ": " + obj[a] + "\t\t"; counter++; }
			if(counter >= tab) { str+="\n"; counter = 0;}
		}

	}
	return str;
}
kee.tool.LOGG = function(lg) {
	if(!kee.tool.enable_log)return;
	var lid = "DEBUGGER_LOGG_STRINGS_INPUT_OBJECT";
	var logger = document.getElementById(lid);
	if(!logger) { 
		var t = document.createElement("textarea");
		t.id = lid;
		document.body.appendChild(t);
		t.style.width = "100%";
		t.style.height = "200px"; 
		kee.tool.LOGG(lg)
	} else{
		logger.value += "\n" + lg;
	}
}

//protoss

String.prototype.toFirstCase = function(){
	return this.replace(this.charAt(1),this.charAt(1).toUpperCase());
}
String.prototype.toCamelCase = function(){
	return this.replace(/-\D/g, function(match){ return match.charAt(1).toUpperCase();	});
}
String.prototype.toDig = function(padZero){
	var n = new Number(this.replace(/[^0-9]/ig,""));
	if(!!padZero){ n = (n > 0 && n < 10) ? "0" + n : n; }
	return n;
}
String.prototype._style = function(elm,ext) {
	if(!elm) return '';
	if (typeof elm == 'string') elm = document.getElementById(elm);
	var ext = (!ext) ? this : (ext + '-' + this).toCamelCase();
	if (!!elm.style[ext]) return '' + elm.style[ext];	
	else return '';	
}
String.prototype._params = function(elm,ext) {
	if(!elm) return '';
	if (typeof elm == 'string') elm = document.getElementById(elm);
	var ext = (!ext) ? this : (ext + '-' + this).toCamelCase();
	if (!!elm[ext]) return '' + elm[ext];	
	else return '';	
}
String.prototype._scroll = function(elm) {
	return this._params(elm,'scroll').toDig();
}
String.prototype._offset = function(elm) {
	return this._params(elm,'offset').toDig();
}
String.prototype._actAsFunc = function(elm) {
	var f = this._params(elm);
	if (typeof f == 'function') return f;
	f = ("on" + this)._params(elm);
	if (typeof f == 'function') return f;
	return function(){ return false; };
}
String.prototype._trim = function() {
	//var s = this;
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}


Array.prototype._spliceid = function(aid){
	aid = kee.tool.dig(aid);
	var targ =[];
	for (var i=0; i<this.length; i++) {	
		if(i != aid) { targ.push(this[i]); } 
	}
	return targ;	
}
Array.prototype._splice = function(obj){
	if (!obj) { return this;  }
	var targ = [];
	for (var i=0; i<this.length; i++) {	
		if(this[i] !== obj) { targ.push(this[i]); } 
	}
	return targ;	
}
Array.prototype._copy = function(){
	var targ = [];
	for (var i=0; i < this.length; i++) {	targ.push(this[i]); }	
	return targ;
}
