
var locations = new Array();
var map;

function Location(name, cat, address, addressrendered, image, link, marker, html) {
		this.name = name;
		this.cat = cat;
		this.address = address;
		this.addressrendered = addressrendered;
		this.image = image;
		this.link = link;
		this.marker = marker;
		this.html = html;
		this.sidebarEntry;
		this.sidebarCatEntry;
		this.infoOpen = false;
		this.showInfo = function showInfo() {
			//this.marker.openInfoWindowHtml(html); 
            this.marker.openExtInfoWindow(
              map,
              "info_window",
              html,
              {beakOffset: 2}
            ); 
         
			this.sidebarEntry.className = "sidebar_entry_on";
			this.sidebarCatEntry.className = "sidebar_entry_on";
			this.infoOpen = true;};
		this.hideInfo = function hideInfo() {
			//this.marker.closeInfoWindow();
			
			
			//alert("1");
			
			this.sidebarEntry.className = "sidebar_entry_off";
			this.sidebarCatEntry.className = "sidebar_entry_off";
			this.infoOpen = false;};
	}
	
           


		var attractionsIcon = new GIcon(G_DEFAULT_ICON);
        attractionsIcon.image = "/images/ehv/map/marker_attractions.png";
        attractionsIcon.iconSize = new GSize(20, 35);
        attractionsIcon.shadow = "/images/ehv/map/marker_shadow.png";
        attractionsIcon.shadowSize = new GSize(32, 35);
        attractionsIcon.iconAnchor = new GPoint(10, 35);
   
        var diningIcon = new GIcon(G_DEFAULT_ICON);
        diningIcon.image = "/images/ehv/map/marker_dining.png";
        diningIcon.iconSize = new GSize(20, 35);
        diningIcon.shadow = "/images/ehv/map/marker_shadow.png";
        diningIcon.shadowSize = new GSize(32, 35);
        diningIcon.iconAnchor = new GPoint(10, 35);
        
        var natureIcon = new GIcon(G_DEFAULT_ICON);
        natureIcon.image = "/images/ehv/map/marker_nature.png";
        natureIcon.iconSize = new GSize(20, 35);
        natureIcon.shadow = "/images/ehv/map/marker_shadow.png";
        natureIcon.shadowSize = new GSize(32, 35);
        natureIcon.iconAnchor = new GPoint(10, 35);
        
        var shoppingIcon = new GIcon(G_DEFAULT_ICON);
        shoppingIcon.image = "/images/ehv/map/marker_shopping.png";
        shoppingIcon.iconSize = new GSize(20, 35);
        shoppingIcon.shadow = "/images/ehv/map/marker_shadow.png";
        shoppingIcon.shadowSize = new GSize(32, 35);
        shoppingIcon.iconAnchor = new GPoint(10, 35);


function createMarker(point, name, address, link, cat, i) {
		
      var marker;
      
      if (cat=="Attractions") {
      	marker = new GMarker(point, { icon:attractionsIcon });
      	} else if (cat=="Dining") {
      	marker = new GMarker(point, { icon:diningIcon });
      	} else if (cat=="Nature") {
      	marker = new GMarker(point, { icon:natureIcon });
      	} else if (cat=="Shopping") {
      	marker = new GMarker(point, { icon:shoppingIcon });
      	} else {
      	marker = new GMarker(point);
      	}
      
      GEvent.addListener(marker, 'click', function() {
        locations[i].showInfo();
      });
      GEvent.addListener(map, 'extinfowindowclose', function() {
        locations[i].hideInfo();
      });
      return marker;
    }

 function createSidebarEntry(marker, name, address, i) {
      var div = document.createElement('div');
      var html = '<b>' + name + '<\/b>';
      div.innerHTML = html;
      div.style.cursor = 'pointer';
      div.className = "sidebar_entry_off";
	  GEvent.addDomListener(div, 'click', function() {
       		 locations[i].showInfo();
       		 });
      GEvent.addDomListener(div, 'mouseover', function() {
      		if(div.className == "sidebar_entry_off")
      			{
       		 	div.className = "sidebar_entry_hover";
       		 	}
       		 });
      GEvent.addDomListener(div, 'mouseout', function() {
       		 if(div.className == "sidebar_entry_hover")
      			{
       		 	div.className = "sidebar_entry_off";
       		 	}
       		 });
      return div;
    }
    
    
    
    
    





