$(document).ready(function(){	
	
	/* HÖHE VON AKTUELLES WIRD GESETZT */
	
	h=0;
	$('#contentSub li .aktuelles li').each(function() {
		x=$(this).height();
		if(parseFloat(x)>h) {
			h=parseFloat(x);
		};
	});
	$('#contentSub li .aktuelles').css('height', h+'px');
	
	
	/* AKTUELLES ITEMS WIRD GESETZT */
	var i = 1;
	$('#contentSub li .aktuelles li').each(function() {
		$(this).css({
			display: 'block',
			position: 'absolute'
		}).attr('id', 'aktuellesItem-'+i);
		
		if(i>1) {
			$(this).css('marginLeft', '215px');
		}
		i++;
	});
		
	/* AKTUELLES NAVIGATION */
	i = 1;
	$('#contentSub li .aktuelles').parent().attr('id', 'aktellesNewsBox');
	$('#aktellesNewsBox .aktuelles').after('<ul id="aktuellesNav" class="bullets"></ul>');
	$('#contentSub li .aktuelles li').each(function() {
		title = $(this).find('h4').text();
		$('#aktuellesNav').append('<li><a id="aktuellesNavBtn-'+i+'" title="'+title+'">'+title+'</a></li>');
		i++;
	});
	$('#aktuellesNav li:first a').addClass('active');

	aktuellesCount = $('a[id^="aktuellesNavBtn-"]').length;
	aktuellesTime = 8000;
	activeBtn = $('#aktuellesNav .active');
	nextBtn = $('#aktuellesNav .active').parent().next('li').find('a');
	
	$('[id^="aktuellesNavBtn-"]').click(function(){
		nextBtn = $(this);
		aktuellesShift();
	});

	aktuellesInterval = setInterval(aktuellesShift,aktuellesTime);
	$('#aktellesNewsBox').hover(
        function(){ clearInterval(aktuellesInterval); },
        function(){ aktuellesInterval = setInterval(aktuellesShift,aktuellesTime); }
    );
	
	
	/* ADDITIONAL INFO BOX */
	if ($('#addInfoBoxNavi').length==1) {
		$('#addInfoBox'+$('#addInfoBoxNavi li a.active').attr('href').replace('#', '')).show(1);
		$('#addInfoBoxNavi li a').click(function(){
			 $('#addInfoBoxNavi li a.active').removeClass('active');
			 $('#addInfoBoxContent li.active').hide(1000);
			 $(this).addClass('active');
			 $('#addInfoBox'+$(this).attr('href').replace('#', '')).addClass('active');
			 $('#addInfoBox'+$(this).attr('href').replace('#', '')).show(1000);
			 return false;
		});
	}

	$('#addInfoBoxFaq dl dt a').click(function(){
		 $('#addInfoBoxFaq dd.active').slideUp(1000);
		 $('#addInfoBoxFaq dd.active').removeClass('active');
		 $(this).parent().next().addClass('active');
		 $(this).parent().next().slideDown(1000);
	});

	/* TESTIMONIAL BROWSING */
	$('#testimonialSelection li:first').addClass('active');

	$('#testimonialSelection li').click(function() {
		clicked=$(this);
		id=$(this).attr('id').substring(21);
		$.get('ajax.php', {page: 'testimonialDetails', id: id}, function(data) {
			$('#testimonialWrapper').html(data);
			$('#testimonialSelection li').removeClass('active');
			clicked.addClass('active');
			$('html,body').animate({
				scrollTop: $('#testimonialWrapper').offset().top-10}, { queue:false, duration:500 });
		});
	});
	
	/* TESTIMONIAL GALLERY */
	$('#testimonialGallery').append('<ul id="testimonialGalleryNav" class="bullets"></ul>');
	i=1;
	$('#testimonialGallery img').each(function() {
		$(this).attr('id', 'testimonialGalleryImg-'+i);
		$('#testimonialGalleryNav').append('<li><a id="testimonialGalleryImgBtn-'+i+'"></a></li>');
		i++;
		});
	$('#testimonialGallery img:first').addClass('active');
	$('#testimonialGallery img:first').fadeIn(1);
	$('#testimonialGalleryNav li:first a').addClass('active');

	$('#testimonialGalleryNav a').live('click', function(){
		$('#testimonialGallery img').fadeOut(1000, function(){
			$(this).removeClass('active');
		});
		$('#testimonialGalleryNav li a').removeClass('active');
		$(this).addClass('active');
		$('#testimonialGalleryImg-'+$(this).attr('id').substring(25)).fadeIn(1000, function(){
			$(this).addClass('active');
		});
	});
	
	/* LAENDER GALLERY */
	
	$('#laenderGallery').append('<ul id="laenderGalleryNav" class="bullets"></ul>');
	i=1;
	$('#laenderGallery img').each(function() {
		$(this).css('cursor', 'pointer');
		$(this).attr('id', 'laenderGalleryImg-'+i);
		$('#laenderGalleryNav').append('<li><a id="laenderGalleryImgBtn-'+i+'"></a></li>');
		i++;
		});
	$('#laenderGallery img:first').addClass('active');
	$('#laenderGallery img:first').fadeIn(1);
	$('#laenderGalleryNav li:first a').addClass('active');

	$('#laenderGalleryNav a').click(function(){
		galleryChangePicture($(this).attr('id').substring(21));
	});
	
	if($('#laenderGallery').length){
		$('#laenderGallery img.active').live('click', function(){ nextPic(); });
		galleryTime = 5000;
		galleryInterval = setInterval("nextPic()", galleryTime);
		$('#laenderGallery').hover(
			function(){ clearInterval(galleryInterval); },
			function(){ galleryInterval = setInterval("nextPic()", galleryTime); }
		);
	};

	
	/*VERLAUF FÜR FORMULARE*/
	vc = $('#verlauf li').length;
	va = $('#verlauf').width() / (vc-1);
	i = 0;
	$('#verlauf li').each(function() {
		margin = (i*va)-($(this).width()/2);
		$(this).css('marginLeft', margin);
		$(this).attr('id', 'stepBtn-'+i++);
	});
	$('#verlauf li:first-child').addClass('selected');
	$('#formularSeiten > div').css('display', 'none');
	$('#formularSeiten > div:first-child').css('display', 'block');
	
	i = 0
	$('#formularSeiten > div').each(function() {
		$(this).attr('id', 'step-'+i++);
	});

	$('#verlauf li a').click(function(){
		if (reqCheck()) {
			$('#verlauf li').removeClass('selected');
			$('#formularSeiten > div').css('display', 'none');
			$(this).parent().addClass('selected');
			$('#step-'+$(this).parent().attr('id').substring(8)).css('display', 'block');
		}
	});

/* END DOCUMENT READY */
});

