var animating = false;

//Shadowbox.loadSkin('classic', '/javascripts/sb/skin');

$(document).ready(function(){
	var options = { animSequence:  'sync' };

	//Shadowbox.init(options);
	Shadowbox.init();

	$('.next a, .prev a, .selects a').click(function(){
		if(animating == false) {
			animating = true;

			var selector = false;

			var images = $(this).parents('.slides').find('.images');
			var active = images.find('img.active');

			var last_image = images.find('img:last-child');
			var first_image = images.find('img:first-child');

		    if ( active.length == 0 ) active = last_image

			var next
			if($(this).parent().is(".next")) {
				next = active.next().length ? active.next() : first_image
			}
			else if($(this).parent().is(".prev")) {
				next = active.prev().length ? active.prev() : last_image
			}
			else if($(this).is(".select a")) {
				var all = $('a', this.parentNode);
				var currentIndex = all.index(this);
				next = images.find('img:eq('+currentIndex+')')

				if(next.is(".active")) { animating = false; return false }

				$(this).siblings('.active').removeClass('active');
				$(this).addClass('active');
				selector = true;

			}			

			active.addClass('last-active');

		    next.css({opacity: 0.0})
		        .addClass('active')
		        .animate({opacity: 1.0}, 300, function() {
		            active.removeClass('active last-active');

					// set the next select active
					if(!selector) {
						var select = $(this).parents('.slides').find('.selects a.active');
						var selects = $(this).parents('.slides').find('.selects a.select')

						var currentIndex = $('img', images).index(this);
						selects.eq(currentIndex).addClass('active');
						select.removeClass('active');
					}
					// stop animation semafore
					animating = false;
		        });
		}
		return false;
	})
	
	 $(".images img").lazyload({         
     		placeholder: "images/loader.gif",
		effect: "fadeIn",
		threshold: 200
		//event: "slideshow"
 	});


});

//$(window).bind("load", function() { 
//    var timeout = setTimeout(function() {$(".images img").trigger("slideshow")}, 5000);
//}); 
