/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
/*
* Modifications by Mike DuChene
*/

var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.showTabCallback(function(t) { return false; });
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));

    var e;
    e = document.getElementsByClassName('tab_next');
    for (var i=0; i<e.length; i++) {
      Event.observe(e[i],'click',this.nextTab.bindAsEventListener(this),false);
    }
    e = document.getElementsByClassName('tab_prev');
    for (var i=0; i<e.length; i++) {
      Event.observe(e[i],'click',this.prevTab.bindAsEventListener(this),false);
    }
    e = document.getElementsByClassName('tab_submit');
    for (var i=0; i<e.length; i++) {
      Event.observe(e[i],'click',this.submitTab.bindAsEventListener(this),false);
    }
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
	  if (!this.cb_showtab($(this.tabID(elm)))) {
	    if (this.validPrev($(this.tabID(elm)))) {
    		this.show(elm);
    		this.menu.without(elm).each(this.hide.bind(this));
    		this.formFocus();
    	}
  	}
//    window.location.hash = this.tabID(elm);
	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
 		$(elm).addClassName('active-tab');
 		$(this.tabID(elm)).addClassName('active-tab-body');
 		this.curtab = $(this.tabID(elm));
	},
	formFocus : function () {
    var f = $(this.curtab).descendants().collect(function(s) { if (s.tagName == 'INPUT' || s.tagName == 'SELECT' || s.tagName == 'TEXTAREA') return s; }).compact();
    if (f.length) f[0].focus();
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	},
	showTabCallback : function(f) {
    if (typeof this.cb_showtab != 'function') {
      this.cb_showtab = f;
    } else {
      var old = this.cb_showtab;
      this.cb_showtab = function(t) {
        var ret = false;
        if (old) {
          ret |= old(t);
        }
        ret |= f(t);
        return ret;
      }
    }
	},
	isPrev : function(e) {
	  var cur = $(this.curtab);
	  var p = cur.previousSiblings();
	  return p.find(function(n) { return e == n; });
	},
	validTab : function(t) {
    var ins = t.descendants().collect(function(s) { if (s.tagName == 'INPUT' || s.tagName == 'SELECT' || s.tagName == 'TEXTAREA') return s; }).compact();
    return ins.all(function(v) { return Validation.validate(v); });
	},
	validPrev : function(t) {
	  var p = t.previousSiblings();
	  var me = this;
	  return p.all(function(v) { return me.validTab(v); });
	},
	showTab : function(tabName) {
	  try {
  	var elm = this.menu.find( function(value) { return value.href.match(/#(\w.+)/)[1] == tabName; });
  	var tab = $(this.tabID(elm));
	  if (!this.cb_showtab(tab)) {
      this.show(elm);
      this.menu.without(elm).each(this.hide.bind(this));
      this.formFocus();
    }
    } catch(err) {
    }
  },
  getNextTab : function(c) {
    var e = c.next();
    if (!e) {
      e = c.siblings()[0];
    }
    return e;
  },
  nextTab : function(ev) {
    var cur = $(this.curtab);
    if (this.validTab(cur)) {
      var elm = this.getNextTab(cur);
      this.showTab(elm.id);
    } else {
//      alert('Please correct the errors on the form before continuing.');
    }
    Event.stop(ev);
  },
  prevTab : function(ev) {
    var cur = $(this.curtab);
    var elm = cur.previous();
    if (!elm) {
      var sibs = cur.siblings();
      elm = sibs[sibs.length-1];
    }
    this.showTab(elm.id);
    Event.stop(ev);
  },
  submitTab : function(ev) {
    var cur = $(this.curtab);
    var n = this.getNextTab(cur);
    var f = cur.ancestors().collect(function(s) { if (s.tagName == 'FORM') return s; }).compact()[0];
    new Ajax.Updater(n.id,
      'form-feedback.php?ieid=' + Math.random(), {
      parameters : f.serialize()
      ,evalScripts:true
      ,requestHeaders: {   
        "Pragma" : "no-cache"
        ,"Cache-Control" : "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
        ,"Expires" : 0
        ,"Last-Modified" : new Date(0)
        ,"If-Modified-Since" : new Date(0)
      }
    });
    this.nextTab(ev);
  }
}

//Event.observe(window,'load',function(){ var mytabs = new Fabtabs('tabs'); },false);
