/*
  Alvin Chan Website
  Copyright (C) 2008 by Systemantics, Bureau for Informatics

  Lutz Issler
  Mauerstr. 10-12
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  mail@systemantics.net

  Permission granted to use the files associated with this
  website only on your webserver.
*/



// this function determines whether the event is the equivalent of the microsoft
// mouseleave or mouseenter events.
// From: http://www.dynamic-tools.net/toolbox/isMouseLeaveOrEnter/
function isMouseLeaveOrEnter(e, handler) {
	if (e.type != "mouseout" && e.type != "mouseover") {
		return false;
	}
	var reltg = e.relatedTarget
		? e.relatedTarget
		: e.type == "mouseout"
			? e.toElement
			: e.fromElement;
	while (reltg && reltg != handler) {
		reltg = reltg.parentNode;
	}
	return (reltg != handler);
}

function showCat(id) {
	$("#cat"+id).stop().queue("fx", []).animate({"opacity": 0}, "normal");
	$("#img"+id).children(".cover").stop().queue("fx", []).animate({"opacity": .3}, "normal");
	$("#selected").stop().queue("fx", []).animate({"opacity": 0}, "normal");
}

function hideCat(id) {
	$("#cat"+id).stop().queue("fx", []).animate({"opacity": 1}, "normal");
	$("#img"+id).children(".cover").stop().queue("fx", []).animate({"opacity": 0}, "normal");
	$("#selected").stop().queue("fx", []).animate({"opacity": 1}, "normal");
}

function showItem(el) {
	$(el).stop().queue("fx", []).animate({"opacity": 0}, "normal");
	$("#selected").stop().queue("fx", []).animate({"opacity": 0}, "normal");
}

function hideItem(el) {
	$(el).stop().queue("fx", []).animate({"opacity": 1}, "normal");
	$("#selected").stop().queue("fx", []).animate({"opacity": 1}, "normal");
}

var lastSub = null;

function toggleSubSelected() {
	$("#subSelected").toggleClass("selected");
	if (lastSub) {
		lastSub.addClass("selected");
		lastSub = null;
	} else {
		lastSub = $("#submenu li .selected");
		lastSub.removeClass("selected");
	}
}

var about = false;

function showSubSelected() {
	if (about) {
		var el = $("#imageSlider");
		var left = parseInt(el.css("marginLeft"));
		var index = Math.abs(left/imgWidth);
		$('#submenu li a').removeClass("selected");
		$($("#submenu li a").get(index)).addClass("selected");
	}
}

function tickleIn() {
	$("#images").stop().queue("fx", []).animate({"marginLeft": -5}, "normal");
}

function tickleOut() {
	$("#images").stop().queue("fx", []).animate({"marginLeft": 0}, "normal");
}

var imgWidth = 737+11;
var dontMove = false;

function next() {
	if (dontMove) {
		dontMove = false;
		return;
	}
	var el = $("#imageSlider");
	var left = parseInt(el.css("marginLeft"));
	if (el.width()+left>imgWidth) {
		// Show next
		el.animate({"marginLeft": "-="+imgWidth}, 1000, showSubSelected);
	} else {
		// Reset to the beginning
		el.animate({"marginLeft": 0}, el.width()/2, showSubSelected);
	}
}

function showPage(item, index) {
	var el = $("#imageSlider");
	el.animate({"marginLeft": -index*imgWidth}, 1000);
	item.blur();
	$('#submenu li a').removeClass("selected");
	lastSub = $(item);
	lastSub.addClass("selected");
}

var field = null;
var bigPortraitField = false;

function nextPortrait() {
	// Select a field to change
	var newField;
	do {
		newField = Math.floor(Math.random()*6)+1;
	} while (newField==field);
	field = newField;
	// Select an image:
	// If it is field 1-4, select a big or small image
	// If it is field 5-6, select a small image
	var newImage;
	do {
		newImage = Math.floor(Math.random()*(field<=4 && !bigPortraitField ? portraits.length : smallPortraitsCount));
	} while (portraits.length>6 && $.inArray(newImage, visibleImages)!=-1);
	visibleImages[newField-1] = newImage;
	if (newImage>=smallPortraitsCount) {
		bigPortraitField = field;
	} else if (bigPortraitField==field) {
		bigPortraitField = false;
	}
	// Show the new image
	$("#img"+field).append('<img class="fade" src="'+portraits[newImage]+'" alt="" />').children(":last").fadeIn("normal");
	$("#img"+field).children(":first").fadeOut("normal", function () {
		$("#img"+field).children(":first").remove();
	});
	// Set timeout for changing the next image
	window.setTimeout("nextPortrait()", Math.floor(Math.random()*1000)+1000);
}

function nextImage(el) {
	// Fade the first image out
	// and append it to the end
	if ($(el).children("img").length>1) {
		$(el).children("img").filter(":last").fadeOut("normal", function () {
			$(el).children("img").filter(":last").insertBefore($(el).children("img").filter(":first")).show();
		});
		window.setTimeout('nextImage("'+el+'")', Math.floor(Math.random()*2000)+1000);
	}
}

$(window).load(function () {
	$("#content").fadeIn("normal");
	$(".cover").fadeTo("normal", 0);
	if ($("#content").hasClass("categories")) {
		$("#content").children("div").each(function (i) {
			window.setTimeout('nextImage("#'+this.id+'")', Math.floor(Math.random()*2000));
		});
	}
	$("#subContainer").animate({height: $("#submenu").height()+17}, "slow");
	$("#newsContainer").animate({height: $("#news").height()}, "slow");
	$("#imageSlider a").click(function() {
		this.blur();
		dontMove=true;
	});
});
