var gSpotType_None = 0;
var gSpotType_FishinSpot = 1;
var gSpotType_BoatLanding = 2;
var gSpotType_Lodging = 3;
var gSpotType_RestaurantBar = 4;
var gSpotType_LakeLink = 99;


function InitializeOnLoad(MouseEvents)
{
	
	RefreshSpots(MouseEvents);

	buildMenu(MouseEvents);

	DetermineVisibility();

	SetAllVisibility();

}


function SendUpdates() {

	var FormHTML="";
	var oFormDiv;
	var UpdateCounter = 0;
	
	for(i=0; i<aSpotInfo.length;i++) 
	{	
		if (aSpotInfo[i]['Status'] != "U")
		{
			UpdateCounter = UpdateCounter + 1;
	
			//** Spot ID Field **
			FormHTML = FormHTML + CreateElement(i,"SpotID",UpdateCounter);
	
			//** Status Field **
			FormHTML = FormHTML + CreateElement(i,"Status",UpdateCounter);
	
			//** SpotName Field **
			FormHTML = FormHTML + CreateElement(i,"Title",UpdateCounter);
	
			//** Spot Description Field **
			FormHTML = FormHTML + CreateElement(i,"Description",UpdateCounter);
	
			//** Latitude Field **
			FormHTML = FormHTML + CreateElement(i,"Lat",UpdateCounter);
	        
			//** Longitude Field **
			FormHTML = FormHTML + CreateElement(i,"Lon",UpdateCounter);
			
			//** Top Pos Field **
			FormHTML = FormHTML + CreateElement(i,"Top",UpdateCounter);
	        
			//** Left Field **
			FormHTML = FormHTML + CreateElement(i,"Left",UpdateCounter);
	        
			//** Spot Type Field **
			FormHTML = FormHTML + CreateElement(i,"Type",UpdateCounter);
	
			//** Species Field **
			FormHTML = FormHTML + CreateElement(i,"Species",UpdateCounter);
	
			//** TimeofYear Field **
			FormHTML = FormHTML + CreateElement(i,"TimeOfYear",UpdateCounter);
	
			//** FishingStyle Field **
			FormHTML = FormHTML + CreateElement(i,"FishingStyle",UpdateCounter);
	
			//** Structure Type Field **
			FormHTML = FormHTML + CreateElement(i,"StructureType",UpdateCounter);

			//** Other Field **
			FormHTML = FormHTML + CreateElement(i,"Other",UpdateCounter);

			//** IsCampground **
			FormHTML = FormHTML + CreateElement(i,"IsCampground",UpdateCounter);

			//** IsResort **
			FormHTML = FormHTML + CreateElement(i,"IsResort",UpdateCounter);

			//** IsHotel **
			FormHTML = FormHTML + CreateElement(i,"IsHotel",UpdateCounter);

			//** IsFamilyStyleLodging **
			FormHTML = FormHTML + CreateElement(i,"IsFamilyStyleLodging",UpdateCounter);

			//** IsResort **
			FormHTML = FormHTML + CreateElement(i,"IsResort",UpdateCounter);

			//** Picture1 **
			FormHTML = FormHTML + CreateElement(i,"Picture1",UpdateCounter);

			//** Picture2 **
			FormHTML = FormHTML + CreateElement(i,"Picture2",UpdateCounter);

			//** Picture3 **
			FormHTML = FormHTML + CreateElement(i,"Picture3",UpdateCounter);

			//** HasDocks **
			FormHTML = FormHTML + CreateElement(i,"HasDocks",UpdateCounter);

			//** HasBaitTackle **
			FormHTML = FormHTML + CreateElement(i,"HasBaitTackle",UpdateCounter);

			//** BoatLandingType **
			FormHTML = FormHTML + CreateElement(i,"BoatLandingType",UpdateCounter);

		}
	}

	if (UpdateCounter > 0) 
	{		
		//** Add the Counter to the end **
		FormHTML = FormHTML + '<input type=hidden name=Countr Value=" ' + UpdateCounter + '">';

		FormHTML = FormHTML + '<input type="hidden" name="DoUpdate" value="True">';
		
		oFormDiv = document.getElementById("FormDiv");
		oFormDiv.innerHTML = FormHTML
	
		// alert(FormHTML);
		
//		document.LakeSpotForm.submit();
	}
}

