var auswahl = "";
var sortier = "name";
var projectID = new Array();
var projectsSelected  = new Array();
var myProjectID = 0;
var projectMaxWidth = 0;

$(document).ready(function(){
	$.get("sortProjekte.php?auswahl="+auswahl+"&sortier="+sortier, function(data) {
		projectID = data.split(',');
  });
});
	
	
function resetMenuProjekte() {
	auswahl = "";
	sortier = "name";
	myProjectID = 0;
	archivJahr = 0;
}
	
function auswahlClicked(menupunkt) {
	menupunkt.parent().find('.auswahlProjekteSelected').each(function(){
		$(this).removeClass().addClass('auswahlProjekte');
	});
	menupunkt.removeClass().addClass('auswahlProjekteSelected');
	auswahl = menupunkt.attr('colname');
	updateProjekte();
}

function sortierClicked(menupunkt) {
		menupunkt.parent().find('.sortierProjekteSelected').each(function(){
			$(this).removeClass().addClass('sortierProjekte');
		});
		menupunkt.removeClass().addClass('sortierProjekteSelected');
		sortier = menupunkt.attr('colname');
		updateProjekte();
}

function updateProjekte(what){
	$.get("sortProjekte.php?auswahl="+auswahl+"&sortier="+sortier, function(data) {
	
		if (myProjectID>0) {
			myProjectID=0;
			$('.linkProjekteSelected').removeClass().addClass('linkProjekte');
			$('#projectPage').fadeOut(500);
		}
	
		// Texte
		projectsSelected = data.split(',');
		$('.linkProjektContainer').attr('display','false');
		var tmpWidth = 0;
		var duration = 500;
		if(what=='init') duration=0;
		
		for(i=1; i<projectsSelected.length; i++){
			var tmpID='#linkProjektContainer'+projectsSelected[i];
			if($(tmpID).css('display')=='none') $(tmpID).css({'top':(i*13-1)+'px','display':'block', 'opacity':'0'});
			$(tmpID).css({'position':'absolute'});
			$(tmpID).animate({top:(i*13-1)+'px', opacity:1}, duration);
			$(tmpID).attr('display','true');
			if ($(tmpID).width()>tmpWidth) tmpWidth = $(tmpID).width();
		}
		
		for(i=1; i<projectID.length; i++){
			var tmpID='#linkProjektContainer'+projectID[i];
			if($(tmpID).attr('display')=='false') {
				$(tmpID).fadeOut(500);
			}
		}
		
		
		// Thumb
		$('.thumb').attr('display','false');
		var w = $(window).width()- ($('#pmenuProjekte').position().left + tmpWidth) - 60;
		var h = $(window).height()- $('#pmenuProjekte').position().top - 20;
		var l = $('#pmenuProjekte').position().left + tmpWidth;
		$('#thumbPanelBG').animate({'left':l+'px', 'width':w+'px'}, duration);
		
		l += 18;
		w -= 18;
		
		projectMaxWidth = w;
		
		var nbProjekte = projectsSelected.length;
		var nbCol = Math.ceil(Math.sqrt(nbProjekte));
		var nbRow = Math.ceil(nbProjekte/nbCol);
		var espacement = 13;
		var thumbWidth = Math.round((w - (nbCol-1)*espacement)/nbCol);
		var thumbHeight =  Math.round((h - (nbRow-1)*espacement)/nbRow);
		if (thumbHeight>Math.round(thumbWidth/4*3)) thumbHeight =  Math.round(thumbWidth/4*3);
		if (thumbHeight<Math.round(thumbWidth*0.65)) {
			nbCol++;
			if (thumbHeight<Math.round(thumbWidth*0.4)) nbCol++;
			nbRow = Math.ceil(nbProjekte/nbCol);
			thumbWidth = Math.round((w - (nbCol-1)*espacement)/nbCol);
			thumbHeight =  Math.round((h - (nbRow-1)*espacement)/nbRow);
			if (thumbHeight>Math.round(thumbWidth/4*3)) thumbHeight =  Math.round(thumbWidth/4*3);
		}

		var myCol = myRow = myWidth = myHeight = 0;
		for(i=1; i<projectsSelected.length; i++){
			var tmpID='#thumb'+projectsSelected[i];
			$(tmpID).attr('display','true');
			
			var myX = l + Math.floor(myCol*(thumbWidth+espacement));
			var	myY = 14 + Math.floor(myRow*(thumbHeight+espacement));

			myCol++;
			if (myCol>=nbCol) {
				myCol=0;
				myRow++;
			}

			
			var myImg = $(tmpID).find('img');
			var ratioImg = myImg.attr('ratio');
			var ratioThumb = thumbWidth/thumbHeight;

			if ((ratioImg-ratioThumb)>0) {
				myHeight = thumbHeight;
				myWidth = myHeight*ratioImg;
			} else {
				myWidth = thumbWidth;
				myHeight = myWidth/ratioImg;
			}
			
			
			var clipX = 0; //(myWidth-thumbWidth)/2; //Bug: the mask only works at the end of the animation
			var clipY = 0;
			if (myHeight > thumbHeight+2) clipY = (myHeight-thumbHeight)/2;
			myX -= clipX;
			myY -= clipY;
			
			var myClip = 'rect('+clipY+'px '+(thumbWidth+clipX)+'px '+(thumbHeight+clipY)+'px  '+clipX+'px)';
			if($(tmpID).css('display')=='none') {
				myImg.css({width:myWidth+'px',height:myHeight+'px'});
				$(tmpID).css({'left':myX+'px', 'top':myY+'px', clip:myClip, 'display':'block', 'opacity':'0'});
			}
			$(tmpID).animate({left:myX+'px', top:myY+'px', clip:myClip, opacity:1}, duration);
			myImg.animate({width:myWidth+'px',height:myHeight+'px'}, duration);
		}
		
		for(i=1; i<projectID.length; i++){
			var tmpID='#thumb'+projectID[i];
			if($(tmpID).attr('display')=='false') {
				$(tmpID).fadeOut(500);
			}
		}

		
	});
}

