
window.attach_placeling_place = function(json) {
    jQuery('#placeling_place_json').val( encodeURI( json ) );
    renderPlaceAdmin( );
};

window.get_placeling_json = function(json) {
    return jQuery('#placeling_place_json').val();
};


jQuery(document).ready(function(){

    jQuery("form[name=post]").validate();
    
    jQuery('form[name=post]').submit(function(){
	jQuery('form[name=post]').validate();
	
	if ( !jQuery('form[name=post]').valid() ) {
	    jQuery("textarea[name=placeling_placemark_memo]").focus();
	    return false;
	}
	return true; 
    });

    renderPlaceAdmin( );

    jQuery("a#placeling_remove_place").live('click', function(){
	jQuery("#placeling_place_json").val();
	renderPlaceAdmin( );		
	return false;
    });
	
});


function renderPlaceAdmin( ){

	if ( jQuery("#placeling_place_json").val() != ""){		
	    raw_json = unescape( jQuery("#placeling_place_json").val() );
	    
	    place =  JSON.parse( raw_json );
	    
	    var raw_place = jQuery('#placeling_place_json').val();
	    var json_place = unescape( raw_place );
	    var place = JSON.parse( json_place );		
	    
	    lat = place.lat;
	    lng = place.lng;
	    name = place.name;
	    map_url = place.map_url;
	    
	    template = getPlacelingAdminTemplate();
	    
	    jQuery("#placeling_tagged_place").html( template( place ) );
	    jQuery("#add_place .placeling_place_name").html( name );
	    jQuery("#add_place #placeling_tagged").show();
	    jQuery("#add_place #placeling_untagged").hide();
	    
	    if ( place.perspectives[0] && place.perspectives[0].mine){
		jQuery("textarea[name=placeling_placemark_memo]").val(  place.perspectives[0].memo );
	    }
	    
	    jQuery("#empty_place").hide();
	    jQuery("#placeling_tagged_place").show();
	    
	    jQuery("#placeling_placemark_memo").rules("add", {
		required: true,
		minlength: 20,
		messages: {
		    required: "Required input",
		    minlength: jQuery.format("At least {0} characters are necessary")
		}
	    });
	    
	} else {
	    jQuery("#placeling_tagged_place").html("");
	    jQuery("#add_place .placeling_place_name").html( "" );
	    jQuery("#add_place #placeling_tagged").hide();
	    jQuery("#add_place #placeling_untagged").show();
	    jQuery("#empty_place").show();
	    jQuery("#placeling_tagged_place").hide();
	}						
}

function getPlacelingAdminTemplate(){
	return _.template("<div id='place_data'>		\
			    <div class='placeling_map_image'>						\
				    <img src='<%= map_url %>'/>							\
			    </div>										\
			    <div class='placeling_place_details'>						\
				    <div style='placeling_place_name'><%= name %></div>			\
				    <div style='placeling_place_address'><%= street_address %></div>	\
				    <div style='placeling_place_city'><%= city_data %></div>			\
			    </div>										\
			    <div class='placeling_place_remove'>						\
				    <div><a href='#' id='placeling_remove_place'>remove place</a></div>	\
			    </div>										\
			</div>		\
			<div id='placeling_placemark'>					\
			    <fieldset>									\
				    <label for='placeling_placemark_memo'>Placemark Note</label>				\
				    <textarea id='placeling_placemark_memo' rows='5' cols='50' name='placeling_placemark_memo'></textarea>	\
				    <label for='placeling_placemark_photos'><input name='placeling_placemark_photos' type='checkbox' id='placeling_placemark_photos' checked='checked'>Attach Photos?</label> \
			    </fieldset>									\
			</div>		\
	");

}


@@ -2562,37 +2562,34 @@
 %09%5C%0A%09%09%09%09    %3Cdiv 
-style
+id
 ='placeling_plac
@@ -2623,37 +2623,34 @@
 %09%5C%0A%09%09%09%09    %3Cdiv 
-style
+id
 ='placeling_plac
@@ -2707,13 +2707,10 @@
 div 
-style
+id
 ='pl
@@ -2944,24 +2944,37 @@
 %09%09%3C/div%3E%09%09%5C%0A
+%09%09%09%3Chr%3E %09%09%09%5C%0A
 %09%09%09%3Cdiv id='
@@ -3029,32 +3029,63 @@
 %09%09%09%09%09%09%5C%0A%09%09%09%09    
+%3Cdiv id='placeling_memo_label'%3E
 %3Clabel for='plac
@@ -3128,16 +3128,22 @@
 %3C/label%3E
+%3C/div%3E
 %09%09%09%09%5C%0A%09%09
@@ -3250,17 +3250,16 @@
 extarea%3E
-%09
 %5C%0A%09%09%09%09  
@@ -3260,16 +3260,48 @@
 %09%09%09%09    
+%3Cdiv id='placeling_photo_label'%3E
 %3Clabel f
@@ -3461,16 +3461,22 @@
 %3C/label%3E
+%3C/div%3E
  %5C%0A%09%09%09  

add some basic templates to the display of placeling stuff