$(document).ready(function(){
    $('#gallery li').each(function(idx) {
        $(this).data('index', (++idx));
    });
	
 	
	
    $('#gallery').jcarousel({
	    wrap: 'both',
		scroll: 7,
		itemFirstInCallback: itemVisibleIn
		});
    	function initCallbackFunction(carousel) {
    		// bind "image-loaded" event to the #img container (we trigger it inside onImage function)
		    $('#img').bind('image-loaded', function() {
		        // find "index" data of the active thumbnail
		        // and subtract by 2 to center
		        var idx =  $('#gallery li.active').data('index') - 0;
		        // scroll carousel and center the active list item
		        carousel.scroll(idx);
		               return false;
		    });
		   
		};
		function itemVisibleIn(carousel, li, index, state) {
				    	$.galleria.activate($(li).contents('img').attr('rel'));
				    	$('#count #aktuell').html($(li).attr('jcarouselindex'));

		}
		
    // load and fade-in thumbnails
    $('#gallery li img').css('opacity', 0).each(function() {    
        if (this.complete || this.readyState == 'complete') { $(this).animate({'opacity': 1}, 300) } 
        else { $(this).load(function() { $(this).animate({'opacity': 1}, 300) }); }
    });

    
    $('#gallery').galleria({
        // #img is the empty div which holds full size images
        insert: '#img',
        
        // enable history plugin
        history: false,
		clickNext : false,
		
       

        // function fired when the image is displayed
		onImage: function(image,caption,thumb){
			thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6)
         image.attr('id','');
         //image.addClass('lightbox');
         image.attr('id', 'big');
         var jindex= thumb.parent().attr('jcarouselindex');

         image.attr('rel',jindex);
          $('.lightbox').slimbox({loop: true},
			function(el) {return [el.src, el.title,el.alt];},
			function(el) {return (el.id!='big');}
			);
		
				   		//#check if thumbnail indicates that a large version of this pic is available
		},
		       
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb,container) {
			var large = thumb.attr('rel');
			var title = thumb.attr('title');
			var alt = thumb.attr('alt');
				
				 
			thumb.hover(function() {
				$('#count #aktuell').html($(this).parent().attr('jcarouselindex'));
				$.galleria.activate(large);
				clearTimeout(nextImage);
				$('#pause').removeClass('pause');
				$('#play img').attr('src','fileadmin/images/styles/play_b.gif');
				}
				);
				var $li = thumb.parent(),
		        opacity = $li.is('.active') ? 1 : 0.6;
					
		            // hover effects for list elements
		            $li.hover(
		                function() { $li.fadeTo(200, 1); },
		                function() { $li.not('.active').fadeTo(200, opacity); }
		            )
					}
    }).find('li:first').addClass('active'); // display first image when Galleria is loaded
        $('#img .caption').css('height', 20);
var bigone = $('.active IMG').attr('src');
$.galleria.activate(bigone);

$('#prev').click(function() {
var prev=$('#wrapper li.active img').parent().prev().find('img').attr('rel');
var aktuell=parseInt($('li.active').attr('jcarouselindex'))-1;
$('#aktuell').html(aktuell).attr('rel',aktuell);
var id=$('#wrapper li.active img').parent().prev().find('img').attr('alt');
$('#addwunsch').attr('rel',id);
if (prev==undefined) {
var gesamt=gesamt=$('#gallery li').length;
$('#aktuell').html(gesamt).attr('rel',gesamt);
prev=$('#wrapper li:last-child img').attr('rel');
id=$('#wrapper li:last-child img').attr('alt');
$('#addwunsch').attr('rel',id);
}
var carousel = $('#gallery').data('jcarousel');
var idx =  $('#gallery li.active').data('index') - 0;

var maxItem=$('#gallery li').length;
if(idx==2) {
$('#prev').hide();
carousel.scroll(idx-1);
} else {
carousel.scroll(idx-1);
}


$.galleria.activate(prev);
});
$('#next').click(function() {
$('#prev').show();

var next=$('#wrapper li.active img').parent().next().find('img').attr('rel');
var aktuell=parseInt($('li.active').attr('jcarouselindex'))+1;
var id=$('#wrapper li.active img').parent().next().find('img').attr('alt');
$('#addwunsch').attr('rel',id);
$('#aktuell').html(aktuell).attr('rel',aktuell);
var carousel = $('#gallery').data('jcarousel');
var idx =  $('#gallery li.active').data('index') - 0;

var maxItem=$('#gallery li').length;

if(idx==maxItem) {
$('#prev').hide();
carousel.scroll(0);
} else {
carousel.scroll(idx+1);
}

if (next==undefined) {
next=$('#wrapper li:first-child img').attr('rel');
$('#aktuell').html(1).attr('rel',1);
id=$('#wrapper li:first-child img').attr('alt');
$('#addwunsch').attr('rel',id);

}
$.galleria.activate(next);
});

