// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// autosave
jQuery.fn.extend({
	autosave: function() { 
		return this.each(function() {			
			form = jQuery(this);
			
			jQuery.fn.extend({
				start: function() { 
					this.delayedObserver(0.5, function(element, value) {
						if (form.attr("timer") == undefined) {
							form.attr("timer", window.setTimeout(form.save, 10000));
						}
					});
				},
				save: function() {
					if (form.attr("timer")) { 
						window.clearTimeout(form.attr("timer"));
						form.removeAttr("timer");
					}
					jQuery.ajax({ url: form.attr("action"), data: form.serialize(), dataType: "script", type: "post" });
				}
			});
			form.start();	
		});
	} 
}); 

// includeOther for select with a new value
// better layout for date select
// tabify entry form and add tab to section menu
jQuery.fn.extend({
	includeOther:function() {
		return this.each(function() {
			jQuery(this).change(function() {
				other_text_field = jQuery("#"+this.id+"_other");
				if (jQuery(this).val() == "other") {
					other_text_field.show();
				} else {
					other_text_field.hide();
				}
			}).triggerHandler('change');
		});
	},
	dateSelect: function() {
		labels = ["Year", "Month", "Day"];
		return this.each(function() {
			jQuery(this).find("select").each(function(i) {
				jQuery(this).wrap("<label class=\"blockLabel\"></label>").parent().prepend(labels[i]);
			});
		});
	},
	popup: function() {
		return this.each(function() {
			element = jQuery(this);
			element.click(function() {
				jQuery("#" + element.attr("rel")).toggle();
				jQuery("#name:visible").focus();
			});
		});
	}
});


jQuery(document).ready(function() {
	jQuery("form.autosave").autosave();
	
	jQuery("#section_nav").tabify();
	jQuery(".tab").each(function () {
		var tab = jQuery(this);
		if (tab.find(".errorField").length > 0 || tab.find(".fieldWithErrors").length > 0) {
			jQuery("a[href=#"+tab.attr("id")+"-tab]").parent().addClass("hasErrors");
		}
	});
	
	jQuery("#af #errorMsg h3").click(function(){ jQuery(this).next("ul").toggle()});
	jQuery(".uniForm .errorField").parents(".ctrlHolder").addClass("error");
	jQuery(".uniForm .multiField > .errorField").each(function() { jQuery(this).prependTo(jQuery(this).parents(".ctrlHolder")) });
	jQuery(".uniForm .fieldWithErrors").parents(".ctrlHolder").addClass("error");
	jQuery(".includeOther select").includeOther();
	jQuery(".dateselect").dateSelect();
	jQuery(".popup").popup();
	jQuery(".printform dt").each(function() {
		jQuery(this).before("<div style=\"clear:both\"></div>");
	});
});


$(function() {
	$(".show_more_design").click(function () {
		hidden = $(this).parent().find('.ctrlHolder:hidden');
		if (hidden.size() > 0) hidden.each(function(i) { if (i < 2) { $(this).show() } });
		if (hidden.size() <= 2) $(this).hide();
	});
})

$(function() {
	$(".show_more_proposal").click(function () {
		hidden = $(this).parent().find('.ctrlHolder:hidden');
		if (hidden.size() > 0) hidden.each(function(i) { if (i < 2) { $(this).show() } });
		if (hidden.size() <= 2) $(this).hide();	});
})