function setFeatured(ele, super_thumb) {
    $('#fwd_featured_items a.active').removeClass('active');
    $('#fwd_featured_thumbs').html(' ');
    var type = ele.attr('rel') ? ele.attr('rel') : 'image';
    var src = ele.attr('href');
    var thumbs;
    if (super_thumb) {
        thumbs = super_thumb;
    } else {
        thumbs = ele.data('thumbs');
    }
    var html;
    
    switch (type) {
        case 'flickr':
            html = '<img src="http://www.farfromrest.com/super_international/flickr_large.php?photo_id=' + src + '"" width="800">';
            if (thumbs) {
                var position = thumbs.length;
                html2 = '';
                for (var i = thumbs.length - 1; i >= 0; i--){
                    if (thumbs[i] == src) {
                        position = i;
                    }
                    html2 += '<a href="' + thumbs[i] + '" rel="flickr" class="thumbs"><img src="http://www.farfromrest.com/super_international/flickr_thumb.php?photo_id=' + thumbs[i] + '" height="50"></a>';
                }
                
                var forward = thumbs[position-1] ? thumbs[position-1] : thumbs[thumbs.length-1];
                var back = thumbs[position+1] ? thumbs[position+1] : thumbs[0];
            }
            break;
        case 'vimeo':
            html = '<iframe src="http://player.vimeo.com/video/' + src + '?title=0&byline=0&portrait=0&autoplay=false" width="800" height="500" frameborder="0"></iframe>';
            break;
    }
    $('#fwd_featured').html( html );
    if (thumbs) {
        html2 += '<div id="fwd_counter"><a href="' + back + '" rel="flickr" class="fwd_back"><</a>&nbsp;-&nbsp;<a href="' + forward + '" rel="flickr" class="fwd_forward">></a> (' + (thumbs.length - position)  + ' of ' + thumbs.length + ')</div>'
        $('#fwd_featured_thumbs').html( html2 );
        $('#fwd_featured_thumbs a.thumbs').each(function(){
            $(this).animate({opacity: .2}, 'slow');
            $(this).hover(function(){
                $(this).animate({opacity: 1}, 'fast');
            }, function(){
                $(this).animate({opacity: .2}, 'slow');
            });
            $(this).bind('click', function(e){
                e.preventDefault();
                setFeatured($(this), thumbs);
            });
        });
        
        $('.fwd_back, .fwd_forward').bind('click', function(e){
            e.preventDefault();
            setFeatured($(this), thumbs);
        });
    }
    ele.addClass('active');
}
jQuery(document).ready(function($) {
    $('#fwd_featured_items a').each(function(){
        $(this).animate({opacity: .2}, 'slow');
        $(this).hover(function(){
            $(this).animate({opacity: 1}, 'fast');
        }, function(){
            $(this).animate({opacity: .2}, 'slow');
        });
        $(this).bind('click', function(e){
            e.preventDefault();
            setFeatured($(this));
        });
    });
    
    $('#fwd_featured_items a[rel=vimeo]').each(function(){
        var ele = $(this);
        var video_id = ele.attr('href');
        ele.html('<img src="http://www.farfromrest.com/super_international/vimeo_thumb.php?video_id=' + video_id + '">');
    });
    $('#fwd_featured_items a[rel=flickr]').each(function(){
        var ele = $(this);
        var photo_id = ele.attr('href');
        var gallery_photos = ele.text();
        if (gallery_photos) {
            var thumbs = gallery_photos.split(',');
            ele.data('thumbs', thumbs);
        }
        ele.html('<img src="http://www.farfromrest.com/super_international/flickr_thumb.php?photo_id=' + photo_id + '">');
    });
    setFeatured($('#fwd_featured_items a:first'));
});
