$(document).ready(function() {

	// resourceStart is the Rages start event for resources that need initialisation 
	$('.resource-container').on('resourceStart', '.slider', function() {

		var $this = $(this);
		var re = /\d+/;
		var id = $this.parent('div.resource').attr('id').match(re)[0];

		// got to have more than one image for a slideshow
		if($this.children().length > 1) {
			startCycle(id, 0);
		}

		// adjust sliderNav top margin to compensate for sliderNav font height
		if($('.sliderNav_r' + id).length) {
			var fontSize = $('.sliderNav_r' + id + ' a').css('font-size').replace('px', '');
			var navPosY = $('.sliderNav_r' + id).css('top').replace('px', '');
			$('.sliderNav_r' + id).css('top', parseInt(navPosY) - parseInt(fontSize));
		}

		$this.show();
	});

	$(".slider").each(function() {
		$(this).trigger('resourceStart');
	});  // get the ball rolling


	function startCycle(id, index) {
		// actually start the cycle plugin
		$('#sliderimages_r' + id).cycle({
		    fx: $('#slidereffect_r' + id).html(),
		    startingSlide: index,
		    timeout: $('#slidertimeout_r' + id).html(),
		    pager: '.sliderNav_r' + id
		});
	}

});

