// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function initialize() { 
	if (GBrowserIsCompatible() && typeof locations != 'undefined') { 
		var map = new GMap2(document.getElementById("map")); 
		map.setCenter(new GLatLng(37.4419, -122.1419), 13); 
		//map.disableDragging();


		// Clicking the marker will hide it 
		function createMarker(latlng, location, tinyIcon) { 
			//var marker = new GMarker(latlng); 
			

			
			var marker = new PdMarker(latlng,tinyIcon)
			var html="<a href='"+location[2]+"'>"+"<img width='65' height='65' src="+location[3]+" alt="+location[0].profile.name+"/><br/>"+location[0].profile.name+"</a> in "+location[1].city.name; 
			
			marker.setDetailWinHTML(html);
			return marker; 
		} 

		var bounds = new GLatLngBounds; 
		
		var tinyIcon = new GIcon();
		tinyIcon.image = marker;
		tinyIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		tinyIcon.iconSize = new GSize(40, 40);
		tinyIcon.shadowSize = new GSize(60, 40);
		tinyIcon.iconAnchor = new GPoint(20, 40);
		tinyIcon.infoWindowAnchor = new GPoint(5, 1);
		
		for (var i = 0; i < locations.length; i++) { 
			var latlng=new GLatLng(parseFloat(locations[i][1].city.lat),parseFloat(locations[i][1].city.lng)) 
			bounds.extend(latlng); 
			map.addOverlay(createMarker(latlng, locations[i],tinyIcon)); 
		} 
		map.setCenter(bounds.getCenter(),(map.getBoundsZoomLevel(bounds)-10)); 
	} 
} 

function initialize_ajax(locals) { 
	if (GBrowserIsCompatible() && typeof locals != 'undefined') { 
		var map = new GMap2(document.getElementById("map")); 
		map.setCenter(new GLatLng(37.4419, -122.1419), 13); 
		//map.disableDragging();

		// Clicking the marker will hide it 
		function createMarker(latlng, location, tinyIcon) { 
			//var marker = new GMarker(latlng); 
			var marker = new PdMarker(latlng, tinyIcon)	
			var html="<a href='"+location[2]+"'>"+"<img width='65' height='65' src="+location[3]+" alt="+location[0].profile.name+"/><br/>"+location[0].profile.name+"</a> in "+location[1].city.name; 
			marker.setDetailWinHTML(html);
			return marker; 
		} 

		var bounds = new GLatLngBounds; 
		
		var tinyIcon = new GIcon();
		tinyIcon.image = marker;
		tinyIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		tinyIcon.iconSize = new GSize(40, 40);
		tinyIcon.shadowSize = new GSize(60, 40);
		tinyIcon.iconAnchor = new GPoint(20, 40);
		tinyIcon.infoWindowAnchor = new GPoint(5, 1);
		
		for (var i = 0; i < locals.length; i++) { 
			var latlng=new GLatLng(parseFloat(locals[i][1].city.lat),parseFloat(locals[i][1].city.lng)) 
			bounds.extend(latlng); 
			map.addOverlay(createMarker(latlng, locals[i],tinyIcon)); 
		} 
		map.setCenter(bounds.getCenter(),(map.getBoundsZoomLevel(bounds)-10)); 
	} 
}

