$.ajaxSetup({
	async:false,
	traditional:false
	/*contentType:"multipart/form-data"*/
});

var urlAppend = function (url, appUrl) {
	var sign = "?";
	if(url.indexOf("?") != -1)  sign = "&";
	return url + sign + appUrl;
};

var mkPostString = function (data) {
	var postString = "";
	var i;
	var value;
	for(i in data) {
		value = data[i].value;
		// zeichen @*/+  werden nicht codiert.

		value = escape(value);
		value = value.replace(/\+/gi, '%2B');
		value = value.replace(/\@/gi, '%40');
		value = value.replace(/\//gi, '%2F');

		postString+= data[i].name+ "=" + value;
		if(data.length-1 == i)
			postString+="";
		else
			postString+="&";
	}

	return postString;
};

var confirmDelete 	= function() {return confirm("Willst Du den Eintrag unwiederruflich löschen ?");};
var dateSelector 	= function() {
	attrib = $(this).attr("date_element");
	var options1 = {
		dateFormat: 'yymmdd',
		onSelect:function(dateText, inst) {

			$('#'+attrib+"_d").val(dateText.substr(6,2));
			$('#'+attrib+"_m").val(dateText.substr(4,2));
			$('#'+attrib+"_y").val(dateText.substr(0,4));

			if($('#'+attrib+"_h")) $('#'+attrib+"_h").val("00");
			if($('#'+attrib+"_i")) $('#'+attrib+"_i").val("00");


			$(this).datepicker('hide');
			return false;
		}
	};
	var options2 = $.datepicker.regional['de'];
	var options = $.extend({}, options2, options1);
	$(this).datepicker(options);
	$(this).datepicker('show');
	return false;
};

var templateContainerisLoaded = false;
var loadTemplates = function() {
	if(templateContainerisLoaded) {
		$("#templates_container").html("").hide();
		templateContainerisLoaded = false;
		return false;
	} else {
		var html = '<p style="background-color:#efefef; border:1px solid black; width:400px; height:300px;overflow:auto; padding:2px;">';
		$.getJSON(BASE_DIR + 'admin/ajax.php?action=get_templates', function(data) {
			$(data).each(function(index) {
				html += '<a href="#" onClick="selectTemplate('+data[index].id+');">'+ data[index].subject +'</a><br>';
			});
		});
		html += '</p>'
		$("#templates_container").html(html).show();
		templateContainerisLoaded = true;
	}
};

var selectTemplate = function(id_template) {
	$.getJSON(BASE_DIR + 'admin/ajax.php?action=get_template&id_template='+ id_template +'', function(data) {

		$("#subject").val(data['subject']);
		$("#category").val(data['category']);
		$("#id_responsible").val(data['id_responsible']);
		$("#id_project").val(data['id_project']);
		$("#duration").val(data['duration']);
		$("#status").val(data['status']);
		$("#priority").val(data['priority']);
		$("#content").val(data['content']);
		$("#responsetext").val(data['responsetext']);

		$("#deadline_d").val(data['deadline'].substr(8,2));
		$("#deadline_m").val(data['deadline'].substr(5,2));
		$("#deadline_y").val(data['deadline'].substr(0,4));

		if($("#deadline_h")) $("#deadline_h").val("00");
		if($("#deadline_i")) $("#deadline_i").val("00");

		$("#is_finished").attr("checked", (data['is_finished'] == 1));

		$("#templates_container").html("").hide();
		templateContainerisLoaded = false;
	});
}

var dialogInit = function() {
	/* generelle Dialog-optionen */
	var dialogOpts 			= {};
	dialogOpts.modal 		= true;
	dialogOpts.autoOpen 	= false;
	dialogOpts.bgiframe		= true;
	dialogOpts.title		= "Dialog";
	dialogOpts.beforeclose 	= function(event, ui) {$('#dialogBox').text(' '); return true;};
	dialogOpts.minHeight 	= 640;
	dialogOpts.width		= 800;

	if(this.title && this.title.length > 0)
	dialogOpts.title = this.title;

	var url = this.href;

	// dialog für formulare, mit speicherfunktion
	dialogOpts.buttons = {
		"Abbrechen" : function() {
			$('#dialogBox').dialog("close");
			return true;
		},
		"Speichern" : function() {
			var form = $('#dialogform').ajaxSubmit({
				// target : '',
				async:false,
				success: function(response, status, xhr, form) {
					$("#dialogBox").dialog("close");
					location.reload(true);
				}
			});
			return false;
		}
	};

	$('#dialogBox').dialog(dialogOpts);
	$('#dialogBox').load(url);
	$('#dialogBox').dialog('open');
	$('.datepicker').click(dateSelector);
	return false;
};


$(document).ready(function() {

	$('.datepicker').click(dateSelector);
	$('.ajaxRequest').click(function() {
		if($(this).hasClass('confirm') && confirmDelete() == false) {return false;}
		var url = this.href;
		$.get(url);
		location.reload(true);
		return false;
	});

	/* wenn ein Link mit attribut class="inDialog" angeklickt wird */
	$('.inDialog').click(dialogInit);
	$("#tabs").tabs({ cookie : { expires: 30 } });

	// Error & Success meldungen in Modaldialogen anzeigen.


	if($('#errorText').text() != "") {
		// $.prompt($("#errorText").text(), {zIndex:9999});
		$("#errorTable").hide().fadeIn(2000).fadeOut(5000);
	}
	if($('#successText').text() != "") {
		// $.prompt($("#successText").text(), {zIndex:9999});
		$("#successTable").hide().fadeIn(2000).fadeOut(5000);
	}


	// Tabellen zu/aufklappbar machen
	$('.grouptitle').click(function() {
		var knoten = this.parentNode; // TR von grouptitle
		while(knoten != null) {
			knoten = knoten.nextSibling; // nächstes Element
			if(knoten && knoten.nodeType == 1) { // nur wenn TR
				if($(knoten).is(":visible")) {
					$(knoten).hide();
				} else {
					$(knoten).show();
				}
			}
		}
	});

	$(".menu ul").supersubs({
		minWidth:    12,
		maxWidth:    30
	})
	.superfish({
		autoArrows:  true,
		dropShadows: false
	});

	$('.progressbar').each(function(index) {
		var value2 = parseInt($(this).text());
		$(this).text("");
		$(this).progressbar({value: value2});
	});
	$("#showHideCalendar").click(function() {
		if($("#googleCalendarIframe").is(":visible")) {
			$("#googleCalendarIframe").hide();
		} else $("#googleCalendarIframe").show();
	});

});
