(function($) {
	$(document).ready(function() {
		if($('a.flickr').length > 0) {
			$('a.flickr').each(function(){
				var $this = $(this);
				var url = $this.attr('href') + '&jsoncallback=?';
				var params = {format:'json'};
				$.getJSON(url, params, function(json) {
					if(json.items) {
						var ul = '<ul class="gallery">';
						$.each(json.items, function(i, n) {
							var item = json.items[i];
							img = item.media.m;
							large = img.replace('_m.jpg','.jpg');
							thumb = img.replace('_m.jpg','_s.jpg');
							ul += '<li><a href="'+ large +'"  title="'+ item.title +'"><img src="'+ thumb +'" title="'+ item.title +'" /></a></li>';
						});
						ul += '</ul>';
						$(ul).insertAfter($this);
						$('.gallery a').lightBox();
					}
				});
				$this.hide();
			});
		}
	});
})(jQuery);

var $imgList;
function slideSwitch() {
    var $active = $imgList.find('li.active');

    if ( $active.length == 0 ) $active = $imgList.find('li:last');

    var $next =  $active.next().length ? $active.next()
        : $imgList.find('li:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function() {
  $imgList = $('#header_image');
  var rand = Math.floor(Math.random() * $imgList.find('li').length);

  $imgList.find('li:eq('+ rand +')').addClass('active');
  setInterval( "slideSwitch()", 8000 );
});

