galleryImages = [];
viewList = [];
thumbList = [];
movies = [];
imageHashMap = {};
maxMovieWidth = 1142;
maxThumbsWidth = maxMovieWidth;
maxNewsImgWidth = 760;
maxNewsImgHeight = 570;
hasImages = false;
hasMovies = false;
VIEW_IDX = 0;
MOVIE_IDX = 0;
numNewsImg = 0;
NEWS_IMG_IDX = -1;
CUR_VIEW = null;
LAST_VIEW = null;
showThumbsTimer = null;
headerHeight = 70;
footerHeight = 30;
thumbHeight = 100;
MAIN_MENU_HAS_FOCUS = false;
galleryTitle = "";
var resizeTimer = null;
var thumbTimer = null;
var thumbScrollTimeout = null;
scrollDirection = "left";
thumbsClosing = false;
bioHidden = true;
var lastSubSubUL = null;

var isiPad = navigator.userAgent.match(/iPad/i) != null;

Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('p');
Cufon.replace('a',{hover:true});
Cufon.replace('#footer-right');
Cufon.replace('#footer-center');
Cufon.replace('.content');
//google analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-23519498-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

function scrollThumbs(flag, direction){
	//alert(hasImages+" "+hasMovies);
	if(!hasImages && !hasMovies){
		return;
	}
	//alert("scrollThumbs "+flag+" "+direction)
	var speed = isiPad ? 100 : 5;
	if(thumbScrollTimeout){
		clearTimeout(thumbScrollTimeout);
	}
	checkPrevNext();
	if(!flag){
		clearTimeout(thumbScrollTimeout);
		return;
	}else if(flag){
		if(direction == "prev"){
			if($("#thumbs").position().left < 0){
				$("#thumbs").css("left", $("#thumbs").position().left + speed + "px");
				
			}else{
				$("#thumb-prev").css("display", "none");
				clearTimeout(thumbScrollTimeout);	
			}
		}else if(direction == "next"){
			var maxLeft = maxThumbsWidth - totalThumbsWidth;
			//window.status = maxLeft;
			if($("#thumbs").position().left >= maxLeft){
				$("#thumbs").css("left", Math.floor($("#thumbs").position().left - speed) + "px");
			}else{
				$("#thumb-next").css("display", "none");
				clearTimeout(thumbScrollTimeout);	
			}
		}
		
		var func = "scrollThumbs("+flag+", '"+direction+"')";
		//alert(func);
		thumbScrollTimeout = setTimeout(func, 40);
	}
}
function showSubnavi(flag){
	return;
	var css = flag ? "block" : "none";
	$("#navi-sub").css("display", css);
	setCookie("sub-navi-open", flag ? "true" : "false");
}
function getCookie(key){
	return $.cookie(key);
}
function setCookie(key, value){
	if(key == "main-navi-history"){
		
	}
	$.cookie(key, null);
	$.cookie(key, value, {expires:1, path:"/"});
	//, {expires: 7});
}
function mainNaviClickHistoryBack(){
	mainNaviClickHistory.pop();
	setCookie("main-navi-history", mainNaviClickHistory.join(","));
	if(mainNaviClickHistory.length <= 0){
		$("#navi-main-inner").show();
		$("#navi-main .title-bar h2").show();
		$("#navi-sub").hide();
		$("#back").remove();
	}else if(mainNaviClickHistory.length == 1){
		$("#navi-sub").show();
		lastSubSubUL.hide();
	}
}
function walkMainNaviClickHistory(){
	//alert(mainNaviClickHistory+" "+mainNaviClickHistory.length);
	addMainNaviClickHistory(0, mainNaviClickHistory[0], true);
	if(mainNaviClickHistory.length > 1){
		addMainNaviClickHistory(1, mainNaviClickHistory[1], true);
	}
}
function mainNaviPhase(depth, idx){
	switch (depth){
		case 0:
			
			$("#navi-main .title-bar h2").hide();
			$("#navi-sub").show();
			$("#navi-main .title-bar").prepend($("<h2 id='back'>BACK</h2>"));
			Cufon.replace('#back');
			//$(this).attr("onClick", "return false;");
			$("#back").bind('mouseup', function() {
				$("#navi-main").draggable( "destroy");
				$("#navi-main").draggable();
				mainNaviClickHistoryBack();
				return false;
			})
			break;
		case 1:
			$("#navi-sub").hide();
			lastSubSubUL = $("#sub-sub-ul_"+idx);
			lastSubSubUL.show();
			if($("#bio").height() != null){
				$(lastSubSubUL).append($("<li><p class='no-icon' id='bio-link'>Biography</p></li>"));
				Cufon.replace('#bio-link');
				$('#bio-link').bind('mouseup', function() {
					$("#bio-title").mouseup();
				})
			}else{
				$('#bio-link').remove();
			}
			break;
		default:
			break;
	}
}
function addMainNaviClickHistory(depth, idx, showPhase){
	mainNaviClickHistory[depth] = idx;
	if(lastSubSubUL){
		lastSubSubUL.hide();
	}
	var delay = 500;
	if(depth == 0 && idx == 0 && showPhase){
		$("#navi-main-inner").hide();
		setTimeout("mainNaviPhase(" + depth + "," + idx + ");", delay);
	}else if(depth == 1 && showPhase){
		//alert(depth);
		setTimeout("mainNaviPhase(" + depth + "," + idx + ");", delay);
		//alert(lastSubSubUL);
	}
	/*
	while(mainNaviClickHistory.length > depth+1){
		mainNaviClickHistory.pop();
	}
	*/
	//alert("mainNaviClickHistory: "+mainNaviClickHistory.join(","));
	setCookie("main-navi-history", mainNaviClickHistory.join(","));
}
function delayedClick(href){
	location.href=href;
}
function init(){
	if(location.href.indexOf("/browse/")>0){
		$("#news_latest_wrapper").remove();
	}
	$(window).unload(function() {
  		//alert('Handler for .unload() called.');
	});

	//$("#wrapper_content").prepend(document.cookie);
	if(hasMovies){
		$("#views-mask").remove();
	}else{
		$("#movieIframe").remove();
	}
	$("#navi-sub").css("display","none");
	
	var numFirstLevelLinks =0;
	var numSecondLevelLinks = 0;
	var numSubSubUls = 0;
	
	$("#navi-main").bind('mouseover', function() {
		//MAIN_MENU_HAS_FOCUS = true;
		//alert(MAIN_MENU_HAS_FOCUS);
	})
	$("#navi-main").bind('mouseout', function() {
		//MAIN_MENU_HAS_FOCUS = false;
		//alert(MAIN_MENU_HAS_FOCUS);
	})

	$("#navi-main li a").each(function(){
		
		$(this).attr("idx", numFirstLevelLinks);
		$(this).attr("id", "mainNaviLink_"+numFirstLevelLinks);
		$(this).bind('mouseup', function() {
			//MAIN_MENU_HAS_FOCUS = true;
			addMainNaviClickHistory(0, $(this).attr("idx"), true);
		})
		if(numFirstLevelLinks > 0){
			$(this).addClass("no-icon");
		}
		numFirstLevelLinks++;
	});
	$("#navi-main").append($("#navi-sub"));
	$("#navi-sub li a").each(function(){
		$(this).attr("idx", numSecondLevelLinks);
		$(this).attr("onClick", "return false;");
		$(this).bind('mouseup', function() {
			addMainNaviClickHistory(1, $(this).attr("idx"), false);
			var href = $(this).attr("href");
			var func = "delayedClick('" + href + "');";
			setTimeout(func, 1000);
			return false;
		})
		numSecondLevelLinks++;
	});
	$("#navi-sub-sub ul ul").each(function(){
		$(this).attr("id", "sub-sub-ul_"+numSubSubUls);
		$(this).addClass("sub-sub");
		$("li a", this).each(function(){
			$(this).addClass("no-icon");
		})
		$(this).hide();
		numSubSubUls++;
		$("#navi-main").append($(this));
	});
	$("#navi-sub-sub").remove();
	
	/*
	$("#navi-main li:first").append($("#navi-sub"));
	$("#navi-main li:first a").toggle(
		function() {
			showSubnavi(true);
			return false;
		},
		function() {
			showSubnavi(false);
			return false;
		});
	*/
	$("#navi-sub li a").bind('mouseup', function() {
		location.href = this;
	});
	$("#navi-startpage div.mask").bind('mouseup', function() {
		//alert(MAIN_MENU_HAS_FOCUS);
		if(MAIN_MENU_HAS_FOCUS){
			return;	
		}
		//var href = $("li a", this)[0];
		//location.href = href;
	});
	$("#navi-startpage div.mask").each(function(){
		var href = $("li a", this)[0];
		//$("li a", this).each(function(){
		//	$(this).attr("href", href);
		//});
		//alert(href);
	});
	if(hasImages){
		
		if(getCookie("thumbs-shown") == "true"){
		
		}else{
			showThumbs(true);
			showThumbs(false);
			setCookie("thumbs-shown", "true");
		}
	}
	if(getCookie("navi-open") == "true"){
		$("#navi-main").show(0);
		setNaviPos();
	}else{
		$("#navi-main").hide("fast");
	}
	if(getCookie("sub-navi-open") == "true"){
		showSubnavi(true);
	}
	var totalViewWidth = 0;
	if(null != galleryImages && galleryImages.length >0){
		var funcs = ["prev", "next"];
		for(var i = 0;i<funcs.length;i++){
			var func = funcs[i];
			$("#thumb-" + func).attr("func", func).bind('mousedown', function() {
				scrollThumbs(true, $(this).attr("func"));
	  	    });
	  	    $("#thumb-" + func).bind('mouseup', function() {
				scrollThumbs(false, $(this).attr("func"));
	  	    });

		}
		if(hasImages){
			$("#view-prev").bind('mouseup', function() {
				scrollDirection = "left";
				VIEW_IDX--;
				selectImg(VIEW_IDX);
	  	    });
			$("#view-next").bind('mouseup', function() {
				scrollDirection = "right";
				VIEW_IDX++;
				selectImg(VIEW_IDX);
	  	    });
	  	    cursor("view-prev");
  	  		cursor("view-next");
		}else{
			$("#thumbs").css("top", "0px");
		}
  	     numThumbsLoaded = 0;
  	     totalThumbsWidth = 0;
  	     var calc = "";
		for(var i = 0;i<galleryImages.length;i++){
			var galleryImage = galleryImages[i];
			var abs_path = galleryImage.abs_path;
			var imgW = galleryImage.width;
			var imgH = galleryImage.height;
			var factor = imgH / thumbHeight;
			if(imageHashMap[abs_path] == null){
				imageHashMap[abs_path] = true;
				var thumbWidth = Math.floor(imgW / factor);
				calc += thumbWidth+" ";
				totalThumbsWidth += (thumbWidth) + 1;
			}else{
				continue;
			}
			//var img = new Image();
			//img.onLoad = imgLoaded();
			//img.src = galleryImage.abs_path;
			$("#thumbs").append(imgHTML(galleryImage));	
			$("#thumbs").children(":last").bind('mouseup', function() {
				if(hasMovies){
					selectMovie($(this).attr("idx"));
				}else{
					selectImg($(this).attr("idx"));
				}
		    }).attr("id", "thumb_" + i).attr("idx", i);
		    thumbList.push($("#thumbs").children(":last"));
		   //var calc = "";
		  // totalThumbsWidth = 0;
		    thumbList[thumbList.length-1].children("img").load(function(){
		    	//window.status = "numThumbsLoaded: "+numThumbsLoaded+" "+$(this).width();
		    	//alert($(this).width());
		    	numThumbsLoaded++;
		    	//$("#thumbs").width($("#thumbs").width() + $(this).width());
		    	//calc += $(this).width() + "+";
		    	//totalThumbsWidth += $(this).width() + 1;
		    	if(numThumbsLoaded == thumbList.length){
		    		//alert(totalThumbsWidth);
		    		//$("#thumbs").width(totalThumbsWidth);
		    		//alert($("#thumbs").width()+" "+calc);
		    	}
		    });
		    if(hasImages){
		    	$("#views").append(imgHTML(galleryImage));
		    	$("#views").children(":last").attr("id", "view_" + i).attr("idx", i);
		    	viewList.push($("#views").children(":last"));
		   		$("#views").children(":last").position().left = totalViewWidth;
		   		$("#views").children(":last").bind('mouseup', function() {
					selectImg(VIEW_IDX + 1);
				});
		    }
		   
		    totalViewWidth += galleryImage.width;
			//alert(galleryImage.abs_path);
		}
		//alert(thumbList.length+" "+totalThumbsWidth);
		$("#thumbs").width(totalThumbsWidth+thumbList.length-1);
		//alert(calc+" "+totalThumbsWidth);
		highestMovie = 0;
		if(hasMovies){
			for(var i = 0;i<movies.length;i++){
				var movie = movies[i];
				if(movie.height>highestMovie){
					highestMovie = movie.height;
				}
			}
			ratio = maxMovieWidth/640;
			var frameHeight = highestMovie;
			//$("#thumbs-mask")
			//Math.floor(highestMovie*ratio);
			$("#movieIframe").css("height", frameHeight + 5 + "px")
			$("#thumbs-mask").css("top", (frameHeight + headerHeight) + "px");
			//$("#thumbs-cover").css("left", maxThumbsWidth + "px");
			$("#footer").css({"top":(frameHeight + headerHeight + thumbHeight ) + "px"});
		}
		
		$("#thumbs").children(":first").mouseup();
		$("#footer-right").html(galleryTitle);
		Cufon.replace('#footer-right');
		if(hasImages){
			$("#thumbs-mask").hover(
				function(){
					showThumbs(true);
				},
				function(){
					showThumbs(false);
				}
			);
		}
	}
	scrollThumbs(true, "prev");
	scrollThumbs(false, "prev");
	windowResized();
	if(!hasImages && !hasMovies && $("#navi-startpage").height() == null){
		if($("#contactIframe").height() != null){
			$("#footer").css("top", 20 + $("div.imprint").position().top + $("div.imprint").height());
		}else{
			$("#footer").css("top", $("#wrapper_content").height());
		}
	}
	if(hasImages || hasMovies){
		
		
	}
	//ini init
	var mainNaviClickHistoryStr = getCookie("main-navi-history")+"";
	if(mainNaviClickHistoryStr.length > 0){
		//alert(mainNaviClickHistoryStr);
		mainNaviClickHistory = mainNaviClickHistoryStr.split(",");
		walkMainNaviClickHistory();
	}else{
		mainNaviClickHistory = [];
	}
	if($("#news_images").height() != null){
		var y = $("#news_images").height() + headerHeight;
		$("#footer").css({"top":(y) + "px"});
		//alert($("#footer").css("top"))
	}
}
//end init
function hideThumbs(){
	thumbsClosing = false;
	$("#thumbs").animate({"top": "+="+((thumbHeight*2)-1)+"px"}, 300);
	$("#thumbs-btns").animate({"top": "+="+(thumbHeight*2)+"px"}, 300);
}
function showThumbs(flag){
	if(flag){
		if(showThumbsTimer){
			clearTimeout(showThumbsTimer);
		}
		if(thumbsClosing){
			return;	
		}
		$("#thumbs").css('top', thumbHeight + "px").animate({"top": "-="+(thumbHeight-2)+"px"}, 500);
		$("#thumbs-btns").css('top', thumbHeight + "px").animate({"top": "-="+(thumbHeight-2)+"px"}, 500);
	}else{
		thumbsClosing = true;
		if(showThumbsTimer){
			clearTimeout(showThumbsTimer);
		}
		showThumbsTimer = setTimeout("hideThumbs();", 1800);
	}
}
function checkPrevNext(){
	//alert($("#thumbs").width()+" "+maxThumbsWidth);
	if($("#thumbs").width() > maxThumbsWidth){
		//alert($("#thumbs").width()+" "+maxThumbsWidth);
		$("#thumb-prev").css("display", "block");
		$("#thumb-next").css("display", "block");
	}else{
		$("#thumb-prev").css("display", "none");
		$("#thumb-next").css("display", "none");
	}
	$("#thumb-next").css("left", (maxThumbsWidth - 48) + "px");
}
function checkBioScroll(){
	var innerDivHeight = ($("#bio-scroll div:first").height());
	var factor = 20;
	var doScroll = false;
	var bioHeight = galHeight;
	if(innerDivHeight < galHeight){
		bioHeight = innerDivHeight + 20;
	}else{
		doScroll = true;
	}
	//$("#wrapper_content").prepend(doScroll+" "+innerDivHeight+" galHeight: "+galHeight+" bioHeight: "+bioHeight+"<br>");
	$("#bio-mask").height(bioHeight);
	if(doScroll){
		//bioHeight -= factor;
	}
	$("#bio").height(bioHeight);
	$('#bio-scroll').height(bioHeight);
	if(!bioHidden){
		if(doScroll){
			$('#bio-scroll').jScrollPane({scrollbarWidth:25});
			$('.jScrollPaneScrollable').height(bioHeight);
			$('#bio-scroll').height(bioHeight - factor);
		}
	}else{
		$("#bio-mask").height(0);
	}
}
function windowResized() {
	//alert("windowResized");
	//alert($(window).width()+" "+$(window).height());
	var windowHeight = $(window).height();
	var windowWidth = $(window).width();
	galHeight = windowHeight - 100;
	checkBioScroll();
	maxThumbsWidth = windowWidth - 60;
	checkPrevNext();
	$("#views-mask").width(maxThumbsWidth);
	$("#views-mask").height(galHeight);
	$("#thumbs-mask").width(maxThumbsWidth);
	$("#thumbs-cover").css("left", maxThumbsWidth +"px");
	if(viewList.length >0){
		$("#thumbs-mask").css("top", (galHeight + headerHeight - thumbHeight) + "px");
		
		$("#view-prev").height(galHeight - thumbHeight - 30);
		$("#view-next").height(galHeight - thumbHeight - 30);
		for(var i = 0;i<viewList.length;i++){
			var view = viewList[i];
			//$(view).children("img:first").height(galHeight);
			
			if(i==VIEW_IDX){
				scaleImg($(view).children("img:first"));
				//alert(galHeight+" "+$(view).children("img:first").css("width")+" "+$(view).children("img:first").css("height"));
			}
		}
		
		//$("#thumbs-cover").css("left", $(view).children("img:first").width() + "px");
		
	}
	if(galleryImages.length > 0 && !hasMovies){
		$("#footer").css({"top":(galHeight + headerHeight ) + "px"});
	}
	
	var startPageNaviHeight = $("#navi-startpage").height();
	
	if(startPageNaviHeight != null){
		if(headerHeight + startPageNaviHeight + footerHeight > windowHeight){
			$("#footer").css({"top":(startPageNaviHeight + headerHeight) + "px"});
		}else{
			$("#footer").css({"top":(windowHeight - headerHeight - footerHeight - 10) + "px"});
		}		
	}
	
};