function CreateElement(indx,propertyname,CounterNumber){

	var sElementText;

	sElementText = '<input type=Hidden name='
		+ propertyname + '_' + CounterNumber
		+ ' Value=" '
		+ aSpotInfo[indx][propertyname]
		+ '">';

	return(sElementText);

}

function AddSpot(Left, Top) {

	var iNewIndex = aSpotInfo.length;  //Array is zero-based
	var selectType = document.SpotType.SpotTypeSelect;
	var lSpotTypeIndex = selectType.selectedIndex;
	var lSpotType = parseInt(selectType.options[lSpotTypeIndex].value);
	 
	if (lSpotTypeIndex <= 0)
	{
		alert('You must select a spot type.');
		return(false);
	}

	
	// Initialize new spot array element
	aSpotInfo[iNewIndex] = new Array();
	aSpotInfo[iNewIndex]['Status'] = 'I' 
	aSpotInfo[iNewIndex]['Type'] = lSpotType
	aSpotInfo[iNewIndex]['Image'] = 'NewSpotDot.gif'

	// Common for all spots
 	aSpotInfo[iNewIndex]['Top'] = Top;
	aSpotInfo[iNewIndex]['Left'] = Left;
	aSpotInfo[iNewIndex]['Lat'] = ''
	aSpotInfo[iNewIndex]['Lon'] = ''
	aSpotInfo[iNewIndex]['Title'] = "New Spot #" + iNewIndex 
	aSpotInfo[iNewIndex]['Description'] = "Undefined"

	switch(lSpotType)
	{
		case(gSpotType_FishinSpot):
			aSpotInfo[iNewIndex]['Species'] = ""
			aSpotInfo[iNewIndex]['Visible'] = "Y"
			aSpotInfo[iNewIndex]['TimeOfYear'] = ''
			aSpotInfo[iNewIndex]['FishingStyle'] = ''
			aSpotInfo[iNewIndex]['StructureType'] = ''
			aSpotInfo[iNewIndex]['SpeciesText'] = ''
			aSpotInfo[iNewIndex]['Other'] = ''
			break;
		
		case(gSpotType_BoatLanding):
			aSpotInfo[iNewIndex]['BoatLandingType'] = 1;
			break;
		
		case(gSpotType_Lodging):
			aSpotInfo[iNewIndex]['IsCampground'] = false;
			aSpotInfo[iNewIndex]['IsResort'] = false;
			aSpotInfo[iNewIndex]['HasElectricalHookups'] = false;
			aSpotInfo[iNewIndex]['IsHotel'] = false;
			aSpotInfo[iNewIndex]['IsFamilyStyleLodging'] = false;
			break;
		
		case(gSpotType_RestaurantBar):
			aSpotInfo[iNewIndex]['HasDocks'] = false;
			aSpotInfo[iNewIndex]['HasBaitTackle'] = false;
			break;
		
		case(gSpotType_LakeLink):
			var lls = document.SpotType.LakeLinkSelect;
			var lLakeID = lls.options[lls.selectedIndex].value;
			aSpotInfo[iNewIndex]['Link'] = 'LakeDetail.asp?L=' + lLakeID;
			break;
	}
		
	//AddArraySpot(iNewIndex, true, true);

	// Adjust for map location
 	// aSpotInfo[iNewIndex]['Top'] = Math.round((parseFloat(mouseY) - gTopOfMap) / zoomfactor );

	frmSpotDetail(iNewIndex, 1);
	
}

function ProcessClick(indx, pbIsNew)
{

	if (aSpotInfo[indx]['Link'] > "") 
	{  
		document.location = aSpotInfo[indx]['Link'];
	}	
	else
	{
		frmSpotDetail(indx, pbIsNew);
	}
}


function RefreshSpots(MouseEvents) {

	for(i=0; i<aSpotInfo.length;i++)
		AddArraySpot(i, MouseEvents);
            
}


