
var tooltip,map;
var marks = new Array();
var loading = false;

  google.load("maps", "2");
   // Call this function when the page has been loaded
   function setBounds()
   {
       var bounds = new google.maps.LatLngBounds;
	for (var i=0; i<sPoints.length; i++) {	
		var ll = new google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
		bounds.extend(ll);
	}
map.setCenter(bounds.getCenter());
    map.setZoom(map.getBoundsZoomLevel(bounds));
   }
   
  function initialize() {
  
	if ($('map') != null)
	{
    map = new google.maps.Map2($('map'));
    map.addControl(new google.maps.SmallMapControl());
    
    
    tooltip = document.createElement("div");
      map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      
      tooltip.style.visibility='hidden';
    
    setBounds();
    
    map.setMapType(G_PHYSICAL_MAP);
	
	
	var tinyIcon = new google.maps.Icon();
	tinyIcon.image = "http://www.cookfood.net/images/map_pointer.png";
	tinyIcon.shadow = "http://www.cookfood.net/images/map_shadow.png";
	tinyIcon.iconSize = new google.maps.Size(12,20);
	tinyIcon.shadowSize = new google.maps.Size(22,20);
	tinyIcon.iconAnchor = new google.maps.Point(6,20);
	tinyIcon.tooltipAnchor = new google.maps.Point(4,8);
	tinyIcon.infoWindowAnchor = new google.maps.Point(5,1);
	
	var fsIcon= new google.maps.Icon();
	fsIcon.image = "http://www.cookfood.net/images/map_pointer_alt.png";
	fsIcon.shadow = "http://www.cookfood.net/images/map_shadow.png";
	fsIcon.iconSize = new google.maps.Size(12,20);
	fsIcon.shadowSize = new google.maps.Size(22,20);
	fsIcon.iconAnchor = new google.maps.Point(6,20);
	fsIcon.tooltipAnchor = new google.maps.Point(4,8);
	fsIcon.infoWindowAnchor = new google.maps.Point(5,1);
	
	
	markerOptions = {icon:tinyIcon};
	altMarkerOptions = {icon:fsIcon};
	
	
    for (var i=0; i<sPoints.length; i++) {	
		var ll = new google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
		 marks[i] = new google.maps.Marker(ll,sPoints[i][4]==1?altMarkerOptions:markerOptions);
		 var mark = marks[i];
		 map.addOverlay(mark);
		 mark.value = i
		 mark.tooltip = '<div class="tooltip">' + sPoints[i][2] + '</div>';
		 mark.id = (sPoints[i][4]==1?"f":"")+sPoints[i][3];
		 google.maps.Event.addListener(mark,'click',function(){
					map.setZoom(15);
					map.setCenter(this.getLatLng());
					map.setMapType(G_NORMAL_MAP);
					map.openInfoWindow(map.getCenter(),$('iw' + this.id).clone(), {onCloseFn: function(){ setBounds();  map.setMapType(G_PHYSICAL_MAP);} });
					});
		google.maps.Event.addListener(mark,'mouseover',function(){showTooltip(this)});
		google.maps.Event.addListener(mark,'mouseout',function(){tooltip.style.visibility='hidden'});
	}
	
	if ($('farmshoplist')!=null)
	{
		var fs = $$('#farmshoplist a');
		
		for(var i=0;i<fs.length;i++)
		{
			fs[i].addEvent('click',function(e){e = new Event(e); showFS(e.target.rel);  e.stop();});
		}
	}
	}
	
	if ($('shopmap')!=null)
	{
	map = new google.maps.Map2($('shopmap'));
    map.addControl(new google.maps.SmallMapControl());
    
    setBounds();
    map.setZoom(15);
    map.setMapType(G_NORMAL_MAP);
    
    var tinyIcon = new google.maps.Icon();
	tinyIcon.image = "http://www.cookfood.net/images/map_pointer.png";
	tinyIcon.shadow = "http://www.cookfood.net/images/map_shadow.png";
	tinyIcon.iconSize = new google.maps.Size(12,20);
	tinyIcon.shadowSize = new google.maps.Size(22,20);
	tinyIcon.iconAnchor = new google.maps.Point(6,20);
	tinyIcon.tooltipAnchor = new google.maps.Point(4,8);
	tinyIcon.infoWindowAnchor = new google.maps.Point(5,1);
	
	markerOptions = {icon:tinyIcon};
	
	
	
    for (var i=0; i<sPoints.length; i++) {	
		var ll = new google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
		 var mark = new google.maps.Marker(ll,sPoints[i][4]==1?altMarkerOptions:markerOptions);

		 map.addOverlay(mark);
		 }
	}
  }
  
  
  function showFS(id)
  {
  
	loading = true;
	map.setZoom(15);

	for(var i=0;i<marks.length;i++)
		if (marks[i].id == "f" + id)
			map.setCenter(marks[i].getLatLng());
		
	map.setMapType(G_NORMAL_MAP);
	map.openInfoWindow(map.getCenter(),$('iwf' + id).clone(), {onCloseFn: function(){ if (!loading) {setBounds();  map.setMapType(G_PHYSICAL_MAP);} }});
	loading = false;
  }
  
  function showTooltip(marker)
  {
	tooltip.innerHTML = marker.tooltip;
	
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint	(0,0),true),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().tooltipAnchor;
	var width=marker.getIcon().iconSize.width;
	var height=tooltip.clientHeight;
	var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, 	offset.y - point.y -anchor.y -height)); 
	pos.apply(tooltip);
	
	tooltip.style.visibility='visible';
  }
  
  
  window.addEvent('unload',function(){google.maps.Unload()});
  window.addEvent('domready',function(){initialize(); Shops.initialise()});

  var Shops = {
      Current: 0,
      Anim: false,
      initialise: function() {

          var shopC = $$('#shopscroll ul');
          if (shopC.length > 0 && !window.ie6) {
              Shops.Container = shopC[0];
              Shops.All = $$('#shopscroll ul li');
              shopC[0].setStyle('width', Shops.All.length * 118 + 'px');

              var lastPos = 20;
              Shops.All.each(function(e) {
                  e.setStyle('left', lastPos + 'px');
                  lastPos += 118;
              });

              $('doLeft').addEvent('click', function(e) { Shops.scrollLeft(e) });
               $('doLeft').addClass('hidden');
              $('doRight').addEvent('click', function(e) { Shops.scrollRight(e) });
              Shops.scroll = new Fx.Scroll('innershopscroll', { duration: 1500, transition: Fx.Transitions.Quad.easeInOut, onStart: function() { Shops.Anim = true; }, onComplete: function() { Shops.Anim = false; } });
          }
          else {
              if ($('shopscroll') != null) {
                  $('shopscroll').removeClass('normalshopscroll');
                  $('doLeft').addClass('hidden');
                  $('doRight').addClass('hidden');
              }
          }
      },
      scrollLeft: function(e) {

          if (!Shops.Anim) {

              Shops.Current = Shops.Current - 7;
              if (Shops.Current <= 0) {
                  Shops.Current = 0;
                  $('doLeft').addClass('hidden');
              }
              else {
                  $('doLeft').removeClass('hidden');

              }
              $('doRight').removeClass('hidden');

              Shops.scroll.toElement(Shops.All[Shops.Current]);
          }
          e = new Event(e);
          e.stop();

      },
      scrollRight: function(e) {

          if (!Shops.Anim) {
              Shops.Current = Shops.Current + 7;

              if (Shops.Current >= Shops.All.length - 8) {
                  Shops.Current = Shops.All.length - 8;
                  $('doRight').addClass('hidden');
              }
              else {
                  $('doRight').removeClass('hidden');

              }

              $('doLeft').removeClass('hidden');

              var nextShop = Shops.All[Shops.Current];
              Shops.scroll.toElement(nextShop);

          }
          e = new Event(e);
          e.stop();
      }
  }
 
