var k = 1;

$.fn.infiniteCarousel = function () {
	
	function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
	
	return this.each(function () {
		var $wrapper = $('> div', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> ul'),
			$items = $slider.find('> li'),
			$single = $items.filter(':first')
			
			singleWidth = $single.outerWidth(),
			visible = Math.ceil($wrapper.innerWidth() / singleWidth),
			currentPage = 1;
			
			if (k > 1) {
			var pages = Math.ceil($items.length / visible) - k;
			} else {
			var pages = Math.ceil($items.length / visible);
			}
			
			// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        	if (($items.length % visible) != 0) {
            	if ( k == 1 ) {
            		$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            		}
            	$items = $slider.find('> li');
            }
        	
        	// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has 				the first
        	if ( k == 1 ) {
        	$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        	$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        	k++;
        	}
        	$items = $slider.find('> li'); // reselect
        	
        	
        	// 3. Set the left position to the first 'real' item
        	$wrapper.scrollLeft(singleWidth * visible);
        	
        	// 4. paging function
       		function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
          		$wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
                }, 500, function() {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                }
                
                currentPage = page;
           		});                
            
           	}
           	
           	// 5. Bind to the forward and back buttons
        	$('.arr_left', this).click(function () {
            	return gotoPage(currentPage - 1);                
        	});
        
       		 $('.arr_right', this).click(function () {
         	   return gotoPage(currentPage + 1);
        	});
        	
        	// create a public interface to move to a specific page
        	$(this).bind('goto', function (event, page) {
          	  gotoPage(page);
        	});
	});
};

//Social Links
$(document).ready(function()
	{
	$(".twit").hoverIntent(function(){
			$(".twittip").stop().animate({
        	left: "1000px"}, 300, function(){ $("#social .twit").animate({
        	left: "3px"}, 100).animate({
        	left: "0"}, 100).animate({
        	left: "2px"}, 100).animate({
        	left: "0"}, 100); }).animate({
        	left: "990px"}, 100 ).animate({
        	left: "1000px"}, 100 );
       	}, function () {
     		$(".twittip").animate({
        	left: "-1000px"}, 300 );
     });
	$(".face").hoverIntent(function(){
		$(".facetip").stop().animate({
        left: "1000px"}, 300, function(){ $("#social .face").animate({
        left: "3px"}, 100).animate({
        left: "0"}, 100).animate({
        left: "2px"}, 100).animate({
        left: "0"}, 100); }).animate({
        left: "990px"}, 100 ).animate({
        left: "1000px"}, 100 );
	}, function () {
     		$(".facetip").animate({
        left: "-1000px"}, 300 );
	});
	$(".twit").click(function(){
		$(".logo").animate({
        top: "520px"}, 600 );
        $("#twitter_div").show(600);
        $(".twit").addClass("activtwit");
        $("#works a").animate({
        top: "-20px", opacity: "0"}, 600, function() { $("#works").hide(0); } );
    });
    $("#close").click(function(){
		$(".logo").animate({
        top: "290px"}, 600 );
        $("#twitter_div").hide(600);
        $(".twit").removeClass("activtwit");
        $("#works").show(0);
        $("#works a").animate({
        top: "0px", opacity: "1"}, 600 );
    });
	$(".works").hoverIntent(function(){
		$(".logotip").animate({
        top: "0px",opacity: "1"}, 300 );
    }, function(){
		$(".logotip").animate({
        top: "-10px",opacity: "0"}, 300 ); 
    }).click(function(){
    	$(".logo").animate({
        top: "520px"}, 600 );
        $("#logo_div").show(600);
        $('div#logo_div').infiniteCarousel();
        $("#tips").hide(0);
        $("#social a").animate({
        left: "-20px", opacity: "0",display: "none"}, 600, function() { $("#social").hide(0); } );
    });
    $(".back").click(function(){
    	$(".logo").animate({
        top: "290px"}, 600 );
        $("#logo_div").hide(600);
        $("#tips").show(0);
        $("#social").show(0);
        $("#social a").animate({
        left: "0px", opacity: "1",display: "block"}, 600 );
    });

	$("#impres h3").click(function(){
    	$("#impres h3").animate({
        bottom: "106px"}, 600 );
        $("#impres span").animate({
        height: "100px"}, 600 );
    });
    
	$("#impres span p").click(function(){
    	$("#impres h3").animate({
        bottom: "6px"}, 600 );
        $("#impres span").animate({
        height: "0px"}, 600 );
    });
    
    $("#bookmarking ul li").hoverIntent(function(){
		$("span", this).css("display", "block").animate({
        top: "30px",opacity: "1"}, 300 );
    }, function(){
		$("span", this).animate({
        top: "40px",opacity: "0"}, 300, function() {
        	$(this).css("display", "none");
        });
    });
    $("#bookmarking ul ul").hoverIntent(function(){
		$(".schuelervz").css("display", "block").animate({opacity: "1"}, 300 );
		$(".meinvz").css("display", "block").animate({opacity: "1"}, 300 );
    }, function(){
    	$(".schuelervz").animate({opacity: "0"}, 300, function(){
    		$(".schuelervz").css("display", "none");
    		});
		$(".meinvz").animate({opacity: "0"}, 300, function(){
    		$(".meinvz").css("display", "none");
    		});
	});
    
}); 

$(document).ready(function()
	{
	$("#news img").click(function ()
		{
		$("#news").animate({opacity: "0"}, 300).hide(500);
		});
	});

/*
$(document).ready(function () {
	$('div#logo_div').infiniteCarousel();
});*/