function AddArraySpot(indx, MouseEvents, IsNew) {

 	var divStr
	var iconSize = 10 //default
	
	if (aSpotInfo[indx]['Visible'] == 'Y')
	{

		if (aSpotInfo[indx]['Type'] == gnSPOT_TYPE_LAKELINK)
			iconSize = 40;
		
		divStr = "<IMG border=0 hspace=0 "
				+ " width=" + iconSize
				+ " id=" + " Image" + indx 
				+ " name=" + Image + indx 
				+ " src=Images/" + aSpotInfo[indx]['Image'] 
	
		if (MouseEvents) 		
		{
			divStr += ' onmouseover=popUp(' + indx + ') onmouseout=popOut() '
				+ ' onclick=ProcessClick('+ indx + ')'
		}
	
		divStr += '>';

		document.body.insertAdjacentHTML("BeforeEnd",divStr);
		
		oHotSpot = document.getElementById("Image" + indx);
	
		oHotSpot.style.position = 'absolute';

		//oHotSpot.style.top = Math.round(parseFloat(aSpotInfo[indx]['Top']) * zoomfactor );
	  //oHotSpot.style.top = Math.round(parseFloat(aSpotInfo[indx]['Top']) * gScaleMapY);

		if (IsNew) 
		{
			oHotSpot.style.top = Math.round((aSpotInfo[indx]['Top'] - gTopOfMap - (iconSize/2)) * zoomfactor);
		}
		else
		{
			oHotSpot.style.top = Math.round((aSpotInfo[indx]['Top']) * zoomfactor) + gTopOfMap - (iconSize/2);
		}

		oHotSpot.style.left = Math.round((aSpotInfo[indx]['Left'] - (iconSize/2)) * zoomfactor);

		
		oHotSpot.style.zIndex = 19;

		if (aSpotInfo[indx]['Type'] == gnSPOT_TYPE_LAKELINK && !MouseEvents)
			aSpotInfo[indx]['Visible'] = 'N';
	
	}  
}


function SetSwitch(menuindex) { 	
	
	var oEl;
	
	
	oEl = document.getElementById("MI" + menuindex);
	if (sI[menuindex][6] == 'T')
	{
		sI[menuindex][6] = 'F'  //Switch to False
		oEl.src = 'Images/empty.gif';
	}
	else
	{
		sI[menuindex][6] = 'T'  //Switch to True
		oEl.src = 'Images/chk1.jpg';
	}

	DetermineVisibility();
	SetAllVisibility();
		
	updateMenuCookie();
}


function updateMenuCookie() {

	var sCurrentCategory='';
  var mItem;
	var sSelectedCodes;
	var i;  
	var trace='';
	var expires = new Date();

	//fixDate(expires);
	// cookie expires in one year (actually, 365 days)
	// 365 days in a year
	// 24 hours in a day
	// 60 minutes in an hour
	// 60 seconds in a minute
	// 1000 milliseconds in a second
	//expires.setTime(expires.getTime() + 365 * 24 * 60 * 60 * 1000);

	
	//Loop thru all menu items
	i = 0;
	while (i < sI.length)
	{

		sSelectedCodes = ' ';	

		if (sI[i][7])  //Not a header item
		{
			mItem = sI[i][7].split(':');

			sCurrentCategory = mItem[0];
					
			while (sCurrentCategory == mItem[0] && i < sI.length)
			{
				//Is menu item selected?
				if (sI[i][6] == 'T')
				{
					sSelectedCodes += mItem[1];
				}					
				i++;

				if (i < sI.length)
					if (sI[i][7])
						mItem = sI[i][7].split(':');

			}
			// Add cookie for the current category
			trace += 'Category: ' + sCurrentCategory + ', Codes: ' + sSelectedCodes + '\n';			
			setCookie(sCurrentCategory, sSelectedCodes);
			
		}
		else
			i++;
	}
	
	//if (trace != '')
		//alert(trace);
		
}