function changeSteps(stepID, bereich) {
	$.get("setCatLog.php", { stepID: stepID, bereich: bereich } );	
	if (reqCheck()) {
		$('#verlauf li').removeClass('selected');
		$('#formularSeiten > div').css('display', 'none');
		$('#stepBtn-'+stepID).addClass('selected');
		$('#step-'+stepID).css('display', 'block');
	}
}

var aktuellesCount, aktuellesInterval, nextBtn, activeBtn, aktuellesTime;
function aktuellesShift(){
	nextItm = $('#aktuellesItem-'+getBulletIDNumber(nextBtn));
	activeItm = $('#aktuellesItem-'+getBulletIDNumber(activeBtn));
	
	nextItm.animate({marginLeft: 0},700);
	activeItm.animate({marginLeft: '-215px'},700, function() {$(this).css('marginLeft', '215px');});
	
	activeBtn.removeClass('active');
	nextBtn.addClass('active');
	
	activeBtn = nextBtn;
	nextID = getBulletIDNumber(activeBtn)+1;
	if(nextID > aktuellesCount){ nextID = 1; }
	nextBtn = $('#aktuellesNavBtn-'+nextID);
}

function getBulletIDNumber(object){
	return parseInt(object.attr('id').replace('aktuellesNavBtn-', ''));
}

/* FORMULAR */

