// font replace
Cufon.replace('body.sub h2 a', { 
	fontFamily: 'Frutiger'
});
Cufon.replace('#pageHead > div.navTop > ul > li > a', { 
	fontFamily: 'Frutiger',
	hover: true
});
Cufon.replace('body.sub h3', { 
	fontFamily: 'Frutiger 57Cn'
});
Cufon.replace('.box > h6', { 
	fontFamily: 'Frutiger'
});

// jquery
$(document).ready(function() {
	// wenn man von einer alten seite kommt zeige layer
	if(lang.referrer === true) {
		$('body').append('<div style="display:none"><div id="referrerLayer"><h6>'+lang.referrerHeadline+'</h6><p>'+lang.referrerText+'</p></div></div>');
		$("#referrerLayer").colorbox({
			width:"400px", 
			inline:true,
			href:"#referrerLayer",
			open: true
		});
	}
	
	// animiertes submenü von hauptnavigation
	$('div.navTop ul ul.subLevel').each(function() {
		$(this).show().attr('getheight', $(this).height()).css({
			'height': 0,
			'overflow': 'hidden',
			'opacity':'0'
		}).hide();
	});
	$('div.navTop > ul > li').hover(function() {
		$(this).find('> a').addClass('hover');
		$(this).find('> ul').show().stop(false, false).animate({
			opacity: 1,
			height: $(this).find('> ul').attr('getheight')
		}, 400, function() { $(this).css('opacity', ''); });
	}, function() {
		$(this).find('> a').removeClass('hover');
		$(this).find('> ul').stop(false, false).animate({
			opacity: 0,
			height: 0
		}, 400, function() {
			 $(this).css('opacity', '').hide();
		});
	});

	// tabs erstellen & switchen
	$('div.tabbing').each(function() {
		$(this).find('> ul li:first').addClass('active');
		$(this).find('> div:not(:first)').hide();
	});
	$('div.tabbing > ul li > a').click(function() {
		$(this).parent().addClass('active').siblings('li').removeClass('active');
		$(this).parents('div:first').find('> div').hide().eq($(this).parents('ul').find('li').index($(this).parent())).show();
		return false;
	});
	
	// externe links im neuen fenster
	$('a.external').attr('target', '_blank');
	
	// produkte bilder wechseln
	$('div.product div.switchImage ul li:first').each(function() {
		$(this).addClass('active');
	});
	$('div.product div.switchImage ul li a').click(function() {
		$(this).parent().addClass('active').siblings('li').removeClass('active');
		var element = $(this).parents('div.switchImage').siblings('div#getImage').find('img');
		var image = $(this).attr('rel');
		var href = $(this).attr('href');
		if(!element.is(':animated')) {
			element.fadeOut(300, function() {
				$(this).parent().attr('href', href);
				$(this).attr('src', image).load(function() {
					$(this).fadeIn(300);
				});
			});
		}
		return false;
	});
	
	// startseitenslider mit prdukten
	$('.slider ul').anythingSlider({
		autoPlay: true,
		delay: 3000,
		animationTime: 1000,
		buildNavigation: false,
		pauseOnHover: true,
		onSlideComplete: function() {
			var marke = '';
			if($('.slider ul li.activePage').hasClass('Datacon')) {
				marke = 'Datacon';
			} else if($('.slider ul li.activePage').hasClass('Esec')) {
				marke = 'Esec';
			} else if($('.slider ul li.activePage').hasClass('Fico')) {
				marke = 'Fico';
			} else if($('.slider ul li.activePage').hasClass('Meco')) {
				marke = 'Meco';
			}
			$('div.introduction li a[rev="'+marke+'"]').parent().addClass('active').siblings('li').removeClass('active');
		}
	});
	
	// markenswitch um im startseitenslider zu produkten zu springen, sowie animation
	$('div.introduction ul li a').hover(function() {
		$(this).parent().siblings('li').find('a').stop(false, false).animate({ opacity: .3 }, 400);
	}, function() {
		$(this).parent().siblings('li').find('a').stop(false, false).animate({ opacity: 1 }, 400);
	}).click(function() {
		var getRev = $(this).attr('rev');
		var whichId = $('#pageBody div.slider li.'+getRev+':not(.cloned):eq(0)');
		var id = parseInt($('#pageBody div.slider li').index(whichId));
		$('.slider ul').anythingSlider(id);
		$(this).parent().addClass('active').siblings('li').removeClass('active');
		return false;
	});
	
	// accordion
	$('.accordion').each(function() {
		$(this).find('> h4').css('cursor','pointer');
		$(this).find('> div').each(function() {
			$(this).css('height', $(this).height())
		});
		$(this).find('> div').hide();
	});
	$('.accordion h4').click(function() {
		$(this).next('div').slideToggle(300);
		$(this).toggleClass('open');
		return false;
	});
	
	// wenn formular abgesendet wird, validiere formular
	$('form').submit(function() {
		var errH4 = ($(this).hasClass('slideForm')) ? true : false ;
		$(this).find('p.required span.error').remove();
		$(this).find('input.error, select.error, textarea.error, h4.error').removeClass('error');
		var error = 0;
		$(this).find('p.required').each(function() {
			$(this).find('input[type="text"], input[type="checkbox"], textarea, select').each(function() {
				if($(this).val() == '') {
					error++;
					if($(this).parent().find('> span.error').length == 0) {
						$(this).parent().append('<span class="error">'+lang.required+'</span>');
					}
					$(this).addClass('error');
					if(errH4 == true) {
						$(this).parents('fieldset:first').prev('h4').addClass('error');
					}
				}
			});
		});
		if(error != 0) {
			$('html, body').animate({ scrollTop: $("form").offset().top }, 'slow');
			return false;
		}
	});
	
	// setze fehlermeldungen bei reset zurück
	$('input[type="reset"]').click(function() {
		$(this).parents('form').find('p.required .error').removeClass('error').siblings('span').remove();
		$(this).parents('form').find('h4.error').removeClass('error');
	});
	
	// animiertes nach oben scrollen
	$('.softscroll').click(function() {
		$('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top }, 'slow');
		return false;
	});
	
	// tabellen sortieren
	if($(".tablesorter").length > 0) {
		$.tablesorter.addParser({
			id: 'germandate',
			is: function(s) {
				return false;
			},
			format: function(s) {
				if(s.length == 10) {
					var a = s.split('.');
					a[1] = a[1].replace(/^[0]+/g,"");
					return new Date(a.reverse().join("/")).getTime();
				} else {
					return s;
				}
			},
			type: 'numeric'
		});
		$(".tablesorter colgroup col:eq(1)").addClass('active');
		$(".tablesorter").tablesorter({
			sortList: [[1,0]], 
			headers: { 
				1: { sorter:'germandate' }, 
				4: { sorter: false } 
			}
		}); 
		$(".tablesorter thead tr th.header").hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		}).click(function() {
			$(this).parents('table').find('colgroup col').eq($(this).parent().find('th').index(this)).addClass('active').siblings('col').removeClass('active');
		});
	}
	
	// colorbox für bilder
	if($('a[rel^="colorbox["], a.colorbox').length > 0) {
		$('a[rel^="colorbox["], a.colorbox').colorbox({
			maxWidth:'80%',
			maxHeight:'80%'
		});
	}
	
	// > ie8 focus
	if($.browser.msie && parseInt($.browser.version) < 8) {
		$('form input, form textarea, form select').focusin(function() {
			$(this).addClass('focus');
		}).focusout(function() {
			$(this).removeClass('focus');
		});
	}
	
	// input switch (job)
	$('p.switchField').each(function() { 
		if(!$(this).prev('.switchInput').find('input[type="radio"][value="Ja"]').is(':checked')) {
			$(this).hide();
		}
	});
	$('.switchInput input[type="radio"]').change(function() {
		if($(this).val() == 'Ja') {
			$(this).parents('p.switchInput:first').next('p.switchField').slideDown();
		} else {
			$(this).parents('p.switchInput:first').next('p.switchField').slideUp();
		}
	});
	
	// slide
	$('.slideForm > h4').css('cursor', 'pointer');
	$('.slideForm > fieldset').hide();
	$('.slideForm > fieldset:first, .slideForm > fieldset:last').show();
	$('.slideForm > h4').click(function() {
		$(this).toggleClass('open').next('fieldset').toggle();
		return false;
	});
	
	// duplicate content
	$('.duplicateLink a').click(function() {
		var e = $('.duplicateContent');
		e.append('<div>'+e.find('> div:last').html()+'</div>');
		e.find('div:last > p').each(function() {
			var index = parseInt($(this).find('label > strong').html()) + 1;
			$(this).find('label > strong').html(index);
			var index = $(this).find('input').attr('name').match(/\[(\d+)\]/);
			var name = $(this).find('input').attr('name').replace(/\[\d+\]/, '['+(parseInt(index[1])+1)+']');
			$(this).find('input').attr('name', name);
		});
		return false;
	});
	
	// replace file input
	$('input[type="file"]').filefaker();
	
	// jquery visualize
	if($('table.diagram').length > 0) {
		$('table.diagram').visualize({
			type: 'line',
			width: '475px',
			height: '200px',
			colors: ['#00963F', '#eb0000']
		});
		$('table.diagram').hide();
	}
	
	// got to page top
	var goToTop = $('a#goTop');
	if($(window).scrollTop() == 0) {
		goToTop.animate({ opacity:0}, 0);
	}
	goToTop.click(function() {
		$('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top }, 400);
		return false;
	});
	$(window).scroll(function(e) {
		if($(window).scrollTop() > 0) {
			goToTop.stop(false, false).animate({ opacity: 1 }, 400);
		} else {
			goToTop.stop(false, false).animate({ opacity: 0 }, 400);
		}
	});
	
	// in_array
	function in_array(item,arr) {
		for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
		return false;
	}
});