function DetermineVisibility() {

  var action;
	var CodeToFind;
  var bFoundMatch;
  var sPrevStringToSearch='';
	var bNoCategorySelections=false;
	var trace='';
	
	
	//Set all spots to invisible
	for (i=0; i<aSpotInfo.length; i++)
	{
		if (aSpotInfo[i]['Link'] == '')
			aSpotInfo[i]['Visible'] = 'N';
	}

	//Outer loop:  spots array
	for (j=0; j<aSpotInfo.length; j++)
	{
		if (aSpotInfo[j]['Link'] == '')
		{
			
			bFoundMatch = true;
					
			// Inner loop:  all menu settings	
			for (i=0; i<sI.length; i++)
			{
				if (sI[i][7])
				{
					action = sI[i][7].split(':');
	
					if (sPrevStringToSearch != action[0])
					{
	
						sPrevStringToSearch = action[0];
	
						if (! bNoCategorySelections)
							if (! bFoundMatch)
								break;
						
						bNoCategorySelections = true;				
						bFoundMatch = false;
					}
	
					//Is menu item selected?
					if (sI[i][6] == 'T')
					{
						bNoCategorySelections = false;
						if (! bFoundMatch)
						{					
							CodeToFind = action[1];
							
							// alert('Spot: ' + aSpotInfo[j]['Title'] + '\nCodeToFind: ' + CodeToFind + '\naSpotInfo[' + j + '][' + sPrevStringToSearch + ']: ' + aSpotInfo[j][sPrevStringToSearch]);
							
							if (matchMe(CodeToFind, aSpotInfo[j][sPrevStringToSearch]) || goVALIDATE.isWhitespace(aSpotInfo[j][sPrevStringToSearch]))
							{
								trace += 'matchMe(): Title = ' + aSpotInfo[j]['Title'] + ', bFoundMatch = true\n';
								
								bFoundMatch = true;
							}
						}
					}
				}
			}	
			if (bFoundMatch || bNoCategorySelections)
			{
				aSpotInfo[j]['Visible'] = 'Y';
			}
		}
	}

	//alert(trace);
	
	//SetAllVisibility();

}


function SetAllVisibility() {

	var oHotSpot;


	for(i=0; i<aSpotInfo.length; i++) 
	{	
		oHotSpot = document.getElementById("Image" + i);

		if (oHotSpot)
		{		
			if (aSpotInfo[i]['Visible'] == "Y")
			{
				oHotSpot.style.visibility="visible"
			}
			else
			{
				oHotSpot.style.visibility="hidden"
			}
		}
	}
}


function zoomit(factr, pCustomFactor) {

	var Image123 = document.getElementById('Image123');
	
	if (MapOrigWidth == 0)
	   MapOrigWidth = Image123.width;

	ZoomCount += factr;
	
	if (pCustomFactor > 0)
		zoomfactor = pCustomFactor
	else
		zoomfactor = 1 + (ZoomCount * .1)

	Image123.width = MapOrigWidth * zoomfactor ;

	for(i=0; i<aSpotInfo.length;i++) 
	{
		imageName = "Image" + i;

		oSpot = document.getElementById(imageName);

		if (oSpot)
		{

			oSpot.style.left = Math.round((aSpotInfo[i]['Left'] - (oSpot.height/2)) * zoomfactor)

			oSpot.style.top = Math.round((aSpotInfo[i]['Top'] - (oSpot.width/2)) * zoomfactor) + gTopOfMap;

			oSpot.height = Math.round(oSpot.height * (1 + (factr * .1)) )
			oSpot.width = Math.round(oSpot.width *  (1 + (factr * .1)) )
			
		}
	}

}

function setDetailsPosition() {

	var map = document.getElementById('Image123');
	var details = document.getElementById('SpotDetails');
	
	//details.className = (glPageWidth > 700) ? 'portrait' : 'landscape';
	details.style.top = (glPageWidth > 700) ? 768 : 1008;
	
	//details.style.top = parseInt(map.height) + 60;

}


