var $iNeu = 1;
var $iAlt = 0;
var $actNode = 5;
var $url;
var $request;
var $loaded = new Array();


$(document).ready(function(){
	var $previewOrig = $('.project_preview').html();
	
	
	$('#bookmark').mouseover(function(){
		$('#bookmark_inner').css('display', 'block');	
	});
	$('#bookmark').mouseout(function(){
		$('#bookmark_inner').css('display', 'none');	
	});
	
	
	$('#follow_us').mouseover(function(){
		$('#follow_us img').attr("src", 'fileadmin/template/img/twitter_color.jpg');
	});
	$('#follow_us').mouseout(function(){
		$('#follow_us img').attr("src", 'fileadmin/template/img/twitter_nocolor.jpg');
	});	
	
	
	//Startseite Projektbox
	$(document).everyTime(8000, 'controlled', function() {
		showProject();
	});

	//Referenzbox aktivieren
	$('#reference_box').infiniteCarousel();


	//ToolTip in der Refrenzbox
	$('#reference_box .wrapper ul li a').mouseover(function(e){
		$('.project_preview').stop().html($previewOrig);
		if($request)
			$request.abort();

	
		var $offset = $(this).offset().left
		$offset = $offset + 54 - 110;
		$url = $(this).attr('href');

		$('.project_preview').css('left',$offset);
		$('.project_preview').css('display','block');
		//$('.project_preview').stop().fadeTo("fast", 0.0, function(event){
		//	$('.project_preview').css("display", "block");
		//	$('.project_preview').fadeTo("fast", 1.0);
		//});	
		
		if(!$loaded[$url]) {
			$request = $.ajax({
				type: "GET",
  				url: $url,
  				cache: false,
  				data: "preview=1",
  				success: function(html){
  	  				$('.project_preview').html(html);
  	  				$loaded[$url] = html;
  				}
			});
		}
		else
			$('.project_preview').html($loaded[$url]);
		

	});

	//Tooltip schließen
	$('#reference_box .wrapper ul li a').mouseout(function(e){
		$('.project_preview').html($previewOrig);
		$('.project_preview').stop().css('display','none');
		
	});

	//Lightbox Referenzen Details
	$(".reference_detail a").fancybox(); 

	

});


$.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), // note: doesn't include padding or border
            currentPage = 1,
            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) {
            $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
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $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;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', 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);
        });
    });  
};


function showProject() {
  		var $alt = $('.middle_shadow div.obersteebene');
	
  		if($alt.next().length)
    			var $neu = $alt.next();
  		else {
  		 	var $neu = $('.middle_shadow div:first');
  		 	$iNeu = 0;	
  		 }

  		$alt.addClass('mittlereebene');
  		$alt.removeClass('obersteebene');
  		$neu.css({opacity: 0.0});
  		$neu.addClass('obersteebene');

  		$neu.animate({opacity: 1.0}, 1000, function() {
    			$alt.removeClass('mittlereebene');
  		});	
  		
  		$('#show_'+$iNeu).animate({marginLeft: -23}, 1000);
  		$('#show_'+$iAlt).animate({marginLeft: -15}, 1000);
		
		$iAlt = $iNeu;
		$iNeu = $iNeu +1;
}

