
/*************************************************** Externe Links vorbereiten */

function externalLinks() 
	{	
	if (!document.getElementsByTagName) return; 
 	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++)
		{ 
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "blank")anchor.target = "_blank"; 
		} 
	} 
	
/*************************************************** IE6 */

function handleIE6()
	{
	if($.browser.msie && $.browser.version<7)
		{
		var input = $("input[type='text']").addClass("ie6textinput");
		var textarea = $("textarea").addClass("ie6textinput");
		var password = $("input[type='password']").addClass("ie6textinput");
		}
	}

/*************************************************** css */

function css(parent,element,css)
	{
	if($(parent).length)
		{
		$(parent).find(element).css(css)
		}
	}

/*************************************************** tabnav */

var tabs, togglers, hash;

function changeTab(id)
	{
	id = id.length>1 ? id.substr(11,1) : id;
	if(!id)id=1;
	tabs.hide();
	var links = togglers.find('a');
	links.removeClass('active');
	var tab = $('#tab'+id);
	var toggler = $('#toggler'+id);
	if(tab.length)
		{
		tab.fadeIn(500);
		toggler.addClass('active');
		$(window.location).attr("hash", '#active_'+tab.attr('id'));
		}
	}
	
function tabNav()
	{
	if($('#content .tabnav').length && $('#content .tab').length>1)
		{
		togglers = $('#content').find('.tabnav li');
		tabs = $('#content .tabnav').css({'display':'block'}).nextAll('.tab');
		hash = window.location.hash ? window.location.hash : '#active_'+tabs.eq(0).attr('id');
		var firstId = tabs.eq(0).attr('id').substr(3,1);
		var id = parseInt(firstId);
		togglers.each(function(idx)
			{
			id = parseInt(idx)+parseInt(firstId);
			html = $(this).html();
			$(this).wrapInner('<a href="javascript:;" onclick="changeTab('+id+');" id="toggler'+id+'" title="'+html+'"></a>');
			$(this).find('a').wrapInner('<span></span>');
			});
		changeTab(hash);
		}
	else if($('#content .tab').length==1)
		{
		$('#content .tab').css({'display':'block'});
		}
	}

/*************************************************** faqnav */

function faqNav()
	{
	if($('#content .faqnav').length)
		{
		var ul = $('#content .faqnav');
		$('#content .tab').css({'display':'block'});
		var h3s = $('#content .tab h3');
		h3s.each(function(idx)
			{ 
			var html = $(this).html();
			$(this).before('<a id="faq'+idx+'" name="faq'+idx+'"></a>');
			$(this).prepend('Q: ');
			$(this).next('p').prepend('<b>A:</b> ');
			ul.append('<li><a href="'+currentPage+'#faq'+idx+'" title="'+html+'">'+html+'</a></li>');
			});
		}
	}

/*************************************************** courseLevels */

function prepareToolTips()
	{
	$("#content .tooltip").simpletooltip();
	}
	
function courseLevels()
	{
	if($('#content .courselevel').length)
		{
		var courselevels = $('#content .courselevel');
		courselevels.each(function()
			{
			var out = '';
			var html = trim($(this).html(),'');
			if(html.indexOf(',')!=-1)
				{
				var parts = html.split(', ');
				var char = ', ';
				//console.log(parts);
				}
			if(html.indexOf('/')!=-1)
				{
				var parts = html.split('/');
				var char = '/';
				}
			if(html.indexOf('-')!=-1)
				{
				var parts = html.split('-');
				var char = '-';
				}
			if(!parts)
				{
				var parts = new Array();
				parts[0] = html;
				}
			$(parts).each(function(idx){
				if(out!="")
					out += char;
				out += '<span class="tooltip">'+this+'<span class="tooltiptext"><strong>'+this+'</strong> <br />'+coursLevelDescs['tooltip'+this.toLowerCase()]+'</span></span>';
				});
			$(this).empty();
			$(this).html(out);
			prepareToolTips();
			});
		}
	}

/*************************************************** calendars */

