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 a').each(function() {
			$(this).hover(
				function() {
					$(this).css('top', '-25px');
				},
				function() {
					$(this).css('top', '0px');
				}
			);
		});
		
		Cufon.replace('h1.title');
		Cufon.replace('h2.subtitle');
		
		$('.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);