function menuOver(pID){
	if (myProjectID==0) {
	 $('.thumb').each(function(){
		if ($(this).attr('ID') == 'thumb'+pID) {
			$(this).stop().animate({opacity:1}, 500);
			$(this).parent().find('.thumbDescription').stop().animate({opacity:1}, 500);
		} else  {
			$(this).stop().animate({opacity:0.25}, 500);
			$(this).parent().find('.thumbDescription').stop().animate({opacity:0.25}, 500);
		}
	 });
 }
}

function menuOut(pID){
	if (myProjectID==0) {
		$('.thumb').stop().animate({opacity:1}, 500);
		$('.thumbDescription').stop().animate({opacity:1}, 500);
	}
}


function thumbOver(pID){
	if (myProjectID==0) {
	 $('.thumb').each(function(){
		if ($(this).attr('ID') == 'thumb'+pID) {
			$(this).stop().animate({opacity:1}, 500);
			$(this).parent().find('.thumbDescription').stop().animate({opacity:1}, 500);
		} else  {
			$(this).stop().animate({opacity:0.25}, 500);
			$(this).parent().find('.thumbDescription').stop().animate({opacity:0.25}, 500);
		}
	 });

	 $('.linkProjekte').each(function(){
		if ($(this).attr('projektID') == pID) $(this).addClass('highlightMenu');
	 });
	}
}

function thumbOut(pID){
	if (myProjectID==0) {
		$('.linkProjekte').removeClass('highlightMenu');
		$('.thumb').stop().animate({opacity:1}, 500);
		$('.thumbDescription').stop().animate({opacity:1}, 500);
	}
}

