var isMobileBrowser = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|mobile)/);

/* Custom Scrollbar */

var scrollBarPressed = undefined;

function updateScrollbar(item) {
	if (!item) item = $("div.scrollbar");
	item.each(function() {
		var ref = $(this).data("scrollobject");
		$(this).css({	"left":		"70px",
						"top":		(ref.position().top+ref.height()-25)+"px",
						"width":	(ref.width()-140)+"px"});
		updateTrackbar($(this));
	});
}
function updateTrackbar(item) {
	if (!item) item = $("div.scrollbar");
	item.each(function() {
		var bar = $(this);
		var track = bar.find(".trackbar");
		var ref = bar.data("scrollobject");
		var childwidth = ref.children().outerWidth(true);
		var barsize = ref.width()/childwidth;
		var barpos = (ref.scrollLeft()/childwidth)*1.012;	// sonst fehlt rechts ein tick
		//console.debug(barpos+", "+ref.width()+", "+ref.children().outerWidth(true)+", "+ref.scrollLeft());
		//console.debug(barsize+"+"+barpos+"="+(barsize+barpos));
		track.css({	"left":		(barpos*100)+"%",
					"top":		0,
					"width":	(barsize*100)+"%",
					"height":	"100%",
					"min-width":"10px"});
		if (barsize < 1.0) {
			bar.show();
			//$("#pubscroll_left, #pubscroll_right, #pubviewscroll_left, #pubviewscroll_right").show();	// easy way
			if (ref.attr("id") == "pubcontent")
				$("#pubviewscroll_left, #pubviewscroll_right").show();	// easy way
			else
				$("#pubscroll_left, #pubscroll_right").show();	// easy way
		} else {
			bar.hide();
			ref.scrollLeft(0);
			if (ref.attr("id") == "pubcontent")
				$("#pubviewscroll_left, #pubviewscroll_right").hide();	// easy way
			else
				$("#pubscroll_left, #pubscroll_right").hide();	// easy way
		}
	});
}
function scrollInput(event) {
	//console.debug(event.clientX);
	var o = $(event.currentTarget);
	var ref = o.data("scrollobject");
	var pos = event.clientX/o.width();
	ref.scrollLeft(ref.children().outerWidth(true)*pos-(ref.width()/2));
	updateTrackbar(o);
}
function scrollTrackInputStart(event) {
	event.stopPropagation();
	event.preventDefault();
	scrollBarPressed = event.currentTarget;
	//console.debug("scrollTrackInputStart("+event.currentTarget+")");
	$(document).bind("mousemove", scrollTrackInput);
	scrollTrackInput(event);
}
function scrollTrackInputStop(event) {
	$(document).unbind("mousemove", scrollTrackInput);
	//console.debug("scrollTrackInputStop("+event.currentTarget+")");
	scrollBarPressed = undefined;
}
function scrollTrackInput(event) {
	if (scrollBarPressed == undefined) return;
	event.stopPropagation();
	event.preventDefault();
	//console.debug("scrollTrackInput("+event.currentTarget+")");
	var bar = $(scrollBarPressed);
	var track = bar.find(".trackbar");
	var ref = bar.data("scrollobject");
	var pos = (event.pageX-bar.offset().left)/bar.width();
	ref.scrollLeft(ref.children().outerWidth(true)*pos-(ref.width()/2));
	updateTrackbar(bar);
}
function addScrollbar(jQueryObject) {
	jQueryObject.css("overflow", "hidden")
	var o = $("<div class=\"scrollbar\"><div class=\"scrollbar-display\"></div><div class=\"trackbar\"><div class=\"trackbar-display\"></div></div>");
	o.data("scrollobject", jQueryObject);
	//o.mousedown(scrollInput);
	o.mousedown(scrollTrackInputStart);
	o.insertAfter(jQueryObject);
	updateScrollbar(o);
	$(document).mouseleave(scrollTrackInputStop).mouseup(scrollTrackInputStop);
}






/* layout */

var thumbnailsvisible = true;

