// cufon replace
Cufon.replace('#en_GB #pageHead > div.navTop > ul > li > a', { hover: true });
Cufon.replace('#de_DE #pageHead > div.navTop > ul > li > a', { hover: true });

// jquery
$(document).ready(function() {
	// ani topnav
	$('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');
		Cufon.refresh();
		$(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');
		Cufon.refresh();
		$(this).find('> ul').stop(false, false).animate({
			opacity: 0,
			height: 0
		}, 400, function() {
			 $(this).css('opacity', '').hide();
		});
	});
	
	// print
	$('a[href="#print"]').click(function() {
		window.print();
		return false;
	});
	
	// top
	$('a[href="#wrapper"]').click(function() {
		$('html, body').animate({ scrollTop: $("#wrapper").offset().top }, 'slow');
		return false;
	});
	
	// externe links
	$('a.external').attr('target', '_blank');
	
	// iframe replace
	$(window).load(function() {
		$('a.replaceIframe').each(function() {
			var size = $(this).attr('rel').split(',');
			$(this).replaceWith('<iframe src="'+$(this).attr('href')+'" width="'+size[0]+'" height="'+size[1]+'" frameborder="0" style="'+$(this).attr('rev')+'"></iframe>');
		});
	});
	
	// welcome slider
	if($("#slider").length > 0) {
		$("#slider").easySlider({
				auto: true, 
				continuous: true
			});
	}
	
	// lightbox video
	$('a.lightVideo').each(function() {
		 var size = $(this).attr('rel').split('|')
		 $(this).colorbox({
			iframe:true, 
			innerWidth: size[0], 
			innerHeight: size[1],
			open: settings.autoopen
		});
	});
	
	// lightbox normal
	$('a.thickbox').colorbox({
		maxWidth: '940px'
	});
	
	// welcome slider
	    var opts = {
        duration: 500,
        fadeDuration: 250,
        minWidth: 148,
        maxWidth: 308,
        tout: null
    }
    
    _over = function(e) {
		window.clearTimeout(opts.tout);
		opts.tout = null;
		
		if ($(this).hasClass('active'))
			return false;
		
		$(this).siblings(':animated, .active').each(function() {
			$(this).stop().animate({
				width: opts.minWidth
			}, {
				duration: opts.duration
			});
			$(this).find('a').stop().animate({ opacity: 0 }, {
				duration: opts.fadeDuration,
				complete: function(){
					$(this).parent('li').removeClass('active');
					$(this).animate({ opacity: 1 }, opts.fadeDuration);
				}
			});
		});
		
		$(this).stop().animate({
			width: opts.maxWidth
		}, {
			duration: opts.duration
		});
		$(this).find('a').stop().animate({ opacity: 0 }, {
			duration: opts.fadeDuration,
			complete: function(){
				$(this).parent('li').addClass('active');
				$(this).animate({ opacity: 1 }, opts.fadeDuration);
			}
		});
	}
	$('.welcomeCon ul li').hover(_over, function() {
		opts.tout = window.setTimeout(function() {
			$('.welcomeCon ul li:first').trigger('mouseover');
		}, 250)
	});
	
	// fake tabs (dont touch)
	if($('div.fakeTab').length > 0) {
		$('div.fakeTab').after('<div class="latestNews"><ul class="jobView"></ul></div>');
		var theTeaser = '';
		$('div.fakeTab li a').each(function() {
			var theItem = $('ul.jobView li.'+$(this).attr('id')+':first a');
			if(theItem.length == 0) {
				theTeaser += '<li class="'+$(this).attr('id')+'">'+settings.noNews+'</li>';
			} else {
				theItem.parent().addClass('isTeaser-'+$(this).attr('id'));
				
				theTeaser += 
					'<li class="'+$(this).attr('id')+'">'
+						'<a href="'+theItem.attr('href')+'" title="'+theItem.find('strong').text()+'">';
				if(theItem.attr('rev') != '') {
					theTeaser += 
							'<span class="left">'
+								'<img src="'+theItem.attr('rev')+'?width=150" alt="'+theItem.find('strong').text()+'" />'
+							'</span>'
+							'<span class="right">'
				} else {
					theTeaser += '<span class="right noImage">'
				}
				theTeaser += '<ins datetime="'+theItem.find('ins').text()+'">'+theItem.find('ins').text()+'</ins>'
+								'<strong>'+theItem.find('strong').text()+'</strong>'
+								'<em>'+theItem.attr('rel')+'</em>'
+								'<span>'+settings.more+'</span>'
+							'</span>'
+						'</a>'
+					'</li>';
			}
		});
		$('div.latestNews ul.jobView').html(theTeaser);
		$('ul.jobView li.isTeaser-all').hide();
		var firstFakeTab = $('div.fakeTab').show().find('ul li:first').addClass('active').find('a').attr('id');
		$('ul.jobView li:not(.'+firstFakeTab+')').hide();
		$('ul.jobView').each(function() {
			$(this).find('li:visible:first').addClass('first').siblings('li').removeClass('first');
			if($(this).find('li:visible').length == 0) {
				$(this).hide().prev('h6').hide();
			}
		});
		$('div.fakeTab li a').live('click', function() {
			var speed = 400;
			var _e = $(this);
			$('ul.jobView').each(function() {  
				$(this).find('li.'+_e.attr('id')+':not(.isTeaser-'+_e.attr('id')+'):first').addClass('first').siblings('li').removeClass('first');
				if($(this).find('li.'+_e.attr('id')+':not(.isTeaser-'+_e.attr('id')+')').length == 0) {
					$(this).slideUp(speed);
					$(this).prev('h6').slideUp(speed);
				} else {
					$(this).slideDown(speed);
					$(this).prev('h6').slideDown(speed);
				}
			});
			$('ul.jobView li:not(.'+_e.attr('id')+')').slideUp(speed);
			$('ul.jobView li.isTeaser-'+_e.attr('id')+'').slideUp(speed);
			$('ul.jobView li.'+_e.attr('id')+':not(.isTeaser-'+_e.attr('id')+')').slideDown(speed);
			_e.parent().addClass('active').siblings('li').removeClass('active');
			return false;
		});
	}
	
	// mediathek
	$('div#mediathek input').hide();
	$('div#mediathek #locations input').show();
	$('div#mediathek #locations #submit').hide();
	$('#locations input:checkbox').change(function(){
        $('#locationform #submit').click();
	});

	counterSwitch = 0;
	$('div#mediathek ul.pagination li.page ul').each(function() {
		var switcher = '<select id="page_'+counterSwitch+'" name="page">';
		var minCount = $(this).find('li:first a').text();
		var maxCount = $(this).find('li:last a').text();
		var active = parseInt($(this).find('li.active a').text());
		for(var i = 1; i <= maxCount; i++) {
			var isSelected = '';
			var newURL = $(this).find('li:first a').attr('href').replace(/(page=\d+)/, 'page='+i);
			if(active == i) {
				isSelected = ' selected="selected"';
			}
			var the_page = settings.seite.replace("{x}", i);
			switcher += '<option value="'+newURL+'"'+isSelected+'>' + the_page + '</option>';
		}
		var the_max_page = settings.von.replace("{y}", maxCount);
		switcher += '</select><label for="page_'+counterSwitch+'">' + the_max_page + '</label>';
		$(this).replaceWith(switcher);
		counterSwitch++;
	});
	
	$('div#mediathek select').live('change', function() {
		var _e = $(this);
		if(_e.attr('name') == 'page') {
			location.href = _e.val();
		} else if(_e.attr('name') == 'language') {
			var theText = $(this).nextAll('a').text();
			_e.nextAll('a').addClass('loading').html('<img src="/pics/loading1.gif" alt="..." />');
			$.get(settings.documents+_e.val(), function(data) {
				var newData = data.split(';');
				_e.nextAll('a').removeClass('loading').attr('href', newData[0]).text(theText);
				_e.parent().find('dl:eq(0) dd').text(newData[1]);
				_e.parent().find('dl:eq(1) dd').text(newData[2]);
			});
			
			
		} else {
			_e.parents('form').submit();
		}
	});
	if($('div#mediathek div.slider ul').length > 0) {
		$('div#mediathek div.slider ul').jcarousel({
			start: $('div#mediathek div.slider ul li.active').index()
		});
	}
	
	

	
	
	
	
	
	
	
	// OLD ???
	 if ($('.contactForm div.delete-upload').length > 0) {
        $('.contactForm div.delete-upload').find('.delete-upload').click(function(e) {
            var parent = $(this).parent();
            var input = $(this).siblings('input[type=hidden]').clone();
            var inputName = input.attr('id').replace(/uploaded/, 'upload');
            input.val('').attr({
                'type': 'file',
                'name': inputName
            }).removeAttr('id').removeAttr('value');
            parent.replaceWith(input);
            e.preventDefault();
            return false;
        })
    }
    
	$('.dateChecker input[type="text"]').focus(function() {
		if($(this).attr('title') == $(this).val())
			$(this).val('');
	}).blur(function() {
		if('' == $(this).val())
			$(this).val($(this).attr('title'));
	});
	
	
	
});
