// WhitefaceLakePlacidInfo.com/tripPlanner dhtml functions, see dhtml.css for affected DIV objects 
var tabbedLayerArray = new Array();
//add Strings of the div objects we want tabbed
tabbedLayerArray.push("'mapDisplay'");
tabbedLayerArray.push("'listDisplay'");
tabbedLayerArray.push("'tripPlannerDisplay'");


var currentTPView = '';

function initializeTabbedLayers()
{
	for(var i=0;i < tabbedLayerArray.length;i++)
	{
		//can't set map display to none, messes up map functionality 
		if(eval(tabbedLayerArray[i]) == 'mapDisplay')
		{
			document.getElementById(eval(tabbedLayerArray[i])).style.visibility = "hidden";
			document.getElementById(eval(tabbedLayerArray[i])).style.position = "absolute";
		}
		else
		{
			document.getElementById(eval(tabbedLayerArray[i])).style.display = "none";
			document.getElementById(eval(tabbedLayerArray[i])).style.visibility = "hidden";
			document.getElementById(eval(tabbedLayerArray[i])).style.position = "absolute";
		}
	}
}

function closeAllTabs()
{
	
	//turn off the welcome sign!
	if( document.getElementById('WelcomeSign') )
	{
		document.getElementById('WelcomeSign').style.display = "none";
		document.getElementById('WelcomeSign').style.visibility = "hidden";
		document.getElementById('WelcomeSign').style.position = "absolute";
	}

	for(var i=0;i < tabbedLayerArray.length;i++)
	{
		//can't set map display to none, messes up map functionality 
		if(eval(tabbedLayerArray[i]) == 'mapDisplay')
		{
			
			document.getElementById(eval(tabbedLayerArray[i])).style.visibility = "hidden";
			document.getElementById(eval(tabbedLayerArray[i])).style.position = "absolute";
			
		}
		else
		{
			document.getElementById(eval(tabbedLayerArray[i])).style.display = "none";
			//document.getElementById(eval(tabbedLayerArray[i])).style.visibility = "hidden";
			document.getElementById(eval(tabbedLayerArray[i])).style.position = "absolute";
		}
	}
	
}


function openTabById(idstring)
{
	closeAllTabs();
	//closeInfoWindows();
	//hideAllMarkers();

	
	/*if(idstring == 'mapDisplay')
	{
		
		document.getElementById(idstring).style.visibility = "visible";
		document.getElementById(idstring).style.position = "static";
		
	}
	else
	{}*/
	
	document.getElementById(idstring).style.visibility = "visible";
	document.getElementById(idstring).style.position = "static";
	document.getElementById(idstring).style.display = "block";
		
	
	
	currentTPView = idstring;
	

	
}

function viewingTP()
{
	if(document.getElementById("tp").style.visibility == "visible")
	{
		return true;
	}
	else return false;
}