function initialize_popular() { 
	if (GBrowserIsCompatible() && typeof popular_locations != 'undefined') { 
		var map = new GMap2(document.getElementById("map")); 
		map.setCenter(new GLatLng(37.4419, -122.1419), 13); 
		map.addControl(new GSmallMapControl());
		//map.setMapType(G_SATELLITE_MAP);
		//map.disableDragging();
		// Clicking the marker will hide it 
		function createMarker(latlng, location, tinyIcon) { 
			//var marker = new GMarker(latlng); 
			var marker = new PdMarker(latlng, tinyIcon);
			var html="<a href="+location[9]+">"+location[7].mekwa.nickname+"</a> <a href="+location[8]+">"
					+location[0]+"</a> wish was fulfilled in <a href="+location[5]+">"
					+location[1].city.name+"</a><br/> <a href="+location[5]+" rel='lightbox[exploremap]' title='"+location[2]
					+"'><img width='65' height='65' src="+location[3]+" alt="+location[2]+"/></a>"; 
			//marker.setTooltip(html);
			marker.setDetailWinHTML(html);
			marker.setOpacity(85);
			return marker; 
		} 
		var bounds = new GLatLngBounds; 
		for (var i = 0; i < popular_locations.length; i++) { 
			
			var tinyIcon = new GIcon();
			tinyIcon.image = mekwa_markers[i];
			tinyIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			tinyIcon.iconSize = new GSize(40, 40);
			tinyIcon.shadowSize = new GSize(60, 40);
			tinyIcon.iconAnchor = new GPoint(20, 40);
			tinyIcon.infoWindowAnchor = new GPoint(5, 1);

			var latlng=new GLatLng(parseFloat(popular_locations[i][1].city.lat),parseFloat(popular_locations[i][1].city.lng)) 
			bounds.extend(latlng); 
			map.addOverlay(createMarker(latlng, popular_locations[i], tinyIcon)); 
		} 
		map.setCenter(bounds.getCenter(),(map.getBoundsZoomLevel(bounds))); 
	} 
}


function replaceImg(id, path) {
	var imgDest = $(id);
	var imgSrc = path;
	imgDest.src = imgSrc;
}


function updateCropper(w, h, x, y){

	$('imgCrop_testImage').remove();
	$$('.imgCrop_dragArea')[0].remove()

	cropper = new Cropper.ImgWithPreview(
		'testImage',
		{
			previewWrap: 'previewWrap',
			minWidth: w,
			minHeight: h,
			onloadCoords: { 
				x1: x, 
				y1: y, 
				x2: x + w, 
				y2: y + h},
			onEndCrop: onEndCrop
		}
	);
	$('previewWrap').setStyle("margin-left:" + x + "px");
	$('previewWrap').setStyle("margin-top:" + y + "px");
}


function setCropMask(id, mask){
	$(id).value = mask;
}

function setCropPicture(picture_id){
	$('mekwa_picture_to_crop').value = picture_id;
}

function initialize_entries_map() { 
	if (GBrowserIsCompatible() && typeof entries_locations != 'undefined') { 
		var map = new GMap2(document.getElementById("entries_map")); 
		map.setCenter(new GLatLng(37.4419, -122.1419), 13); 
		map.addControl(new GSmallMapControl());
		
		map.setMapType(G_PHYSICAL_MAP);
		//map.disableDragging();

		// Clicking the marker will hide it 
		function createMarker(latlng, location) {
			
			var tinyIcon = new GIcon();
			tinyIcon.image = location[7];
			// tinyIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
			tinyIcon.iconSize = new GSize(40, 40);
			tinyIcon.shadowSize = new GSize(60, 40);
			tinyIcon.iconAnchor = new GPoint(20, 40);
			tinyIcon.infoWindowAnchor = new GPoint(5, 1); 
			//var marker = new GMarker(latlng); 
			var marker = new PdMarker(latlng, tinyIcon);
			var html="<div class='map_balloon_header'><h3><a href="+location[6]+">"+location[0]+"</a></h3></div><div class='clear'></div><div class='map_balloon_content'>fulfilled in: <a href="+location[5]+">"+location[1].name+"</a><a href="+location[3]+" rel='lightbox[exploremap]' title='"+location[2]+"'></a></div>"; 
			//marker.setTooltip(html);
			marker.setDetailWinHTML(html);
			marker.setOpacity(85);
			return marker; 
		} 

		var bounds = new GLatLngBounds; 
		
		
		
		for (var i = 0; i < entries_locations.length; i++) { 
			var latlng=new GLatLng(parseFloat(entries_locations[i][1].lat),parseFloat(entries_locations[i][1].lng)) 
			bounds.extend(latlng);
			//tinyIcon.image = entries_locations[i][7]; 
			map.addOverlay(createMarker(latlng, entries_locations[i])); 
		} 
		var boundaux = 1;
		if(entries_locations.length < 2) boundaux=10;
		map.setCenter(bounds.getCenter(),(map.getBoundsZoomLevel(bounds)-boundaux)); 
	
	} 
}