$(function() { 

	// Schriften
	Cufon.replace('p.newstitel, h1, h2, #rechte_spalte h3');

	// Truncate
	$('div.truncate').each(function() {	
		var $cont = $(this);	
		var text = $(this).html();	
		var textKurz = $(this).text();
		var len = 320;

		if(textKurz.length > len) {
			textKurz = textKurz.substring(0, len);
			textKurz = textKurz.replace(/\w+$/, '');
			$(this).text(textKurz+'...');
			$('<a class="linie" title="mehr" href="#">mehr</a>')
				.insertAfter(this)
				.click(function() { 
					$(this).toggleClass('auf');
					if($(this).hasClass('auf')) { 
						$cont.slideUp('slow', function() { 
							$(this).html(text).animate({ opacity: '+=0' }, 50, function() { 
								$(this).slideDown('slow');
							});
						});
						$(this).text('weniger').attr({ title: 'weniger' });						
					} else { 
						$cont.slideUp('slow', function() { 
							$(this).text(textKurz+'...').animate({ opacity: '+=0' }, 50, function() { 
								$(this).slideDown('slow');
							});
						});
						$(this).text('mehr').attr({ title: 'mehr' });				
					};
					return false;
				});
		}
	}); 
	
	// Galerie
	if($('#slider').length) { 
/* 		$('#slider').removeClass('jsaus').s3Slider({
			timeOut: 6500
		}); */
		$('#slider').cycle({
			speed:  1500, 
			timeout: 6500,
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
	};
	
	// Themenbox
	$('body.start .startbox .licontent').hide();
	$('body.start .startbox ul.thementoggle li h2 a').addClass('zu');
	$('.thementoggle li:first a').removeClass('zu').parent().parent().find('.licontent').show();
	equalHeight($('body.start .startbox'));
	$('.thementoggle li h2 a').click(function() { 
		$(this).toggleClass('zu').parent().parent().find('.licontent').slideToggle(400);
		return false;
	});

	// Navigation
	$('#nav').removeClass('jsaus');
	$('#nav li:has(ul)').hover(function() { 
		$(this).addClass('hover');
	}, function() { 
		$(this).removeClass('hover');
	});

	// Suche
	$("input.suche").val('Suchbegriff eingeben');
	$("input.suche").focus(function() {
		if($(this).attr("value") == "Suchbegriff eingeben") $(this).attr("value", "")
	}).blur(function() {
		if($(this).attr("value") == "") $(this).attr("value", "Suchbegriff eingeben");
	});
	
	// Twitter
	$('<h3>Twitter</h3><ul class="tweets"></ul>').appendTo('.twitterbox');
	getTweets();

});

// Twitter-Meldungen holen
function getTweets() { 
	$('ul.tweets').empty();
	$.getJSON('http://search.twitter.com/search.json?&q=from:soenkerix&rpp=2&callback=?', function(data) { 
		$.each(data.results, function(i,item) { 
			var text = '<p>'+item.text+'</p>';
			var source = item.source.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/twitter.com/g,'twitter.com/soenkerix');;
			var datum = '<p class="created_at">vor '+prettyDate(item.created_at)+' via '+source+'</p>';
			$('<li>'+text.linkify() + datum+'</li>').appendTo('ul.tweets');
			$('.twitterbox a').attr({ target: '_blank' });
		});
		$('.twitterbox').show();
	});
	//setTimeout("getTweets()", 30000);
};


// Link aus Text
String.prototype.linkify = function() {
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
		return m.link(m);
	});
};

// Twitter-Datum
function prettyDate(time){
	var date = new Date((time || "").replace(/-/g,"/")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && " wenigen Sekunden" ||
			diff < 120 && " einer Minute" ||
			diff < 3600 && Math.floor( diff / 60 ) + " Minuten" ||
			diff < 7200 && " einer Stunde" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " Stunden") ||
		day_diff == 1 && " einem Tag" ||
		day_diff < 7 && day_diff + " Tagen" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " Wochen";
}

// Boxen auf gleiche Höhe bringen
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.attr({ style: 'height: auto !important; height: '+tallest+'px; min-height: '+tallest+'px' });
}

