
$(document).ready(init);

function init(){
	// global
	//defeatSpam3000();
	
	// ie6 menufix
	if($.browser.msie && $.browser.version < 7){
		$('.topnav').hover(
		function() {
			$(this).children('ul.subnav').show();
		},
		function() {
			$(this).children('ul.subnav').hide();
		});
	}
	
	$("html").removeClass("nojs");
	
	$('h1').click(function(){window.location.href = '/index.php'});
	
	
	$('.topnavlink').hover(
		function() {
			$(this).parent().addClass('active');
	},
		function() {
			$(this).parent().removeClass('active');
	});
	
	
	$('.subnav > li').hover(
		function() {
			$(this).parent().parent().addClass('active');
	},
		function() {
			$(this).parent().parent().removeClass('active');
	});
	
	
	$('.listPanel h4').click(function(){showInfosEntries(this)});
	
	
	$('.thumbNavPic').hover (
		function() {
			$(this).fadeTo(200, 0.7);
		},
		function() { 
			$(this).fadeTo(200, 1.0);
		}
	);
	
	
	$('.thumbDiv>img').hover (
		function() {
			$(this).parent('.thumbDiv').children('.thumbLegende').fadeIn('fast');
			//$('.blackoutDiv').fadeTo(0, 0.5);
			$(this).fadeTo(200, 0.7);
		},
		function() { 
			$(this).parent('.thumbDiv').children('.thumbLegende').hide();
			//$('.blackoutDiv').fadeTo(200, 0);
			$(this).fadeTo(200, 1.0);
		}
	);

	
	$('.karteOver').hover (
		function() {
			$(this).parent('.kartenOrt').children('.karteLegende').fadeIn('fast');
		},
		function() { 
			$(this).parent('.kartenOrt').children('.karteLegende').hide();
		}
	);
	
	// klick-funktion auf kartenort fuer fototour-div
	$('.karteOver').click(function() {showFototourDiv(this)});
	

	
	$('#blackoutDiv').click(function() {hideFototourDiv()});
	$('.closeWindowButton').click(function() {hideFototourDiv()});
		
	$(".thumbsContainerDiv>div").click(function() {loadFototour(this)});

	$('.pictureBig').live('click', function() {showNextPicture(this)});
	//$('.klickPicNote').live('click', function() {showNextPicture(this)});
	$('.nextPic').live('click', function() {showNextPicture(this)});
	$('.lastPic').live('click', function() {showPrevPicture(this)});
	$('.last .pictureBig').live('click', function() {showFirstPicture()});
	$('.last .nextPic').live('click', function() {showFirstPicture()});
	$('.first .lastPic').live('click', function() {showLastPicture()});
	
	
	
	$('.thumbDiv').click(function() {showPersonDiv(this)});
	
	$('#links_kantonsschulen .kartenOrt').click(function() {showAdressDiv(this)});
	
	$('.showAllAdresses').click(function() {showAllAdressDivs()});


}


/* create a timeOut function in jQuery */
jQuery.fn.idle = function(time){
return this.each(function(){
var i = $(this);
i.queue(function(){
setTimeout(function(){
i.dequeue();
}, time);
});
});
};

function showInfosEntries(h4){
	$(h4).parent('div').children('h5').slideToggle('fast');
	$(h4).parent('div').children('p').slideToggle('fast');

}


function showPersonDiv(person){
	var selectedPerson = $(person).attr('id').replace(/Thumb/,'Div');
	
	$('.portraitDiv').hide();
	$('#'+selectedPerson).fadeIn(600);
	//$('.blackoutDiv').fadeTo(200, 0);
	$('.thumbLegende').hide('fast');
	//alert(selectedPerson);
}


function showFototourDiv(school){
	if  ( !($(school).attr('id'))) {
		return false;
	}
	
	var selectedSchool = $(school).attr('id').replace(/Kartenort/,'Fototour');
	
	$('.fototourGalleryDiv').hide();
	// fuer bruehl nicht machen, da externes Fenster mit galerie
	
	$('#'+selectedSchool).fadeIn(600);
	$('#blackoutDiv').show().fadeTo(200, 0.8);
	
	
	$('.thumbLegende').hide('fast');
}

function loadFototour(category){
	var selectedSchool = $(category).attr('id').replace(/_schulalltagNav/,'').replace(/_projekteNav/,'').replace(/_veranstaltungenNav/,'').replace(/_schulgebaeudeNav/,'');
	var categoryFilename = $(category).attr('id').replace(/Nav/,'_gallery');
	//$('#'+selectedSchool+'Fototour .pictureContainerDiv').children().remove();
	$('#'+selectedSchool+'Fototour .pictureContainerDiv').load('../fototour/'+categoryFilename+'.php');
}

function showNextPicture(current) {
	//alert(current);
	$('.pictureBig').css({'display':'block'}).parent().fadeOut('fast');
	$(current).parent().next().fadeIn('fast');
}

function showPrevPicture(current) {
	//alert(current);
	$('.pictureBig').css({'display':'block'}).parent().fadeOut('fast');
	$(current).parent().prev().fadeIn('fast');
}

function showFirstPicture() {
	$('.pictureBig').parent().hide();
	$('.first').fadeIn('fast');
}

function showLastPicture() {
	$('.pictureBig').parent().hide();
	$('.last').fadeIn('fast');
}


function hideFototourDiv() {
	$('.fototourGalleryDiv').fadeOut('fast');
	$('#blackoutDiv').fadeOut('fast');
}

function showAdressDiv(adress){
	var selectedAdress = $(adress).attr('id').replace(/karte/,'adresse');
	
	$('.adressPanel').hide();
	$('#'+selectedAdress).slideDown('fast');
	$('.karteLegende').hide('fast');
	//alert(selectedAdress);
}

function showAllAdressDivs() {
	$('.adressPanel').slideDown('fast');
	}

function getRandomBetween(min, max){
	var randomNum = Math.random() * (max-min); 
	return(Math.round(randomNum) + min); 
}



$(function() {
var portraitmenge = 	$('.portraitDiv').length - 1;   
 var randomNum = getRandomBetween(0, portraitmenge);
$('.portraitDiv:eq(' + randomNum + ')').css("display", "block");
});

