var archivJahr = 0;

$(document).ready(function(){
	
	$('img').load(function () {$(this).animate({ opacity: 1}, {duration:500});});
	setTimeout(function(){addPanel(0,'menu',0);},500);
	setTimeout(function(){addPanel(1,'seite',2);},1500);
	$('#logo').click(switchScreen);
	var isFullScreen = false;
	
	if (debug) {
		document.title="preview";
	}

});
	


function menuClicked(menupunkt, categorie, menuID) {
	if (!menupunkt.hasClass('linkNavigationSelected')){
		menupunkt.parent().find('.linkNavigationSelected').each(function(){
			$(this).removeClass().addClass('linkNavigation');
		});
		menupunkt.removeClass().addClass('linkNavigationSelected');
		
		var newLevel = parseInt(menupunkt.closest('.panel').attr('level'))+1;
		addPanel(newLevel, categorie, menuID);
	}
}

function openPage(IDStr) {
	IDStr+="";
	var myID;
	if (IDStr.search(',')>=0){
		var IDArray = IDStr.split(',');
		myID = IDArray.pop();
		setTimeout('openPage(\''+IDArray.join(',')+'\')',750);
	} else {
		myID=IDStr;
	}
	$('.linkNavigation').each(function(){
			if ($(this).attr('menuID')==myID){
				menuClicked($(this), $(this).attr('categorie'), $(this).attr('menuID'));
			}
	});
}

function openProject(pID) {
		if (archivJahr==0) setTimeout(function(){projectClick(pID);},2000);
		else setTimeout(function(){projectClick(pID);},3000);
		$('.linkNavigation').each(function(){
			if ($(this).attr('menuID')==1){
				menuClicked($(this), $(this).attr('categorie'), $(this).attr('menuID'));
			}
		});
}


function addPanel(level, categorie, menuID) {
	var newLevelx = 0;
	// remove childrens
	$('.panel').each(function(){
			var thisLevel = parseInt($(this).attr('level'));
			if(thisLevel==level-1) newLevelx = $(this).position().left + $(this).width() + 19;
			if(thisLevel>=level) {
				var rect = 'rect(0px auto auto '+$(this).width()+'px)';
				var duration = $(panel).outerWidth()*1.5;
				if (duration<500) duration=500;
				$(this).stop().animate({clip:'rect((0px auto auto 0px))'}, 0).animate({clip:rect},  {duration:duration, complete:function(){$(this).remove();}});
				
			}
	});
	
	$(document).find('#thumbPanelBG').animate({width:'0px'}, {duration:300, complete:function(){$(this).remove(); $('#thumbPanelMargin').remove();}});
	$(document).find('.thumb').animate({opacity:'0'}, {duration:300, complete:function(){$(this).remove();  $('#thumbPanel').remove();}});
	$(document).find('#projectPage').animate({opacity:'0'}, {duration:300, complete:function(){$(this).remove();}});

	//add new level
	$('#navigation').append("<div class='panel' ID='p"+menuID+"' level='"+level+"'></div>");
	
	var loadURL;
	if (menuID==1) loadURL = "loadMenuProjekteAuswahl.php";
	else if (menuID==2) loadURL = "startseite.php";
	else if (menuID==4) { loadURL = "loadMenuArchiv.php"; archivJahr = 0;}
	else if (categorie=='menuProjekte') { loadURL = "loadMenuProjekte.php?sortier=name"; resetMenuProjekte() }
	else if (categorie=='menuArchivProjekte') { loadURL = "loadMenuArchivProjekte.php?jahr="+archivJahr;}
	else if (categorie=='menu') loadURL = "loadMenu.php?motherID="+menuID+"&debug="+debug;
	else if (menuID==24) loadURL = "partner.html";
	else loadURL = "loadSeite.php?menuID="+menuID;
	var panel = "#p"+menuID;
	$(panel).load(loadURL, function() {
			var duration = $(panel).outerWidth()*1.5;
			if (duration<500) duration=500;
			$(panel).css({ left: newLevelx});
			var myClip = 'rect(0px '+$(panel).width()+' auto 0px)';
			$(panel).delay(500).animate({clip:myClip}, duration);
			if (menuID==1) addPanel((level+1), 'menuProjekte', 'menuProjekte');
			if (menuID=='menuProjekte')  setTimeout(function(){addThumbPanel();},500);
			if (categorie=='menuArchivProjekte') setTimeout(function(){addArchivThumbPanel();},500);
	});

}

function addThumbPanel() {
	$('#navigation').append("<div id='thumbPanelBG' ><div id='thumbPanelMargin'>&nbsp;</div></div><div id='thumbPanel'></div><div id='projectPage'></div>");
	var tmp = $('#pmenuProjekte').position().left + $('#pmenuProjekte').width();
	$('#thumbPanelBG').width(0);
	$('#thumbPanelBG').css({'left':tmp+'px'});
	$('#thumbPanelBG').delay(500).animate({width:$(window).width()-tmp-60+'px'}, 500);
	$('#thumbPanel').load('loadGridProjekte.php?sortier=name');
	$('#thumbPanel').css({'opacity':'0'}).delay(1250).animate({opacity:1}, 500);
}


function addArchivThumbPanel() {
	$('#navigation').append("<div id='thumbPanelBG' ><div id='thumbPanelMargin'>&nbsp;</div></div><div id='thumbPanel'></div><div id='projectPage'></div>");
	var tmp = $('#pmenuArchivProjekte'+archivJahr).position().left + $('#pmenuArchivProjekte'+archivJahr).width();
	$('#thumbPanelBG').width(0);
	$('#thumbPanelBG').css({'left':tmp+'px'});
	//$('#thumbPanelBG').delay(500).animate({width:$(window).width()-tmp-60+'px'}, 500);
	$('#thumbPanel').load('loadArchivGridProjekte.php?jahr='+archivJahr);
	$('#thumbPanel').css({'opacity':'0'}).delay(500).animate({opacity:1}, 500);
}


function switchScreen(){
	if($('#thumbPanelBG').length) {
		//alert('ok');
	}
	
	
}


