var thumbnailsexist = false;
var thumbnailsvisible = false;
var isMobileBrowser = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|mobile)/);
if (isMobileBrowser) $("head").append("<link rel=\"stylesheet\" href=\"css/rj_mobile.css\" />");

var useCSSTransition = ($.support.cssProperty("transition") == true) && ($.support.cssProperty("opacity") == true);

var layoutTimer = undefined;



/* layout */

function layoutTimeout() {
	layoutTimer = undefined;
	layoutPage();
}

function layoutPageDelayed() {	// to avoid multiple calls
	if (layoutTimer == undefined)
		layoutTimer = setTimeout(layoutTimeout, 50);
}

function layoutPage(dontanimate) {
	var ww = $(window).width();
	var wh = $(window).height();//-2*125;
	var menuheight = $("#menu").height();
	var top = 0;
	var gallerytop = 20;
	var galleryheight = wh-2*gallerytop-menuheight+18;
	if (thumbnailsvisible) {
		wh -= 100;
		top = 100;
		gallerytop = 100;
		galleryheight = wh;
	}
	var wasp = ww/Math.max(1, galleryheight);

	//$("#content, #content-back").css({"top":top, "height": wh});
	// gallery
	$("#picmain img").each(function() {
		var pic = $(this);
		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(galleryheight*picasp);
			newh = galleryheight;
		}
		var newt = Math.round((galleryheight-newh)/2)+gallerytop;
		var newl = Math.round((ww-neww)/2);
		if (pic.is(":visible") && (pic.data("positioned")==true) && (!dontanimate)) {
			if (useCSSTransition)
				pic.addClass("animation").css({"position":"absolute", "width":neww, "height":newh, "left":newl, "top":newt});
			else
				pic.css({"position":"absolute"}).stop().animate({"width":neww, "height":newh, "left":newl, "top":newt}, 800, "easeOutCubic");
		} else {
			pic.css({"position":"absolute", "width":neww, "height":newh, "left":newl, "top":newt}).data("positioned", true);
		}
	}).css("text-align", "left");
	
	$("#intropics-container").css("top", (wh-200)/2);
	$("#logo").css("bottom", Math.max(40, menuheight+22));
}


// icon actions

function changeBackgroundColor(event, forceclass) {
	if (event) event.preventDefault();
	
	var targetbackcolor = "#fff";//$(this).css("background-color");
	var targetfontcolor = "#000";
	var targetfontcolor2 = "#8691B7";
	var targetclass = "white";
	
	if ((forceclass == "white") || ((!forceclass) && $(this).hasClass("black"))) {
		//targetfontcolor = "#fff";
		targetclass = "white";
		$("#picdescription span, #menu a").removeClass("dark", 1000);
	}
	if ((forceclass == "grey") || ((!forceclass) && $(this).hasClass("white"))) {
		targetbackcolor = "#808080";
		targetfontcolor = "#fff";
		targetclass = "grey";
	}
	if ((forceclass == "black") || ((!forceclass) && $(this).hasClass("grey"))) {
		targetbackcolor = "#000";
		targetfontcolor = "#aaa";
		targetfontcolor2 = "#494f64";
		targetclass = "black";
		$("#picdescription span, #menu a").addClass("dark", 1000);
	}
	//console.debug("targetclass: "+targetclass+", forceclass: "+forceclass);
	$("#button-colorchanger").removeClass("white").removeClass("grey").removeClass("black").addClass(targetclass);
	$("#button-colorchanger a").text("background: "+targetclass);
	if (forceclass != undefined)
		$("body, #blendcover, h1, h2, h3, table.nice th, #introcover-top, #introcover-bottom").css({"background-color": targetbackcolor, "color": targetfontcolor});
	else
		$("body, #blendcover, h1, h2, h3, table.nice th, #introcover-top, #introcover-bottom").stop().animate({"background-color": targetbackcolor, "color": targetfontcolor}, 1000);
	$.cookie('rjbackcolor', targetclass);
	//$("#picdescription span, #menu a").animate({"color": targetfontcolor2}, 1000);
}

function growview() {
	$("#thumbnail-container").stop().animate({
		top: -120
		}, 1000, "easeInOutCubic");
	$("#footer")/*.css("overflow-y", "hidden")*/.stop().animate({
		//bottom: -41,
		bottom: 0
		}, 1000, "easeInOutCubic", function() {
			thumbnailsvisible = false;
			layoutPage();
		});
	//$("#menu, #picdescription").addClass("fullscreen");
}
function shrinkview() {
	$("#thumbnail-container").stop().animate({
		top: 0
		}, 300, "easeOutCubic");
	$("#footer")/*.css("overflow-y", "visible")*/.stop().animate({
		//bottom: 0,
		bottom: 20
		}, 300, "easeOutCubic", function() {
			if (thumbnailsexist) thumbnailsvisible = true;
			layoutPage();
		});
	//$("#menu, #picdescription").removeClass("fullscreen");
}
function changeview(e) {
	if (e) e.preventDefault();
	var t = $("#button-fullscreen");
	if (t.hasClass("off")) {
		t.removeClass("off").addClass("on").children("a").text("fullscreen: on");
		growview();
	} else {
		t.removeClass("on").addClass("off").children("a").text("fullscreen: off");
		shrinkview();
	}
}




// menu

var closeMenuID = undefined;
function openMenu(which) {
	closeMenus();
	$(which).addClass("hover");
}
function closeMenus() {
	if (closeMenuID != undefined)
		clearTimeout(closeMenuID);
	closeMenuID = undefined;
	$("#menu .hassubmenu").removeClass("hover");
}




// startup

$(document).ready(function() {
	layoutPage(true);

	$("#menu .hassubmenu").click(function(event) {
		//console.debug(event.target);
		if ($(event.target).hasClass("topmenulink"))
			event.preventDefault();
		openMenu(this);
	}).mouseenter(function(event) {
		openMenu(this);
	}).mouseleave(function(event) {
		if (closeMenuID == undefined)
			closeMenuID = setTimeout("closeMenus()", 500);	// etwas auf lassen
	});

	if ((typeof(isie6) != 'undefined') && (isie6 == true))
		$("#button-colorchanger").hide();
	else {
		$("#button-colorchanger").click(changeBackgroundColor);
		if ($.cookie('rjbackcolor') == "black")
			changeBackgroundColor(null, "black");
		else if ($.cookie('rjbackcolor') == "grey")
			changeBackgroundColor(null, "grey");
	}
	$("#button-fullscreen, #picdescription").click(changeview);
	$("#menu a.news").bind("mouseenter", function(e) {
		$("#newstip").fadeIn("slow");
	}).bind("mouseleave", function(e) {
		$("#newstip").stop().hide().css("opacity",1);//.stop().show().fadeOut("fast");
	});
	$(document).bind("mousemove", function(e) {
		$("#newstip").css({left: e.pageX + 10, top: e.pageY - 15});
	});
});
$(window).load(function() {
	$(window).resize(layoutPageDelayed);
	layoutPage(true);
});

