$(document).ready(function() {
	/* IE6 Hover */
	
	$('#menu li').bind("mouseover mouseout", function() {
		$(this).toggleClass('sfhover');
	});
	
	/* Lightbox */
	
	$('img[rel*="imgbox"]').bind('mouseenter', function() {
		$(this).css('cursor', 'pointer');
	}).each(function(i, el) {
		$(this).attr('href', el.src.replace('/thumbs', ''));
		$(this).attr('title', el.alt);
	}).lightBox({
		txtImage: 'Foto',
		txtOf: 'van'
	});
	
	/* Tooltips */
	
	$('.tips').tooltip({
		delay: 0,
		track: true,
		showURL: false,
		showBody: '::',
		left: 18,
		top: 8
	});
	
	/* Gallery */
	
	$.fn.galleryLeft = function() {
		if ($(this).data('loop') == true && $(this).position().left < 0) {
			this.animate({left: '+=2'}, 2, 'linear', function() {
				if ($(this).data('loop') == true && $(this).position().left < 0) {
					$(this).galleryLeft();
				};
			});
		}
		return this;
	}
	
	$.fn.galleryRight = function() {
		if ($(this).data('loop') == true && $(this).position().left > $(this).parent().width() - $(this).width()) {
			this.animate({left: '-=2'}, 2, 'linear', function() {
				if ($(this).data('loop') == true && $(this).position().left > $(this).parent().width() - $(this).width()) {
					$(this).galleryRight();
				};
			});
		}
		return this;
	}
	
	$('.galleryLeft').hover(
		function() {
			$(this).next().children('.galleryContent').data('loop', true).galleryLeft();
		},
		function() {
			$(this).next().children('.galleryContent').data('loop', false);
		}
	);
	
	$('.galleryRight').hover(
		function() {
			$(this).prev().children('.galleryContent').data('loop', true).galleryRight();
		},
		function() {
			$(this).prev().children('.galleryContent').data('loop', false);
		}
	);
	
});

$(window).load(function() {
	//$('.galleryContent img:first').css('margin-left', '10px');
	$('.galleryContent').each(function(i, el) {
		var galleryWidth = 0;
		$(this).children('img:last').css('margin-right', '0px');
		$(this).children('img').each(function() { galleryWidth += $(this).outerWidth(true); });
		galleryWidth = (galleryWidth > $(this).parent().width()) ? galleryWidth : $(this).parent().width();
		$(this).css('width', galleryWidth+'px');
		if (galleryWidth == $(this).parent().width()) {
			$(this).parent().siblings('.galleryLeft, .galleryRight').hide();
		}
	});
});
