var shadowInit = false;

function LoadFlickr(setId, userId, elId)
{
	if (shadowInit == false) {
		Shadowbox.init();
		shadowInit = true;
	}

    $.getJSON(
		"http://api.flickr.com/services/feeds/photoset.gne?format=json&set=" + setId + "&nsid=" + userId + "&jsoncallback=?"
		,function(data){
			if (data.items) {
				var items = data.items;
				var count = items.length;
				var par = $(elId);
				var imgSrc = '';
				var imgSrcFull = '';
				if(count){
					par.append('<button type="button" class="previous">Previous</button>');
					var ul = $('<ul class="nav"></ul>').appendTo(par);
					par.append('<button type="button" class="next">Next</button>');
					for(var i = 0; i < count; ++i){
						imgSrc = items[i].media.m.replace(/_m(\.[^\.]*)/,'_s$1');
						imgSrcFull = items[i].media.m.replace(/_m(\.[^\.]*)/,'$1');
						ul.append('<li><a href="'+imgSrcFull+'"><img alt="" src="'+imgSrc+'" /></a></li>');
					}
					par.jcarousel({
						scroll: 7
						, buttonPrevHTML: null
						, buttonNextHTML: null
						, initCallback: initFlickrCarousel
					});
				}
			}
		}
	);
}

function initFlickrCarousel(carousel){
    jQuery('button.next', carousel.container).bind('click', function(e) {
        e.preventDefault();
        carousel.next();
        return false;
    });
    jQuery('button.previous', carousel.container).bind('click', function(e) {
        e.preventDefault();
        carousel.prev();
        return false;
    });

    $(carousel.container).delegate('a', 'click', function(e){
        e.preventDefault();

        Shadowbox.open({
            content: $(this).attr('href')
            , player: 'img'
            , title: $(this).attr('title')
        });
    });
};

