// Javascript UI classes for easier effects
// Requires prototyp 1.6.0 or higher

var Watermark = Class.create({
  initialize: function(target, value, opts) {
    this.element = $(target);
    this.element.observe('focus',this.onFocus.bind(this));
    this.element.observe('blur',this.onBlur.bind(this));
    this.textValue = value;
    this.edited = false;
    
    this.element.addClassName('watermark');
    
    if(this.element.value == '')
        this.element.value = this.textValue;
  },
  onFocus: function(evt){
    if(evt.element().value == this.textValue)
    {
        evt.element().value = '';
        this.edited = true;
    }
    
    this.element.removeClassName('watermark');
  },
  onBlur: function(evt){        
      if(this.element.value == '')
      {
        this.element.value = this.textValue;        
        this.edited = false;
      }
      else
      {
        this.edited = true;
      }
        
    if(this.element.value == this.textValue)
    {
        this.element.addClassName('watermark');        
    }
  },
  
  reset: function() {    
    this.element.value = this.textValue;    
    this.element.addClassName('watermark');
    this.edited = false;
  }  
  
});

// Take
// target - the element id or reference to the image
// imgOffSrc - the src that is visible when the mouse is not over the image.
// imgOnSrc - the src that is visible when the mouse is over the image.

var Rollover = Class.create({
  initialize: function(target, imgOffSrc, imgOnSrc) {
    this.element = $(target);
    this.imgOffSrc = imgOffSrc;
    this.imgOnSrc = imgOnSrc;
    
    this.element.observe('mouseover',this.onMouseOver.bind(this));
    this.element.observe('mouseout',this.onMouseOut.bind(this));
    
  },
  onMouseOut: function(evt){
    this.element.src = this.imgOffSrc;
  },
  onMouseOver: function(evt){
    this.element.src = this.imgOnSrc;
  }
});

var CaptureEnterAndSubmit = Class.create({
  initialize: function(target, form) {  
    this.element = $(target);
    this.form = form;
    if(!form)
        this.form = document.forms[0];
    
     Event.observe(this.element,'keydown', this.onEnter.bindAsEventListener(this));
    
  },
  onEnter: function(evt){

    if(evt.keyCode == Event.KEY_RETURN)
    {
        this.form.submit();
        evt.stop();                                                           
        return false;
    }
  }  
});

var CaptureEnterAndCallMethod = Class.create({
    initialize: function(target, method) {  
    this.element = $(target);
    this.method = method;
    
     Event.observe(this.element,'keydown', this.onEnter.bindAsEventListener(this));
    
  },
  onEnter: function(evt){

    if(evt.keyCode == Event.KEY_RETURN)
    {        
        this.method();
    }
  } 
});

var InlineEditor = Class.create({
    initialize: function(token, objectId, target, saveMethod, onSuccess, onFail)
    {
        this.onSuccess = onSuccess;
        this.onFail = onFail;
        this.value = target.innerHTML;
        this.target = target;
        this.target.style.cursor = 'pointer';
        this._create(target);
        this.input = new Element('input', {type: 'text'});
        this.input.hide();
        $($(target).parentNode).insertBefore(this.input,$(target).next());
        
        var This = this;
        this.input.observe('blur',function(evt){
        
            var onSuccess = function(result)
            {             
                This.value = This.input.value;   
                This.target.update(This.value);
                This.target.toggle();
                This.input.toggle();
            }
            
            var onFail = function(response)
            {
            }
            
            if(This.input.value != This.value)
                saveMethod(token, objectId, This.input.value, onSuccess,onFail)            
            else
            {
                This.target.toggle();
                This.input.toggle();
            }
        });
    },
    
    _create: function()
    {
        var This = this;
        this.target.observe('click', function(evt){
            This.target.toggle();
            This.input.toggle();
            This.input.focus();
            This.input.value = This.value;
        });
    }
});


/**** Prototype Extensions ****/

var methods = {
   getInnerText: function(element){     
        element = $(element);     

        if(element.innerText){
            return element.innerText.trim();
        } else{
            return element.textContent.trim();
        }
    }
}

//Array.addMethods();
Element.addMethods(methods);

Array.prototype.remove=function(item){
	for (i=0; i < this.length; i++){
		if (item == this[i]) this.splice(i, 1);
	}
}