function scaleImg(img, disiredHeight, _maxWidth){
	//$(img).height = galHeight;
	//return;
	var _w = img.attr("_width");
	var _h = img.attr("_height");
	var tempH = galHeight;
	if(disiredHeight > 0){
		tempH = disiredHeight;
	}
	var factor = _h/tempH;
	var tempW = _w / factor;
	var maxWidth = maxThumbsWidth;
	if(_maxWidth > 0){
		maxWidth = _maxWidth;
	}
	if(tempW > maxWidth){
		tempW = maxWidth;
		factor = _w/tempW;
		tempH = _h / factor;
	}else{
		
		
	}
	//alert(tempW+"x"+tempH);
	img.css("width", Math.floor(tempW)+"px");
	img.css("height", Math.floor(tempH)+"px");	
	imgHeight = (Math.floor(tempH));
}
function selectMovie(idx){	
	if(idx <= 0){
		idx = 0;
	}else if(idx >= movies.length-1){
		idx = movies.length-1;
	}
	MOVIE_IDX = idx;
	selectThumb(MOVIE_IDX);
	var movie = movies[MOVIE_IDX];
	var img = galleryImages[MOVIE_IDX];
	var src = "/fileadmin/sys/php/movie.php?poster=/"+img.abs_path+"&movie=/"+movie.abs_path+"&width="+movie.width+"&height="+movie.height;
	//alert("selectMovie: "+idx+" "+src+" "+$("#movieIframe"));
	$("#movieIframe").attr("src", src);
}
function setActiveThumb(){
	//$(CUR_THUMB).append($("#thumb-active"));
	//alert($(CUR_THUMB).attr("id")+" "+$("#thumb-active"));
	$("#thumb-active").remove();
	$(CUR_THUMB).append('<div id="thumb-active"></div>');
	//.append(imgHTML(galleryImage));
	$("#thumb-active").css("top", "5px");
	$("#thumb-active").css("left", "5px");
}
function selectThumb(idx){
	CUR_THUMB = thumbList[idx];
	//alert(idx + " " + CUR_THUMB);
	setActiveThumb();
	//$("#thumb-active").css("left", Math.floor($(CUR_THUMB).position().left)+"px");
	//$("#thumb-active").css("left", Math.floor($(CUR_THUMB).position().left)+"px");

	//alert( Math.floor($(CUR_THUMB).position().left))
	//$("#thumb-active").css("display", "block");
	//alert(CUR_THUMB+" "+idx);
}
function selectImg(idx){
	//alert(idx);
	$("#view-prev").show(0);
	$("#view-next").show(0);
	if(idx <= 0){
		$("#view-prev").hide(0);
		idx = 0;
	}else if(idx >= viewList.length-1){
		$("#view-next").hide(0);
		idx = viewList.length-1;
	}
	VIEW_IDX = idx;
	//alert(VIEW_IDX);
	if(null != LAST_VIEW){
		//$(LAST_VIEW).fadeOut("fast");
	}
	if(null != CUR_VIEW){
		$(LAST_VIEW).fadeOut(0);
		LAST_VIEW = CUR_VIEW;
	}
	selectThumb(VIEW_IDX);
	CUR_VIEW = viewList[VIEW_IDX];
	if(hasImages){
		$(CUR_VIEW).css("left", "0px");
		$(CUR_VIEW).fadeIn("fast");
	}
	$("#footer-center").html(galleryImages[VIEW_IDX].title);
	for(var i = 0;i<galleryImages.length;i++){
		var galleryImage = galleryImages[i];
		var view = viewList[i];
		if(i==VIEW_IDX){
			windowResized();
			$(view).css("left", "0px");
			$(view).css("visibility", "visible");
		}else if(i<VIEW_IDX){
			$(view).css("visibility", "hidden");
		}else if(i>VIEW_IDX){
			$(view).css("visibility", "hidden");
		}
		
	}
	
	if (thumbTimer) {
		clearTimeout(thumbTimer);
	}
	thumbTimer = setTimeout(setupThumbs, 200);
	Cufon.replace('#footer-right');
	Cufon.replace('#footer-center');
	//alert($(CUR_VIEW).width());
}
function setupThumbs(){
	//alert($(CUR_VIEW).width());
	//$("#thumbs-mask").width($(CUR_VIEW).width());
	if(hasMovies){
		CUR_VIEW = $("#movieIframe");
	}
	var left = Math.floor($(CUR_THUMB).position().left + ($(CUR_THUMB).width()/2) - ($(CUR_VIEW).width() / 2));
	
}
function imgHTML(img){
	var ret = "<div class='mask'><img _width='" + img.width + "' _height='" + img.height + "' ";
	ret += "src='" + img.abs_path + "' /></div>";
	return ret;

}
function imgLoaded(){
	
}
function openMenu(flag){
	if(flag){
		setNaviPos();
		setCookie("navi-open", "true");
		$("#navi-main").show("fast");
	}else{
		setCookie("navi-open", "false");
		$("#navi-main").hide("fast");
	}
}
function setNaviPos(){
	if(getCookie("navi-position")){
		var pos = getCookie("navi-position").split("x");
		var left = pos[0];
		var top = pos[1];
		$("#navi-main").css('left', left + "px");
		$("#navi-main").css('top', top + "px");
	}
}


