﻿google.load("maps", "2");

var Map = Class.create({
    maps: {},
    create: function(elementID, feedURL) {
        if (GBrowserIsCompatible()) {
            this.init(elementID);
            this.updateFeed(elementID, feedURL);
        } else {
            $(elementID).update('Maps Unavailable');
        };
    },
    updateFeed: function(elementID, feedURL) {
        //centre the map
        this.maps[elementID].setCenter(new GLatLng(0, 0), 10);

        new Ajax.Request(feedURL, {
            method: 'get',
            onSuccess: function(transport) {
                map.maps[elementID].clearOverlays();
                var b = new GLatLngBounds();
                var response = transport.responseText.evalJSON();

                //add each item to the map
                response.items.each(function(item) { b.extend(map.createMarker(elementID, item)) });

                //fit the map to the markers added
                map.maps[elementID].setZoom(map.maps[elementID].getBoundsZoomLevel(b));
                map.maps[elementID].setCenter(b.getCenter());
                map.maps[elementID].checkResize();
            }
        });
    },
    init: function(elementID) {
        this.maps[elementID] = new GMap2($(elementID));
        this.maps[elementID].addControl(new GLargeMapControl());
        this.maps[elementID].addControl(new GMapTypeControl());
        this.maps[elementID].setMapType(G_NORMAL_MAP);
    },
    createMarker: function(elementID, item) {
        var i = new GIcon();
        i.image = item.icon;
        i.iconSize = new GSize(25, 25);
        i.iconAnchor = new GPoint(13, 13);

        var m = new GMarker(new GLatLng(item.latitude, item.longitude), i);

        m.title = item.name;

        GEvent.addListener(m, "click", function() { map.maps[elementID].openInfoWindowHtml(m.getLatLng(), item.content) });

        this.maps[elementID].addOverlay(m);

        return m.getLatLng();
    },
    getDirections: function(from, to) {
        window.open('http://maps.google.co.uk/maps?daddr=' + to + '&h1=en&saddr=' + from, 'directions');
    }
});

var map = new Map();

/*========================================================================================*/
/*OLD MAP STUFF, DON'T USE THIS*/
/*========================================================================================*/

var searchMap;

function Maps_Startup()
{
    if($('map'))
    {
        if (GBrowserIsCompatible()) 
        {
            searchMap = new GMap2(document.getElementById("map"));
            searchMap.addControl(new GLargeMapControl());
            searchMap.addControl(new GMapTypeControl());
            searchMap.setMapType(G_PHYSICAL_MAP);

	        var oicon = new GIcon();
	        oicon.image='http://www.skifrance.co.uk/graphics/marker.gif';
	        oicon.iconSize= new GSize(15, 15);
	        oicon.iconAnchor = new GPoint(7, 7);
	        		       		        
	        if($('mapinfo_count'))
	        {
	            var p;
	            var bounds = new GLatLngBounds();
	            searchMap.setCenter(new GLatLng(0, 0), 10);                                             
	            for(var i=0; i<$F('mapinfo_count'); i++)
	            {
	                if($F('mapinfo_lat_'+i)!='')
	                {
	                    if($F('mapinfo_lon_'+i)!='')
	                    {
	                        p=new GLatLng($F('mapinfo_lat_'+i), $F('mapinfo_lon_'+i));		                		                        								
	                        searchMap.addOverlay(createMarker(p, oicon, $F('mapinfo_acc_' + i), $F('mapinfo_res_' + i),$F('mapinfo_ref_' + i),$F('mapinfo_thu_' + i)));	                		                		                
                            bounds.extend(p);                                                
                        }
                    }
	            }  
	            
	            searchMap.setZoom(searchMap.getBoundsZoomLevel(bounds));
                searchMap.setCenter(bounds.getCenter());
	        }
	        else
	        {
                
                searchMap.setCenter(new GLatLng($F('mapinfo_lat'), $F('mapinfo_lon')), parseInt($F('mapinfo_zoo')));                                                
	            setMarker=new GMarker(searchMap.getCenter(), oicon);									
	            searchMap.addOverlay(setMarker);

	        };

	        searchMap.checkResize();
        }
    }
}    
    function createMarker(point, oicon, name, resort, reference, thumbnail) 
    {    
        var marker = new GMarker(point, oicon);
        var aname = name;
        var aresort = resort;
        var aref = reference;
        var info = '<div style="height:160px"><p class="center">' + name + '<br/><img class="image" src="' + thumbnail + '" style="width:160px; height: 100px;"/><br/><a href="/Accommodation/' + aref + '">More Info</a></p></div>';
        var apoint = point;
     
        
        marker.myRef=aref;
		marker.myName=aname;
		                        
		//GEvent.addListener(marker, "click", function() {document.location.href='villa.aspx?reference='+aref;});
		GEvent.addListener(marker, "click", function() {searchMap.openInfoWindowHtml(apoint,info,{pixelOffset: new GSize(8,-8)});});
		//GEvent.addListener(marker, "mouseout", function() {setTimeout("searchMap.closeInfoWindow()",5000);});
		                                                       
        return marker;
    }    
        
        
  
