function pr(s) {
	if(console && false) {
		console.log(s);		
	}
}

$.fn.imageslide = function(product, count) {
	
	var current = 1;
	var product = product;
	var count = count;
	var path = '/content/';
				
	setProduct = function(p, c) {
				
		product = p;
		count = c;
		current = 1;
				
		$('#p-'+product).addClass('selected');	// set selected State on link		
		$('#product-image').empty(); //emtpy product image container 		
		$('#nav-frames').empty(); //emtpy product image container 		
		for(var i = 1; i <= count; i++) { // fill the container with n images
			$('#product-image').append('<img src="' + path + product + '/' + i + '.jpg" id="'+ product + '-' + i +'">');
			$('#nav-frames').append('<a href="#'+i+'"><img src="' + path + 'thumbs/'+ product + '/' + i + '.jpg" id="'+ product + '-' + i +'"></a>'); // give me some extra frames			
		}	
		
		
		$('#nav-frames a:first').addClass('selected');
		$('#product-image').children().hide(); // and hide the images		
		$('#'+product+'-'+current).fadeIn(1000); // fade in 1st image
		
		updateView();
		
		$('#nav-frames a').click(function(e){
			e.preventDefault();
			if(current != $(this).attr('href').substr(1)) {
				$(this).siblings().removeClass('selected');
				$(this).addClass('selected');
				$('#'+product+'-'+current).fadeOut(1000);
				$('#'+product+'-'+$(this).attr('href').split('#')[1]).fadeIn(1000);			
				current = $(this).attr('href').split('#')[1];				
			}			
		});
				
	}
		
	nextImage = function() {
		
		if(current == count) {
			$('#'+product+'-'+current).fadeOut(1000);
			current = 1;
			$('#'+product+'-'+current).fadeIn(1000);
		} else {
			$('#'+product+'-'+current).fadeOut(1000);			
			current++;
			$('#'+product+'-'+current).fadeIn(1000);			
		}
				
		updateView();			
	}

	prevImage = function() {
		
		if(current == 1) {
			$('#'+product+'-'+current).fadeOut(1000);
			current = count;
			$('#'+product+'-'+current).fadeIn(1000);
		} else {
			$('#'+product+'-'+current).fadeOut(1000);
			current--;
			$('#'+product+'-'+current).fadeIn(1000);
		}
				
		updateView();	
				
	}
		
	updateView = function() {
		
		$('#current').text(current);
		$('#count').text(count);		
		$('#nav-frames a').removeClass('selected');
		$('#nav-frames a[href=#'+current+']').addClass('selected');
	}	
	
	// events
		
	$('.products-teaser p a').click(function(e) {
		e.preventDefault();
		$('.products-teaser').removeClass('selected');
		$(this).parent().parent().addClass('selected');
		setProduct($(this).attr('href').split('#')[1], $(this).attr('count'));			
	});
	
	$('#next').click(function(e) {
		e.preventDefault();
		nextImage();		
	});

	$('#prev').click(function(e) {
		e.preventDefault();
		prevImage();		
	});
	
	// init:
	setProduct(product, count);	


	// auto animation:
	var interval = window.setInterval(function(){
		nextImage();
	}, 5000);
	
	$('#product').mouseover(function(){
		window.clearInterval(interval);
	});
	
	$('#product').mouseout(function(){
		interval = window.setInterval(function(){
			nextImage();	
		}, 5000);
	});	
	
}
	
$(document).ready(function () {
	
	$('#product').imageslide('raglan', 4);
	
});