$(window).bind('resize', function() {
	if (resizeTimer) {
		clearTimeout(resizeTimer);
	}
	resizeTimer = setTimeout(windowResized, 250);
});

$(document).ready(function() {
	init();
	$("#navi-main").draggable({
			//cursor: 'move'
	});
	$("#navi-main").bind('mouseup', function() {
       var left = $(this).position().left;
       var top = $(this).position().top;
       //alert(left+" "+top);
       //alert("left: "+left+ " top: "+top);
		setCookie("navi-position", left + "x" + top);
    });
    $("#bio").css('left', "-537px");
	$("#bio-title").bind('mouseup', function() {
		//$("#bio").show();
		if(bioHidden){
			//$("#bio-mask").css("height", "537px");
			$("#bio").css('left', "-537px").animate({"left": "+=537px"}, 500);
			//height:537px;
			$(this).append("<div id='bio-close' class='icon close'>&nbsp;</div>");
		}else{
			$("#bio").css('left', "-537px");
			$("#bio-mask").css("height", "0px");
			$("#bio-close").remove();
		}
		bioHidden = !bioHidden;
		checkBioScroll();
  	});
  	if($("#news_text").height()>0){
  		$('#news_text .content').jScrollPane({scrollbarWidth:25});
  		//$('.jScrollPaneScrollable').width(680);
		//$('.jScrollPaneScrollable').height(680);
		//$('#bio-scroll').height(bioHeight - factor);
  	}
  	if($("#news_list_wrapper").height()>0){
  		newsDidiver($("#news_list_wrapper"));
  		var newsAnchorIdx = 0;
  		$(".news").each(function(){
  			newsAnchorIdx++;
  			$(this).attr("idx", newsAnchorIdx);
   			$(this).bind('mouseup', function() {
   				//alert(this);
				saveLastSeenNews($(this).attr("idx"));
			});
			
  			$(this).prepend("<a name='news_" + newsAnchorIdx + "' id='news_anchor_" + newsAnchorIdx + "' class='ancor'></a>");
  		})
  		var lastSeen = getCookie("last-news-seen");
  		if(lastSeen){
  			$('html, body').animate({ scrollTop: $("#news_anchor_"+lastSeen).offset().top }, 500);
  		}
  	}
	var idx = 0;
	lastNewsImg = null;
	if($("#news_images").height()>0){
		//NEWS_IMG_IDX
		$("#news_images img:first").remove();
		if($("#news_images img:first").height()>0){
	  		$("#view-prev").bind('mouseup', function() {
				showNewsImg(-1);
			});
			$("#view-next").bind('mouseup', function() {
				showNewsImg(1);
			});
			$("#view-prev").hide();
			$("#view-next").show();
			cursor("view-prev");
	  	  	cursor("view-next");
	  	  	$("#view-next").mouseup();
		}else{
			$("#view-prev").remove();
			$("#view-next").remove();
		}
  	}
  	var highestNewsImg = 0;
	$("#news_images img").each(function(){
		numNewsImg++;
		$(this).attr("_width", $(this).width());
		$(this).attr("_height", $(this).height());
		scaleImg($(this), maxNewsImgHeight, maxNewsImgWidth);
		highestNewsImg = $(this).height() > highestNewsImg ? $(this).height() : highestNewsImg;
		var y = highestNewsImg + headerHeight;
		$("#footer").css({"top":(y) + "px"});
		if(idx == 0){
			$(this).show();
		}else{
			$(this).hide();
		}
		idx++;
	});
	$("#navi-startpage div.mask").hover(
			function(){
				$(this).children("div:first").css('left', "-380px").animate({"left": "+=380px"}, 500);
			},
			function(){
				$(this).children("div:first").animate({"left": "-=380px"}, 100);
			}
	);
	$("a").attr("onfocus", "if (this.blur) this.blur();");
});
function blurLink(link){
	link.blur();
}
function saveLastSeenNews(idx){
	//alert("saveLastSeenNews: "+idx)
	setCookie("last-news-seen", idx);
}
function showNewsImg(offset){
	NEWS_IMG_IDX += offset;
	$("#view-prev").show();
	$("#view-next").show();
	if(NEWS_IMG_IDX <= 0){
		NEWS_IMG_IDX = 0;
		$("#view-prev").hide();
	}else if(NEWS_IMG_IDX >= numNewsImg -1){
		NEWS_IMG_IDX = numNewsImg - 1;
		$("#view-next").hide();
	}
	$("#news_images img:eq("+NEWS_IMG_IDX+")").show();
	if(lastNewsImg){
		$(lastNewsImg).hide();
	}
	lastNewsImg = $("#news_images img:eq("+NEWS_IMG_IDX+")");
	
}
function newsDidiver(wrapper){
	var idx = 0;
	$("#news_list_wrapper .news").each(function(){
		idx++;
		if(idx%3 == 0){
			$('<div class="divider"></div>').insertAfter($(this));
		}
		
	});
}
function cursor(container){
  $("#"+container).mousemove(function(e){
		var left = (e.pageX - $(this).offset().left);
		var top = (e.pageY - $(this).offset().top);
		if ( left >= 0 ) {
			//left = 0;
		}
		$("#" + container + "-img").css('left', left + "px");
		$("#" + container + "-img").css('top', top + "px");
		//$("#view-p-img").css('left', left + "px");
	});
	$("#"+container).bind('mouseenter', function() {
		$("#" + container + "-img").show();
  	});
  	$("#"+container).bind('mouseleave', function() {
		$("#" + container + "-img").hide();
  	});
}
