
var BigArtMap = function(items, options){
    this.items = items;
    this.markers = new Array();
    this.cluster = null;
    this.map = null;
    if(options && options.region){
	this.region = options.region;
    } else {    
	this.region = null;
    }
    if(options && options.arts_council){
        this.arts_council = true;
    } else {
        this.arts_council = false;
    }
    if(options && options.embedded){
        this.embedded = true;
    } else {
        this.embedded = false;
    }
}

BigArtMap.prototype.get_icon = function(item){
    var icon = new GIcon();
            
    if(item.profile__username=='arts_council_england'){
        var image = item.num_highlights?"mm_20_white_highlight.png":"mm_20_green.png";
        icon.image = "/media/images/" + image;
    } else {
        var image = item.num_highlights?"mm_20_red.png":"mm_20_blue.png";
	icon.image = "http://labs.google.com/ridefinder/images/" + image;
    }
    
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
    
    return icon;    
}

BigArtMap.prototype.setup_region = function(no_centering){
    if (this.region) {
	bounds = new GLatLngBounds(
            new GLatLng(this.region.min_lat, this.region.min_lng), 
            new GLatLng(this.region.max_lat, this.region.max_lng));
	if(!no_centering){
	    this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds), G_NORMAL_MAP);
	}
    }
}

BigArtMap.prototype.show = function(){


    if(!GBrowserIsCompatible()) return;
    
    
    this.map = new GMap2($('#map')[0]);
    this.map.addControl(new GLargeMapControl());
    this.map.addControl(new GMapTypeControl());
    this.map.enableContinuousZoom();
    this.map.setCenter(new GLatLng(49.937079757, 3.44970703125), 13);

    
    for(var i=0; i < this.items.length; i++){
	var item = this.items[i];
	this.create_marker(item);
    }


    var clusterImage = "http://www.acme.com/resources/images/markers/blue_large.PNG"

    var clusterMarkerIcon = new GIcon();
    clusterMarkerIcon.image='/media/images/arrow.png';
    clusterMarkerIcon.iconSize=new GSize(39, 34);
    clusterMarkerIcon.iconAnchor=new GPoint(9, 31);
    clusterMarkerIcon.infoWindowAnchor=new GPoint(9, 31);
    clusterMarkerIcon.shadow='http://www.google.com/intl/en_us/mapfiles/arrowshadow.png';
    clusterMarkerIcon.shadowSize=new GSize(39, 34);
    

    var clusterOptions = {
        markers : this.markers,
        clusterMarkerIcon : clusterMarkerIcon
    }
    
    this.cluster=new ClusterMarker(this.map, clusterOptions);
    
    this.cluster.fitMapMaxZoom = 5;	
    this.cluster.fitMapToMarkers();
    
    
    if(this.markers.length == 1){
	this.markers[0].open();
        var item = this.items[0];
        this.map.setCenter(new GLatLng(item.latitude, item.longitude), 16);
    } else {
        this.setup_region();
    }
}

BigArtMap.prototype.create_marker = function(item, icon) {

    var data = item.fields;
    var lastpos = null;
    var point = new GLatLng(data.latitude, data.longitude);
    var icon = this.get_icon(item);

    var marker = new GMarker(point, {
                title :  data.title,
                draggable : true,
		icon : icon
    });
    marker.id = item.pk;

    if(this.embedded){
    marker.open=function(){
      var url = "http://bigartmob.com/view/" + item.pk + "/";
      if(parent){
          parent.location.href=url;
      }
  }

    GEvent.addListener(marker, "click", function(){
        marker.open();
        });

   } else {
    marker.open=function(){
        $.get('/map/detail/' + item.pk, null, function(response){
            marker.openInfoWindowHtml(unescape(response));
        });
    }

    GEvent.addListener(marker, "click", function(){
        marker.open();
 	});

    
    GEvent.addListener(marker, "dragstart", function() {
        lastpos=marker.getPoint(); 
    });
            
        
    GEvent.addListener(marker, "dragend", function() {

        if(confirm("Are you sure to want move the marker to the new position?")) { 
            newpos = marker.getPoint();                    
            args = {lat : newpos.lat(), lng : newpos.lng()}
            $.get('/map/updatepos/' + item.pk, args, function(response){
                if (response != 'success'){
                    alert("You do not have permission to move this marker");
                    marker.setPoint(lastpos);
                }
            });
        } else {  
          marker.setPoint(lastpos);
        }
      });
    }             	
    this.markers.push(marker);
    return marker;

}
