var httpReceivePage = getHTTPObject();
var httpSendData = getHTTPObject();
var httpGetEnquiries = getHTTPObject();

var objectArray = new Array();
var extendWidthArray = new Array();
var closedWidthArray = new Array();
var titleArray = new Array();
var accelerateArray = new Array();
var pageArray = new Array();
var activeTab = '';
var pageHash;
var pageFound = 0;

objectArray[0] = 'nav_home';
objectArray[1] = 'nav_featured';
objectArray[2] = 'nav_search';
objectArray[3] = 'nav_status';
objectArray[4] = 'nav_comments';

extendWidthArray[0] = '75';
extendWidthArray[1] = '95';
extendWidthArray[2] = '80';
extendWidthArray[3] = '80';
extendWidthArray[4] = '109';


pageArray[0] = 'home.php';
pageArray[1] = 'featured.php';
pageArray[2] = 'search.php';
pageArray[3] = 'status.php';
pageArray[4] = 'search.php';

titleArray[0] = 'Home';
titleArray[1] = 'Featured';
titleArray[2] = 'Search';
titleArray[3] = 'Status';
titleArray[4] = 'Comments';

accelerateArray[0] = '1';
accelerateArray[1] = '1';
accelerateArray[2] = '1';
accelerateArray[3] = '1';
accelerateArray[4] = '1';
accelerateArray[5] = '1';
accelerateArray[6] = '1';
accelerateArray[7] = '2';
accelerateArray[8] = '2';
accelerateArray[9] = '2';
accelerateArray[10] = '2';
accelerateArray[11] = '3';
accelerateArray[12] = '3';
accelerateArray[13] = '4';

//Get tab widths
function gatherWidths(){

	for(i=0; i<5; i++){
		closedWidthArray[i] = document.getElementById(objectArray[i]).offsetWidth;
	}
}
function openTab(tabIndex){

	//alert(document.getElementById(tabArray[tabIndex]).offsetWidth);
	activeTab = tabIndex;
	for(i=0; i<5; i++){
		if(i==tabIndex){
			window.location.hash = pageArray[i];
			document.getElementById(objectArray[i]).style.width = extendWidthArray[i]+'px';
			
				document.getElementById(objectArray[i]).style.borderLeft = '1px solid #999';
				document.getElementById(objectArray[i]).style.borderRight = '1px solid #999';
				document.getElementById(objectArray[i]).style.borderTop = '1px solid #999';
				document.getElementById(objectArray[i]).innerHTML = '<span>'+titleArray[i]+'</span>';
				document.getElementById(objectArray[i]).style.backgroundColor = '#FFF';
				document.getElementById(objectArray[i]).style.height = '34px';
			
			loadPage(i); //Set page content
		}else{
				document.getElementById(objectArray[i]).innerHTML = '&nbsp;';
				document.getElementById(objectArray[i]).style.borderLeft = '1px solid #0097CB';
				document.getElementById(objectArray[i]).style.borderRight = '1px solid #0097CB';
				document.getElementById(objectArray[i]).style.borderTop = '1px solid #0097CB';
				document.getElementById(objectArray[i]).style.backgroundColor = '#00E3F0';
				document.getElementById(objectArray[i]).style.height = '26px';
				closeTab(closedWidthArray[i], objectArray[i], 0);
			
		}
	}
}

function closeTab(toSize, elID, itterationValue){
	
	var currentWidth = document.getElementById(elID).offsetWidth;
	var remainingWidth = currentWidth - toSize;
	var targetWidth = new String();
	var pixelChange = '';

	if(toSize != currentWidth){
	
		if(itterationValue > 13){
			pixelChange = '5';
		}else{
			pixelChange = accelerateArray[itterationValue];
		}
		
						
		if(remainingWidth < pixelChange) { 
			pixelChange = remainingWidth; 
			targetWidth = (parseInt(currentWidth) - parseInt(pixelChange))-2;
			document.getElementById(elID).style.width = targetWidth + 'px';
		}else{			
			targetWidth = (parseInt(currentWidth) - parseInt(pixelChange))-2;
			//alert('rate of change: '+pixelChange+'\ncurrent width: '+currentWidth+'\nRemaining width: '+remainingWidth+'\nTarget Width: '+toSize+'\nAdjustment: '+targetWidth);
			if (targetWidth < currentWidth) {
				document.getElementById(elID).style.width = targetWidth + 'px';
			}
			
			itterationValue = parseInt(itterationValue) + 1;
			setTimeout('closeTab(\''+toSize+'\', \''+elID+'\', '+itterationValue+')',5);
		}
	}			
}




function initJS(){
	
	gatherWidths();
	pageHash = window.location.hash;
	
	if(pageHash == ''){
			openTab(0);
	}else{
		for(i=0; i<5; i++){
			if(pageHash.indexOf(pageArray[i]) != -1) { openTab(i);  }
		}
	}
	loadPage('');
	checkEnquiries();
	window.setInterval("changePage()",500);
}

function changePage(){
	if(pageHash != window.location.hash) {
		loadPage('');
	}
}


function loadPage(pageID){
	window.scrollTo(0, 1);
	pageHash = window.location.hash;
	var pageURL = pageHash.replace(/#/,"");
	var contentContainer = document.getElementById('content_container');

	contentContainer.innerHTML = 'Loading...';
	
	if (httpReceivePage.readyState == 4 || httpReceivePage.readyState == 0) {
		httpReceivePage.open("GET", pageURL, true);
		httpReceivePage.onreadystatechange = handlehHttpReceivePage; 
		httpReceivePage.send(null);
	}
}

function setPage(hashString){
	window.location.hash = hashString;
	loadPage('');
}

function handlehHttpReceivePage(){
	var contentContainer = document.getElementById('content_container');
	
	if (httpReceivePage.readyState == 4){
		
		contentContainer.innerHTML = httpReceivePage.responseText; //Set response
		try { buildChart(); }
		catch(err) { }
	}
}


function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

