/**
 * kMy
 *
 * small extension plugins and prototype functions
 */

function var_dump(v,level){
  var dt="";
  var lp="";
  if(!level) level=0;
  for(var j=0;j<level+1;j++) lp+="  ";
  if(typeof(v)=='object'){
    for(var item in v) if(typeof(v[item])=='object') dt+=lp+"'"+item+ "'...\n"+dump(v[item],level+1); else dt+=lp+"'"+item+"' => \""+v[item]+"\"\n";
  }else dt=v+" ("+typeof(v)+")";
  return dt;
};

String.prototype.trim=function(){
  return this.replace(/^\s+|\s+$/g,'');
};
String.prototype.empty=function(){
  return this.length==0?true:false;
};
String.prototype.blank=function(){
  return this.trim().length==0?true:false;
};
String.prototype.removeWhiteSpace=function(){
  return this.replace(/\s*/g,"");
};
String.prototype.removeBlankLines=function(){
  return this.replace(/(\s*?[\r\n])+/g,"\n");
};
String.prototype.eval=function(){
  if(this.trim().length==0) return null;
  var d;
  eval("d="+this);
  return d;
};

// returns a range of an array; from, to inclusive
Array.prototype.range=function(from,to){
  var d=[];
  if(to==null) to=this.length-1;
  for(var i=from;i<=to && i<this.length;i++) d.push(this[i]);
  return d;
};

// remove empty strings from an array
Array.prototype.removeEmpty=function(){
  var d=[];
  for(var i=0;i<this.length;i++) if(this[i]!="") d.push(this[i]);
  return d;
};

// returns the beginning of a string until the substring parameter
String.prototype.getUntil=function(until){
  var pos=this.search(until);
  return pos==-1?this:this.substring(0,pos);
};

Array.prototype.findByKey=function(x){
  var i;
  var key;
  var result=[];
  var h;
  for(i=0;i<this.length;i++){
    h=1;
    for(key in x) if(!this[i][key] || this[i][key]!=x[key]){
      h=0;
      break;
    }
    if(h==1) result.push(this[i]);
  }
  return result;
};

Array.prototype.in_array=function(x){
  for(var i=0;i<this.length;i++) if(this[i]==x) return true;
  return false;
};


jQuery(function(){
  // fillForm
  // usage: $(form).kFillForm(datas)
  jQuery.fn.kFillForm=function(x){
    $("input,select,textarea",this).each(function(){
      var name=$(this).attr("name");
      if(name!=undefined && x[name]!=undefined){ 
        $(this).val(x[name]);
      }
    });
    return $(this);
  };

  jQuery.fn.kGetForm=function(){
    var x={};
    $("input,select,textarea",this).each(function(){
      var name=$(this).attr("name"); 
      if(name!=undefined && name.length && ($(this).attr("type")!="checkbox") || $(this).attr("checked")) x[name]=$(this).val();
    });
    return x;
  };
  
  jQuery.fn.formError=function(attr,action){
    var id=$(this).attr("id")+"_"+attr;
    if(action=="show"){
      if($(this).siblings("#"+id).size()==0) $(this).before("<div class='formError' id='"+id+"'>"+$(this).attr(attr)+"</div>");    
    }else if(action=="hide"){
      $(this).siblings("#"+id).remove();
    }
  };
  
  jQuery.fn.onEnter=function(cb){
    return $(this).keyup(function(e){
      var c=e.charCode || e.keyCode || e.which;
      if(c==13) cb(this);
    });
  };
  
  jQuery.fn.numeric=function(){
    return $(this).keypress(function(e){
      var c=e.charCode || e.keyCode || e.which;
      if(!((c>47 && c<58) || c==46 || c==8 || c==37 || c==39 || c==9)) return false;
    });
  };

  jQuery.fn.defaultHideText=function(defaultValue,options){
    if(!options) options={
      css:[{
        color:"#aaaaaa"
      },{
        color:"black"
      }]
      };
    this.css(options.css[0]).val(defaultValue).focus(function(){
      if($(this).val()==defaultValue) $(this).val("").css(options.css[1]);
    }).blur(function(){
      if($(this).val()=="" || $(this).val()==defaultValue) $(this).val(defaultValue).css(options.css[0]);
    });
  }

  jQuery.lpost=function(data,callback,type){
    if(jQuery.isFunction(data)){
      type=type||callback;
      callback=data;
      data={};
    }
    return jQuery.ajax({
      type:"POST",
      data:data,
      success:callback,
      dataType:type
    });
  };

  jQuery.k={
    afterHash:function(){
      var i=window.location.href.indexOf("#",0);
      return i==-1?null:window.location.href.substr(i+1);
    }
  };
});




