/*
 * kalendergenerator.kesto.de
 * (c) 2009 Stefan Schramm
 */

var advanced = false;

// the "c"-variable is defined at php side and contains templates, month names, options etc.

function init() {

	loadTemplates();

	populateForm();
	selectDefaults();

	$("#advanced-toggle").bind("click", toggleAdvanced)
	$("#opt-advanced").css("display", "none");
	$("#create").bind("click", function(ev) {
		$("#errormessage").remove();
		return true;
	});

	// show ui
	$("#container").css("display", "block");
}

function populateForm() {

	// remove dummy option-nodes (they are only to pass the xhtml-validation)
	$("#opt-from-y").children().remove();
	$("#opt-to-y").children().remove();
	$("#opt-from-m").children().remove();
	$("#opt-to-m").children().remove();
	$("#opt-firstweekday").children().remove();

	var now = new Date();

	// +/- 10 years
	for (var y = now.getFullYear() - 10; y <= now.getFullYear() + 10; y++) {
		$("#opt-from-y").append("<option>" + y + "</option>");
		$("#opt-to-y").append("<option>" + y + "</option>");
	}
	for (var m = 1; m <= 12; m++) {
		$("#opt-from-m").append('<option value="' + m + '">' + c.months[m] + "</option>");
		$("#opt-to-m").append('<option value="' + m + '">' + c.months[m] + "</option>");
	}

	$.each(c.weekdays, function(i, name) {
		$("#opt-firstweekday").append('<option value="' + i + '">' + name + "</option>");
	});
}

function selectDefaults() {

	$("#opt-from-m").val(c.options['opt-from-m']);
	$("#opt-from-y").val(c.options['opt-from-y']);
	$("#opt-to-m").val(c.options['opt-to-m']);
	$("#opt-to-y").val(c.options['opt-to-y']);
	$("#opt-highlightweekday input").attr("checked", false);
	$.each(c.options['opt-highlightweekday'], function(i, v) {
		$("#opt-highlightweekday input").eq((i-1)%7).attr("checked", v);
	});
	$("#opt-usercolumns").val(c.options['opt-usercolumns']);
	$("#opt-firstweekday").val(c.options['opt-firstweekday']);
	if (c.options['opt-holidays'] && c.options['opt-holidays']['germany'])
		$("#opt-holidays input").attr("checked", true);
	else
		$("#opt-holidays input").attr("checked", false);
	$('#' + c.options['tpl']).change(); // choose template
	$("#opt-language").val(c.options['opt-language']);
}

function loadTemplates() {

	// display template list
	// remove old content from list
	$("#templates").children().remove();
	// add templates with events
	$.each(c.templates, function(index, tpl) {
		tpl.div = $('<tr class="template"><td><input type="radio" name="tpl" id="' + tpl.name + '" value="' + tpl.name + '" /></td><td><label for="' + tpl.name + '">' + tpl.description + '</label></td></tr>').appendTo($("#templates"));
		tpl.div.bind("mouseover", tpl, function(ev) {
			ev.data.div.addClass("template-selected");
			$("#template-preview").css("background-image", "url(tpl/" + ev.data.name + ".png)");
		} );
		tpl.div.bind("mouseout", function() {
			var tplSelected = $("#templates input:radio:checked").val();
			$("#templates .template-selected").removeClass("template-selected");
			$('#' + tplSelected).parents("tr.template").addClass("template-selected");
			$("#template-preview").css("background-image", "url(tpl/" + tplSelected  + ".png)");
		});
		tpl.div.find('input').bind("change", tpl, chooseTemplate);
	});
}

function chooseTemplate(ev) {

	var tpl = ev.data;

	tpl.div.find('input').attr('checked', 'checked');

	$("#templates .template-selected").removeClass("template-selected");
	tpl.div.addClass("template-selected");

	// first hide all options then show all options for selected template
	$("#opt .opt").each(function(index, item) {
		disableOpt(item);
	});
	$.each(tpl.options, function(index, item) {
		enableOpt($('#' + item));
	});
	
	$("#template-preview").css("background-image", "url(tpl/" + tpl.name + ".png)");
	
	return false;
}

function disableOpt(opt) {

	if (opt == null || $(opt).attr("id") == "opt") {
		return;
	}
	$(opt).addClass("disabled")
	disableOpt($(opt).parent());
}

function enableOpt(opt) {

	// enable options recursively
	if (opt == null || ! opt.hasClass("disabled"))
		return;
	opt.removeClass("disabled");
	enableOpt(opt.parent());
}

function toggleAdvanced() {

	if (advanced) {
		$("#opt-advanced").css("display", "none");
		$("#advanced-toggle a .dots").html("...");
		advanced = false;
	} else {
		$("#opt-advanced").css("display", "block");
		$("#advanced-toggle a .dots").html("");
		advanced = true;
	}
	return false;
}