function loadSubCats(maincat)
{
	//clear the select box until this call returns
	document.getElementById('subCatsSelect').innerHTML = "<span class='updating'>Updating Types....</span>";
	  var xmlHttp;
	  try
		{    // Firefox, Opera 8.0+, Safari    
				xmlHttp=new XMLHttpRequest();    }
	  	catch (e)
		{    // Internet Explorer    
			try
		  	{      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
			catch (e)
		  	{
				try
				{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		  		catch (e)
				{
					//no AJAX support, so just refresh the browser with the new main cat, which will auto load the subcats
					document.location = 'index.php?category='+maincat;
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
						
			if(xmlHttp.readyState==4)
			{
				document.getElementById('subCatsSelect').innerHTML=xmlHttp.responseText;
			}
		}
		
		var queryURL = 'includes/loadSubCategories.php?category='+maincat;
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);	
}



function loadCitiesByRegion(regionID)
{
	//reset the hidden field
	document.getElementById("citiesDiv").innerHTML="<span class='updating'>Updating Communities List....</span>";
	  var xmlHttp;
	  try
		{    // Firefox, Opera 8.0+, Safari    
				xmlHttp=new XMLHttpRequest();    }
	  	catch (e)
		{    // Internet Explorer    
			try
		  	{      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
			catch (e)
		  	{
				try
				{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		  		catch (e)
				{
					//no AJAX support, so just refresh the browser with the regionID
					document.location = 'index.php?region='+regionID;
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				document.getElementById("citiesDiv").innerHTML=xmlHttp.responseText;
			}
		}
		var queryURL = 'includes/loadCitiesByRegion.php?region='+regionID;
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);	
}


function updateRegionDropDown(id)
{
	
	for(var i=0; i < document.getElementById("region").options.length; i++)
	{
		if(document.getElementById("region").options[i].value == id)
		{
			document.getElementById("region").selectedIndex = i;
			break;
		}
	}
	
}


var defaultRegion = '';
function updateRegionTextDisplay(region)
{
	//grab the current value if entered
	if(defaultRegion == '' && document.getElementById("currentRegion").innerHTML != '')
	{
		defaultRegion = document.getElementById("currentRegion").innerHTML;
	}
	
	document.getElementById("currentRegion").innerHTML=region;
	
	//if this is a CLEAR call fromthe flash movie, reset to default
	if(region == '')
	{
		document.getElementById("currentRegion").innerHTML=defaultRegion;
	}
}


function swapImgSrc(imgObjectReference,newSource)
{
	imgObjectReference.src = newSource;
}


function toggleRegionView()
{
	if(currentTPView == 'mapDisplay')
	{
		document.getElementById("regionViewImg").src='images/bt-view-as-map.gif';
		openTabById('listDisplay');
	}
	else
	{
		document.getElementById("regionViewImg").src='images/bt-view-as-list.gif';
		openTabById('mapDisplay');
	}
}



// auto build event onclick event handlers for any object with a given class
// for this example, and DIV with the class "regionRollover" should have an onclick handler pointing to the clickedRegionDiv() method
function dom_define_handlers_by_class(targetTag,targetClassName,eventName,customMethod,args)
{
	
	var allElements = document.getElementsByTagName(targetTag);

	for(var i=0; i < allElements.length; i++)
	{
		if(allElements[i].className == targetClassName)
		{
			//we hit the target tag and class, so set the handler

			
			allElements[i].onclick = function() {clickedRegionDiv(this)};
		}
	}
}

function tripPlannerTabSwitch(tabID){
	tabs = new Array('tripPlannerSearchResults', 'tripPlannerMyTripPlanner', 'tripPlannerTips');
	for(i=0; tabs[i]; i++){
		if(tabID==tabs[i]){
			document.getElementById(tabs[i]+'Tab').className = 'tripPlannerTabOn';
			showElement(tabs[i]);
		}
		else{
			document.getElementById(tabs[i]+'Tab').className = 'tripPlannerTabOff';
			hideElement(tabs[i]);
		}
	}
	return true;
}


function tripPlannerDialogue(s)
{
	var ScrollTop = document.body.scrollTop;
	
	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
		{
			ScrollTop = window.pageYOffset;
		}
	
		else
		{
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	
	}

	//alert(ScrollTop);
	var topValue = ScrollTop + 200;
	
	var topValueString = new String(topValue+"px");
	
	
	
	document.getElementById('tpDialogue').style.visibility = 'visible';
	document.getElementById('tpDialogue').style.top = topValueString;
	//document.getElementById('tpDialogue').style.left = '239px';
	document.getElementById('tpDialogue').innerHTML='<div class="tptext">'+s+'</div><br clear="all" /><div align="center"><a href="javascript:void(0);" onclick="closetripPlannerDialogue();">- CLICK HERE TO CLOSE -</a></div>';
}
function closetripPlannerDialogue()
{
	document.getElementById('tpDialogue').style.visibility = 'hidden';
	document.getElementById('tpDialogue').innerHTML='';
}


function updateTripPlannerHTML()
{
	var xmlHttp;
	  try
		{    // Firefox, Opera 8.0+, Safari    
				xmlHttp=new XMLHttpRequest();    }
	  	catch (e)
		{    // Internet Explorer    
			try
		  	{      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
			catch (e)
		  	{
				try
				{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		  		catch (e)
				{
					//no AJAX support, add via URL request
					document.location = "index.php";
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				
				document.getElementById('tripPlannerDisplay').innerHTML = xmlHttp.responseText;
				
			}
		}
		var queryURL = 'includes/loadTripPlanner.php';
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);	


}

function addToTripPlanner(theID,theCAT,hideElementID,showElementID)
{
	tripPlannerDialogue('Adding item to your trip planner...');
	//document.location = "index.php?add=true&id="+theID+"&category="+theCAT;
	var xmlHttp;
	  try
		{    // Firefox, Opera 8.0+, Safari    
				xmlHttp=new XMLHttpRequest();    }
	  	catch (e)
		{    // Internet Explorer    
			try
		  	{      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
			catch (e)
		  	{
				try
				{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		  		catch (e)
				{
					//no AJAX support, add via URL request
					document.location = "index.php?add=true&id="+theID+"&category="+theCAT;
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				//alert(xmlHttp.responseText);
				tripPlannerDialogue(xmlHttp.responseText);
				
				updateTripPlannerHTML();
				
				if( (typeof hideElementID != "undefined") && (typeof showElementID != "undefined") )
				{
					//document.getElementById(hideElementID).style.visibility='hidden';
					document.getElementById(hideElementID).className='hiddenElement';
					document.getElementById(showElementID).className='visibleElement';
				}
			}
		}
		var queryURL = '/tripplanner/includes/addItemToTripPlanner.php?category='+theCAT+'&id='+theID;
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);	


}


function removeFromTripPlanner(theID,theCAT,hideElementID,showElementID)
{
	tripPlannerDialogue('Removing item to your trip planner...');
	var xmlHttp;
	  try
		{    // Firefox, Opera 8.0+, Safari    
				xmlHttp=new XMLHttpRequest();    }
	  	catch (e)
		{    // Internet Explorer    
			try
		  	{      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e)
		  	{
				try
				{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}

		  		catch (e)
				{
					//no AJAX support, add via URL request
					document.location = "index.php?remove=true&id="+theID+"&category="+theCAT;
				}
			}
		}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				//alert(xmlHttp.responseText);
				tripPlannerDialogue(xmlHttp.responseText);
				updateTripPlannerHTML();
				
				if( (typeof hideElementID != "undefined") && (typeof showElementID != "undefined") )
				{
					document.getElementById(hideElementID).className='hiddenElement';
					document.getElementById(showElementID).className='visibleElement';
				}
				//document.getElementById("subCats").innerHTML=xmlHttp.responseText;
			}
		}
		var queryURL = '/tripplanner/includes/removeItemFromTripPlanner.php?category='+theCAT+'&id='+theID;
		xmlHttp.open("GET",queryURL,true);
		xmlHttp.send(null);	


}

var existingMarkers = new Array();
var currentGGLMarkers = new Array();

var baseIcon = new GIcon();

baseIcon.iconSize = new GSize(43, 63);
baseIcon.iconAnchor = new GPoint(21, 63);


function createMarker(theID, theCAT, latitude, longitude, count, name, contactInfo,isTPItem,imagePath)
{
	var iconArray = new Array();
	iconArray["Lodging"] = "lodging";
	iconArray["Attractions"] = "attractions";
	iconArray["Hiking"] = "hiking";
	iconArray["Paddling"] = "paddles";
	iconArray["Camping"] = "camping";
	iconArray["Birding"] = "birding";
	iconArray["Events"] = "events";
	iconArray["Recreation"] = "recreation";
    iconArray["Shopping"] = "shopping";
    iconArray["Dining"] = "dining";
    
    
	var icon = new GIcon(baseIcon);
	icon.image = 'images/icons/'+iconArray[theCAT]+count+'.gif';
    icon.infoWindowAnchor = new GPoint(21,63);

	
	var point = new GLatLng(latitude,longitude);

	if(existingMarkers.indexOf(theID+''+theCAT)==-1)
	{ // does the marker already exist on the map
		
		var marker = new GMarker(point,icon);
        //var marker = new GMarker(point);
		map.addOverlay(marker);
        
		existingMarkers[existingMarkers.length] = theID+''+theCAT; // add to existing markers array
		currentGGLMarkers[currentGGLMarkers.length] = new GLatLng(latitude,longitude); // store map point object so we can recenter the map

        GEvent.addListener(marker, "click", function(){
						 openListingWindow(theID, theCAT, latitude, longitude,name,imagePath,contactInfo,isTPItem);});//end of event handler
	}
}


function openListingWindow(theID, theCAT, latitude, longitude,item_name,imagePath,contactHTML,isTPItem)
{
	var point = new GLatLng(latitude,longitude);
	//add the directions form
	
	  
	var paidListingPath = "/images/paid_listings/";//start from root
	
	//trim lat/lon
	latitude = new String(latitude).slice(0,7);
	longitude = new String(longitude).slice(0,8);//catch the "-"
	
	
	if(imagePath != '')
	{
		var htmlStr = '<span class="bubbleText"><img src="'+paidListingPath+imagePath+'" hspace="2" style="margin-bottom:2px;" align="left"><b>'+item_name+'</b><br />('+latitude+','+longitude+')<br />'+contactHTML+'<br clear="all" />';
	}
	else
	{
		var htmlStr = '<span class="bubbleText"><b>'+item_name+'</b><br />('+latitude+','+longitude+')<br />'+contactHTML;
	}
	  
	  
	  //add the directions form
	  htmlStr = htmlStr+'<p><form action="http://maps.google.com/maps" method="get" target="_blank">For a map &amp; driving directions to this site,<br /> enter your starting address here:<br /><input name="saddr" type="text" id="saddr" style="width:160px; height:15px;"> <input type="submit" value="GO" style="height:20px; width:25px; font-size:10px;"><input type="hidden" name="daddr" value="'+latitude+' '+longitude+'"><input type="hidden" name="hl" value="en"></form>';
	  
	  //add details, zoom to street
	  htmlStr = htmlStr+'<p><a href="javascript:zoomToStreet('+longitude+','+latitude+');" style="color:#333333;">zoom to street level</a>';
	  
	  //add a TP add/remove links
	  if(isTPItem)
	  {
		  //provide remove link
		  htmlStr = htmlStr+'<br /><a href="javascript:removeFromTripPlanner(\''+theID+'\',\''+theCAT+'\');" style="color:#333333;">remove from trip planner</a><br /><a href="javascript:map.closeInfoWindow();" style="color:#333333;">close window</a>';
	  }
	  else
	  {
		  htmlStr = htmlStr+'<br /><a href="javascript:addToTripPlanner(\''+theID+'\',\''+theCAT+'\');" style="color:#333333;">add to trip planner</a><br /><a href="javascript:map.closeInfoWindow();" style="color:#333333;">close window</a>';
	  }
	  
	   
	  
	  htmlStr = htmlStr+"</span>";
	
	
	map.openInfoWindowHtml(point, htmlStr);
	map.setCenter(point);
    
    
}

function centerMapOnPoint(latitude,longitude)
{
	var point = new GLatLng(latitude,longitude);
	map.setCenter(point);
}


function zoomToBestView()
{
	if(currentGGLMarkers.length){
		var minLat = 90;
		var maxLat = -90;
		var minLon = 180;
		var maxLon = -180;
		
		for(var k=0; k < currentGGLMarkers.length;k++)
		{
			if(currentGGLMarkers[k].lat() < minLat){minLat = currentGGLMarkers[k].lat();}
			if(currentGGLMarkers[k].lat() > maxLat){maxLat = currentGGLMarkers[k].lat();}
			if(currentGGLMarkers[k].lng() < minLon){minLon = currentGGLMarkers[k].lng();}
			if(currentGGLMarkers[k].lng() > maxLon){maxLon = currentGGLMarkers[k].lng();}
		}
		//alert(minLat + ' ' + minLon + ' ' + maxLat + ' ' + maxLon);
		var GLLBounds = new GLatLngBounds(new GLatLng(minLat,minLon),new GLatLng(maxLat,maxLon));
		
				
		map.setCenter(GLLBounds.getCenter(),map.getBoundsZoomLevel(GLLBounds));
	}
	
}


function zoomToStreet(longitude,latitude)
{
	var point = new GLatLng(latitude,longitude);
	map.setCenter(point,16);
}

//nextPage is used by the google map display pages to show the next block of listings
function nextPage(pageNum)
{
	var theLocation = new String(document.location)
	var varCheck = theLocation.indexOf("?");
	var placeholder = theLocation.indexOf("currentPage=");
	if(varCheck == -1)//there were no URL vars, so append currentpage and load new url
	{
		document.location = document.location+"?currentPage="+pageNum;
	}
	else if(placeholder != -1)//there was already a currentPage var , get rid of it and reload w/ new page num.
	{
		var tmpLocation = theLocation.slice(0,placeholder);
		document.location = tmpLocation+"currentPage="+pageNum;
	}
	else
	{
		document.location = document.location+"&currentPage="+pageNum;
	}
}

function submitPageNumFor(pageNum)
{
	document.selectPageForm.currentPage.value = pageNum;
	document.selectPageForm.submit();
}
