window.sharedFilesURL = 'http://www.williams.edu/Economics/ArtsEcon/mappages/SharedMapFiles/';
var map;
var markers = new Array();

var bounds2 = null;
var handle = null;
var started = false;
var opened = false;
var coords = [];
var m;

function markerOrder(marker,b) {
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

function getClientRect( obj ) {
    //Only works on API V3        
    var rect = obj.Ui;
    var x = obj.x;
    var y = obj.y;
    var height = obj.getIcon().iconSize.height;
    var width = obj.getIcon().iconSize.width;
    /*
    var left = x - width  < 0 ? 0 : x - width;
    var top  = y - height < 0 ? 0 : y - height;
    */
    var left = x;
    var right = x + width;
    var top = y;
    var bottom = y + height;
    
    return { top: top, left: left, right: right, bottom: bottom, height:height, width: width};
    
}

function CheckDouble(marker)
{
    var point = marker.getPoint();
     
     var hash = point.lat() + point.lng();  
     hash = hash.toString().replace(".","").replace(",", "").replace("-","");  
     // check to see if we've seen this hash before  
     if (coords[hash] == null)
     {
        coords[hash] = 1;  
        return false
     }
     else return true;
}


function testbounds(original, target) {

	var rect1 = /* typeof(original.aa)!='undefined'? original.aa[0].getBoundingClientRect(): */ getClientRect(original);
	var rect2 = /* typeof(target.aa)!='undefined'? target.aa[0].getBoundingClientRect() : */ getClientRect(target);
    
    //if (original.title == target.title) return false;      //if there is only 1 item, then no collision
	if(rect1.left > (rect2.left+rect2.width)) return false; // rect1 is too far right, no collision
	else if((rect1.left+rect1.width) < rect2.left) return false; // rect1 is too far left, no collision
	else if(rect1.top > (rect2.top+rect2.height)) return false; // rect1 is too far down, no collision
	else if((rect1.top+rect1.height) < rect2.top) return false; // rect1 is too far up, no collision
	

	return true; // there is a collision
}

function addbounds(marker) {
    var rect = /* typeof(marker.aa)!='undefined' ? marker.aa[0].getBoundingClientRect() : */ getClientRect(marker);
	bounds2.extend(new GPoint(rect.left, rect.top));
	bounds2.extend(new GPoint(rect.right, rect.bottom));
}

function newMarker(location, html, markerOptions, iconnum)
{
    var temp_marker = new GMarker(location, markerOptions)

    
    if (iconnum==null ) 
    {
		temp_marker.startimport=2;
		temp_marker.importance=2;
		
	}
	else {
		temp_marker.startimport=1;
		temp_marker.importance=1;
		
	}

	temp_marker.targeted=0;
	temp_marker.startcoord=location;
	temp_marker.openwindow=false;
	temp_marker.html=html;
	temp_marker.line = null;
	temp_marker.shifted = 0;
	temp_marker.title = iconnum;
	
	GEvent.addListener(temp_marker, "click", function() {
			for (i=0; i < markers.length; i++) {
				markers[i].openwindow=false;
			}
			temp_marker.openInfoWindowHtml(temp_marker.html);
			temp_marker.openwindow=true;
		});
		
	
	return temp_marker;
}	



function HideRozet()
{
    for (x = 0; x < markers.length; x++) {
		if (!map.getInfoWindow().isHidden()) { map.closeInfoWindow(); }
		if (markers[x].targeted==1)
		{
		    map.removeOverlay(markers[x].line);
		    map.removeOverlay(markers[x].point);
		    map.removeOverlay(markers[x]);
		    markers[x].setLatLng(markers[x].startcoord);
		    markers[x].targeted=0;
		    markers[x].shifted=0;
		    markers[x].importance = markers[x].startimport;
		    map.addOverlay(markers[x]);
		}

		if (markers[x].openwindow==true) {
			markers[x].openInfoWindowHtml(markers[x].html);
		};
	};
	opened = false;
	started = false;
}

function CreateRozet()
{
    if (!opened) 
    {


        for (i=0; i<markers.length; i++)
		{
		    var step = 0;
		    for (j=0; j<markers.length; j++) {
		        
		        var withinBounds = testbounds(markers[i], markers[j]);
		        if (withinBounds && markers[j].isHidden()==false && markers[j].targeted==0) {
				    step++;
				    markers[j].targeted=1;
				    
				}
			}
			
			if (step > 1) {
			    if (!map.getInfoWindow().isHidden()) { map.closeInfoWindow(); }
			    var tick = 1;
			    var shift = .0005;
			    var deg = (360 / (step));
			    started = false;
			    bounds2 = new GBounds(map.fromLatLngToContainerPixel(markers[i].getLatLng()), map.fromLatLngToContainerPixel(markers[i].getLatLng()));
			    for (x = 0; x < markers.length; x++) {
			        markers[x].openwindow = false;
				    if (markers[x].targeted==1 && markers[x].shifted==0) {
				        
					    map.removeOverlay(markers[x]);
					    var delta_lat = (shift +  shift*(Math.pow(2, (17 - map.getZoom())) - 1)) * Math.cos(((deg * tick) * Math.PI) / 180);
					    var delta_lng = (shift +  shift*(Math.pow(2, (17 - map.getZoom())) - 1)) * Math.sin(((deg * tick) * Math.PI) / 180);
					    var point_latlng = new GLatLng((markers[x].startcoord.lat() + (delta_lat * .15)), (markers[x].startcoord.lng() + (delta_lng * .15)));
					    var new_lat = markers[x].getLatLng().lat() + delta_lat;
					    var new_lng = markers[x].getLatLng().lng() + delta_lng; 
					    markers[x].setLatLng(new GLatLng(new_lat, new_lng));
					    markers[x].importance=10;
					    map.addOverlay(markers[x]);
					    addbounds(markers[x]);
    					
					    linepointcolor = "#000000";
					    if (map.getCurrentMapType()==G_SATELLITE_MAP||map.getCurrentMapType()==G_HYBRID_MAP) {linepointcolor = "#FFFFFF";}
					    markers[x].line = new GPolyline([
						      markers[x].startcoord,  
						      markers[x].getLatLng()], 
						      linepointcolor, 4, 0.75);
					    map.addOverlay(markers[x].line);
    			  
					    //var linepointcolor = '#9BE61A';
					    var linepointcolor = '#FF0000';

					    markers[x].point = new GPolyline([
							    markers[x].startcoord,
							    point_latlng],
							    linepointcolor, 3, 0.75);
					    map.addOverlay(markers[x].point);
    					
					    markers[x].shifted=1;
					    tick++;
				    }
			    }
			}
	    }
    }
}

function AdjustRozet()
{
        map.clearOverlays();
        
        var tmpMarkers = [];
        for (t=0; t<markers.length; t++) {
            
            tmpMarkers[t] = markers[t];
            tmpMarkers[t].setLatLng(markers[t].startcoord);
		    tmpMarkers[t].targeted=0;
		    tmpMarkers[t].shifted=0;
		    tmpMarkers[t].importance = markers[t].startimport;
        }
        
        markers = [];
        
        for (a=0; a<tmpMarkers.length; a++)
        {
            markers.push(tmpMarkers[a])
            map.addOverlay(tmpMarkers[a]);
            
        }
        
        opened = false;
        CreateRozet();
    
}