function writeSpotDetails() {

	var details = document.getElementById('SpotDetails');
	var t='';
	var lSpotCt = 0;
	var aVisible = new Array();
	var lRowCt = new Number(0);
	var lColWidth = new Number(0);
	
	//setDetailsPosition();

	t += '<table border="0" cellpadding="0" cellspacing="0" width="' + glPageWidth + '">';
	t += '<tr>';	
	t += '<td height="10"><hr class="noprint"></td>';
	t += '</tr>';
	t += '<tr>';
	t += '<td height="40" valign="top" align="center" class="heading">Spot Details</td>';	
	t += '</tr>';
	t += '<tr>';
	t += '<td>';

	t += '<table border="0" cellpadding="0" cellspacing="0" width="' + glPageWidth + '">';

	for (i=0; i<aSpotInfo.length;i++) 
	{
		if (aSpotInfo[i]['Link'] == '')
		{
			if (aSpotInfo[i]['Visible'] == 'Y')
			{
				aVisible[lSpotCt] = Number(i);
				lSpotCt++;		
			}
		}
	}

	lColWidth = Math.round((glPageWidth-140) / 2);
	
	lRowCt = Math.round(lSpotCt/2);
	
	for (i=0; i<lRowCt; i++)
	{
		t += '<tr>';
		
		t += '<td valign="top" width="20" nowrap><img src="images/SpotDot_' + Number(aVisible[i] + 1) + '.gif" width="15"></td>';
		t += '<td valign="top" width="' + lColWidth + '" nowrap><b>' + aSpotInfo[aVisible[i]]['Title'] + '</b>';
		if (aSpotInfo[aVisible[i]]['Description'].length > 0 ) 
		    { 
			t += '<br>' + aSpotInfo[aVisible[i]]['Description'] ;
		    }
		

		if (gbPrintAbreviated == 0)
	    	   {
			t += '<br><br>LAT: ' + aSpotInfo[aVisible[i]]['Lat'];
			t += '<br>LONG: ' + aSpotInfo[aVisible[i]]['Lon'];
			t += '<br>SPECIES: ' + aSpotInfo[aVisible[i]]['SpeciesText'];
			t += '<br>STRUCTURE: ' + aSpotInfo[aVisible[i]]['StructureText'];
			t += '</td>';
		    }
		else
		    {
			t += '<br>LAT: ' + aSpotInfo[aVisible[i]]['Lat'] + ' N ';
			t += 'LONG: ' + aSpotInfo[aVisible[i]]['Lon'] + ' W ';
		    }


		if (i == 0)
			t += '<td rowspan="999"><img src="images/ts.gif" width="100" height="1"></td>';

		if (i+lRowCt < lSpotCt)
		{
			t += '<td valign="top" width="20" nowrap><img src="images/SpotDot_' + Number(aVisible[i+lRowCt] + 1) + '.gif" width="15"></td>';
			t += '<td valign="top" width="' + lColWidth + '" nowrap><b>' + aSpotInfo[aVisible[i+lRowCt]]['Title'] + '</b>';
			if (aSpotInfo[aVisible[i]]['Description'].length > 0 ) 
			    { 
				t += '<br>' + aSpotInfo[aVisible[i+lRowCt]]['Description'] ;
			    }
			if (gbPrintAbreviated == 0)
	    	   	    {
				t += '<br><br>LAT: ' + aSpotInfo[aVisible[i+lRowCt]]['Lat'];
				t += '<br>LONG: ' + aSpotInfo[aVisible[i+lRowCt]]['Lon'];
				t += '<br>SPECIES: ' + aSpotInfo[aVisible[i+lRowCt]]['SpeciesText'];
				t += '<br>STRUCTURE: ' + aSpotInfo[aVisible[i+lRowCt]]['StructureText'];
			    }
			else
			    {
				t += '<br>LAT: ' + aSpotInfo[aVisible[i+lRowCt]]['Lat'] + ' N ';
				t += 'LONG: ' + aSpotInfo[aVisible[i+lRowCt]]['Lon'] + ' W ';

			    }
		}
		t += '</tr>';

		if (i < (lSpotCt - 1))
			t += '<tr><td bgcolor="#ffffff"><img src="images/ts.gif" height="20" width="1"></td></tr>';

	}
	
	t += '</table>';

	t += '</td>';
	t += '</tr>';
	t += '</table>';

	document.write(t);
	
}

