function init(CityID) {
    $('#frmSubmitMap').submit(function() { 
        $(this).ajaxSubmit({
				dataType: "json",
				beforeSubmit: submitMapRequest, 
      	  		success: submitMapResponse
			}); 
    	return false; 
   	 }); 
    
	$("#submitMapDialog").dialog({
		draggable: false,
		autoOpen: false,
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		resizable: false,
		height: 300,
		width: 435,
		buttons: {
			Cancel: function() {
				$(this).dialog("close");
			},
			Submit: function() {
				$("#frmSubmitMap").submit();
			}
		}
	});
	
	$("#alertDialog").dialog({
		draggable: false,
		autoOpen: false,
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		resizable: false,
		height: 300,
		width: 435,
		buttons: {
			Ok: function() {
				$(this).dialog("close");
			}
		}
	});
	
	//$("#ddlCity").flexselect();
	$("#txtLongName").maxlength({ "feedback" : "#txtLongNameCharsLeft" });
 	$("#SubmitMapLink").show();
}

function RefreshMapList(CityID)
{
  	var flobu = new flower_bubble ({
  		base_obj: $( "#maplistresults" ),
  		base_dir: '/theme/images',
  		background: { css: 'white', opacity: 0.78 },
  		bubble: { image: 'bubble.png', width: 130, height: 98 },
  		flower: { image: 'flower.gif', width: 32, height: 32 },
  		block_mode: 'base_obj'
  	});
  	
	flobu.enable();
	$.getJSON("/system/ajax/maplist.asp?CityID=" + CityID, function(json){
 		var data = { 'maps': json };
 		$("#maplistresults").setTemplateURL("/shared/templates/maplist.tpl");
		$("#maplistresults").processTemplate(data);
		$("#maplistresults").height("auto");
		
		$("input.star").rating({
			split: 4
		});
		
		flobu.disable();
 	});
}
	 
function OpenSubmitMapRateThisDialog(MapID, LongName, CityMapID)
{ 	 	
	if ($("#submitMapRateThisDialog_" + MapID).length == 0) {
		var data = { 
			'MapID': MapID,
			'LongName': LongName
		};
	
		$("#hiddencontent").append("<div id=\"submitMapRateThisDialog_" + MapID + "\" title=\"Rate this!\"></div>");
	
		$("#submitMapRateThisDialog_" + MapID).dialog({
			draggable: false,
			autoOpen: false,
			closeOnEscape: true,
			bgiframe: true,
			modal: true,
			resizable: false,
			height: 300,
			width: 435,
			buttons: {
				Cancel: function() {
					$(this).dialog("close");
				},
				Submit: function() {
					$("#frmSubmitMapRateThis_" + MapID).submit();
				}
			}
		});
	
 		$("#submitMapRateThisDialog_" + MapID).setTemplateURL("/shared/templates/mapratethis.tpl");
		$("#submitMapRateThisDialog_" + MapID).processTemplate(data);
		
		$(".ratethisstar_" + MapID).rating({
			split: 4
		});
	
		$('#frmSubmitMapRateThis_' + MapID).submit(function() { 
    		$(this).ajaxSubmit({
    			dataType: "json",
				beforeSubmit: function(formData, jqForm, options) {
					var message = "";
					for (var i=0; i < formData.length; i++) {
						if (!formData[i].value) {
							if (formData[i].name == "ratethismap_"+ MapID) {
        						message += "<li>You must select a rating before you can submit</li>";
        					}
						}
					}
					if (message.length > 0) {
   						OpenAlertDialog(
   							"Whoops...", 
    						"You must correct the following to continue:<br /><ul>" + message + "</ul>",
    						"alert"
    					);
    					return false;
   					}
				}, 
      		  	success: function(data)  { 
	 				if (data.result <= 0) {
    					OpenAlertDialog(
    						"Whoops...", 
    						data.message,
    						"alert"
    					);
	 				}
					else if (data.message.length > 0) {
    					OpenAlertDialog(
    						"Thank you", 
    						data.message,
    						"check",
    						function() { $('#submitMapRateThisDialog_' + MapID).dialog("close"); }
    					);
	 				}
	 				else {
	 					$('#submitMapRateThisDialog_' + MapID).dialog("close");
	 				}
				}
			});
			
			var buildUrl = "<a href='javascript:void(0)' onclick='RefreshMapList(" + CityMapID + ")'><span style='display: inline-block; position: relative; top: 2px;' class='ui-icon ui-icon-refresh'></span> Update List</a>";
			document.getElementById('MapRateThisDialog' + MapID).innerHTML = buildUrl;
			
			
    		return false; 
   		 }); 
	}
	
	$("#submitMapRateThisDialog_" + MapID).dialog("open");
}
	 
function OpenSubmitMapDialog()
{
	 $("#txtLongName").val("");
	 $("#txtUrl").val("http://");
	 //$("#txtEmail").val("");

	 $('#txtLongNameCharsLeft').html($("#txtLongName").attr("maxlength"));	 	 	
	 $("#submitMapDialog").dialog("open");
	 $("#ddlCity_flexselect").blur();
	 $("#txtLongName").focus();
}
	 
function submitMapRequest(formData, jqForm, options) {
	 var message = "";
	for (var i=0; i < formData.length; i++) {
        if (!formData[i].value) {
        	switch(formData[i].name)
        	{
        		case "ddlCity":
        			message += "<li>You must select a city</li>";
        			break;
        		case "txtLongName":
        			message += "<li>Provide a map name for your map</li>";
        			break;
        		case "txtEmail":
        			message += "<li>Provide your email address</li>";
        			break;
        		case "txtUrl":
        			message += "<li>Provide the map link (URL) to the map to view or download</li>";
        			break;
        	}
        } 
        else {
        	var EmailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        	var UrlPattern = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
        	
        	switch(formData[i].name)
        	{
        		case "txtEmail":
        			if (!EmailPattern.test(formData[i].value)) {
        		    	message += "<li>Your email address is not in a valid format</li>";
        			}
        			break;
        		case "txtUrl":
        			if (!UrlPattern.test(formData[i].value)) {
        		    	message += "<li>The map link (URL) is not in a valid format</li>";
        			}
        			break;
        	}
        }
	}
	
    if (message.length > 0) {
    	OpenAlertDialog(
    		"Whoops...", 
    		"You must correct the following to continue:<br /><ul>" + message + "</ul>",
    		"alert"
    	);
    	return false;
    }
}
	 
function submitMapResponse(data)  { 
	 if (data.result <= 0) {
    	OpenAlertDialog(
    		"Whoops...", 
    		data.message,
    		"alert"
    	);
	 }
	 else if (data.message.length > 0) {
    	OpenAlertDialog(
    		"Thank you", 
    		data.message,
    		"check",
    		function() { $("#submitMapDialog").dialog("close"); }
    	);
	 }
	 else {
	 	$("#submitMapDialog").dialog("close");
	 }
}
	 
function OpenAlertDialog(title, message, icon, onOk) {
	$("#alertMessage").html(message);
    $("#alertDialog").dialog("option", "title", title);
    		
    $("#alertDialog").dialog('option', 'buttons', { "Ok": function() {
    		if (typeof(onOk) == "function") onOk();
    		$(this).dialog("close"); 
    	}});
    		
    $("#alertIconAlert").hide();
    $("#alertIconCheck").hide();
    		
    switch(icon)
    {
    	case "alert":
    		$("#alertIconAlert").show();
    		break;
    	case "check":
    		$("#alertIconCheck").show();
    		break;
    }
    		
    $("#alertDialog").dialog("open");
}