var roboclick = false;

$(document).ready(function() {
	$('#carousel ul li a.thumb').click(function() {
		//stop autoplay on click
		autoplay("off");
		
		activate_item($(this));
		
		return false;
	});
	
	colorbox_init();
	
	$('#count').html($("#carousel li").length);

	
	$('#current_gallery').click(function(event) {
		$('#gallery_selector').slideToggle();
		event.stopPropagation();
		
		$('body').one('click', function() {
			$('#gallery_selector').slideUp();
		});
		
		return false;
	});
	
	if($("#carousel").length > 0) {
		if(window.location.hash == "") {
			activate_item($("#carousel ul li a:first"));
		} else {
			activate_item($(window.location.hash+" a:first"));
		}
	}
	
	$('.jcarousel ul').jcarousel({
		wrap: 'last',
		visible: 10,
		scroll: 10,
		buttonPrevHTML: null,
		buttonNextHTML: null,
		initCallback: carousel_init,
		buttonNextCallback: function(carousel, button, enabled) {
			
		},
		buttonPrevCallback: function(carousel, button, enabled) {
			
		},
		itemFirstInCallback: function(carousel, item, idx, state) {
			//alert(carousel+" "+item+" "+idx+" "+state);
			max = $("#carousel li a.thumb").length;
			if(max > (idx+9)) {
				max = idx+9;
			}
			
			var selected = selected_index();
			
			$('#range').html(idx+"-"+max);
			//adjust the selected item based on the carousel position, only if the selected item's index
			//is outside the visible range
			if(selected >= max || selected < (max-9)) {
				if(idx != 1) {
					//activate_item($('#carousel li a.current').parent().next().find("a.thumb"));
					activate_item($("#carousel li a.thumb").eq(idx-1));
				} else {
					activate_item($('#carousel li a.thumb:first'));
				}
			} else if(auto != null) {
				activate_item($('#carousel li a.current').parent().next().find("a.thumb"));
			}
			
		}
	});
	
	//autoplay();
	
});

var auto = null;

function autoplay(status) {
	if(auto == null && status != "off") {
		$("#auto_status").html("On");
		auto = setInterval(function() {
			var index = $('#carousel li a.thumb').index($("#carousel li a.current"));
			
			if(index < $('#carousel li a.thumb').length - 1) {
				//if index is the last visible item, we need to shift the carousel
				if((index+1)%10 == 0) {
					$('#carousel #next').click();
				} else {
					activate_item($('#carousel li a.current').parent().next().find("a.thumb"));
				}
			} else {
				$('#carousel #next').click();
			}
		}, 8000); //TIME FOR AUTOPLAY
	} else {
		clearInterval(auto);
		auto = null;
		$("#auto_status").html("Off");
	}
}

function activate_item(obj) {
	$("#gallery").html('');
	$("#gallery").addClass("loading");
	
	$("#carousel li a.current").removeClass("current");
	
	$.ajax({
		url: obj.attr("href"),
		data: "ajax=1",
		dataType: "html",
		type: "GET",
		success: function(data) {
			$('#gallery').html(data);
			$('#gallery').removeClass("loading");
			
			$('.large').click(function() {
				//look up the corresponding hidden lightbox link and click that
				$("#carousel #post-"+$("#post-id").html()+" a.colorbox").click();
				return false;
			});
			
			colorbox_init();
		}
		
	});
	
	obj.addClass("current");
	
	window.location.hash = obj.parent("li").attr("id");
}

function carousel_init(carousel) {
	$('#prev').click(function(){
		carousel.prev();
		return false;
	});
	
	$('#next').click(function(){
		carousel.next();
		return false;
	});
	
	if($("#carousel li a.thumb").length <= 10) {
		$("#prev").hide();
		$("#next").hide();
	}
}

function colorbox_init() {
	$(".colorbox").colorbox({
		scalePhotos: true,
		maxWidth: "100%",
		maxHeight: "100%",
		onOpen: function() {
			autoplay("off");
		}
	});
}

function fullScreen() {
	autoplay("off");
	$("#carousel li a.colorbox[href='"+$("#gallery #image a").attr("href")+"']").click();
}

function selected_index() {
	return $('#carousel li a.thumb').index($("#carousel li a.current"));
}