function toggleMarkers(category) {
    	if ($('#' + category + '_sidehead').next().is(":visible")) { //if open, 
    		hideMarkers(category);
    	} else {
    		showMarkers(category);
    	}
    }
    
    function showMarkers(category) {
    	for (x in locations) {
    			if (locations[x].cat == category) {
    				locations[x].marker.show();
    			}
    		}
    }
    
    function hideMarkers(category) {
    	for (x in locations) {
    			if (locations[x].cat == category) {
    				locations[x].marker.hide();
    				locations[x].hideInfo();
    				hideBox("hello");
    			}
    	}
    }
    
    function hideBox(text) {
            //alert(map.getExtInfoWindow() != null);

      	map.closeInfoWindow();
  		if (map.getExtInfoWindow() != null) {
 		 //alert(text);
 		   map.closeExtInfoWindow();
 		 }
      }
      
      
      
      
      
      
   jQuery(document).ready(function(){
	$('#viewbycategory .head').click(function() {
		$(this).next().slideToggle(400,toggleMarkers(this.innerHTML));
		if($(this).is(".open")) {
			$(this).removeClass('open');
		} else {
			$(this).addClass('open');
		}
		return false;
	}).next().hide();
	
	$("#mapsidebar").tabs();
	
	$('#mapsidebar').bind('tabsshow', function(event, ui) {
		if (ui.index == 0) {
			for (x in locations) { //selected View All tab
				locations[x].marker.show();
			}
		} else if (ui.index == 1) { //selected Category tab
			var isOpenAtt = false;
			var isOpenDin = false;
			var isOpenNat = false;
			var isOpenSho = false;
			
			var isMarkAtt = false;
			var isMarkDin = false;
			var isMarkNat = false;
			var isMarkSho = false;
			
			if ($('#Attractions_sidehead').next().is(":visible")) { //if open, 
    			isOpenAtt = true;
    		}
    		if ($('#Dining_sidehead').next().is(":visible")) { //if open, 
    			isOpenDin = true;
    		}
    		if ($('#Nature_sidehead').next().is(":visible")) { //if open, 
    			isOpenNat = true;
    		}
    		if ($('#Shopping_sidehead').next().is(":visible")) { //if open, 
    			isOpenSho = true;
    		}
    	
			
			for (x in locations) {
				if (locations[x].infoOpen) {
					if (locations[x].cat == "Attractions") {
						isMarkAtt = true;
					} else if (locations[x].cat == "Dining") {
						isMarkDin = true;
					} else if (locations[x].cat == "Nature") {
						isMarkNat = true;
					} else if (locations[x].cat == "Shopping") {
						isMarkSho = true;
					}
				}
				locations[x].marker.hide();
			}
			
			if (isMarkAtt && !isOpenAtt) {
				$('#Attractions_sidehead').trigger('click');
			} else if (isOpenAtt) {
				showMarkers("Attractions");
			}
			
			if (isMarkDin && !isOpenDin) {
				$('#Dining_sidehead').trigger('click');
			} else if (isOpenDin) {
				showMarkers("Dining");
			}
			
			if (isMarkNat && !isOpenNat) {
				$('#Nature_sidehead').trigger('click');
			} else if (isOpenNat) {
				showMarkers("Nature");
			}
			
			if (isMarkSho && !isOpenSho) {
				$('#Shopping_sidehead').trigger('click');
			} else if (isOpenSho) {
				showMarkers("Shopping");
			}
			
			//toggleMarkers("Attractions");
		}
    // Objects available in the function context:
    ui.tab     // anchor element of the selected (clicked) tab
    ui.panel   // element, that contains the selected/clicked tab contents
    ui.index   // zero-based index of the selected (clicked) tab
	
	});



	$(window).load(function () {
 		 initialize();
	});

	$(window).unload(function () {
 		 GUnload();
	});
	
	$("#directionsmenu").change( function() {
		var startadd = $("#directionsinput").val();
		var endadd = $("#directionsmenu").val();
  		$("#directions-link-vassar").attr("href","http:\/\/maps.google.com\/maps?saddr=124+Raymond+Ave,+Poughkeepsie,+NY+12604&daddr=" + endadd);
  		
  		if ($("#directionsinput").val() != "") {
  		$("#directions-link-other").attr("href","http:\/\/maps.google.com\/maps?saddr=" + startadd + "&daddr=" + endadd);
		}
	});
	
	$("#directionsinput").keyup( function() {
		if ($("#directionsmenu").val() != "select") {
			var startadd = $("#directionsinput").val();
			var endadd = $("#directionsmenu").val();
  			$("#directions-link-other").attr("href","http:\/\/maps.google.com\/maps?saddr=" + startadd + "&daddr=" + endadd);
		}
	});
	
	$("form").submit( function() {
		window.location = $("#directions-link-other").attr("href");
	});
});