function layoutPage(event) {
	var ww = $(window).width();
	var wh = $(window).height()-2*125;//Math.max(550, $(window).height()-2*125);
	//if (thumbnailsvisible) wh -= 100;
	var wasp = ww/Math.max(1, wh);
	
	$("#content, #content-back").css({"top":125, "bottom":"auto", "height": wh});
	//$("#pdfportfolio").css({"bottom": "auto", "top":(125+wh)});
	// gallery
	$(".piccontainer").each(function() {
		var pic = $(this).find("img");
		var picw = pic.attr("data-width");
		var pich = pic.attr("data-height");
		var picasp = picw/Math.max(1, pich);
		var neww;
		var newh;
		if (picasp > wasp) {
			neww = ww;
			newh = Math.round(ww/picasp);
		} else {
			neww = Math.round(wh*picasp);
			newh = wh;
		}
		var newt = Math.round((wh-newh)/2);
		var newl = Math.round((ww-neww)/2);
		if (pic.is(":visible") && (pic.data("positioned")==true))
			pic.css({"position":"absolute"}).stop().animate({"width":neww, "height":newh, "left":newl, "top":newt}, "slow", "easeOutCubic");
		else
			pic.css({"position":"absolute", "width":neww, "height":newh, "left":newl, "top":newt}).data("positioned", true);
	}).css("text-align", "left");
	// text-content
	var fontsize = Math.max(1.0, ww/1500);
	$(".textcontent").css("font-size", fontsize+"em");
	$(".textcontent .single-column").css({"min-width": Math.round(/*ww*0.38*/520*fontsize)+"px"}).parents(".publications").css("margin", "0");//, "min-height": Math.round(wh*0.41)+"px"});
	var th = $(".textcontent").height();
	if (th > 0) {
		th = th+30+30;	// trackbar x2
		if (th > wh) {
			$("#content, #content-back").css({"top":125, "bottom":"auto", "height":th});	// override
			$(".textcontent").css("margin-top", 30);
			$("#pdfportfolio").css({"bottom": "auto", "top":(th+125)});
		} else {
			$(".textcontent").css("margin-top", Math.round((wh-th)/2));
			$("#pdfportfolio").css({"bottom": 50, "top": "auto"});
		}
	}
	if ($(".contentcolumn").length > 1) {
		$(".contentcolumn").each(function() {
			$(this).css({"max-width":"none", "width": Math.round(ww*0.4)});	// try to force width
			var innerw = $(this).children(".contentcolumn-inner").outerWidth(true);	// check for wider elements
			$(this).css("width", innerw);	// set wider elements
		});
		$(".textcontent").css("margin-left", Math.round(ww*0.1));
		$(".contentspacer").css("width", Math.max(20, Math.round(ww*0.1)));
	}
	// archive
	if (location.search.search("content=archive") > -1) {
		//$("#content div.imagelist").css("overflow", "visible");
		var countx = Math.min(10, Math.max(1, Math.floor((ww-40)/150)));
		var county = Math.min(5, Math.max(1, Math.floor((wh-30)/185)));
		var aw = countx*150;
		var ah = county*185+25;
		$("#archivebox").width(aw).height(ah);
		$("#archivebox").css({"left": (ww-aw)/2, "top": (wh-ah)/2});
		//console.debug(
		var newpagecount = countx * county;
		//console.debug("archive: "+newpagecount+" != "+archivepagecount+"? ("+ww+")");
		if (newpagecount != archivepagecount) {
			archivepagecount = newpagecount;
			$.get("ajax-archive.php", {content:specialcontent, c:archivepagecount, s:archivestart, search:archivesearch}, function(data) {
				$("#archivebox").html(data);
			}, "html");

		}
	}
	// publications
	var freespaceorig = (wh-40-3*130);
	var newheight = Math.min(120, Math.floor(120+(freespaceorig/3)));
	$("a.publication .img").height(newheight);
	freespaceorig = (wh-40-3*(newheight+10));
	var freespace = Math.max(0, freespaceorig/3);
	$(".publications .pubrow").each(function(index) {
		$(this).css("top", Math.round(freespace/2+index*freespace));
	});
	$("#pubscroll_left, #pubscroll_right").css("top", ($("#content").position().top+$("#content").height()-25)+"px");
	var pw = $(".publications").width();
	var cw = $("#content").width();
	if (pw < cw) {
		$(".publications").css("left", (cw-pw)/2);
		$("#pubscroll_left, #pubscroll_right").hide();
	} else $("#pubscroll_left, #pubscroll_right").show();
	updateScrollbar();
}


var hometimeoutid = undefined;

function logoHomeClick(event) {
	event.preventDefault();
	event.stopPropagation();
	doHomeAnim();
}

function doHomeAnim() {
	clearTimeout(hometimeoutid);
	$("#animlogo").stop().fadeOut(1000, "easeInQuad", function() {
		$("#animlogo").remove();
		$("#content, #content-back, #logomenu, #thumbnails, #thumbscroll_left_container, #thumbscroll_right_container, #pdfportfolio").fadeIn(1000, "easeOutQuad", function() {
			$("#banner").dialog({
				modal: false,
				title: "news",
				width: 300,
				minHeight: 50,
				position: [(($(window).width()*0.95)-300), ($(window).height()*0.25)],
				show: 'fade',
				hide: 'fade',
				resizable: false
			}).children().blur();	// for safari
		});
	});
}

function startHomeAnim() {
	/*var ww = $(window).width();
	var wh = $(window).height();
	$("#content, #content-back, #logomenu, #thumbnails, #thumbscroll_left_container, #thumbscroll_right_container, #pdfportfolio").hide();
	var logo = $("<img src=\"img/checker.gif\" alt=\"\" id=\"animlogo\" />");
	$("body").append(logo);
	logo.width("50%").height("auto");
	var logow = logo.width();
	var logoh = logo.height();
	logo.css({"left":((ww-logow)/2), "top":((wh-logoh)/2)}).bind("click", logoHomeClick);
	hometimeoutid = setTimeout("doHomeAnim()", 2000);*/
	$("#content, #content-back, #logomenu, #thumbnails, #thumbscroll_left_container, #thumbscroll_right_container, #pdfportfolio").hide();
	$("#animlogo").show().bind("click", logoHomeClick);
	hometimeoutid = setTimeout("doHomeAnim()", 2000);
}





var closeMenuID = undefined;
function openMenu(which) {
	closeMenus();
	$(which).addClass("hover");
}
function closeMenus() {
	if (closeMenuID != undefined)
		clearTimeout(closeMenuID);
	closeMenuID = undefined;
	$("#menu .hassubmenu").removeClass("hover");
}
var closeMainMenuID = undefined;
function openMainMenu(which) {
	closeMainMenus();
	$(which).addClass("hover");
}
function closeMainMenus() {
	if (closeMainMenuID != undefined)
		clearTimeout(closeMainMenuID);
	closeMainMenuID = undefined;
	$("#menu .menuentry").removeClass("hover");
}
		

