(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(99999999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $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');
            
            // 3. reset scroll
            $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)').fadeTo(20,0.06).animate({
                    
                    scrollLeft : '+=' + left

                }, 100, function () {
                    // if page == last page - then reset position
                     $wrapper.fadeTo(250,0.8);
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                        
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                   
                   $("#navigationbody a").css("color", "#d9d9d9");
					
              		$("#page" + page).css("color", "#ffffff");

     				// $("#" + page).css("paddingRight", "1");
     				 
      				$wrapper.fadeTo(80,1);
                });
                 
            }
          
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back">Previous</a><a href="#" class="arrow forward">Next</a>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
       
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
           
              
            $('.bildchange').click(function () {

              	var page = parseInt(this.id);
              	$("#navigationbody a").css("color", "#d9d9d9");
              		
              	$("#page" + this.id).css("color", "#ffffff");
                gotoPage(page);
                return false;
            });
            
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            var myurl = document.URL;
			var mypage = myurl.search(/#mypage/);
			var guidlines = myurl.search(/#guidelines/);
			var downloads = myurl.search(/#downloads/);
			var contest = myurl.search(/#contest/);
			
			if(mypage != -1) {
    			 gotoPage(11);
			}
			
			if(downloads != -1) {
    			 gotoPage(7);
			}
			
			if(guidlines != -1) {
    			 gotoPage(9);
    			 $('#gallery #contestslides').stop().fadeOut(300).animate({marginLeft:'-2560px'},1).fadeIn(300);
			
			}
			
			if(contest != -1) {
    			 gotoPage(9);
    			 $('#gallery #contestslides').stop().fadeOut(300).animate({marginLeft:'0px'},1).fadeIn(300);
			
			}
			
			$('#guidelines').click(function () {
    			$('#gallery #contestslides').stop().fadeOut(300).animate({marginLeft:'-2560px'},1).fadeIn(300);
			
    		
    		});
    			
          	 $('#backtocontest, #backtocontest2, #stoerer').click(function () {
          	 	gotoPage(9);
          	 	$('li.menuItem').removeClass('act').addClass('inact');
    			$('#gallery #contestslides').stop().fadeOut(300).animate({marginLeft:'0px'},1).fadeIn(300);
			
    		
    		});
    		
    		
    		

            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);



function getMovie(Path){

			jwplayer("container").setup({
				autoplay: true,
				flashplayer: "include/jwplayer/player.swf", 
				plugins: {
            		sharing: { link: false }
        		},
				autostart: true,
				width: 852,
				height: 480,
				levels: [
					{ file: "media" + Path + ".mp4" },
            		{ file: "media" + Path + ".flv" }    // H.264 version
            		//
       		 	]

				
		});
		
		
		
		
		}


$(document).ready(function () {
		
	 $("#chess").load("getData2.php?pageId=289");
		
	 $('#navigationbodyOver2').mouseover(function () {

 	 		$("#navigationbodyOver").stop().animate({width:'0px',right:'232px'},{queue:false,duration:250});
 	
      
    }).mouseout(function () {
      	$("#navigationbodyOver").stop().animate({width:'232px',right:'0px'},{queue:false,duration:250});
      	
    });	
		
		
 	 $('.boxgridover').mouseover(function () {
 	 	$("."+ this.id).fadeIn(500);
    }).mouseout(function () {
      	$("."+ this.id).fadeOut(300);
    });
    
  
    
     $("#chess").hover(function() { //On hover...

		 $("#subtext2").fadeIn(200);
    
		
	} , function() { //on hover out...
		 
		$("#subtext2").fadeOut(200);
    
	});
     
     
     $('.bildchange').click(function () {
	
               $("#chess").load("getData2.php?pageId="+ this.id +"");
               $('#subtext').fadeOut();
      			 $('.back').fadeOut();
      			 $("#videocontentframe").fadeOut(100);
      			 setTimeout(function () {
					 
			
              		 $('#chess').fadeIn(300);
              		
              	 }, 400);
      });
  
    
    
    
     $('#slides, #aboutslides, #contestslides').mouseover(function () {

 	 	$(".lupe").fadeIn(500);
      
    }).mouseout(function () {
      	$(".lupe").fadeOut(500);
      	
    });
	
	

	

    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = false;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = false;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel ').trigger('next');
        }
    }, 8000);
    
 });
