/*-- 
Description: Singleton utility object for common UI interaction behaviors
--*/

function JInteractionUtil(){
  this.hideStyle='hide';
};

//
JInteractionUtil.toggleElem = function(objref){
  if (typeof objref != 'object'){return false;}
  var pattern = new RegExp(this.hideStyle,'g');
  try{
    objref.className = (objref.className.indexOf(this.hideStyle) != -1) ? objref.className.replace(pattern,'') : objref.className+" "+this.hideStyle;
  }catch(e){
    return false;
  }
}

//  
JInteractionUtil.hideElem = function(objref){
  try{
    objref.className = (objref.className.indexOf(this.hideStyle) != -1) ? objref.className : objref.className+" "+this.hideStyle;
    return true;
  }catch(e){
    return false;
  }
}

//Remove a given class from an elements style  
//example turn style="firstclass secondclass" into style="secondclass" when firstclass is passed in as second argument
JInteractionUtil.removeClassName = function(_node,_class){
  var _pattern = new RegExp(_class,'g');
  try{
    _node.className = (_node.className.indexOf(_class) == -1) ? _node.className : _node.className.replace(_pattern,'');
    return true;
  }catch(e){
    return false;
  }
}

//Add a given class from an elements list of styles
JInteractionUtil.addClassName = function(_node,_class){
  //try{
    _node.className = (_node.className.indexOf(_class) == -1) ? _node.className+" "+_class:_node.className;
    //return true;
  //}catch(e){
    //return false;
  //}
}

//*------------------------------------------------------------*//
// Fade effect arrays and functions
//  todo:need to rewrite - avoid use of style.background somehow? prototype fade()
function ClassColorSequence(){
  this.yellow = ["transparent", "#ffffff","#ffffee","#ffffdd","#ffffcc","#ffffbb","#ffffaa","#ffff99"];
}

function ClassFadeIn() {
  ClassColorSequence.call(this);

  //compare two colors to see if the same
  this.compare_color = function(c1, c2) {
    if (c1.charAt(0) != c2.charAt(0)) {
      c1 = this.to_rgb(c1);
      c2 = this.to_rgb(c2);
    }
    
    return c1.toLowerCase() == c2.toLowerCase();
  }

  //convert hex to rgb (hext must be in the format of #xxxxxx)
  //returns string: rgb(x, y, z)
  this.to_rgb = function(c) {
    if (!c || c.charAt(0) != '#') {
      return c;
    }
    var r = parseInt(c.substring(1, 3), 16);
    var g = parseInt(c.substring(3, 5), 16);
    var b = parseInt(c.substring(5, 7), 16);
    
    return 'rgb(' + r + ', ' + g + ', ' + b + ')';
  }
  
  this.fade = function(where,who){
    var obj = document.getElementById(who);

    if (!obj) { return; }

    //edge cases of color changing?
    var color_changed = (this.yellow[where] && (this.to_rgb(this.yellow[where]) != this.to_rgb(obj.style.backgroundColor)));
    
    //background was changed, do not fade anymore
    if (color_changed) {
      return;
    }
    
    where--;
    obj.style.backgroundColor = this.yellow[where];
    
    if (where > 0) {
      setTimeout("this.fade("+where+",'"+who+"')", 200);
    }
  }
}

function waittofade(elementStr) {
   ClassFadeIn.call(this);
   this.fade(this.yellow.length,elementStr);
}

