jQuery(document).ready(function($){
	$('ul#bottommenu li:last').addClass('nospan');
	$('a[rel=lightbox]').lightBox();
	$("a[rel='colorbox']").colorbox({iframe: true, width:"95%", height:"95%"});
	$("a[rel='revistas']").colorbox({iframe: true, width:"90%", height:"95%", rel:"nofollow"});
	$("#slider").easySlider({
		speed: 800,
		auto: true, 
		continuous: true,
		numeric: true
	});
	$('div.online-expande> div').hide();
	$('div.online-expande> h3').click(function() {
		var $nextDiv = $(this).next();
		var $visibleSiblings = $nextDiv.siblings('div:visible');
		if ($visibleSiblings.length ) {
			$visibleSiblings.slideUp('slow', function() {
				$nextDiv.slideToggle('slow');
			});
		} else {
			$nextDiv.slideToggle('slow');
		}
	});
    $('#mycarousel').jcarousel({
        scroll: 1,
		auto: 4,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemLoadCallback: mycarousel_itemLoadCallback
    });	

	var mycarousel_tags = '';
	function mycarousel_initCallback(carousel, state)
	{
		// Do nothing of state is 'reset'
		if (state == 'reset')
			return;
	
		jQuery('form', carousel.container)
		.bind('submit', function(e) {
			mycarousel_tags = jQuery('input[@type=text]', carousel.container).val();
			carousel.reset();
			return false;
		});
	};

	function mycarousel_itemLoadCallback(carousel, state)
	{
		// Only load items if they don't already exist
		if (carousel.has(carousel.first, carousel.last)) {
			return;
		}
	
		jQuery.get(
			'dynamic_flickr_feed.php',
			{
				tags: mycarousel_tags
			},
			function(data) {
				mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);
			},
			'json'
		);
	};

	function mycarousel_itemAddCallback(carousel, first, last, data)
	{
		if (first == 1) {
			var plural = data.length == 1 ? '' : 's';
			jQuery('.results', carousel.container).html(data.length + ' photo' + plural + ' found');
	
			// Set size
			if (data.length == 0) {
				// Add a "no results" feedback as first item if data is empty
				carousel.size(1);
				carousel.add(1, '<p>No results</p>');
				return;
			} else {
				carousel.size(data.length);
			}
		}
	
		for (var i = first; i <= last; i++) {
			if (data[i - 1] == undefined) {
				break;
			}
	
			carousel.add(i, mycarousel_decodeEntities(data[i - 1].description));
		}
	};

	/**
	 * Decodes entites.
	 */ 
	function mycarousel_decodeEntities(s)
	{
		return s.replace(/&amp;/g,  "&")
				.replace(/&quot;/g, '"')
				.replace(/&#039;/g, "'")
				.replace(/&lt;/g,   "<")
				.replace(/&gt;/g,   ">");
	};

	/**
	 * This function is needed for the flickr feed.
	 */
	function jsonFlickrFeed(o)
	{
		return o.items;
	};
	function mycarousel_initCallback(carousel)
	{
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});
	
		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});
	
		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};

});
