$(document).ready(function(){
	var speed, startScrollHandler, stopScrollHandler, scrollTimer, windowWidth, listWidth, itemWidth;

	//if we are looking at the activity, location or arrangement-page, append producttree to menu
	$('#page10587766.selected, #page10587798.selected, #page10587839.selected, #page11022785.selected').each(function() {
		$('#activities').clone(false).appendTo($(this)).attr('id', 'activitiesInMenu').find('img').remove();

		//lightbox!
		$('#productShots a').lightBox({
			'txtImage': 'afbeelding',
			'txtOf': 'van'
		});
	});

	/* scroller stuff, depends on number of items... */
	if ($('#activities li').length > 9) {
		windowWidth = $('#window').width();
		listWidth = $('#activities').width();
		itemWidth = $('#activities li').outerWidth();

  		scrollTimer = $.timer(10, function (timer) {
  			if (speed != 0) {
  				var newPos = $('#activities').position()['left']+speed;
  				//unshift item when needed
  				if (newPos > -itemWidth) {
					$('#activities li:last').prependTo('#activities');
					newPos -= itemWidth;
  				}

  				//push item when needed
  				if (newPos < (windowWidth - listWidth + itemWidth)) {
  					$('#activities li:first').appendTo('#activities');
  					newPos += itemWidth;
  				}

  				if (!isNaN(newPos)) $('#activities').css('left', newPos+'px');
  			}
		});

		startScrollHandler = function(){
			speed = $(this).is('#next')?-8:8; //define direction
			return false;
		}

		stopScrollHandler = function(){
			speed = 0;
			return false;
		}

		$('#activityScroller #next, #activityScroller #prev').mousedown(startScrollHandler).mouseup(stopScrollHandler).mouseleave(stopScrollHandler).mouseout(stopScrollHandler).click(function() { return false; });

		 //position the ul#activities one li to the left. This way we can scroll both forward and backward...
		$('ul#activities').css('left', -$('#activities li').outerWidth());

		//now make sure the first element is visible, so move the last item in front of the rest...
		$('#activities li:last').prependTo('#activities');

		//#13296706 - schuifbalk if there is an active webProduct, it would be nice if it was second on the list
		if (($('#webProductId').length > 0) && ($('#webProduct_'+$('#webProductId').val()).length > 0)) {
			//push sibblings till we have te active webProduct at the desired position
			while ($('#activities li:eq(1)').attr('id') != 'webProduct_'+$('#webProductId').val()) {
				$('#activities li:first').appendTo('#activities');
			}
		}
	} else {
		$('#activityScroller #next, #activityScroller #prev').hide();
	}

	/* label hiding stuff */
	$('input.hideMyLabel').labelHider();

	/* cycle header images*/
	$('#headerImages').cycle();

	/* init formchecking */
	$('#sidebar form').validate();
	$('#content form').validate();

	/* init datepicker */
	$('input.dateNL').datepicker();

	/* drop some shadows */
	$('#motto').clone().insertAfter('#motto').attr('id', 'mottoShadow');
});

$(window).load(function() {
	$('#headerImages img').each(function() {
		$(this).attr('src', '/download/?id=' + $(this).attr('id').split('_')[1] + '&width=978&height=435');
	});
	$('body').css('background-image', 'url(\'/img/background.jpg\')');
});