
jQuery(function($){
	function make_button_html(ref, href, value) {
		var html = [
			"<div class=\"", ref,"\">",
			"<div class=\"btnleft\"></div>",
			"<div class=\"btncontent\">",
			"<a class=\"js-form-submit\" href=\"", href, "\">", value, "</a>",
			"</div>",
			"<div class=\"btnright\"></div>", "</div>"
		];
		return html.join("");
	}

	$("input.submit_button").each(function(button) {
		var $this = $(this);
		var ref = $this.attr("ref");
		var href = $this.attr("name");
		var value = $this.attr("value");
		$this.replaceWith(make_button_html(ref, href, value));
	});
});

jQuery(function($){
	$("a.js-form-submit").click(function(evt){
		var $form, href;
		evt.preventDefault();
		href = $(evt.target).attr("href");
		$form = $(this).parents("form");
		$form.attr("action", href);
		$form.submit();
	});
});

/*
jQuery(function($){
	$("#date\\_\\_date").datepicker({ yearRange: '-50:0' });
});

jQuery(function($) {
	$("#date__date").focus(function(evt){
		if ($(evt.target).val() === "mm/dd/yyyy")
			$(evt.target).val("");
	}).blur(function(evt){
		if ($(evt.target).val().length === 0)
			$(evt.target).val("mm/dd/yyyy");
	});
});
*/

function make_map_popup2(feature) {
	var html = [
		"<div style='font-size:.8em'>",
		"Feature: ", feature.id,
		"<br />",
		"Area: ", feature.geometry.getArea(),
		"</div>"
	];
	return html.join("");
}

function require_scripts(names, fn) {
	function loaded(name) {
		for (var i=0; i<names.length; i++) {
			if (name == names[i]) {
				names.splice(i, 1);
				break;
			}
		}
		if (names.length == 0) {
			fn();
		}
		return false;
	}

	function loader(name) {
		return function() {
			loaded(name);
			return false;
		};
	}

	return loader;
}

jQuery(function($) {
	var map_div = "map_div";
	if ($("#" + map_div).length > 0)
		map = new LRC.Map(map_div);

	//var init_map_guard = require_scripts(["googlemaps", "openlayers", "x"], init_map);
	//$("a#show_map").click(function(event) {
	//	event.preventDefault();
	//	//console.debug("HEY");
	//	$.getScript(
	//		"http://openlayers.org/api/OpenLayers.js",
	//		init_map_guard("openlayers"));
	//	$.getScript(
	//		"http://maps.google.com/maps?file=api&v=2&key={% google_api_key %}",
	//		init_map_guard("googlemaps"));
	//});
});

/*jQuery(function($) {
	$("textarea.rte").rte({
    	content_css_url: "/assets/ext/js/jquery-rte-light/rte.css",
    	media_url: "/assets/ext/js/jquery-rte-light/"
	});
});*/

/*
// Add overlays to text input elements
jQuery(function($) {
	var form_text_overlays = {
		title: "Title",
		url: "Permalink",
		}
	var overlays = {};

	function make_field_text_overlay($elem, text) {
		var $overlay = $('<div class="field_text_overlay"></div>');
		$overlay.css({
			top: $elem.position().top + 2, left: $elem.position().left + 6,
			width: $elem.innerWidth(), height: $elem.innerHeight()
			}).html(text);
		$overlay.click(function() { $elem.focus(); });
		return $overlay;
	}

	$("#article_create_form input").focus(function() {
		var $this = $(this);
		$this.removeClass("default_value");
		if (overlays[$this.attr("id")])
			overlays[$this.attr("id")].hide();
	}).blur(function() {
		var $this = $(this);
		$this.addClass("default_value");
		if ($this.val() == "" && overlays[$this.attr("id")])
			overlays[$this.attr("id")].show();
	}).each(function() {
		var $this = $(this);
		$overlay = make_field_text_overlay($this, form_text_overlays[$this.attr("id")]);
		$overlay.appendTo("body").hide();
		overlays[$this.attr("id")] = $overlay;
		if ($this.val() == "" && form_text_overlays[$this.attr("id")] != "") {
			$this.addClass("default_value");
			$overlay.show();
		}
	})

});

*/


// Create-or-edit review

// Entities
jQuery(function($){
  $("a[id^='entities__created__']").click(function(evt){
    var aid, aname, form_path;
    evt.preventDefault();
    form_path = $(evt.target).attr("id").substr(0,20);
    aid = $(evt.target).attr("id").substr(27);
    aname = $(evt.target).parent().parent().children(":first-child").children('a').text();
    $("#" + form_path + "__nom").val(aname);
    $("#" + form_path + "__id").val(aid);
  });
});

// Event
jQuery(function($){
	//$("#{{ form.date.date.get_path_id }}").datepicker({ yearRange: '-50:0' });
	//$("#{{ form.date.date.get_path_id }}").focus(function(evt){
	$("#date__date").datepicker({ yearRange: '-50:0' });
	$("#date__date").focus(function(evt){
		if ($(evt.target).val() === "mm/dd/yyyy")
			$(evt.target).val("");
	}).blur(function(evt){
		if ($(evt.target).val().length === 0)
			$(evt.target).val("mm/dd/yyyy");
	});
	
	$("#date__end_date").datepicker({ yearRange: '-50:0' });
	$("#date__end_date").focus(function(evt){
		if ($(evt.target).val() === "mm/dd/yyyy")
			$(evt.target).val("");
	}).blur(function(evt){
		if ($(evt.target).val().length === 0)
			$(evt.target).val("mm/dd/yyyy");
	});
});

