jQuery(function($){
	$(function()
	{
		$('.galleryimage').each(function(i)
		{
			if(i!=0)
				$(this).css('opacity', .5);
			
			$(this).attr('rel', i);
		});
		
		var paddingLeft = ($(window).width()-895)/2-10;
		$('#scroller #scrollbar').css('padding-left', paddingLeft);
		
		$(window).resize(function() {
			var paddingLeft = ($(window).width()-895)/2-10;
			$('#scroller #scrollbar').css('padding-left', paddingLeft);
		});

		
		var currentItem = 0;
		var maxItems = $('.galleryimage').size();

		function startScroll(toNum)
		{
			var nextEl = $('.galleryimage[rel=' + toNum + ']');
			$('#scroller').scrollTo(nextEl, {
				'duration': 250,
				'offset': {left: (-($(window).width()-895)/2+10)},
				'axis' : 'x'
			});

		};		
		
		$(document).keydown(function(e)
		{
			if(e.keyCode==37)
				slideleft();
			else if(e.keyCode==39)
				slideright();
		});
		
		function slideleft() 
		{
			$(this).blur();
			if( currentItem > 0 )
			{
				$(".galleryimage[rel='"+currentItem+"']").animate({'opacity':.5}, 500);
				currentItem--;
				startScroll(currentItem);
				$(".galleryimage[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
			return false;
		};
		
		function slideright()
		{
			$(this).blur();
			if( currentItem < maxItems - 1 )
			{
				$(".galleryimage[rel='"+currentItem+"']").animate({'opacity':.5}, 500);
				currentItem++;
				startScroll(currentItem);
				$(".galleryimage[rel='"+currentItem+"']").animate({'opacity':1}, 500);
			}
			return false;
		};
		
		$('#scroll-left').click(slideleft);
		$('#scroll-right').click(slideright);
		
	});
});

$(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();
});


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 +'"><p>'+ tweet + ' <span class="date">'+ created +'</span></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();
    	
    	if(success)
    	{
    		$("#tweetcout").html("Nachricht 1 von "+ maxTweets);
    	}
    	
    	$("#control .up").click(function(e) {
    		if(currentTweet > 0 && success)
    		{			
    			$("#messages li[class='tweet_"+currentTweet+"']").hide("clip", {}, 500);
    			currentTweet--;
    			$("#messages li[class='tweet_"+currentTweet+"']").show("blind", {}, 500);
    			$("#tweetcout").html("Nachricht "+ (currentTweet+1) +" von "+ maxTweets);
    		}
    		e.preventDefault();
    	});
    	
    	$("#control .down").click(function(e) {
    		if(currentTweet < maxTweets - 1 && success)
    		{		
    			$("#messages li[class='tweet_"+currentTweet+"']").hide("blind", {}, 500);
    			currentTweet++;
    			$("#messages li[class='tweet_"+currentTweet+"']").show("clip", {}, 500);
    			$("#tweetcout").html("Nachricht "+ (currentTweet+1) +" von "+ maxTweets);
    		}
    		e.preventDefault();
    	});	
    });
}

function fadeStartpage()
{
	$("body").show("fade", {}, 2000);
}

$(document).ready(function(){
	
	if($("#container").hasClass("start"))
	{
	       setTimeout("fadeStartpage()", 500);
	}

	twitter("bsudau", 20);

	$(".column .project")
		.hover(
			function () {
				$(this).find(".overlay").css('cursor', 'pointer').stop(true,true).fadeTo(250, 0.9);
			}, 
			function () {
				$(this).find(".overlay").fadeOut(750);
		}
		).click(
			function () {
				$(this).find(".overlay").hide();
			}
		);
	
	$("#footer li a").mouseover(function() {
		$class = $(this).attr('class');
		$("#footer div." + $class).show("fade", {}, 200);
	}).mouseout(function(){
		$("#footer div." + $class).hide();
	});	

});
