﻿var currentpage, lastpage;

$(document).ready(function () {

	$(window).resize(function () {
		$("#content").css('min-height', $(window).height() - $("#content").offset().top - $("#footer").outerHeight(true) + "px");
	}).resize();

	$("a").live('click', function (e) {
		var page = $(e.target).attr('href'); if (!page) page = $(e.target).parents('[href]').first().attr('href');
		if (page == '#') return false; // some jqueryui buttons

		if (!page || page.indexOf('http') == 0 || page.indexOf('mailto:') == 0 || page.indexOf('callto:') == 0) // open directly
			return true;

		changeContent(page);
		return false;
	});

	$('.toggle').live('click', function (e) {
		var $toggle = $(e.target);
		if (!$toggle.length) $toggle = $(e.target).parents('[toggle]').first();
		var toggle = $toggle.attr('toggle');
		if (toggle) {
			$toggle.hide();
			$(toggle).fadeIn();
		}
	});

	$(".startchat").live('click',function () {
		if (!$(".chat").is(":visible")) {
			$(".chat").show();
			new Chat($("#messages"), $('#newmsg'));
		}
	});

	$(".chat .close").live('click', function () {
		$(".chat").hide();
	});

	$("img.tn").live('click', function () {
		var $popout = $("<div class='bigtn'></div>");
		var closeDlg = function () {
			$popout.dialog('destroy').remove();
			$(document).off('click', closeDlg);
		}
		$popout.click(closeDlg);
		$(document).on('click',closeDlg);
		var $img = $("<img src='" + $(this).attr('bigimg') + "' alt='' title='Zum Schließen anklicken.'/>").appendTo($popout);
		$popout.dialog({ resizable: false, modal: true,
			width: 'auto', height: 'auto',
			dialogClass: 'dialogWithDropShadow dialogCloseOnly',
			open: function () {
				var wait = setInterval(function () {
					if ($img[0].complete) {
						clearInterval(wait);
						if ($img.height() > $(window).height() - 80) $img.height($(window).height() - 80);
						if ($img.width() > $(window).width() - 80) $img.height($(window).width() - 80);
						$popout.dialog('option', 'position', 'center');
					}
				}, 50);
			},
			close: closeDlg // destroy it
		});
	});

});


function changeContent(page) {
	if (currentpage == page)
		return;

	var $content = $("#content");
	$content.animate({ opacity: 0 }, 'fast');

	$.ajax({
		url: page + "?partial=true",
		success: function (html, textStatus, jqXHR) {
			//			if (history.pushState) history.pushState({}, "", page);
			History.pushState(null,null, page);
			setTitle(jqXHR.getResponseHeader("PageTitle"));
			lastpage = currentpage;
			currentpage = jqXHR.getResponseHeader("Page");
			$content.stop().css({ opacity: 0 });
			$content.html(html);
			$(window).resize();
			$content.animate({ opacity: 1 }, 500, function () {
				//				var setpagecss = function () {
				$("#container").attr('page', currentpage);
				$(window).resize();
				//				}

				//				if (currentpage == 'home')
				//					$("#container #top").slideUp('fast', setpagecss);
				//				else if (lastpage == 'home')
				//					$("#container #top").slideDown('fast', setpagecss);
				//				else
				//					setpagecss();
			});
		},
		error: function () {
			window.location.href = page;
		}
	});
}

function setTitle(page) {
	if (page)
		document.title = page;
};

function msgBox(msg) {
	$("<div>" + msg + "</div>").dialog({ resizable: false, width: Math.min(msg.length * 15, $(window).width() / 2), modal: true, dialogClass: 'dialogWithDropShadow dialogNoTitle', buttons: { OK: function () { $(this).dialog("close"); } } });
};


