var mapBounds = new GLatLngBounds();
var map;
var guideDiv;
var tripDiv;
var activeGuide;
var activeTrip;

var map;
var sectionDiv;
var activePoi = null;
var activeGuide = null;
var openMapId = null;


/*
function addTrip(id, name, lat, lon) {
	var latlng = new GLatLng(lat, lon);
	var marker = new GMarker(latlng);
	
	map.addOverlay(marker);
	marker.hide();
	
	mapBounds.extend(latlng);
}
*/

function textareaClick(object, defaultText) {
	if(object.value == defaultText) {
		object.value = "";
	}
}

function onClickPost(obj) {
	jQuery(obj).text("");
	jQuery(obj).toggleClass("post-textarea-clicked","post-textarea");
	jQuery(obj).next().show();
}

function celsiusFahrenheit(temp) {
	if (temp == 'C') {
		jQuery(".forecast-box .fahrenheit").hide();
		jQuery('#celsius').addClass("black");
		jQuery('#fahrenheit').removeClass("black");
		jQuery(".forecast-box .celsius").fadeIn("slow");
	} else {
		jQuery(".forecast-box .celsius").hide();
		jQuery('#celsius').removeClass("black");
		jQuery('#fahrenheit').addClass("black");
		jQuery(".forecast-box .fahrenheit").fadeIn("slow");
	}
}

function hideForecast() {
	jQuery("#forecast-panel").hide();
}

function guideClicked(guide) {
	top.location.href = "/guide/" + guide.url;
}

jQuery(document).ready(function() {
	
	// Topic post textarea effects 
	//jQuery('textarea.topic-textarea').autoResize();
	tarea = jQuery('textarea.post-textarea');
	tarea.autoResize();
	tarea.text("Write a comment");
	tarea.click(function() {
		if (jQuery(this).text() == "Write a comment")
			onClickPost(this);
	});
	
	
	tarea = jQuery('#top-topic-textarea');
	tarea.autoResize();
	tarea.text("Share your experiences");
	tarea.click(function() {
		if (jQuery(this).text() == "Share your experiences") {
			jQuery(this).text("");
			jQuery(this).next().show();
		}
	});
	
	//---
		
	loadMap(document.getElementById("map-container"), G_NORMAL_MAP);
	tripDiv = document.getElementById("tripDiv");
	guideDiv = document.getElementById("guideDiv");
	sectionDiv = document.getElementById("sectionDiv");
	

	
	
	// add pois
	for(var i=pois.length -1; i>= 0; i--) {
		addSection(pois[i]);
	}
	changePoiIcons(false);
	
	// add trips
	for(var i=trips.length -1; i>= 0; i--) {
		addTrip(trips[i]);
	}


	// add guides
	for(var i=guides.length -1; i>= 0; i--) {
		addGuide(guides[i]);
	}
	


	mapBounds.extend(new GLatLng(minLat, minLon));
	mapBounds.extend(new GLatLng(maxLat, maxLon));
	
	
	map.setCenter(mapBounds.getCenter(), map.getBoundsZoomLevel(mapBounds));
	
	if (startPoint) 
		loadDirectionsMap();
	
	// Weather Foreacst
	if (minLat && maxLat && minLon && maxLon) {
		var centerLat = (minLat + maxLat) / 2;
		var centerLon = (minLon + maxLon) / 2;
		loadForecast(centerLat, centerLon);	
	} else {
		hideForecast();
	}
	
	try {
		loadFacebookLike(pageUrl);
	} catch (e) {} // no FacebookLike to load
	//---
});





function destinationReviewed(url) {
	GB_hide();
	
	var container = document.getElementById("review-container");
	if(container) {
		container.innerHTML = '<h2 class="big-title bold">Thank you for your review!</h2>';
	} else {
		location.reload();
	}
}

function removeDestinationReview(reviewId, containerId) {
	if(!containerId) containerId = "review-"+reviewId;
	
	jQuery.ajax({
		url: "/destinations/remove-review",
		dataType: "json",
		data: "review_id="+reviewId,
		success: function(msg){
			if(msg.success == 1) {
				jQuery("#" + containerId).hide();
			} else {
				alert("An error occured, please try again later");
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
		}
	});
}


function favoriteCallback(data) {
	jQuery("#unfavorite-container").show();
	jQuery("#favorite-container").hide();
}

function favoriteDestination(url) {
	_favoriteDestination(url, function() { favoriteCallback();}, null);	
}

function unfavoriteCallback(data) {
	jQuery("#unfavorite-container").hide();
	jQuery("#favorite-container").show();
}

function unfavoriteDestination(url) {
	_unfavoriteDestination(url, function() { unfavoriteCallback();}, null);	
}
