// JavaScript Document

function init_ajax_tabs (url,default_tab_id) {
	
	var tabs = document.getElementsByClassName('tabs');
	for (var i = 0; i < tabs.length; i++) {
		//added by AMF
		$(tabs[i].id).onclick = function () {
			initTabRollover(this.id);
			getTabData(this.id,url);
		}
	}
	//For default tab functionality
	if(isThisFirstVisit() == true && default_tab_id != false) {
		initTabRollover(default_tab_id);
		getTabData(default_tab_id,url);
	}
}
//added by AMF
/*** NOTE: unfortunately, I had to hard-code style attributes (bg color) directly into these functions because the elements already have id and class tags that are required to stay the same in order for the ajax functionality to work ***/
function initTabRollover(selected_id) {
	
	var tabs = document.getElementsByClassName('tabs');
	for (var i = 0; i < tabs.length; i++) {
		if(tabs[i].id == selected_id) {
			
			tabs[i].style.backgroundColor = '#985534';
		}
		else {
			//switch back to its usual style
			tabs[i].style.backgroundColor = '#D2A972';
		}
	}
}
function getTabData(id,url) {
	
	var rand = Math.random(9999);
	var pars = 'id=' + id + '&rand=' + rand;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );
	
	//For default tab functionality
	createMarker();
}
function showLoad () {
	$('load').style.display = 'block';
}
function showResponse (originalRequest) {
	var newData = originalRequest.responseText;
	$('load').style.display = 'none';
	$('content').innerHTML = newData;
	
	
}

//For default tab functionality
function createMarker() {
   var newdiv = document.createElement('div');
   newdiv.setAttribute('id', 'marker_div');
   newdiv.style.visibility = "hidden";
   newdiv.style.position = "absolute";
   newdiv.width = "1px";
   newdiv.height = "1px";
   
   newdiv.style.left = "2px";
   newdiv.style.top = "0px";
   
   document.body.appendChild(newdiv);	
}
//For default tab functionality
function isThisFirstVisit() {
	
	var marker = document.getElementById("marker_div");
	
	if(marker) {
		return false;	
	}
	
	return true;
}