//new jaf object
var jaf = new jaf();

      var to_htmls = [];
      var from_htmls = [];
var htmls = [];
var gmarkers = [];
var i=0;

function main()
{

        var flashvars = {};
		var params = { allowScriptAccess:"always", wmode:"transparent", menu:'false' };
		var attributes = { id:"header", name:"header" };
	//	swfobject.embedSWF("swf/tacori_nav.swf", "header", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    
    dynamicScroll();
    initialize_map();
    

    return false;
}


var map;
var geocoder;



//map_data array set from where-to-buy.php

function initialize_map()
{    
    geocoder = new GClientGeocoder();
    
    map = new GMap2(document.getElementById("map_canvas"));

    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
	
	if( typeof(window["map_data"]) == "undefined" )
	{
		map.setCenter(new GLatLng(43,-100), 3);
	}
	else
	{
		map.setCenter(new GLatLng(map_data['A']['lat'], map_data['A']['lon']), 9);
	}

    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);

    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function createMarker(point, index) 
    {
        // Create a lettered icon for this point using our icon class
        var letter = index;
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

        // Set up our GMarkerOptions object
        markerOptions = { icon:letteredIcon };
        var marker = new GMarker(point, markerOptions);

        var infoHTML = "<div>" + map_data[index]['name']  + "</div>";
            infoHTML+= "<div>" + map_data[index]['phone'] + "</div>";

			if(map_data[index]['url']!='') infoHTML+= "<div><a href='" + map_data[index]['url'] + "' target='_blank'>Visit Website</a></div>";
        
            if(map_data[index]['rank']!='') infoHTML+= "<div><img src='/assets/" + map_data[index]['rank'] + ".gif'></div>";
        
 // The info window version with the "to here" form open
        to_htmls[i] = infoHTML + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
           '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
                  // "(" + name + ")" + 
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = infoHTML + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
           '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
                  // "(" + name + ")" + 
           '"/>';
        // The inactive version of the direction info
        infoHTML = infoHTML + '<br>Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a>';        


var clickItem = function(){ marker.openInfoWindowHtml( infoHTML ); }
        
        GEvent.addListener(marker, "click", clickItem);

        getObj("name"+letter).onclick = clickItem;
        
        gmarkers[i] = marker;
        htmls[i] = infoHTML;
        i++;

        return marker;
    }
    
    
    var data_keys = new Array('A', 'B', 'C');
    
    if( typeof(window["map_data"]) != "undefined" )
	{
	for(var idd=0; idd<3; idd++ )
    {
        var key   = data_keys[idd];
        
		var point = new GLatLng( map_data[key]['lat'],  map_data[key]['lon']);
            
        map.addOverlay( createMarker(point, key) );
    }		
	}

}


      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }


/*
var headerDefaults = field_defaults;
function clearHeader(obj)
{
obj.select();
if( obj.value == headerDefaults[obj.id] || obj.value == '' )
{
//obj.value  = '';
obj.onblur = function(){ if(obj.value == '') obj.value = headerDefaults[obj.id]; }
}
}
*/