function addBewerbungsZiel() {
		var lastID = $('#bewerbungsZiele li:last-child').attr('id');
		
		if(!lastID){
			nextID = 0;}
		else {
		lastID = lastID.split('-');
		var nextID = parseInt(lastID[1])+1;
		}
		
		var content ='';
		
		content+='<li id="bewerbungsZiel-'+nextID+'">';
		content+='<select name="zielSchule[]">';
		content+='<option>High School USA</option>';
		content+='<option>Private School USA</option>';
		content+='<option>Internat/Boarding School USA</option>';
		content+='<option>High School Kanada</option>';
		content+='<option>Internat/Boarding School Kanada</option>';
		content+='<option>High School Nova Scotia</option>';
		content+='<option>High School Australien</option>';
		content+='<option>Internat/Boarding School Australien</option>';
		content+='<option>High School Neuseeland</option>';
		content+='<option>Internat/Boarding School Neuseeland</option>';
		content+='</select>';
		content+=' ';
		content+='<select name="austauschDauer[]">';
		content+='<option>ein Schuljahr</option>';
		content+='<option>ein Semester</option>';
		content+='<option>3 Monate (Kanada)</option>';
		content+='<option>4 Terms (AUS/NZ)</option>';
		content+='<option>3 Terms (AUS/NZ)</option>';
		content+='<option>2 Terms (AUS/NZ)</option>';
		content+='<option>1 Term (AUS/NZ)</option>';
		content+='<option>bis zum High School Diplom</option>';
		content+='</select>';
		content+=' ';
		content+='<a href="javascript: deleteBewerbungsZiel(\'bewerbungsZiel-'+nextID+'\');"><img src="img/formular/delete.png"></a>';
		content+='</li>';
		
		$('#bewerbungsZiele').append(content);
}

function deleteBewerbungsZiel(deleteID) {
		$('#'+deleteID).remove();
}

function reqCheck() {
	$('div.reqNF').removeClass('reqNF');
	check = true;
	error = 'Es wurden nicht alle Pflichtfelder ausgef\u00fcllt!';
	if ($('#verlauf li.selected').length) {
		stepID = $('#verlauf li.selected').attr('id').split('-');
		ansprache = '#step-'+stepID[1]+' div.req input';
	}
	else {
		ansprache = 'div.req input';
	}
	$(ansprache).each(function() {
		switch($(this).attr('type')) {
			case 'text':
				if($(this).val()=='') {
					check = false;
					$(this).parent().addClass('reqNF');
				}
			break;
			case 'radio':
			case 'checkbox':
				if($('div.req input[name="'+$(this).attr('name')+'"]:checked').length<1) {
					check = false;
					$(this).parent().addClass('reqNF');
				}
			break;
		}
	});
	$('div.req textarea').each(function() {
		if($(this).val()=='') {
			check = false;
		}
	});
	if(check!=true) {
		alert(error);
	}
	return check;
}

/* LAENDER GALLERY */

function galleryChangePicture(targetID){
	if (targetID > $('#laenderGalleryNav li').length) {
		targetID = 1;
	}
	$('#laenderGallery img').fadeOut(1000);
	$('#laenderGalleryNav li a').removeClass('active');
	
	$('#laenderGalleryImgBtn-'+targetID).addClass('active');
	$('#laenderGalleryImg-'+targetID).fadeIn(1000, function(){
		$(this).addClass('active');
	});
}

function nextPic() {
	activePicID = $('#laenderGalleryNav li a.active').attr('id').substring(21);
	nextPicID = parseInt(activePicID)+1;
	galleryChangePicture(nextPicID);
}

/* PRessemitteilungen */

if($('#accordion').length){
	$("#accordion").accordion();
}

function showPM(prtid)
{
	$.get('getPressemitteilung.php', {prtid: prtid}, function(data) {
		$('#pminhalt').html(data);
	});
	$('#dialog').dialog({ autoOpen: false, height: 500, width: 600 });
	$('#dialog').dialog('open');
}

function showPS(psid)
{
	$.get('getPressespiegel.php', {psid: psid}, function(data) {
		$('#pminhalt').html(data);
	});
	$('#dialog').dialog({ autoOpen: false, height: 500, width: 600 });
	$('#dialog').dialog('open');
}

function showNL(nlid)
{
	$.get('getNewsletter.php', {nlid: nlid}, function(data) {
//		$('#nlinhalt').html(data);
		$('#nlinhalt_frame').attr("src", "newsletter/" + data);
	});
	$('#dialog').dialog({ autoOpen: false, height: 500, width: 750 });
	$('#dialog').dialog('open');
}

function log(txt){ if(window.console && window.console.log) window.console.log(txt); }
