/*
function mycarousel_initCallback(carousel) {

    jQuery('.gallery-controls ul li a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });

    jQuery('#gallery-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#gallery-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
    
};

// Ride the carousel...
jQuery(document).ready(function() {

	$('.gallery-controls').prepend('<a href="#" id="gallery-prev"><img src="/radio4/worldonthemove/assets/ui/gallery-controls-prev.gif" alt="Previous" /></a><a href="#" id="gallery-next"><img src="/radio4/worldonthemove/assets/ui/gallery-controls-next.gif" alt="Next" /></a>');
	
	$('.gallery-controls').css('opacity', 0.8);
	
	$('.gallery-controls').mouseover(function(event) {
		
		$(this).css({'opacity' : '1'});
	
	});
	
	$('.gallery-controls').mouseout(function(event) {
	
		$(this).css({'opacity' : '0.8'});
			
	});
	
	
	
    jQuery("#gallery ul").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

});

*/

/* Flickr carousel */

var carousel_gallery_pos_next;
var carousel_gallery_pos_prev;
var carousel_gallery_current_item = 0;
var carousel_gallery_animating = false;
var carousel_gallery_item_display = 1;


$(document).ready(function(){

	$('.gallery-controls').css('opacity', 0.8);
	
	$('.gallery-controls').mouseover(function(event) {
		
		$(this).css({'opacity' : '1'});
	
	});
	
	$('.gallery-controls').mouseout(function(event) {
	
		$(this).css({'opacity' : '0.8'});
			
	});

	/* programme slider */
	
	var carousel_gallery = $('#gallery');
	
	//$('ul', carousel_gallery).hide();
	
	var carousel_gallery_unit = $('ul li:first-child', carousel_gallery);
	var carousel_gallery_margin = 0; //parseInt($('ul li', carousel_gallery).css('margin-right'));
	var carousel_gallery_total = $('li', carousel_gallery).length;
	
	carousel_gallery_width = (isIE) ? $(carousel_gallery_unit).width() + carousel_gallery_margin : ( parseInt($(carousel_gallery_unit).width()) + parseInt($(carousel_gallery_unit).css('padding-left')) + parseInt($(carousel_gallery_unit).css('padding-right')) + carousel_gallery_margin );
	carousel_gallery_pos = parseInt($('ul', carousel_gallery).css('left'));
	carousel_gallery_pos_next = (carousel_gallery_pos - carousel_gallery_width);
	carousel_gallery_pos_prev = (carousel_gallery_pos + carousel_gallery_width);
	
	// add navigation
	if(carousel_gallery_total > 1) {
		$('.gallery-controls').prepend('<a href="#" id="gallery-prev"><img src="/radio4/worldonthemove/assets/ui/gallery-controls-prev.gif" alt="Previous" /></a><a href="#" id="gallery-next"><img src="/radio4/worldonthemove/assets/ui/gallery-controls-next.gif" alt="Next" /></a>');
	}
	
	$('.gallery-controls').append('<p id="galery_caption"></p>');


	$('#gallery-prev').css({opacity : 0.4, cursor : 'default'});
	
	$('#galery_caption').text($('ul li:first-child img', carousel_gallery).attr('alt'))
	
	
	// empty the link text
	
	/*
	$('ul li a', carousel_gallery).each(function() {
	
		var s = $(this).attr('href');
	
		$(this).css({background : 'url('+s+') center center', display : 'block'});
		
		$(this).attr('href', $(this).attr('rel'));
	
	});
	*/
	
	$('ul', carousel_gallery).show();
	
	
	// set width of episode holder
	$('ul', carousel_gallery).width(($('ul li', carousel_gallery).length * carousel_gallery_width) + 'px');
	
	$('#gallery').css('background', 'none');
	
	
	// next prev clicks
	$('#gallery-next').click(function() {
	
		
		if(carousel_gallery_animating) {
				
			return false;

		}
		
		if(carousel_gallery_current_item == (carousel_gallery_total - carousel_gallery_item_display)) {
			
			return false;
			
		}
		
		
		// set button states
		if ((carousel_gallery_current_item + 2) == (carousel_gallery_total)) {
	
			$('#gallery-next').css({opacity : 0.4, cursor : 'default'});
			$('#gallery-prev').css({opacity : 1, cursor : 'pointer'});
			
		} else {
		
			$('#gallery-prev').css({opacity : 1, cursor : 'pointer'});
			
		}

		
		$('#galery_caption').text($('ul li:eq('+(carousel_gallery_current_item+1)+') img', carousel_gallery).attr('alt'))
		
								
		carousel_gallery_pos_next = (parseInt($('ul', carousel_gallery).css('left')) - carousel_gallery_width);
		carousel_gallery_pos_prev = (parseInt($('ul', carousel_gallery).css('left')) + carousel_gallery_width);
		
		carousel_gallery_animating = true;
		
		$('ul', carousel_gallery).animate({left: carousel_gallery_pos_next }, 500, function() {
		
			carousel_gallery_animating = false;
		
		});

	
		carousel_gallery_current_item ++;
		
		return false;
		
	});
	
	$('#gallery-prev').click(function() {
	
		if(carousel_gallery_animating) {
				
			return false;

		}
	
		if(carousel_gallery_current_item == 0) {
				
			return false;
			
		}
		
		// set button states
		$('#gallery-next').css({opacity : 1, cursor : 'pointer'});
		
		if (carousel_gallery_current_item == 1) {
	
			$('#gallery-prev').css({opacity : 0.4, cursor : 'default', 'text-decoration' : 'none'});
			
		} else {
		
			$('#gallery-prev').css({opacity : 1, cursor : 'pointer'});
		
		}
		
		$('#galery_caption').text($('ul li:eq('+(carousel_gallery_current_item-1)+') img', carousel_gallery).attr('alt'))
			
		carousel_gallery_pos_next = (parseInt($('ul', carousel_gallery).css('left')) - carousel_gallery_width);
		carousel_gallery_pos_prev = (parseInt($('ul', carousel_gallery).css('left')) + carousel_gallery_width);
		
		carousel_gallery_animating = true;
		
		$('ul', carousel_gallery).animate({left: carousel_gallery_pos_prev }, 500, function() {
			
			carousel_gallery_animating = false;
			
		});
		
		carousel_gallery_current_item --;


		return false;
		
	});


});
