/*
 * Tabs
 */
var Tabs = function() {

	// bind reference
	var that = this;

	var defaults = {
		'anchors': [],
		'tabs_classname': 'tabs',
		'form': $('form')
	};

	var options = arguments[0] || {};

	for(key in defaults) {
		this[key] = options[key] ? options[key] : defaults[key];
	}

	this.setup = function() {
		var hash = '';
		var form_action = this.form.attr('action');

		// hide all
		$('.' + this.tabs_classname).hide();

		// set selected
		this.anchors.attr('class', '');

		// show and mark selected
		this.anchors.each(function() {
			if($(this).attr('href') == window.location.hash) {
				hash =  window.location.hash;
				return;
			}
		});
		
		if(hash) {
			$(hash).show();
			$('a[href=' + hash + ']').attr('class', 'selected');

			// update form action
			this.form.attr('action', form_action + hash);
		} else {
			// show and mark first
			$('.' + this.tabs_classname).first().show();
			$(this.anchors).first().attr('class', 'selected');
		}

		// bind events
		this.anchors.bind('click', function() {
			var selected = $(this).attr('href');

			that.anchors.attr('class', '');
			$(this).attr('class', 'selected');

			// hide all
			$('.' + that.tabs_classname).hide();

			// show selected
			$('.' + that.tabs_classname + selected).fadeIn(); //.show();

			// update form action
			var forms = document.getElementsByTagName('form');
			for(var i = 0; i < forms.length; i++) {
				forms[i].setAttribute('action', form_action + selected);
			}

			return false;
		});
	};
};
