jQuery.noConflict();
(function($) {
	$.fn.extend({
		limit: function(limit, element){
			var interval,f;
			var self=$(this);
			$(this).focus(function(){
				interval = window.setInterval(substring, 100)
			});
			$(this).blur(function(){
				clearInterval(interval);
				substring()
			});
			substringFunction = "function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
			if (typeof element!='undefined') substringFunction += "if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}";
			substringFunction+="}";
			eval(substringFunction);
			substring()
		}
	});
	
	$(document).ready(function() {
		// External links/uploads
		$('a[href^="http://"]').attr('rel', 'external');
		$('a[href^="/fileadmin"]').attr('rel', 'external');
		$('a[href^="/uploads"]').attr('rel', 'external');
		$('a[rel="external"]').attr('target', '_blank');

		if ($('#search').length > 0) {
			var init_search_value = $('#search input.sword').attr('value');
			$('#search input.sword').bind('focus', function() {
				if ($(this).val() == init_search_value) {
					$(this).val('');
				}
			});
			$('#search input.sword').bind('blur', function() {
				if ($(this).val() == '') {
					$(this).val(init_search_value);
				}
			});
			$('#search form').bind('submit', function(evt){
				if ($('#search input.sword').val() == init_search_value || $('#search input.sword').val() == '') {
					$('#search input.sword').val('');
					evt.preventDefault();
				}
			});
		}
		// Empty searchform when it is clicked and the default word is in there
		//$('#search input.sword').attr('rel', $('#search input.sword').val());
	
		// Accordion for publications and weblog
		$('#publications_weblog:first').each(function(){
			$(this).find('h3.publications').click(function(){
				$(this).parent().find('h3.weblog').css('margin-top', '0px');
				$(this).parent().find('div.weblog').slideUp();
				$(this).parent().find('h3.weblog').removeClass('active_item');
				
				$(this).addClass('active_item');
				$(this).next('div.publications').slideDown();
			});
			
			$(this).find('h3.weblog').click(function(){
				$(this).parent().find('div.publications').slideUp();
				$(this).parent().find('h3.publications').removeClass('active_item');
				
				$(this).addClass('active_item');
				$(this).next('div.weblog').slideDown('normal', function(){
					$(this).parent().find('h3.weblog').css('margin-top', '2px');
				});
			});
		});
		
		
		
		// Main navigation
		$('#main_nav > ul > li').each(function() {
			// Open submenu's on mouseover, close all other submenu's
			$(this).bind('mouseenter', function() {
				$(this).addClass('hover');
			}).bind('mouseleave', function() {
				$(this).removeClass('hover');
				$('a:first', $(this)).removeClass('hover');
			});
		});
	
		// Extension: tt_news
		$('.news-single-item .news-single-backlink > a').each(function(){
			$(this).attr('href', '#');
			$(this).click(function(){
				history.go(-1);
				return false;
			});
		});
		
		// Image buttons hover
		$('.image-button :first-child').each(function() {
			$(this).hover(
				function() {
					$(this).css('top', '-25px');
				},
				function() {
					$(this).css('top', '0px');
				}
			);
		});
		
		Cufon.replace('h1.title');
		Cufon.replace('h2.subtitle');
		
		// tests intro
		Cufon.replace('#tfe_powermail_thx h2');
		Cufon.replace('#content .tests-item h3');
		Cufon.replace('#content .tests-item dd.csc-textpic-caption');
		Cufon.replace('#tests .text-item p.bodytext b');
		Cufon.replace('#tests .text-item p.bodytext a');
		Cufon.replace('.content-tests .tests-item a');
		Cufon.replace('.tests-subheader');
		
		// tests navigation
		Cufon.replace('#tfe-pm-n-previous a');
		Cufon.replace('#tfe-pm-n-next a');
		Cufon.replace('.tfe_powermail_label h3');
		Cufon.replace('.tfe_powermail_results h3');
		Cufon.replace('.tfe_powermail_textwrap h3');
		
		Cufon.replace('.carousel .items .highlight div strong');
		Cufon.replace('.carousel .items .highlight div a');
		Cufon.replace('.carousel .items .highlight .subtitel'); 

		// Home Carousel
		$('.carousel .items').cycle({
			prev:'.carousel a.prev',
			next:'.carousel a.next',
			after:function(curr, next, opts, fwd) {
				$('.carousel .nav span.current').html(opts.currSlide + 1);
			},
			timeout: 8000,
			fx: 'scrollLeft'
		});
		
		
		$('.poll-vote, .poll-answer-horizontal, .comment-form').corners('10px transparent');
		
		// Comments
		if (window.location.href.match(/#tx_tfe_blog_comment/)) {
			$(".tx-tfeblog-comment:first").animate({opacity: 0 }, 1);
			$(".tx-tfeblog-comment:first").animate({opacity: 1 }, 1000);
		}
		$('.reactions a').each(function() {
			$(this).attr('href', $(this).attr('href')+'#tx_tfe_blog_all_comments');
		});
		
		// Comment form rules
		$('.comment-form .introduction a[rel=rules]').bind('mouseover', function(evt){
			$('.comment-form .rules').css('display', 'block').css({'top': $(this).css('top'), 'left':$(this).css('left')});
		}).bind('mouseout', function(evt){
			$('.comment-form .rules').css('display', 'none');
		}).bind('click', function(evt){
			evt.preventDefault();
		});
		
		// Limit characters in comment field
		if ($('#tx_tfeblog_comment_form_comment').length > 0) {
			$('#tx_tfeblog_comment_form_comment').limit(1000, '#tx_tfeblog_comment_form_characters');
		}
		
		// fix for powermail mail-a-friend
		if ($('.powermail_uid41').length > 0)
		{
			var i = location.search.indexOf('http');
			var s = unescape(location.search.substr(i));
			$('.powermail_uid41').val(s);
		}
	});
})(jQuery);
