// GLOBAL = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 

// hide show tabs
function showDiv(sContentDivName) {
	document.getElementById(sContentDivName).style.display="block";
}

function hideDiv(sContentDivName) {
	document.getElementById(sContentDivName).style.display="none";
}


// change header color
function tabOff(tab){
	document.getElementById(tab).className = "rhc_tabOff";
}

function tabOn(tab) {
	document.getElementById(tab).className = "rhc_tabOn";
}

// set current section settings
function storeCurrent(current) {
	currentSection = current;
}

function checkRMG(rmg){
	currentRMG = rmg;
}

function checkTab(nChoice,hdrChoice){
	for(i=0; i<5; i++){
		if(currentSection != nChoice){
			if(i==nChoice){
				if(hdrChoice=='friendSideBars_hdr'){
					tabOff(friendSideBars_hdr[nChoice]);
				} else if (hdrChoice=='rmgSideBars_hdr'){
					tabOff(rmgSideBars_hdr[nChoice]);					
				} else if (hdrChoice=='rmgSideBarsHide_hdr'){
					tabOff(rmgSideBarsHide_hdr[nChoice]);					
				}
			}
		}
	}
}

// change div function
function changeDiv(nChoice) {

	for (i=1; i<3; i++) {
		if(nChoice==i) {
			showDiv(sSectionId[i-1]);
			document.getElementById(sLinkId[i-1]).src = sImgsON[i-1];

		} else {
			hideDiv(sSectionId[i-1]);
			document.getElementById(sLinkId[i-1]).src = sImgsOFF[i-1];
		}
	}
}


// format window
function formatWindow(msg,address,label) {
	var windowMsg="<div class='bubble'><div class='label'>" + label + "</div>";
	windowMsg += "<div class='address'>" + address + "</div><br />";
 	windowMsg += "<div class='msg'>" + msg + "</div>";
	windowMsg += "</div>";
	return windowMsg;
}



function closeAllSections(){
	for (i=0; i<aSideBars.length; i++) {
		hideDiv(aSideBars[i]);
		document.getElementById(aSideBars_hdr[i]).className = "rhc_tabOff";		
	}
}

function openSection(state){
	for (i=0; i<aSideBars.length; i++) {
		if (i==state) {
			showDiv(aSideBars[i]);
			document.getElementById(aSideBars_hdr[i]).className = "rhc_tabOn";
		} else {
			hideDiv(aSideBars[i]);
			document.getElementById(aSideBars_hdr[i]).className = "rhc_tabOff";
		}
	}
}

// open form category
function openFormCat(form) {
	//storeCurrent(form);
	for (i=0; i<rmgSideBars.length; i++) {
		if (i==form) {
			tabOn(rmgSideBars_hdr[i]);
		} else {
			tabOff(rmgSideBars_hdr[i]);
		}

		if (i>form) {
			hideDiv(rmgSideBars_hdr[i]);
			showDiv(rmgSideBarsHide_hdr[i-1]);
		} else {
			showDiv(rmgSideBars_hdr[i]);
			if (i>=1) {
				hideDiv(rmgSideBarsHide_hdr[i-1]);
			}
		}
	}
}


function setSideBars(){
	if (currentRMG=='y') {
		aSideBars = rmgSideBars;
		aSideBars_hdr = rmgSideBars_hdr;
	} else {
		aSideBars = friendSideBars;
		aSideBars_hdr = friendSideBars_hdr;
	}
}


// set current state and restart map only for index.php
function checkCurrent(state){
	setSideBars();
	if(state != currentSection){
		storeCurrent(state);
		showAll= false;
		startMap();
		openSection(state);
	} else {
		storeCurrent("");
		showAll= true;
		startMap();
		closeAllSections();
	}
	
}


// change tabs on index.php
function tabChange(tab, rmg){
	changeDiv(tab);
	checkRMG(rmg);
	setSideBars();
	state= currentSection;
	// check if a section is open
	if(!showAll){
		startMap();
		openSection(state);
	} 
	else if(showAll){
		startMap();
		closeAllSections();
	}

}


function writeNext(nEntries, nPages, sSection, nCurrPageItem, sCurrPageArray) {
	if (nPages > 0) {
		return "<a href=\"javascript:void[0];\" onclick=\"showNext('"+nEntries+"','"+nPages+"','"+sSection+"','"+nCurrPageItem+"','"+sCurrPageArray+"');\">next page &raquo;</a>";
	} else {
		return "";
	}
}


function writePrev(nEntries, nPages, sSection, nCurrPageItem, sCurrPageArray) {
	if (nPages > 0) {
		return "<a href=\"javascript:void[0];\" onclick=\"showPrev('"+nEntries+"','"+nPages+"','"+sSection+"','"+nCurrPageItem+"','"+sCurrPageArray+"');\">&laquo; previous page</a> ";
	} else {
		return "";
	}
}

function showNext(nEntries, nPages, sSection,nCurrPageItem,sCurrPageArray) {
	if (sCurrPageArray==1) {
		aPageArray = aCurrentPages;
	} else {
		aPageArray = aCurrentPages2;
	}
	if (aPageArray[nCurrPageItem]==nPages) {
		nNextPage = 0;
		aPageArray[nCurrPageItem]=0;
	} else {
		nNextPage = aPageArray[nCurrPageItem]+1;
		aPageArray[nCurrPageItem]++;
	}
	for (var z = 0; z <= nPages; z++) {
		sPageName = sSection + z;
		if (z==nNextPage) {
			showDiv(sPageName);
		} else {
			hideDiv(sPageName);		
		}
	}	

}

function showPrev(nEntries, nPages, sSection,nCurrPageItem,sCurrPageArray) {
	if (sCurrPageArray==1) {
		aPageArray = aCurrentPages;
	} else {
		aPageArray = aCurrentPages2;
	}
	if (aPageArray[nCurrPageItem]==0) {
		nNextPage = nPages;
		aPageArray[nCurrPageItem]=nPages;
	} else {
		nNextPage = aPageArray[nCurrPageItem]-1;
		aPageArray[nCurrPageItem]--;
	}
	for (var z = 0; z <= nPages; z++) {
		sPageName = sSection + z;
		if (z==nNextPage) {
			showDiv(sPageName);
		} else {
			hideDiv(sPageName);		
		}
	}	

}



// FORMS PAGE ONLY = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

// change category
function changeCategory(nValue) {
	document.getElementById('category').value = nValue;
}

// display the form
function showForm() {
	document.getElementById('side_bar_form').style.display = "block";
	hideDiv('pickCategory');
}


function addToForm(latlon, address) {
	var points = latlon.split(",");
	var lat = points[0].substring(1);
	var lon = points[1].substring(0, points[1].length - 1);
	document.newPoint.lat.value = lat;
	document.newPoint.lon.value = lon;
	document.newPoint.address.value = address;
}

function createMap() {
	if (GBrowserIsCompatible()) {
		var map = new GMap(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(40.742217, -74.004636), 12);
	} else {
		alert("Sorry, the Google Maps API is not compatible with this browser");
	}
}

function submitform() {
	document.newPoint.submit();
}