function calendars()
	{
	if($('form .date').length)
		{
		var a=new Date();
		var dateInputs = $('form').find('.date input');
		$.datepicker.setDefaults($.extend({constrainInput:true,showOn: 'button', buttonImage: './files/assets/ico_calendar.gif',buttonImageOnly: true, prevText:'&laquo;',nextText:'&raquo;',showOtherMonths:true,showMonthAfterYear:false,defaultDate:a,showOptions:{direction:'right'},dateFormat:'dd.mm.yyyy'},$.datepicker.regional['de']));
		dateInputs.each(function(){ $(this).datepicker(); });
		}
	}

function calendarsRange()
	{
	/*if($('form .daterange').length)
		{
		var a=new Date();
		var dateInputs = $('form').find('.daterange input');
		$.datepicker.setDefaults($.extend({constrainInput:true,showOn: 'button', buttonImage: './files/assets/ico_calendar.gif',buttonImageOnly: true, prevText:'&laquo;',nextText:'&raquo;',showOtherMonths:true,showMonthAfterYear:false,defaultDate:a,showOptions:{direction:'right'},dateFormat:'dd.mm.yy'}));
		dateInputs.each(function(){ $(this).datepicker(); });
		}*/
	}

/*************************************************** formSlide */

function formSlide(slide,toggler)
	{
	if($(toggler).length)
		{
		var slide = $(slide).hide();
		$(toggler).click(function(){ slide.toggle(500); });
		if($(toggler).is(':checked'))
			slide.toggle();
		}
	}

function formSlideRadio(slide,toggler,others)
	{
	if($(toggler).length)
		{
		var slide = $(slide).hide();
		$(others).click(function(){ if($(toggler).is(':checked') && slide.is(':hidden')) slide.show(500);else if(!$(toggler).is(':checked') && slide.is(':visible')) slide.hide(500)});
		if($(toggler).is(':checked'))
			slide.show();
		}
	}

function formSlideSelect(slide,toggler,triggerValue)
	{
	if($(toggler).length)
		{
		var slide = $(slide).hide();
		$(toggler).change(function() {
			if(triggerValue==$(this).val()) slide.show(500);
			else slide.hide(500);
			});
		if($(toggler).val()==triggerValue) slide.show();
		}
	}


/*************************************************** testChanger */

function testChanger()
	{
	if($('select#slice').length)
		{
		var countTabs = $('.tabnav').find('a').length-1;
		$('select#slice').change(function(){ window.location = currentPage+'?slice='+$(this).attr('value')+'#active_tab'+countTabs; });
		}
	}

function testChangerCompany()
	{
	if($('select#sliceCompanies').length)
		{
		var countTabs = $('.tabnav').find('a').length-1;
		$('select#sliceCompanies').change(function(){ window.location = currentPage+'?slice='+$(this).attr('value')+'&testId='+$('#testid').attr('value')+'#active_tab'+countTabs; });
		}
	}

/*************************************************** clients */

function clients()
	{
	if($('#content .clients').length)
		{
		var clients = $('#content .clients').find('.client');
		clients.find('.greyscale').css('opacity',0.5).find('img').css('opacity',0);
		clients.each(function()
			{ 
			$(this).find('a').hover(
				function()
					{
					$(this).stop().animate({'opacity':1}).find('img').stop().animate({'opacity':1});
					},
				function()
					{
					$(this).stop().animate({'opacity':0.5}).find('img').stop().animate({'opacity':0});
					}
				);
			});
		}
	}

/*************************************************** courses detail info */

var courseintros;

function courseDetailInfo()
	{
	if($('#content .courseintro').length)
		{
		courseintros = $('#content .courseintro').hide();
		courseintros.each(function()
			{ 
			var trigger = $(this).prev("h4").find(".details");
			trigger.wrapInner('<a href="javascript:;"></a>');
			var _this=this; 
			trigger.find('a').click(function(e)
				{ 
				e.preventDefault();
				$(_this).toggle(500);  
				$(this).toggleClass('open');
				}); 
			});
		}
	}

/*************************************************** courses */

var courseinfo;

function courses()
	{
	if($('#content .courses').length)
		{
		courseinfo = $('#content .courses').find('.courseinfo').hide();
		var togglers =  $('#content .courses').find('.courseToggler');
		togglers.each(function()
			{ 
			var _this=this; 
			$(this).click(function(e)
				{ 
				e.preventDefault();
				$(_this).parent().next('.courseinfo').toggle(500);  
				$(_this).toggleClass('open');
				}); 
			});
		}
	}


/*************************************************** team */

