var Fabtabs = function(element,options) {
	this.initialize(element,options);
}
$j.extend(Fabtabs.prototype, {
	initialize : function(element,options) {
		var parent = this;
		
		this.element = $j('#' + element);
		this.options = options || {
			hover:true,
			remotehover:false,
			anchorpolicy:'allow' // 'protect', 'allow', 'allow initial', 'disable'
		};
		this.menu = this.element.find('a');
		var hrefs = new Array();
		$j.each(this.menu, function(key,value) {
			if (value.toString().match(/#(\w.+)/)) {
				hrefs.push(RegExp.$1);
			}
		});
		
		this.hrefs = hrefs;
		this.on(this.getInitialTab());
		
		var onLocal = function(element,caller) {
		  if (caller.options.anchorpolicy !== 'allow') { 
			  return; 
	  	  }
  		  caller.activate(element,caller);
  		  if (caller.options.anchorpolicy === 'protect') { 
  			  window.location.hash = '.' + caller.tabID(element); 
  		  }
		};
		
  	    var onRemote = function(caller) {
  	    	if(caller.options.anchorpolicy !== 'allow'){ 
  	    		return; 
  	    	}
  	    	var trig = $j(this).find("a");
  	    	caller.activate(caller.tabID(trig));
  	    	if(caller.options.anchorpolicy === 'protect') { 
  	    		window.location.hash = '.' + caller.tabID(elm); 
  	    	}
  	    }
  	    
  	    if (this.options.hover) {
			$j.each(this.menu,function() {
				$j(this).bind('mouseover',function() {
					onLocal($j(this),parent);
				});
			});
		}
	},
	activate: function(elm,caller) {
	  $j.each(caller.menu, function() {
		  caller.off($j(this));
	  });
	  caller.on(elm);
	},
	off: function(elm) {
		elm.removeClass('active-tab');
		this.tabID(elm).removeClass('active-tab-body');
	},
	on: function(elm) {
		elm.addClass('active-tab');
		elmTab = this.tabID(elm);
		if (elmTab) {
			elmTab.addClass('active-tab-body');
		}
	},
	tabID: function(elm) {
		if (elm.attr('href')) {
			return $j('#' + elm.attr('href').match(this.re)[1]);
		}
		return false;
	},
	getInitialTab: function() {
		if(this.options.anchorpolicy !== 'disable' && document.location.href.match(this.re)) {
		  var hash = RegExp.$1;
		  if(hash.substring(0,1) == "."){
		    hash = hash.substring(1);
		  }
		  return this.element.find('a[href="#'+ hash +'"]');
		} else {
		  return this.menu.first();
		}
	},
	re: /#(\.?\w.+)/
});

$j(function() {
	new Fabtabs('tabs');
});












