// ---- Map -----------------------------------------

mapKey = 'ABQIAAAAIoFwHCCkJqPsdoTRMJPWlBRfb7BskzuN3hTUWaOjUSgtp0K8vxRgM8E7vocjKBwhEozcKkbBA7mgvg'	// Bound to uncovr.com

gMap = null;
gGeocoder = null;
gMarker = null;
gPanning = false;
gMarkerVisible = false;

var Map = {
	SetUp : function() {
		if (!GBrowserIsCompatible()) return;

		defaultPoint = new GLatLng(30.5937, 15.3672);

		var gHomeIcon = new GIcon(G_DEFAULT_ICON);

		//gHomeIcon.image = "/img/markers/mm_20_red.png";
		//gHomeIcon.shadow = "/img/markers/mm_20_shadow.png";
		//gHomeIcon.iconSize = new GSize(12, 20);
		//gHomeIcon.shadowSize = new GSize(22, 20);
		//gHomeIcon.iconAnchor = new GPoint(6, 20);
		//gHomeIcon.infoWindowAnchor = new GPoint(5, 1);

		gMap = new GMap($('map'));
		gMap.setCenter(defaultPoint, 1);

		gMarker = new GMarker(gMap.getCenter(), {draggable: true, icon: gHomeIcon, bouncy: true});

		gMap.disableDragging();    

		gMap.addControl(new GSmallZoomControl());

		gGeocoder = new GClientGeocoder();

		GEvent.addListener(gMap, 'move', function() {
			if (gPanning) return;

			if (gMarker) {
                		gMarker.setPoint(gMap.getCenter());
                		Map.UpdatePoint(gMap.getCenterLatLng());
            		}
      		});

		GEvent.addListener(gMap, 'moveend', function() {
			if (gPanning) {
                		Map.UpdatePoint(gMap.getCenterLatLng());
                		gPanning = false;
            		}
		});

		GEvent.addListener(gMarker, 'dragend', function() {
			setTimeout(function() {
				gPanning = true;
				gMap.panTo(gMarker.getPoint());
            		}, 500);
		});
	},
	CenterOnRegion : function(el) {
		// Display Spinner
		
		var number = el.selectedIndex;
		regionId = el.options[number].value;
		
		address = $('region_' + regionId).value;

	
    	Map.LookupAddress(address);
	},
	LookupAddress : function(address) {
		// Display Spinner

    	gGeocoder.getLocations(address, Map.HandleGeocoderResponse);
	},
	HandleGeocoderResponse : function(response) {
    		var stat = '';
    		var accuracy = 0;
    		var zoom = 10;
    		var point = null;
    		
    		if (!response || response.Status.code != 200) {
        		// google lookup failed; fall through to manual case
    		}
    		else {
        		place = response.Placemark[0];
        		accuracy = place.AddressDetails.Accuracy;
        		point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

        		if (accuracy >= 6) {
           			stat = '<p><b>Is that you?<br \/>Please drag and zoom the map to a precise location.<\/b><\/p>';
            		zoom = 13;
        		}
       		else {
            		stat = '<p><b>Partial address found.<br \/>Please drag and zoom the map to a precise location.<\/b><\/p>';
            		zoom = 10;
        		}
    		}

    		if (accuracy == 0) {
        		var a = address.toLowerCase();

        		if (a.indexOf('japan') != -1) {
            		point = new GLatLng(35.7465, 139.5703);
            		zoom = 5;
        		}
        		else if (a.indexOf('england') != -1) {
            		point = new GLatLng(51.5361, -0.0879);
            		zoom = 6;
        		}
        		else {
            		stat = '<p><b>Address not found.<br \/>Please try another address, or use a well-known city or postal code.<\/b><\/p>';
        		}
    		}

    		//$('lookup_status').innerHTML = stat;

    		if (point) {
			Map.UpdatePoint(point);
			Map.Show(point, zoom);
         	}
	},
	Show : function(point, zoom) {
		if (!gMarkerVisible) {
            	gMap.addOverlay(gMarker);
            	gMarkerVisible = true;
            	gMap.enableDragging();
        	}

        	gMap.setCenter(point, zoom);

        	var outerMap = $('outermap');
        	outerMap.style.visibility = 'visible';
        	outerMap.style.position = 'static';
        	
        	$('mapbox').show();
	},
	UpdatePoint : function(point) {
    		$('latitude').value = point.y;
    		$('longitude').value = point.x;
	}
}

// ---- Experiences -----------------------------------------

var Experiences = { 
	Sift : function(tab, theClass) {		
		// Make sure the experience container is show if it exists
		if($('experiences')) {
			$('experiences').show();
		}
		
		// Hide the photos if there are photos on the page
		if($('photos')) {
			$('photos').hide();
		}
		
		// Activate the current tab
		Tabs.Activate(tab);
		
		if(theClass != "experience") {
			var allElements = document.getElementsByClassName("experience");
			
			// Hide all the elements
			for (var i = 0; i < allElements.length; i++) {
				element = allElements[i];
				
				element.hide();
			}
		}
		
		var siftedElements = document.getElementsByClassName(theClass);
		
		// Show the elements that meet the critiqa
		for (var i = 0; i < siftedElements.length; i++) {
			element = siftedElements[i];
			
			element.show();
		}
	}
}

// ---- Tags -----------------------------------------

var Tabs = {
	Activate : function(tab) {
		var activeTabs = document.getElementsByClassName("active");
		
		// Deactive the currently active tab
		for (var i = 0; i < activeTabs.length; i++) {
			element = activeTabs[i];
			
			element.removeClassName('active');
		}
		
		tab.addClassName('active');
	}
}

// ---- Reviews -----------------------------------------

var Reviews = {
	Delete : function(venueUrl, id) {

		if(confirm('Are you sure?')) {
			// Destory it on the server
			var url = '/venues/' + venueUrl + '/reviews/destroy/' + id;
		
			new Ajax.Request(url, {
				onComplete:function(request) {
					new Effect.DropOut('review_' + id);

					var reviewCountEl = $('review_count');

					if(reviewCountEl) {
						var reviewCountString = reviewCountEl.innerHTML.replace('(', '').replace(')', '')
						var reviewCountMinusOne = parseInt(reviewCountString ) - 1;

						$('review_count').innerHTML  = '(' + reviewCountMinusOne + ')';
					}
				}
			});	
		}
	}
}

// ---- Faqs -----------------------------------------

var Faqs = {
	ToggleFaq : function(id) {
		faqQuestionEl = $('faq_' + id + '_question');

		if(faqQuestionEl.hasClassName('selected')) {
			faqQuestionEl.removeClassName('selected');
		}
		else {
			faqQuestionEl.addClassName('selected');
		}

		$('faq_' + id + '_answer').toggle();
	}
}