function team()
	{
	if($('#content .team').length)
		{
		prepareToolTips();
		var team = $('#content .team').find('a');
		team.find('.greyscale').css('opacity',0.5).find('img').css('opacity',0);
		team.each(function()
			{ 
			$(this).hover(
				function()
					{
					$(this).find('.greyscale').stop().animate({'opacity':1}).find('img').stop().animate({'opacity':1});
					},
				function()
					{
					$(this).find('.greyscale').stop().animate({'opacity':0.5}).find('img').stop().animate({'opacity':0});
					}
				);
			});
		}
	}

/*************************************************** checkWindowSize */

function checkWindowSize(level)
	{
	$(window).resize(function()
		{ 
		if($(window).width()<860) 
			{
			if($('#bgimage').css('visibility')=='visible')$('#bgimage').css('visibility','hidden');
			}
		else
			if($('#bgimage').css('visibility')=='hidden')$('#bgimage').css('visibility','visible');		
		});
	}

/*************************************************** remove */

var counter=1;
function copy(element,withRemove,removeText)
	{
	if($(element).length)
		{
		var el = $(element+':first');
		el.clone().insertAfter(element+':last').hide().toggle(500);
		var elNew = $(element+':last');
		elNew.find(":text").attr('value','');
		elNew.prepend('<div class="hr"><hr /></div>');
		if($(element).parent().find('.removeDiv').length==0)elNew.after("<p class=\"removeDiv\"><a href=\"javascript:;\" onclick=\"remove('"+element+"');\">"+removeText+"</a></p>");
		var labels = $(elNew).find('label');
		var inputs = $(elNew).find(':text,textarea');
		var dateInputs = $(elNew).find('.datefrom,.dateto');
		dateInputs.each(function(){ 
			$(this).removeClass('hasDatepicker');
			});
		labels.each(function(idx){ 
			$(this).attr('for',$(this).attr('for').substr(0,$(this).attr('for').length-1)+counter);
			$(inputs[idx]).attr('id',$(inputs[idx]).attr('id').substr(0,$(inputs[idx]).attr('id').length-1)+counter);
			});
		$(elNew).find('.ui-datepicker-trigger').remove();
		counter++;
		calendarsRange();
		}
	}
	
function remove(element)
	{
	if($(element).length)
		{
		var el = $(element+':last');
		el.remove();
		if($(element).length==1)$(element).parent().find('.removeDiv').remove();
		}
	}

function checkCopyRemove()
	{
	if($('.qualification').length>1)
		{
		$('.qualification:last').after("<p class=\"removeDiv\"><a href=\"javascript:;\" onclick=\"remove('#content form .qualification');\">Remove qualification</a></p>");
		}
	if($('.employment').length>1)
		{
		$('.employment:last').after("<p class=\"removeDiv\"><a href=\"javascript:;\" onclick=\"remove('#content form .employment');\">Remove employment</a></p>");
		}
	}

/*************************************************** Helper Functions */

function trim(str, chars) 
	{
	return ltrim(rtrim(str, chars), chars);
	}
 
function ltrim(str, chars) 
	{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
 
function rtrim(str, chars) 
	{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
	
/*************************************************** Funktionsaufrufe */

$(document).ready(function(){
	tabNav();
	externalLinks();
	css('#bgimage','.img',{'opacity':0.3});
	css('#subnav','li:first',{'border-top':'none'});
	css('.courses .courseintro .tinytxt','>:last',{'margin':0});
	css('.courses .courseinfo',':last',{'margin-bottom':0});
	css('.courses .courseinfo','>:last',{'margin-bottom':0});
	css('#content','fieldset:last',{'border':'none'});
	css('#content .tab','fieldset:last',{'border':'none'});
	formSlide('#abweichendslide','#abweichend');
	formSlide('#detailsxpslide','#hasteachingxp');
	formSlide('#firmaslide','#firmenbestellung');
	formSlideRadio('#wounterrichtslide','#wounterricht5','input[name="wounterricht"]');
	formSlideSelect('#frequenzslide','#frequenz','5 mal/Woche (Intensiv)');
	testChanger();
	testChangerCompany();
	courseDetailInfo();
	courseLevels();
	courses();
	faqNav();
	calendars();
	calendarsRange();
	clients();
	team();
	checkCopyRemove();
	checkWindowSize();
	handleIE6();
});
