(function($){

	$.fn.slideshow = function(){
		var $all = $(this);
		var $spinner = $("#loader");
		var $img = $("#stretch_background img.first");
		var $old_img = $img;
		var $front = $("#image_1");
		var $back = $("#image_2");
		var bussy = false;
		var root = ("onorientationchange" in window) ? $(document) : $(window);
		var ratio = 1;
		var show_time = 0;
		var $all_specs = $('ul.specs');
		
		function do_resize(overwrite)
		{
			var bgCSS = {left: 0, top: 0}
			var bgWidth = root.width();
			var bgHeight = bgWidth / ratio;
				if(bgHeight >= root.height()) {
					bgOffset = (bgHeight - root.height()) /2;
					$.extend(bgCSS, {top: "-" + bgOffset + "px"});
				} else {
					bgHeight = root.height();
					bgWidth = bgHeight * ratio;
					bgOffset = (bgWidth - root.width()) / 2;
					$.extend(bgCSS, {left: "-" + bgOffset + "px"});
				}
				$img.width( bgWidth ).height( bgHeight ).css(bgCSS);
		}
		
		$(window).bind('do_resize',function(){
			do_resize(true);
		});
		$(window).resize(do_resize);
		var counter = 1;
		var current = 1;
		$("#next").click(function(){
			current++;
			$next = $("#thumb_"+current);
			//$next = $(".active_group #thumb_"+current);
			if($next.length == 0)
			{
				$(".thumb").first().click();
				//$(".active_group .thumb").first().click();
			}
			else
			{
				$next.click();
			}
		});
		
		var last_src = '';
		var last_img = false;
		
		$all.each(function(){
			var $obj = $(this);
			$obj.data('number', counter);
			$obj.attr('id', 'thumb_'+counter);
			var obj_counter = counter;			
			counter++;
			var image_src = $(this).attr('href');
			var project = $(this).attr('rel');
			$obj.click(function(e){
				e.preventDefault();
				current = obj_counter;
				if(bussy) return;
				bussy = true;
				$all.removeClass('active');
				$obj.addClass('active');
				$img.attr('id','old_image');
				$old_img = $('#old_image');
				$old_img.css('z-index',1);
				$all_specs.hide();
				$("#specs_detail_"+project).show();
				//     ^     hier de goede neerzetten
				
				var timout = setTimeout(function(){
					$spinner.animate({
						top: 25
					}, {
						duration: 650,
						easing: 'easeOutQuint'
					});
				},1000);
				$img = $('<img/>');
				$img.css({
						'z-index': 2,
						'opacity': 0
					});
				$img.unbind('load').load(function(){
					clearTimeout(timout);
					ratio = this.width / this.height;
					$(this).prependTo("#stretch_background");
					do_resize(true);
					$(this).animate({
						opacity: 1
					},400, function(){
							$('#old_image').remove();
							bussy = false;
						$spinner.animate({
							top: -65
						}, {
							duration: 650,
							easing: 'easeInQuint'
						});
					});
				});
				$img.unbind('error').error(function(){
					alert($(this).attr('src'));
				});
				$img.attr('src', image_src);
				last_img = $img;
				last_src = image_src;
			});
				
		});
		
		setTimeout(function(){
			var rnd = Math.floor(Math.random()*counter);
			$('#thumb_'+rnd).click();
		},0);
		do_resize();
		return $(this);

	}
	
})(jQuery);

(function($){
	$.fn.hoverClass = function(class_name){
		return $(this).each(function(){
			var the_class = class_name;
			$(this).mouseover(function(){
				$(this).addClass(the_class)
			}).mouseout(function(){
				$(this).removeClass(the_class);
			});
		});
	}
})(jQuery);

