// Common UI functions

EveryBlockUI = {
	map_sizes: ["smallermap", "widermap", "tallermap", "shortermap"],
	class_names: ["smallmap", "widemap", "bigmap", "widemap"],
	resize_map: function(class_name) {
		// Assumes a global 'map' variable has been defined.
		if (typeof map !== "undefined") {
			$j("#mapcontainer").attr("class", class_name);
			map.updateSize();
			map.zoomToClusters();
		}
	},
	init_map_resizing: function() {
		var ui = this;
		// Resize from the URL
		if (window.location.hash) {
			var hash = window.location.hash.slice(1);
			var index = $j.inArray(hash, ui.map_sizes);
			if (index != -1) {
				ui.resize_map(ui.class_names[index]);
			}
		}
		// Click to resize map
		$j('ul.mapcontrols a').click(function() {
			var map_size = $j(this).attr('class');
			var index = $j.inArray(map_size, ui.map_sizes);
			ui.resize_map(ui.class_names[index]);
			window.location.hash = map_size;
			// Clear any location highlighting
			$j("span.location").removeClass("selected");
			return false;
		});
	}
};

function feedback() {
	var msg = $j('#feedbackmessage').val();
	if (msg) {
		$j('#feedbackstatus').html('Sending...');
		$j('#feedbackbutton').attr('disabled', 'disabled').addClass('disabled').removeClass('default').blur();
		$j.ajax({
			type: "POST", url: "/send-feedback/", data: {message:msg, email: $j('#feedbackemail').val()},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$j('#feedbackstatus').html("Whoops! We're experiencing some technical hiccups. If you're not too frustrated by this, please e-mail us at feedback@everyblock.com instead.");
			},
			success: function(data, textStatus) {
				$j('#feedbackstatus').html('Success! Thanks for taking the time to write.');
			}
		});
	}
}
$j(function() {
    $j('body').click(function(event) {
	if (!$j(event.target).is('.dropdown, .navitem'))
	    $j('.dropdown, .navitem').removeClass('selected');
    });
    $j.cookie('h', 't', {'path': '/'}); // Set the cookie to hide ads

    // Footer feedback form
    var msg = $j("#feedbackmessage");
    var placeholder = msg.val();
    msg.click(function() {
	if ($j(this).val() == placeholder) {
	    $j(this).removeClass('placeholder').val('');
	    $j('#feedbacksubmit').show();
	};
    });
    $j("#footerfeedback form").submit(function() {
	feedback();
	return false;
    });
});
function toggle_nav(name) {
	if ($j('#gn-' + name).hasClass('selected'))
		$j('.dropdown, .navitem').removeClass('selected');
	else {
		$j('.dropdown, .navitem').removeClass('selected');
		$j('#ul-' + name + ', #gn-' + name).addClass('selected');
	}
}
function show_saved_places() {
	if ($j('#ul-saved li.replaceme').length == 0) return;
	$j.getJSON('/accounts/api/saved-places/', function(data) {
		if (data.length) {
			var html = '';
			for (var i=0; i < data.length; i++) {
				if (data[i]['nickname'] != '') {
					var name = data[i]['nickname'] + ' (' + data[i]['name'] + ')';
				} else {
					var name = data[i]['name'];
				}
				html += '<li><a href="' + data[i]['url'] + '">' + name + '</a></li>';
			}
			html += '<li><a href="/accounts/dashboard/">Edit your saved places&hellip;</a></li>';
			$j('#ul-saved').html(html);
		}
		else {
			$j('#ul-saved').html('<li><a href="/accounts/dashboard/">No saved places yet&hellip;</a></li>');
		}
	});
}
