/** mooTab v0.1 is a tab class for mootools 1.1
 *  DEV-DATE: 2008-09-08 JRM
 *	
 *	use:
 *	var mytabs = new tabs(tab_id, dest_id, {func: Ajax_func, height: '400px', width: '600px', select: default_tab, mooVer: '1.1', onCompleteFunc: 'load_my_function();'});
 **/

var tabs = new Class({
	initialize: function(element, dest, options) {
		var home = $(dest);
		
		// select the version of mootools you are using
		if (options.mooVer != '1.1' && options.mooVer != '1.2') {
			options.mooVer = '1.1';
		}
		
		// update size
		if (options.height) {
			home.style.height = options.height;
		}
		if (options.width) {
			home.style.width = options.width;
		}
		
		if ($('pageHeaderTitle'))
		{
			var nameTab = $('pageHeaderTitle').innerHTML;
			$('pageHeaderTitle').style.display = 'none';
			var oldTabs = $(element).innerHTML;
			
			$(element).innerHTML = '<li id="pageHeaderName"><acronym>'+nameTab+'<acronym></li>' + oldTabs;
			
		}
		
		var allTabs = $$('ul.'+element+' li');
		
		if (allTabs[0].id == 'pageHeaderName') {
			allTabs[0].className += ' left currentLocation';
		}
		else {
			allTabs[0].className += ' left';
		}
		
		// set the first element to the 'left' class and the last element to the 'right' class
		allTabs[allTabs.length-1].className += ' right';
		
		// update the default selected
		if (!options.select) {
			options.select = allTabs[0].title;
		}
		
		allTabs.each(function(el) {
			if(el.title == options.select) {
				// if the option is set for default content
				el.className += ' active';
				if (options.func) {
					home.innerHTML = '<span class="loadingText">Loading...</span>';
					if (options.mooVer == '1.1')
					{
						var a = new Ajax(location.href, {
							method: 'post',
							onComplete: function(ret) {
								home.innerHTML = ret;
								if (options.onCompleteFunc)
								{
									eval(options.onCompleteFunc);
								}
							}
						}).request("func="+options.func+"&tab="+el.title);
					}
					else if (options.mooVer == '1.2')
					{
						var a = new Request({
							method: 'post',
							url: location.href,
							onComplete: function(ret) {
								home.innerHTML = unescape(ret);
								if (options.onCompleteFunc)
								{
									eval(options.onCompleteFunc);
								}
							}
						}).send("func="+options.func+"&tab="+el.title);
					}
				}
			}
			if (el.className != 'left currentLocation')
			{
				el.addEvent('click', function() {
					$(document.body).getElement('.active').className = '';
					allTabs[0].className += ' left';
					allTabs[allTabs.length-1].className += ' right';
					
					this.className += ' active';
					
					if (options.func) {
						home.innerHTML = '<span class="loadingText">Loading...</span>';
						
						if (options.mooVer == '1.1')
						{
							var a = new Ajax(location.href, {
								method: 'post',
								onComplete: function() {
									home.innerHTML = a.response.text;
									if (options.onCompleteFunc)
									{
										eval(options.onCompleteFunc);
									}
								}
							}).request("func="+options.func+"&tab="+this.title);
						}
						else if (options.mooVer == '1.2')
						{
							var a = new Request({
								method: 'post',
								url: location.href,
								onComplete: function(ret) {
									home.innerHTML = unescape(ret);
									
									if (options.onCompleteFunc)
									{
										eval(options.onCompleteFunc);
									}
								}
							}).send("func="+options.func+"&tab="+this.title);
						}
					}
				});
			}
		});
		
		
	}
});