/*
ns dc       http://purl.org/dc/elements/1.1/
dc:title    SemSol JavaScript library
dc:creator  benjamin nowack, bnowack at appmosphere.com
dc:date     2006-06-26
*/

if(!window["js_libs"]){window.js_libs={};}

window.js_libs["form"]={
	onload : function(){
   	form_mark_req_fields();
    form_add_focus_handlers();
    focus_focus_field();
   	focus_error_field();
    activate_textareas();
	} 
}

function form_mark_req_fields(){
  var forms, items, el;
  if(forms=elsByTag("form")){
    for(var i=0; i< forms.length; i++){
      if((items=elsByClass("req", forms[i], 'div')) && items.length){
        if(el=elByClass('form-info', forms[i])){
          el.innerHTML='* required field';
        }
        for(var j=0; j< items.length; j++){
          if((el=elByTag('label', items[j])) && !elByClass('req-info', el)){
            el.innerHTML+=' <span class="req-info">*</span>';
          }
        }
      }
    }
  }
}

function form_add_focus_handlers(){
  var els, i, j, el, el_id;
  var tags=['input', 'textarea'];
  for (i in tags) {
    if (els = elsByTag(tags[i])) {
      for (j=0; j<els.length; j++) {
        var el = els[j];
        if (el['id']) {
          form_add_focus_handler(el);
        }
      }
    }
  }
}

function form_add_focus_handler(el){
  el.onfocus = function(){return focus_field(el)};
  el.onblur = function(){return blur_field(el)};
}


function focus_field(el){
  el.className+=' active';
}

function blur_field(el){
  el.className=el.className.replace(/\s*active/, '');
}


function focus_focus_field(){
  var item, id, el;
  if((el = elByClass("focus", document)) && el['focus']) {
    try{el.focus()} catch(e){}
  }
}

function focus_error_field(){
  var item, id, el;
  if((item=elByClass("error", document, 'div')) && item['id'] && (el=elById(item.id.replace(/-item$/, ''))) && el['focus']){
    try{el.focus()} catch(e){}
  }
}

/* textarea 2.0 */

function activate_textareas() {
  var els;
  if (els = elsByTag('textarea')) {
    for (var i = 0; i < els.length; i++) {
      if (!hasClass(els[i].parentNode, 'resizable-textarea')) {
        new textArea(els[i]);
      }
    }
  }
}

function dimensions(el) {
  return { width: el.offsetWidth, height: el.offsetHeight };
}
function removeNode(node) {
  if (typeof node == 'string') {
    node = $(node);
  }
  if (node && node.parentNode) {
    return node.parentNode.removeChild(node);
  }
  else {
    return false;
  }
}
function absolutePosition(el) {
  var sLeft = 0, sTop = 0;
  var isDiv = /^div$/i.test(el.tagName);
  if (isDiv && el.scrollLeft) {
    sLeft = el.scrollLeft;
  }
  if (isDiv && el.scrollTop) {
    sTop = el.scrollTop;
  }
  var r = { x: el.offsetLeft - sLeft, y: el.offsetTop - sTop };
  if (el.offsetParent) {
    var tmp = absolutePosition(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
};
function stopEvent(event) {
  if (event.preventDefault) {
    event.preventDefault();
    event.stopPropagation();
  }
  else {
    event.returnValue = false;
    event.cancelBubble = true;
  }
}


function textArea(element) {
  var ta = this;
  this.element = element;
  this.parent = this.element.parentNode;
  this.dimensions = dimensions(element);

  // Prepare wrapper
  this.wrapper = document.createElement('div');
  this.wrapper.className = 'resizable-textarea';
  this.parent.insertBefore(this.wrapper, this.element);

  // Add grippie and measure it
  this.grippie = document.createElement('div');
  this.grippie.className = 'grippie';
  this.wrapper.appendChild(this.grippie);
  this.grippie.dimensions = dimensions(this.grippie);
  this.grippie.onmousedown = function (e) { ta.beginDrag(e); };

  // Set wrapper and textarea dimensions
  this.wrapper.style.height = this.dimensions.height + this.grippie.dimensions.height + 1 +'px';
  this.element.style.marginBottom = '0px';
  this.element.style.width = '100%';
  this.element.style.height = this.dimensions.height +'px';

  // Wrap textarea
  removeNode(this.element);
  this.wrapper.insertBefore(this.element, this.grippie);

  // Measure difference between desired and actual textarea dimensions to account for padding/borders
  this.widthOffset = dimensions(this.wrapper).width - this.dimensions.width;

  // Make the grippie line up in various browsers
  if (window.opera) {
    // Opera
    this.grippie.style.marginRight = '4px';
  }
  if (document.all && !window.opera) {
    // IE
    this.grippie.style.width = '100%';
    this.grippie.style.paddingLeft = '2px';
  }
  // Mozilla
  this.element.style.MozBoxSizing = 'border-box';

  this.heightOffset = absolutePosition(this.grippie).y - absolutePosition(this.element).y - this.dimensions.height;
}

textArea.prototype.beginDrag = function (event) {
  if (document.isDragging) {
    return;
  }
  document.isDragging = true;

  event = event || window.event;
  // Capture mouse
  var cp = this;
  this.oldMoveHandler = document.onmousemove;
  document.onmousemove = function(e) { cp.handleDrag(e); };
  this.oldUpHandler = document.onmouseup;
  document.onmouseup = function(e) { cp.endDrag(e); };

  // Store drag offset from grippie top
  var pos = absolutePosition(this.grippie);
  this.dragOffset = event.clientY - pos.y;

  // Make transparent
 // this.element.style.opacity = 0.5;

  // Process
  this.handleDrag(event);
}

textArea.prototype.handleDrag = function (event) {
  event = event || window.event;
  // Get coordinates relative to text area
  var pos = absolutePosition(this.element);
  var y = event.clientY - pos.y;

  // Set new height
  var height = Math.max(32, y - this.dragOffset - this.heightOffset);
  this.wrapper.style.height = height + this.grippie.dimensions.height + 1 + 'px';
  this.element.style.height = height + 'px';

  // Avoid text selection
  stopEvent(event);
}

textArea.prototype.endDrag = function (event) {
  // Uncapture mouse
  document.onmousemove = this.oldMoveHandler;
  document.onmouseup = this.oldUpHandler;

  // Restore opacity
  this.element.style.opacity = 1.0;
  document.isDragging = false;
}
