jQuery.fn.photo_slider = function(options) {
	var settings = {		
		photoWidth: 79,
		loadUrl: "http://dev.buitenlampentotaal.nl/shop/photos/show_resized/",
		popupUrl: "http://dev.buitenlampentotaal.nl/shop/photos/show_in_popup/",
		productId: null
	};
	
	if (options) {
		jQuery.extend(settings, options);
	}
		
	$(this).find(".slide_photos").css({
		"width": this.find("li").length * settings.photoWidth,
		"position": "absolute",
		"left":"0px"
	});
	
	settings.beginLeft = $(this).find(".slide_photos").css("left");
	
	setArrows($(this).find(".slide_photos"));
	
	$("#slide_show img#large_image").click(function() {
		openImagePopup();
	});

	$("#slide_show img#magni_image").click(function() {
		openImagePopup();
	});


	$(this).find("li a").each( function() {
		$(this).click(function() {
			loepImage = $("#slide_show img#loep");
			$("#slide_show .image").load( 
				settings.loadUrl + $(this).attr("href"),
				null,
				function() {
					$("#slide_show").append( loepImage );
					
					$("#slide_show img").click(function() {
						openImagePopup();
					});
				}
			);
			return false;
		});
	});
	
	$(this).find(".left_arrow").click(function() {
		var slidePhotos = $(this).parent().find(".slide_photos");
		
		if (parseInt(slidePhotos.css("left")) < parseInt(settings.beginLeft)) {
			slidePhotos.animate({
				"left": "+=" + (3 * settings.photoWidth)
			},
			300,
			"linear",
			function(){
				setArrows( $(this) )
			});
		}
	});
	
	function openImagePopup() {
		var largeImage  = $("#slide_show img");
		window.open(settings.popupUrl + settings.productId + largeImage.attr("src"), '', 'width=600,height=600,scrollbars=no,toolbar=no,location=no'); 
		return false;
	}
	
	$(this).find(".right_arrow").click(function() {
		var slidePhotos = $(this).parent().find(".slide_photos");
		
		if( ( parseInt(slidePhotos.css("left")) + (3 * settings.photoWidth) ) > parseInt(settings.beginLeft) ) {
			slidePhotos.animate({
				"left": "-=" + (3 * settings.photoWidth)
			},
			300,
			"linear",
			function(){
				setArrows( $(this) )
			});
		}
	})
	
	function setArrows( slide_photos ) {
		var slidePhotos = slide_photos;
		var leftArrow = slidePhotos.parent().parent().find(".left_arrow");
		var rightArrow = slidePhotos.parent().parent().find(".right_arrow");
		
		if (slidePhotos.find("li").length <= 3 || parseInt(slidePhotos.css("left")) < parseInt(settings.beginLeft)) {
			rightArrow.addClass("disabled_arrow");
		} else {
			rightArrow.removeClass("disabled_arrow");
		}
		
		if ((parseInt(slidePhotos.css("left")) + (3 * settings.photoWidth)) > parseInt(settings.beginLeft)) {
			leftArrow.addClass('disabled_arrow');
		} else {
			leftArrow.removeClass('disabled_arrow');
		}
	}
}