// JavaScript Document

    //<![CDATA[

    if (GBrowserIsCompatible()) {
		
      var baseIcon = new GIcon();
          baseIcon.iconSize=new GSize(32,32);
          baseIcon.shadowSize=new GSize(56,32);
          baseIcon.iconAnchor=new GPoint(16,32);
          baseIcon.infoWindowAnchor=new GPoint(16,0);
		  
      var gmarkers = [];
      var gicons = [];		  
          
      gicons["sprintstudio"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
      gicons["live"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
      gicons["pgaanalyst"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
      gicons["pgatower"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
      gicons["weekoff"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
	  gicons["champions"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
	
	      // ==================================================
      // A function to create a tabbed marker and set up the event window
      function createTabbedMarker(point,week,date,name,html,category,showdetails,label1,label2) {
        //var marker = new GMarker(point);
		var marker = new GMarker(point,gicons[category]);
        // === Store the category and name info as a marker properties ===
        marker.mycategory = category;                                 
        marker.myname = name;
		marker.myweek = week;
		marker.mydate = date;
		marker.myshowdetails = showdetails;
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html), new GInfoWindowTab(label2,showdetails)]);
        });
        return marker;
      }
      // ==================================================
	
      // A function to create the marker and set up the event window
      function createMarker(point,week,date,name,html,category) {
        var marker = new GMarker(point,gicons[category]);
        // === Store the category and name info as a marker properties ===
        marker.mycategory = category;                                 
        marker.myname = name;
		marker.myweek = week;
		marker.mydate = date;
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers.push(marker);
        return marker;
      }

      // == shows all markers of a particular category, and ensures the checkbox is checked ==
      function show(category) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category) {
            gmarkers[i].show();
          }
        }
        // == check the checkbox ==
        document.getElementById(category+"box").checked = true;
      }

      function myclick(i) {
        GEvent.trigger(gmarkers[i],"click");
      }


	  // === discard all except the first two results ===
      function doSearchCompleteCallback(search) {
        search.results.splice(2);       
      }
	  
	  	// == gets the week number, starting monday ==
	  
Date.prototype.getWeek = function (dowOffset) {

dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 1;
var newYear = new Date(this.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset;
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if(day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(this.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
}
else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
	  
	  var map = new GMap2(document.getElementById("map"),{googleBarOptions:
        {onSearchCompleteCallback:doSearchCompleteCallback}
    	})
      
	  map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      //map.setCenter(new GLatLng( 37.52366817528083,-97.294921875), 3);
      //map.setCenter(new GLatLng( 40.0,-82.0), 2);
	  //map.enableGoogleBar();

      // Read the data
      GDownloadUrl("scheduleevents2010.xml", function(doc) {
        var xmlDoc = GXml.parse(doc);
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
        for (var i = 0; i < markers.length; i++) {
          // obtain the attribues of each marker
		  
		  var today = new Date();
		  //var weekno = today.getWeek();
		  
  		var my_date = new Date();
		var weekno = my_date.getWeek();
		  
		  
		  var week = markers[i].getAttribute("week");
	  	  var lat = parseFloat(markers[i].getAttribute("lat"));
          var lng = parseFloat(markers[i].getAttribute("lng"));
       	  var point = new GLatLng(lat,lng);
		  var date = markers[i].getAttribute("date");
       	  var address = markers[i].getAttribute("address");
       	  var name = markers[i].getAttribute("name");
	      var champion = markers[i].getAttribute("champion"); 
	      var purse = markers[i].getAttribute("purse"); 
	      var html = "<font size='1' face='Verdana'><b>"+name+"</b><br>"+address+"<br>Defending champion: <b>"+champion+"</b><br>Purse: "+purse+"</font>";
	      var category = markers[i].getAttribute("category");
		  var dayone = markers[i].getAttribute("dayone");
		  var daytwo = markers[i].getAttribute("daytwo");
		  var daythree = markers[i].getAttribute("daythree");
		  var dayfour = markers[i].getAttribute("dayfour");
		  //var html1 = "<font size='1' face='Verdana'><b>"+showdetails+"<br></font>";	
		  //var html1 = "<font size='1' face='Verdana'>"+day1+"<br>"+day2+"<br>"+day3+"<br>"+day4+"</font>";
		  //var html1 = "<font size='1'>"+dayone+"</font>";
		  var html1 = "<font size='1' face='Verdana'>"+dayone+"<br>"+daytwo+"<br>"+daythree+"<br>"+dayfour+"</font>";
           // create the marker
          var marker = createTabbedMarker(point,week,date,name,html,category,html1,'Event','TV Coverage');
		  
		  if (weekno < 10)
			{
				var weektext = "Week 0";
			}
			else
			{
				var weektext = "Week ";
			}
		   
		    if (week == weektext + weekno)
		    {	
			   //map.setCenter(new GLatLng( 40.0,-82.0), 2);
			   //map.setCenter(new GLatLng(lat,lng),7,G_SATELLITE_MAP);
			   map.setCenter(new GLatLng(lat,lng),15,G_SATELLITE_MAP);
			   //map.setCenter(new GLatLng(lat,lng),14);
			   map.addOverlay(marker);
            }
		}

        // == show or hide the categories initially ==
        show("live");
        show("pgaanalyst");
		show("pgatower");
		show("champions");		
		show("sprintstudio");
		show("weekoff");
        // == create the initial sidebar ==
        //makeSidebar();
      });

}

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }


    //]]>