$('#addwunsch').live("click", function () {
var uid=$(this).attr('rel');

$.ajax({
	type:'POST',
    url: '/?eID=wunschliste',
    data: {
	eID: "wunschliste",	// die erstellte eID
	uid: uid	// Benutzereingabe
	},
    dataType: 'json',
    success: function(result) {
    if (result.myResult=='ok') {
    var witems = parseInt($('#artikel').html())+1;
    $('#artikel').html(witems);
    var message='Der Artikel wurde der Wunschliste hinzugefügt';
    }
    else {
    var message='Dieser Artikel wurde bereits der Wunschliste hinzugefügt';
    }
    $('<div id="info"><p>'+ message +'</p></div>').animate({opacity: 0.8}).insertAfter('#lbBottomContainer');
	$('#info').hide().fadeIn(100).delay(2000).fadeOut(100);    
    }
    });

});
countImage();
function countImage(eqindex,nextIndex) {

var gesamt=$('#gallery li').length;
var id=$('#wrapper li.active img').attr('alt');
	$('#addwunsch').attr('rel',id);
$('#count').remove();
$('<div id="count"><div id="addwunsch" rel="'+ id +'"><img src="fileadmin/images/styles/stern_b.gif" /></div><div id="play"><img src="fileadmin/images/styles/play_b.gif" /></div><div id="gesamt">/'+ gesamt +'</div><div id="aktuell" rel="1">1</div></div>').appendTo('.center');
}
$('#addwunsch').hover(function() {
$('#addwunsch img').attr('src','fileadmin/images/styles/stern_w.gif');
$('<div class="info"><div class="hide">Nicht wieder zeigen <input type="checkbox" name="hide" /></div><div class="text">Der Stern fügt den angezeigten Artikel zur Wunschliste hinzu</div></div>').appendTo('#addwunsch');
}, function() {
$('#addwunsch .info').remove();
$('#addwunsch img').attr('src','fileadmin/images/styles/stern_b.gif');

});
$('#play').hover(function() {
$('<div class="info"><div class="hide">Nicht wieder zeigen <input type="checkbox" name="hide" /></div><div class="text">Mit Hilfe dieses Buttons können Sie die Diashow steuern</div></div>').appendTo('#play');
if($('#play').hasClass('pause')) {} else {
$('#play img').attr('src','fileadmin/images/styles/play_w.gif');
}
}, function() {
$('#play .info').remove();
if($('#play').hasClass('pause')) {} else {
$('#play img').attr('src','fileadmin/images/styles/play_b.gif');
}
});
function getDecimal(wert) {
	var wert=wert;
	if(wert < 10) {
		wert='0' + wert;
	}
	return wert;
	}
$('#play').click(function() {
if($('#play').hasClass('pause')) {
clearTimeout(nextImage);

$(this).removeClass('pause');
$('#play img').attr('src','fileadmin/images/styles/play_b.gif');
} else {
$('#play').addClass('pause');
$('#play img').attr('src','fileadmin/images/styles/pause.gif');
timeout();
}
});

function timeout(){
	nextImage=setTimeout(function(){
	var next=$('#wrapper li.active img').parent().next().find('img').attr('rel');
	var aktuell=parseInt($('li.active').attr('jcarouselindex'))+1;
	$('#aktuell').html(aktuell).attr('rel',aktuell);
	var id=$('#wrapper li.active img').parent().next().find('img').attr('alt');
	$('#addwunsch').attr('rel',id);

	if (next==undefined) {
		next=$('#wrapper li:first-child img').attr('rel');
		$('#aktuell').html(1).attr('rel',1);
		id=$('#wrapper li:first-child img').attr('alt');
		$('#addwunsch').attr('rel',id);
	}	
	var carousel = $('#gallery').data('jcarousel');
var idx =  $('#gallery li.active').data('index') - 0;

var maxItem=$('#gallery li').length;

if(idx==maxItem) {
$('#prev').hide();
carousel.scroll(0);
} else {
carousel.scroll(idx+1);
}

	$('#play img').attr('src','fileadmin/images/styles/pause.gif');
	timeout();
	},4500);

}

});
