$(document).ready(function() {
	$("input.save-button").each(init_save_button);

	$(".detailed").tableHover({colClass:"hover"});

	$("input[type=submit]").click(function() {
		$(".multiplecombobox option").attr("selected", "selected");
	});
	
	$("form.form_area a.add-all").click(function() {
		$("form.form_area input[type=checkbox]").attr('checked', "checked");
		return false;
	});

	$("form.form_area a.remove-all").click(function() {
		$("form.form_area input[type=checkbox]").attr('checked', "");
		return false;
	});

	$("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() {
			targets = $(this).attr("data-target").split(",");
			for (i in targets) {
				$("#" + targets[i]).toggle();
			}
		})
	});

	$(".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();
		})
	});

	var select_ajax_timer;
	var select_ajax_input;
	
	var input_ajax_timer;
	var input_ajax_input;

	window.select_ajax_get = function() {
		var input = select_ajax_input;
		$('#'+input.attr("id")+'-dynamic').empty();
		input.addClass("waiting");
		$.getJSON(
			input.attr("data-url"),
			{ method: "json", action: "search", name: input.val(), format: input.attr("data-format") },
			function(data){
				if ($('#'+input.attr("id")+'-dynamic').is(":empty")) {
					$('#'+input.attr("id")+'-dynamic').hide();
				}
				for (i in data) {
					var checkbox = $("<div><input type=\"checkbox\" value=\""+i+"\" name=\""+input.attr("data-name")+"\" /></div>");
					checkbox.append(data[i]);
					$('#'+input.attr("id")+'-dynamic').append(checkbox);
					if ($('#'+input.attr("id")+'-dynamic').is(":visible") == false) {
						$('#'+input.attr("id")+'-dynamic').show();
					}
					checkbox.click(function() {
						if ($(this).parent().attr("id") == input.attr("id")+'-dynamic') {
							$(this).detach();
							$(this).children("input").attr('checked', "checked");
							$('#'+input.attr("id")+'-static').prepend($(this));
							$('#'+input.attr("id")+'-dynamic').hide();
							$('#'+input.attr("id")+'-dynamic').empty();
						}
					});
				}
				input.removeClass("waiting");
			}
		);
	}

	$("input.select-ajax").keyup(function(event) {
		if (event.keyCode == 27) {
			$(this).parent().find(".select-ajax-dynamic").hide();
		}
		else {
			select_ajax_input = $(this);
			clearTimeout(select_ajax_timer);
			select_ajax_timer = setTimeout("select_ajax_get()", 200);
		}
	});

	$("*").click(function () {
		$(this).find(".select-ajax-dynamic").hide();
		$(this).find(".input-ajax-dynamic").hide();
	});

	window.input_ajax_get = function() {
		var input = input_ajax_input;
		$('#'+input.attr("id")+'-dynamic').empty();
		$('#'+input.attr("id")+'-dynamic').show();
		input.addClass("waiting");
		$.getJSON(
			input.attr("data-url"),
			{ method: "json", action: "search", name: input.val(), format: input.attr("data-format") },
			function(data){
				for (i in data) {
					var checkbox = $("<div><input type=\"checkbox\" value=\""+i+"\" name=\""+input.attr("data-name")+"\" class=\"input-ajax-checkbox\" /></div>");
					checkbox.append(data[i]);
					$('#'+input.attr("id")+'-dynamic').append(checkbox);
					checkbox.click(function() {
						if ($(this).parent().attr("id") == input.attr("id")+'-dynamic') {
							$(this).detach();
							$(this).children("input").attr('checked', "checked");
							$('#'+input.attr("id")+'-static').prepend($(this));
							$('#'+input.attr("id")+'-dynamic').empty();
							input.hide();
						}
					});
				}
				input.removeClass("waiting");
			}
		);
	}

	$("input.input-ajax").keyup(function(event) {
		if (event.keyCode == 27) {
			$(this).parent().find(".input-ajax-dynamic").hide();
		}
		else {
			input_ajax_input = $(this);
			clearTimeout(input_ajax_timer);
			input_ajax_timer = setTimeout("input_ajax_get()", 200);
		}
	});
	
	$("input.input-ajax-checkbox").click(function() {
		if ($(this).is(':checked') == false) {
			$(this).hide();
			$(this).parent().parent().parent().children("input:first").show();
			$(this).parent().empty();
		} else {
			$(this).parent().parent().parent().find(".input-ajax-dynamic").hide();
		}
	});

	$("input.input-ajax-checkbox").live("click", function() {
		if ($(this).is(':checked') == false) {
			$(this).hide();
			$(this).parent().parent().parent().children("input:first").show();
			$(this).parent().empty();
		} else {
			$(this).parent().parent().parent().find(".input-ajax-dynamic").hide();
		}
	});

	$(".row-clone").live("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("");
					}
				}
			}
		}
	});
	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" : "");
			}
		}
	);
}

