var twitter = function(user, count)
{
	var url = "http://twitter.com/status/user_timeline/" + user + ".json?count=" + count + "&callback=?";
	var html = "";
	var success = false;
    $.getJSON(url, function(data) {
    	$.each(data, function(i, item) {
    		var splitedDate = item.created_at.toString().split('+');
    		var created = splitedDate[0];
    		var tweet = item.text
    			// Change the text links to active links with a target of new window
				.replace(/(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig, '<a href="$1" target="_blank"  title="$1">$1</a>')
				// Change references to other Tweeters to links
				.replace(/@+([_A-Za-z0-9-]+)/ig, '<a href="http://twitter.com/$1" target="_blank" title="$1 on Twitter">@$1</a>')
				// Change hashtags to active links
				.replace(/#+([_A-Za-z0-9-]+)/ig, '<a href="http://search.twitter.com/search?q=$1" target="_blank" title="Search for $1 on Twitter">#$1</a>');
    		
    		if(tweet != "")
    			html += '<li class="tweet_'+ i +'"><span class="date">'+ created +'</span><p>'+ tweet + '</p></li>';
    		
    	});
    	$("#messages li").remove();
    	if(html != "")
    	{
    		$("#messages").append(html);
    		$("#messages li:first").show("fade", {}, 200);
    		success = true;
    	}
    	else
    	{
    		$("#messages").html('<li><p>Tweets konnten nicht geladen werden</p></li>');
    		$("#messages li:first").show("fade", {}, 200);
    	}
    	
    	var currentTweet = 0;
    	var maxTweets = $('#messages li').size();
   	
    	$("#control .up").click(function(e) {
    		if(currentTweet > 0 && success)
    		{			
    			$("#messages li[class='tweet_"+currentTweet+"']").hide("clip", function() {
        			currentTweet--;
        			$("#messages li[class='tweet_"+currentTweet+"']").show("blind", {}, 500);   				
    			}, 500);
    		}
    		e.preventDefault();
    	});
    	
    	$("#control .down").click(function(e) {
    		if(currentTweet < maxTweets - 1 && success)
    		{		
    			$("#messages li[class='tweet_"+currentTweet+"']").hide("blind", function() {
        			currentTweet++;
        			$("#messages li[class='tweet_"+currentTweet+"']").show("clip", {}, 500);    				
    			}, 500);
    		}
    		e.preventDefault();
    	});	
    });
}

$(function(){
	   
	$.fn.scrollToTop=function(){
		
		$(this).hide().removeAttr("href");
		
		if($(window).scrollTop()!="0"){
			$(this).fadeIn("slow")
		}
		var scrollDiv=$(this);
		
		$(window).scroll(function(){
								  
			if($(window).scrollTop()=="0"){
				$(scrollDiv).fadeOut("slow")
			}else{
				$(scrollDiv).fadeIn("slow")
			}
		
		});
		
		$(this).click(function(){
			$("html, body").animate({scrollTop:0},"slow");
		});
	
	}
			
});

$(function() {
	$("#totop").scrollToTop();
});

$(document).ready(function(){
	
	/* Twitter */
	twitter("IManigel", 20);
	
	/* To Top Position */
	var middle = ($(window).height()/2)+($("#totop").height()/2);
	$("#totop").css('top', middle);
	
	/* Navigation Line */
	$('.navigation li').not('.active').hover(function() {
			var bg = $(this).css('backgroundPosition');
			if($(this).attr('title') == '') {
				$(this).attr({rel: bg, title: $(this).text()});
			}
			bg = bg.replace(' ','');
			bg = bg.split('px');

			$(this).stop().animate({
				backgroundPosition: parseInt(bg[0],10) + $(this).width() + 'px 54%'
			}, 500, 'easeInBack');
		}, function() {
			$(this).stop().animate({
				backgroundPosition: $(this).attr('rel')
			}, 250, 'easeInBack');
	});
	
	/* Slider */
	$('#carousel').infiniteCarousel({
		displayTime: 2000,
		textholderHeight : .25,	
		displayProgressBar : 0,
		displayThumbnailBackground: 0

	});
	
	/* Hover */
	$(".column .project").hover(function () {
		   $(this).find(".overlay").css('cursor', 'pointer').stop(true,true).fadeTo(250, 0.9);
		}, 
		function () {
			$(this).find(".overlay, .options").fadeOut(750);
	});
	
	/* E-Mail Addresses */
	$('a.email').each(function()
	{
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		$(this).removeAttr('rel');
	});	
	
	/* SORT WORK */
	if($(document).getUrlParam("show") == "web")
	{
		$(".columns_work").find('.print').parent().fadeOut("slow");
	}
	
	if($(document).getUrlParam("show") == "print")
	{
		$(".columns_work").find('.web').parent().fadeOut("slow");
	}
	
	$("#showall").click(function(e) {
		if(!$(this).hasClass("active"))
		{
			$(".sort a").removeClass('active');
			$(".columns_work .column").find('.print, .web').parent().fadeIn("slow");
			$(this).addClass('active');
		}
		e.preventDefault();
	});
	
	$("#showweb").click(function(e) {
		if(!$(this).hasClass("active"))
		{
			$(".sort a").removeClass('active');

			$(".columns_work").find('.print').parent().fadeOut("slow", function() {
				$(".columns_work").find('.web').parent().fadeIn("slow");				
			});
			
			$(this).addClass('active');
		}
		e.preventDefault();
	});
	
	$("#showprint").click(function(e) {
		if(!$(this).hasClass("active"))
		{
			$(".sort a").removeClass('active');

			$(".columns_work").find('.web').parent().fadeOut("slow", function() {
				$(".columns_work").find('.print').parent().fadeIn("slow");				
			});
			
			$(this).addClass('active');
		}
		e.preventDefault();
	});
});
