$(document).ready(function() {


	//Show Banner
	$(".main_image .desc").show(); //Show Banner
	$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity

	//Click and Hover events for thumbnail list
	$(".image_thumb ul li:first").addClass('active'); 
	$(".image_thumb ul li").click(function(){ 
		//Set Variables
		var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
		var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
		var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
		var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
		
		if ($(this).is(".active")) {  //If it's already active, then...
			return false; // Don't click through
		} else {
			//Animate the Teaser				
			$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
				$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
				$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
			});
		}
		
		$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
		$(this).addClass('active');  //add class of 'active' on this list only
		return false;
		
	}) .hover(function(){
		$(this).addClass('hover');
		}, function() {
		$(this).removeClass('hover');
	});
			
	//Toggle Teaser
	$("a.collapse").click(function(){
		$(".main_image .block").slideToggle();
		$("a.collapse").toggleClass("show");
	});
	
	
	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: 4,
        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();
    });
};

});