//Make sure to load MooTools first.
//Takes an element containing the tabs.
//Make sure to create public variables 'primaryA', 'currentPrimary'

function chrisTabs(el){

	//Creates an array of possible tabs.
	primaryA = $(el).getElementsByTagName('a');


	//If linking to a specific tab
	var linkedLocation = window.location.hash.substring(1);
	if(linkedLocation){
		currentPrimary = linkedLocation;
	} else {
		currentPrimary = 0;
	}

	//Gets active tab
	var initialChild = primaryA[currentPrimary];  //Active <a> tag
	var initialParent = $(initialChild).getParent(); //Active <li> tag

	//Set tab styles and content
	initialParent.set(parentStyleTag,parentStyleName);
	initialChild.set(childStyleTag,childStyleName);

	//Max AJAX Call
	retrieveData(initialChild.get(nameOfGetOption),0);

	var element;

	for(var i=0; i<primaryA.length; i++){ //Add click events to each tab
		var tabLink = $(primaryA[i]).get(nameOfGetOption);
		if(tabLink){
			element = $(primaryA[i]);
			//element.set('onclick','toggleTab(this)');
			element.addEvent('click',function(){ toggleTab(this);	});
			element.set('href','#'+i);
			element.set('tabNumber',i);
		}
	}
}

/* Works in IE. Not sure how. */
function toggleTab(clickedTab){
	var clickedPrimary = clickedTab.get('tabNumber');
	var clickedLink = clickedTab.get(nameOfGetOption);

	if(clickedPrimary!=currentPrimary){ //If clicking on a non-selected tab
		var currentA = primaryA[currentPrimary];
		var clickedA = primaryA[clickedPrimary];
		var currentParent = currentA.getParent();
		var clickedParent = clickedA.getParent();
		//Switch active tab
		clickedParent.set('id','active');
		currentParent.set('id','');
		clickedA.set('id','current');
		currentA.set('id','');
		currentPrimary = clickedPrimary;
	}

	//Make AJAX call	
	retrieveData(clickedLink,1);
}

//Determines what kind of data needs to be retrieved.
function retrieveData(requestURL,needTransition){

	if(dataRetrievalType=="html"){
		requestTabHTML(requestURL);
	} else if (dataRetrievalType=="php") {
		requestTabPHP(requestURL,needTransition);
	}

}

function requestTabHTML(clickedLink){

	/*
	var req = new Request.HTML({url:clickedLink, 
		onSuccess: function(html) {
			
			//Clear the text currently inside the results div.
			$(loadedDiv).set('text', '');
			//Inject the new DOM elements into the results div.
			$(loadedDiv).adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$(loadedDiv).set('text', 'The request failed.');
		}
	});
	req.send();
	*/
	needTransition = 0;
	var req = new Request(
		{	
			method: 'post',
			url:clickedLink,
			onRequest: function() {
				//Progress or loading alert?
			},
			onSuccess: function(result) {
				//Takes returned data and does something with it.  Descriptive, eh?
				if(result!=""){
					el = $(loadedDiv);
					if(needTransition==1){
						var fadeIn = new Fx.Tween(el,{
							duration: '400',
							onComplete: function(){
								//alert("Done fading in.");
							}
						});
						var fadeOut = new Fx.Tween(el,{
							duration: '400',
							onComplete: function(){
								//alert("Done fading out.");
								el.innerHTML = result;
								fadeIn.start('opacity','1');
							}
						});
						fadeOut.start('opacity', '0');
						//myElement.innerHTML = result;
					} else {
						el.innerHTML = result;
					}
				}
			}
		}
	).send();

}

function requestTabPHP(clickedSeries,needTransition){
	//Calls PHP file to grab tab information
	
	var myRequest = new Request(
		{	
			method: 'post',
			url:'php/updateTab.php',
			onRequest: function() {
				//Progress or loading alert?
			},
			onSuccess: function(result) {
				//Takes returned data and does something with it.  Descriptive, eh?
				if(result!=""){
					el = $(loadedDiv);
					if(needTransition==1){
						var fadeIn = new Fx.Tween(el,{
							duration: '400',
							onComplete: function(){
								//alert("Done fading in.");
							}
						});
						var fadeOut = new Fx.Tween(el,{
							duration: '400',
							onComplete: function(){
								//alert("Done fading out.");
								el.innerHTML = result;
								fadeIn.start('opacity','1');
							}
						});
						fadeOut.start('opacity', '0');
						//myElement.innerHTML = result;
					} else {
						el.innerHTML = result;
					}
				}
			}
		}
	).send('series='+clickedSeries);
}