// Upload window id
var uploadID = 0;

// Window sizes
photoWidth = 400;
photoHeight = 500;

// Opens a new photo upload window
function openUploadWindow(e, classType) {

	width = photoWidth;
	height = photoHeight;

	// Open window
	window.open($(e).attr("href"), "upload_" + uploadID, "top=200,left=300,width=" + width + ",height=" + height + ",resizable=yes,toolbar=no,scrollbars=yes,status=no,menubar=no,directories=no");

	// Increase upload id
	uploadID = uploadID + 1;

	return false;
}

// Deletes a media item
function deleteItem(e) {

	if (confirm("Are you sure you wanna delete this?")) {

		// Set error handler
		$(e).ajaxError(function(event, request, settings){
		   alert("Something went wrong while trying to delete the item. Try again later.");
		 });

		// Do AJAX request
		itemDeleteURL = $(e).attr("href") + "?format=json";
		$.getJSON(itemDeleteURL, function(results){

			if (results.status) {
				if (results.status[0].message == "success") {
					$(e).parent().fadeOut("slow", function(){
						$(this).remove();
					});
				}
				else alert(results.status[0].message);
			}

		});

	}

	return false;

}

// Toggle start/end time windows
function toggleTimes() {

	if ($("#dates_allday").is(":checked")) {
		$("#dates_start_time_container, #dates_end_time_container").hide();
	}
	else {
		$("#dates_start_time_container, #dates_end_time_container").show();
	}

}

$(document).ready(function(){

	// Load new localities on country change
	$(".country").change(function(){

		// Get locality dropdown
		localitySelect = $(".locality");
		countrySelect = $(".country");

		// Disable and get country code
		$(".country").attr("disabled","disabled");
		$(".locality").attr("disabled","disabled");

		var countryCode = $(".country option:selected").val();
		var countryName = $(".country option:selected").text();
		$(".country option:selected").text("One sec... fetching geographic info.");

		// Enable dropdowns on complete
		$(".country").ajaxComplete(function(request, settings){
			$(".country option:selected").text(countryName);
			$(this).attr("disabled","");
		});

		// Do AJAX request
		localityURL = "/api/getlocalities/?format=json&country_code=" + countryCode;
		$.getJSON(localityURL, function(results){

	   		// Delete old localities
	   		$("select.locality").empty();

	   		var localityType = "Locality";

	   		if (results.localities) {
	   			$(".locality_holder").show();

	   			// Check for different types
	   			var groups = true;
	   			if (results.localities[0].type == results.localities[results.localities.length - 1].type) {
	   				groups = false;
	   				localityType = results.localities[0].type;
	   			}

	   			var currentType = "";
	   			// Enter HTML
		   		for (var i = 0; i < results.localities.length; i++) {
		   			thisLocality = results.localities[i];

		   			if (groups && thisLocality.type != currentType) {
		   				$("select.locality").append("<optgroup label=\"" + thisLocality.type + "\" id=\"locality_" + thisLocality.type + "\"></optgroup>");
		   				currentType = thisLocality.type;
		   			}

		   			if (groups) $("#locality_" + thisLocality.type).append("<option value=\"" + thisLocality.id + "\">" + thisLocality.name + "</option>");
		   			else $("select.locality").append("<option value=\"" + thisLocality.id + "\">" + thisLocality.name + "</option>");
		   		}

		   		// Enable localities
		   		$("select.locality").attr("disabled","");

		   	}
		   	else {
		   		$(".locality_holder").hide();
		   		$("select.locality").append("<option value=\"none\">No localities available</option>");
		   	}

		   	// Set locality Type
		   	$(".locality").parent().find("label").html(localityType + ":");

		 });
	});

	$("#dates_allday").click(function() {

		toggleTimes();

	});
	toggleTimes();

	// Upload photo window
	$(".photos li a.edit").click(function() {

		return openUploadWindow(this, "photo");

	});

});