function projectClick(pID){
	myProjectID = pID;
	
	// menu
	$('.linkProjekteSelected').removeClass().addClass('linkProjekte');
	$('.linkProjekte').each(function(){
		if ($(this).attr('projektID') == pID) $(this).removeClass().addClass('linkProjekteSelected');
	});
	
	//thumb
	var myThumb='#thumb'+myProjectID;
	var w = projectMaxWidth;
	var h = $(window).height()- $('#thumbPanelBG').position().top - 140;
	var l = $('#thumbPanelBG').position().left + 18;
		
	var myImg = $(myThumb).find('img');
	var ratioImg = myImg.attr('ratio');
	if (ratioImg<1) {
			myHeight = h;
			myWidth = myHeight*ratioImg;
			if (myWidth>w) {
				myWidth = w;
				myHeight = myWidth/ratioImg;
			}
	} else {
			myWidth = w;
			myHeight = myWidth/ratioImg;
			if (myHeight>h) {
				myHeight = h;
				myWidth = myHeight*ratioImg;
			}
	}

	var myClip = 'rect(0px '+myWidth+'px '+myHeight+'px  0px)';
	if($(myThumb).css('display')=='none'){
		$(myThumb).css({left:l+'px', top:'29px', clip:myClip, 'display':'block'});
		myImg.css({width:myWidth+'px',height:myHeight+'px'});
	}
	$(myThumb).animate({left:l+'px', top:'29px', clip:myClip, opacity:1}, 500);
	myImg.animate({width:myWidth+'px',height:myHeight+'px'}, 500);
	$('#thumbPanelBG').animate({'width':(myWidth+18)+'px'}, 500);
	
	$('.thumb').each(function(){
		if ($(this).attr('ID') != 'thumb'+pID) $(this).stop().fadeOut(500);
	});
	
	$('#projectPage').css({left:l+'px'});
	$('#projectPage').animate({'width':myWidth+'px'}, 500);
	$('#projectPage').load('loadProjekt.php?projektID='+myProjectID);

}

function submenuClick(submenu) {
	$(document).find('.submenuSelected').each(function(){
		$(this).removeClass().addClass('submenu');
	});
	submenu.removeClass().addClass('submenuSelected');
	if (submenu.html()!='1') $('.thumb').fadeOut(0);
	$('#projectContent').animate({opacity:0}, {duration:250, complete:function(){
		var theHTML ='';
		//load image
		if($(submenu).attr('dateiname')!='') {
			var w = projectMaxWidth;
			var h = $(window).height()- $('#thumbPanelBG').position().top - 140;

			var ratioImg = $(submenu).attr('w')/$(submenu).attr('h')
			if (ratioImg<1) {
					myHeight = h;
					myWidth = myHeight*ratioImg;
					if (myWidth>w) {
						myWidth = w;
						myHeight = myWidth/ratioImg;
					}
			} else {
					myWidth = w;
					myHeight = myWidth/ratioImg;
					if (myHeight>h) {
						myHeight = h;
						myWidth = myHeight*ratioImg;
					}
			} 		
			theHTML +="<img ID='projectImg' src='loadPict.php?dateiname="+$(submenu).attr('dateiname')+"&w="+myWidth+"' width='"+myWidth+"' height='"+myHeight+"'>";
		} else {
			myWidth = 400;
		}
		
		theHTML += "<div class='headlineBloc' style='margin-top:6px;'>"+$(submenu).attr('projectName')+"</div>";
		theHTML += "<div class='textBloc' style='margin-top:6px;'>"+$(submenu).attr('txt')+"</div>";
		theHTML += "<div ID='projectText'></div>";
		theHTML += "<div ID='datenBloc'></div>";
		$(this).html(theHTML);
		
		if (submenu.html()=='1') $('#datenBloc').load('loadDatenBloc.php?summary=true&projektID='+$(submenu).attr('projektID'));
		if (submenu.html()=='Info') {
			$('#projectText').load('loadSeite.php?projektID='+$(submenu).attr('projektID'));
			$('#datenBloc').load('loadDatenBloc.php?projektID='+$(submenu).attr('projektID'));
		}
		
		if($(submenu).attr('dateiname')=='' || $(submenu).html()=='Info') $(this).animate({opacity:1},500);
		else {
			$('#projectImg').load(function () {$(this).css({ opacity: 1}, 500); $(this).parent().animate({ opacity: 1}, 500);});
			$('#projectImg').click(function () {
				if (archivJahr==0) nextSubmenu($(submenu));
				else showArchivThumbs();
			});
			
		}
		$('#projectPage').animate({'width':myWidth+'px'}, 500);
		$('#thumbPanelBG').animate({'width':(myWidth+18)+'px'}, 500);
	}});
}

function nextSubmenu(submenu) {
	if (submenu.html() == 'Info') tmp = 1;
	else tmp = parseInt(submenu.html())+1;
	if (tmp > $(".submenu").size()) submenuClick($('#subInfo'));
	else submenuClick($('#sub'+tmp));
}

