$(document).ready(function() {
	$("input.save-button").each(init_save_button);
	
	$("input[type=submit]").click(function() {
		$(".multiplecombobox option").attr("selected", "selected");
	});

	$("a.autosubmit").each(function() {
		$(this).click(function() { 
			$(this).parent("form").submit();
			return false;
		})
	});

	$(".pickable").each(function() {
		$(this).click(function() {
			var from = $(this).attr("data-from").replace(/(\[|\])/g, "\\$1");
			var to = $(this).attr("data-target").replace(/(\[|\])/g, "\\$1");
			var values = eval($(this).attr("data-values"));

			for (i in values) {
				var id = values[i];
				$("#" + from).children().each(function() {
					if ($(this).val() == id) {
						if ($("#" + to).text() == "--") {
							$("#" + to).children().remove();
						}
						$("#" + to).append(this);
					}
				});
			}
			$("#" + to).sortOptions();
			$("#" + from).restoreOption();
		})
	});
	
	$(".maskable").each(function() {
		$(this).click(function() {
			$("#"+$(this).attr("data-target")).toggleMask();
		})
	});

	$(".multiplecombobox option").each(function() {
		$(this).dblclick(function() {
			var from = $(this).parent().attr("id").replace(/(\[|\])/g, "\\$1");
			var to = $(this).parent().attr("data-target").replace(/(\[|\])/g, "\\$1");

			if ($("#" + to).text() == "--") {
				$("#" + to).children().remove();
			}

			$("#" + to).append(this);
			$("#" + to).sortOptions();
			$("#" + from).restoreOption();
		})
	});

	$(".multiplecombobox_all").each(function() {
		$(this).click(function() {
			var from = $(this).attr("data-from").replace(/(\[|\])/g, "\\$1");
			var to = $(this).attr("data-target").replace(/(\[|\])/g, "\\$1");

			if ($("#" + from).text() == "--") {
				return false;
			}

			if ($("#" + to).text() == "--") {
				$("#" + to).children().remove();
			}

			$("#" + to).append($("#" + from).children());
			$("#" + to).sortOptions();
			$("#" + from).restoreOption();

			return false;
		})
	});

	$(".multiplecombobox_button").each(function() {
		$(this).click(function() {
			var from = $(this).attr("data-from").replace(/(\[|\])/g, "\\$1");
			var to = $(this).attr("data-target").replace(/(\[|\])/g, "\\$1");

			if ($("#" + to).text() == "--") {
				$("#" + to).children().remove();
			}
			$("#" + to).append($("#" + from).children(":selected"));
			$("#" + to).sortOptions();
			$("#" + from).restoreOption();
		})
	});

	$(".row-clone").change(Row_clone);
});

jQuery.fn.toggleMask = function() {
	this.each(function() {
		if ($(this).css("visibility") == "hidden") {
			$(this).css("visibility", "visible");
			$(this).css("display", "block");
		} else {
			$(this).css("visibility", "hidden");
			$(this).css("display", "none");
		}
	})

	return this;
}

jQuery.fn.sortOptions = function() {
	this.each(function() {
		if(this.nodeName.toLowerCase() != "select") return;

		var optionsLength = this.options.length;
		var sortArray = [];

		for(var i = 0; i < optionsLength; i++) {
			sortArray[i] = {
				value: this.options[i].value,
				text: this.options[i].text
			};
		}

		sortArray.sort(function(option1, option2) {
			opt1 = option1.text.toLowerCase();
			opt2 = option2.text.toLowerCase();

			if(opt1 == opt2) {
				return 0;
			} else {
				return opt1 < opt2 ? -1 : 1;
			}
		});
		
		for(var i = 0; i<optionsLength; i++) {
			this.options[i].value = sortArray[i].value;
			this.options[i].text = sortArray[i].text;
		}
	})

	return this;
}

jQuery.fn.restoreOption = function() {
	if (this.children().length == 0) {
		var option = new Option();
		option.value = "--";
		option.text = "--";
		this.append(option);
	}

	return this;
}

var row_clone_index = 1;
function Row_clone() {
	$(this).parent().parent().after($(this).parent().parent().clone());
	$(this).removeClass("row-clone");

	var html = $(this).parent().parent().next().html().replace(/new/g, "new" + row_clone_index);
	$(this).parent().parent().next().html(html);

	$(this).parent().parent().next().find("select,input").each(function() {
		if (!$(this).hasClass("preserved")) {
			if (!$(this).attr("id")) {
				$(this).val("");
			} else {
				match = $(this).attr("id").match(/((start|stop)holiday_new)[0-9]*_(calendar(month|year))/);
				if (match) {
					id = match[1] + "_" + match[3];
					$(this).val($("#" + id).val());
				} else {
					if ($(this).attr("type") == "checkbox") {
						$(this).removeAttr("checked");
					} else if ($(this).attr("id").match(/(start|stop)holiday_new.*/)) {
						$(this).val("");
					} else if ($(this).attr("id").search(/day/) == -1) {
						$(this).val("");
					}
				}
			}
		}
	});
	
	$(this).unbind("change", Row_clone);
	$(".row-clone").each(function() {
		$(this).change(Row_clone);
	})

	row_clone_index = parseInt(row_clone_index) + 1;
};

function init_save_button(index, element) {
	$(element).css("display","none");

	var span = document.createElement("span");
	$(span).addClass("save-button");

	var link = document.createElement("a");
	$(link).css("cursor", "pointer");
	$(link).text($(element).val());
	$(link).click(function() {
		$(element).click();
	});

	$(span).prepend(link);
	$(element).after(span);
}

function absence_validation_check_all(value) {
	$("form#form_holiday input[type='checkbox']").each(
		function init_save_button(index, el) {
			if (el.id.indexOf("check_") !=-1) {
				$(el).attr("checked", value ? "checked" : "");
			}
		}
	);
}
