var ajax = new sack();
var currentTab = new String();

function whenLoading(divId){
	var e = document.getElementById(divId); 
	e.innerHTML = "<p>Sending Data...</p>";
}

function whenLoaded(divId){
	var e = document.getElementById(divId); 
	e.innerHTML = "<p>Data Sent...</p>";
}

function whenInteractive(divId){
	var e = document.getElementById(divId); 
	e.innerHTML = "<table border='0' width='100%' height='100%'><tr><td align='center' valign='middle'><img src='./images/tabbed_pages/Loading.gif'></td></tr></table>";
}

function whenCompleted(divId){
	
}

function loadContent(divId, sourceFile){
	ajax.requestFile = sourceFile;
	ajax.method = "GET";
	ajax.element = divId;
	ajax.onLoading = whenLoading(divId);
	ajax.onLoaded = whenLoaded(divId);
	ajax.onInteractive = whenInteractive(divId);
	ajax.onCompletion = whenCompleted(divId);
	ajax.runAJAX();
}

function loadTab(tabId, tabSource, size){
	lastTab = currentTab;
	currentTab = tabId;
	
	// support bigger tabs
	var tabSize = 50;
	if(size != undefined){
		tabSize = size;
	}
	
	// check the tab size
	var tabBackground = 'tab_background.jpg';
	if(tabSize > 50){
		tabBackground = 'tab_background_large.jpg'
	}
	
	if( lastTab.length > 0){
		var lastTabDiv = document.getElementById(lastTab);
		lastTabDiv.style.background = "url(images/tabbed_pages/null_background.jpg) no-repeat right top;";
	}

	var tabDiv = document.getElementById(currentTab); 
	tabDiv.style.background = "url(images/tabbed_pages/" + tabBackground + ") no-repeat right top;";
	
	loadContent("tab_content", tabSource);
}

function writeTabCode( tabId, tabTitle, tabIcon, tabSource, size ){
	var quote = '"';
	
	// set the default tab size
	var divWidth 		= 237;
	var divHeight 	= 60;
	var tableWidth 	= 225;
	var tabSize 		= 50;
	if(size != undefined){
		tabSize = size;
		tableWidth = 175 + tabSize;
		divWidth   = tableWidth + 2;
		divHeight	 = tabSize + 10;
	}
	
	var tabCode = "<div style='width:" + divWidth + "px; height:" + divHeight + "px; position:relative; z-index:0; cursor:pointer;' onClick='javascript:loadTab(" + quote + tabId + quote + ", " + quote + tabSource + quote + ", " + tabSize + ");'>";
	tabCode = tabCode + "<div id='" + tabId + "' style='background:url(images/tabbed_pages/null_background.gif) no-repeat right top; width:100%; height:100%; position:relative; left:1px; z-index:2;'>";
	tabCode = tabCode + "<table border='0' cellspacing='0' cellpadding='6' width='" + tableWidth + "'><tr>";
	tabCode = tabCode + "<td style='padding-left:10px; text-align:right;'>" + tabTitle + "</td>";
	tabCode = tabCode + "<td width='" + tabSize + "'><img src='" + tabIcon + "' width='" + tabSize + "' height='" + tabSize + "'></td>";
	tabCode = tabCode + "</tr></table></div></div>";
	
	document.write(tabCode);
}