$(document).ready(function(){

/*
 * jQuery jCarousel
 ********************/
	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;
	};
	
	jQuery(document).ready(function() {
		jQuery('#mycarousel').jcarousel({
			scroll: 1,
			auto: 1,
			wrap: 'last',
			initCallback: mycarousel_initCallback,
			itemLoadCallback: mycarousel_itemLoadCallback
		});
	});
	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();
		});
	};

/*
 * Lightbox
 ***********/
 	$(function() {
        $('#fTexto a[rel*=lightbox]').lightBox();
		//$('#fTexto a[@rel*=lightbox]').lightBox();
    });

}); //end document