    var secondcounter = 1;
    test = 0;

    $(document).ready(function(){
        $("a[rel='colorbox']").colorbox({transition:"none", opacity:"0.6", current:"Bild {current} von {total}"});
        $("a.imageOverlay").colorbox({transition:"none", opacity:"0.6", maxWidth:1024, maxHeight:768});

        $('.imagechanger a.paginate').click(function(){
            var bool = false;
            var type = $(this).attr('rel');
            var counter = $('.imagechanger a.picture').length;

            $('.imagechanger a.picture').each(function(){
              if (bool == false){
               var that = $(this);
                   if (that.hasClass('active')) {
                     that.removeClass('active');

                     if (type == 'next'){
                       if (secondcounter == counter){
                           bool = true;
                           var newThat = $('.imagechanger a.picture').get(0);
                           $(newThat).addClass('active').trigger('click');
                           test = 1;
                           secondcounter = 1;
                       } else {
                           that.next().addClass('active').trigger('click');
                           bool = true;
                           secondcounter++;
                           test = 1;
                       }
                    } else {
                       if (type == 'previous'){
                         if (secondcounter == 1){
                           bool = true;
                           var newThat = $('.imagechanger a.picture').get(counter - 1);
                           $(newThat).addClass('active').trigger('click');
                           secondcounter = counter;
                           test = 1;
                         } else {
                           that.prev().addClass('active').trigger('click');
                           bool = true;
                           secondcounter--;
                           test = 1;
                         }
                       }
                    }
                 }
               }
            });

            return false;
        });

        $('.imagechanger a.picture').click(function()
        {

        	var relation = $(this).attr('rel');
        	$('div.bildbeschreibung.active').removeClass('active').addClass('hide');
        	$("div.bildbeschreibung." + relation +"").removeClass('hide').addClass('active');
        	
            $('#mainImage').attr('src', $(this).attr('href'));
            $('#firstimg').attr('href', $(this).attr('href'));
            $('.imagechanger a.active').removeClass('active');
            $(this).addClass('active');
            
            if (test != 1) secondcounter = $(this).attr('rel');
            test = 0;	
            return false;
        });

        $('span.colorboxopener').click(function()
        {
            $('#firstimg').click();
            return false;
        });
    });
