  /*
 * 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/
 */
var Fabtabs = Class.create();
var FabtabsObj;

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

		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++){
  		var anchor = anchors[i];

  		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "fabTabLink")){
  		  Event.observe(anchor,'click',this.activate.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 (elm.getAttribute("href") && elm.getAttribute("rel") == "fabTabLink")
		{
		  var tab = FabtabsObj.menu.find(function(value) {
	      return value.href.match(/#(\w.+)/)[1] == elm.href.match(/#(\w.+)/)[1]
	    })
	    FabtabsObj.show(tab)
	    FabtabsObj.menu.without(tab).each(function (tabElm) {FabtabsObj.hide(tabElm)})

	    var page = this.getPageSize();
	    var scroll = this.getWindowScroll();
	    window.scroll(scroll.width,page.pageHeight);
		}
		else
		{
		  this.show(elm);
  		this.menu.without(elm).each(this.hide.bind(this))
		}
	},

	hide : function(elm) {
		$($(elm).parentNode).removeClassName('current')
		$(this.tabID(elm)).removeClassName('tabCurrent')
	},

	show : function(elm) {
		$($(elm).parentNode).addClassName('current')
		$(this.tabID(elm)).addClassName('tabCurrent')
	},

	tabID : function(elm) {
		return 'tab_'+elm.href.match(/#(\w.+)/)[1];
	},

	getInitialTab : function(first) {
		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 if (first) {
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == first; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	},
	//
  // getPageSize()
  // Returns array with page width, height and window width, height
  // Core code from - quirksmode.org
  // Edit for Firefox by pHaez
  //
  getPageSize: function(){
  	var xScroll, yScroll;

  	if (window.innerHeight && window.scrollMaxY) {
  		xScroll = document.body.scrollWidth;
  		yScroll = window.innerHeight + window.scrollMaxY;
  	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
  		xScroll = document.body.scrollWidth;
  		yScroll = document.body.scrollHeight;
  	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
  		xScroll = document.body.offsetWidth;
  		yScroll = document.body.offsetHeight;
  	}

  	var windowWidth, windowHeight;

  	if (self.innerHeight) {	// all except Explorer
  		windowWidth = self.innerWidth;
  		windowHeight = self.innerHeight;
  	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
  		windowWidth = document.documentElement.clientWidth;
  		windowHeight = document.documentElement.clientHeight;
  	} else if (document.body) { // other Explorers
  		windowWidth = document.body.clientWidth;
  		windowHeight = document.body.clientHeight;
  	}
  	var pageHeight, pageWidth;

  	// for small pages with total height less then height of the viewport
  	if(yScroll < windowHeight){
  		pageHeight = windowHeight;
  	} else {
  		pageHeight = yScroll;
  	}

  	// for small pages with total width less then width of the viewport
  	if(xScroll < windowWidth){
  		pageWidth = windowWidth;
  	} else {
  		pageWidth = xScroll;
  	}

  	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  },
  // From script.aculo.us
  getWindowScroll: function() {
    var w = window;
      var T, L, W, H;
      with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
      }
      return { top: T, left: L, width: W, height: H };
  }
}

Event.observe(window,'load',function(){ new Fabtabs('tabs', defaultTab); },false);
