var current_slide_num = 1;
var intervalID;

function nextSlide() {
	// default to the first edition
	var next_num = 1;
	// get child count
	var children_count = $("#sv_pagination").children().size(); 
	// if current_photo_num is less than to the number of children
	if (current_slide_num < children_count){
		next_num = $("#sv_button_" + current_slide_num).next().attr("id").replace(/sv_button_/,"");		
	}
	clickAction(next_num);
}

function previousSlide() {
	// get child count
	var children_count = $("#sv_pagination").children().size(); 
	// default to the first edition
	var previous_num = children_count;
	// if current_photo_num is less than to the number of children
	if (current_slide_num > 1){
		previous_num = $("#sv_button_" + current_slide_num).prev().attr("id").replace(/sv_button_/,"");		
	}
	clickAction(previous_num);
}

function clickAction(num) {
	$("#sv_button_" + current_slide_num).removeClass("current");
	$("#sv_slide_" + current_slide_num).fadeOut("medium"); //fadeTo(400, 0);
	current_slide_num = num;
	$("#sv_button_" + current_slide_num).addClass("current");
	$("#sv_slide_" + current_slide_num).fadeIn("medium"); //fadeTo(400, 1);
}

function buildGallery(dir,photos,slide_time) {
	var photo_array = photos.split(",");
	var output = '<div id="sv_screen">'+"\n";
	var photo_count;
	for ( var i in photo_array ) {
		photo_count = parseFloat(i)+1;
		output += '<div id="sv_slide_'+photo_count+'" class="sv_slide"><img src="'+photo_array[i]+'" alt="" /></div>'+"\n";
	}
	output += '</div> <!-- /#sv_screen -->'+"\n";
	if (photo_count > 1) {
		output += '<div id="sv_controls"><div id="sv_pagination">'+"\n";
		for(i=1; i<=photo_count; i++) {
			output += '<a id="sv_button_'+i+'" class="sv_button"><img src="/wp-content/themes/mjp/media/images/blank.png" alt="" width="13" height="13" /></a>'+"\n";
		}
		output += '</div>'+"\n";
		output += '<a id="sv_button_back" href="#"><img src="/wp-content/themes/mjp/media/images/blank.png" alt="" /></a> <a id="sv_button_next" href="#"><img src="/wp-content/themes/mjp/media/images/blank.png" alt="" /></a>'+"\n";
		output += '</div> <!-- /#sv_controls -->'+"\n";
	} else {
		// strip out id="photo1" to keep from animating
		output = output.replace(/id="sv_slide_1"/,"");
	}		
	
	//swap show
	$("#sv_projector").html(output);
	// hide all photos
	$('.sv_slide').hide(); //fadeTo(1, 0);
	// if only 1 photo, fade it in
	if (photo_count == 1) {
		$(".sv_slide").fadeIn("medium"); //fadeTo(400, 1);
	}
	//stop transitions
	clearInterval(intervalID);
	//"click" the first photo
	current_slide_num = 1;

	// Prepare gallery navigation functionality
	$("#sv_pagination a").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		var num = $(this).attr("id").replace(/sv_button_/,"");
		clickAction(num);
	});
	// Click on "Next Photo"
	$("#sv_button_next").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		nextSlide();
		return false;
	});
	// Click on "Previous Photo"
	$("#sv_button_back").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		previousSlide();
		return false;
	});
	clickAction(current_slide_num);
	//start transitions
	intervalID = window.setInterval('nextSlide()', slide_time);
}

$(document).ready(function(){

	// initialize gallery on page load
	$("#sv_projector").each(function(){
		// dir, title, & photos defined in inline js
		var projector_class = $(this).attr("class");
		if ($(this).hasClass("header_img")) {
			// get other class
			projector_class = projector_class.replace(/header_img /,'');
		} 
		
		if (projector_class == '') {
			var dir = false;
			// no gallery folder... see if images exist on page
			var photos = '';
			var count_photos = 0;
			$(".post img").each(function(){
				if (count_photos < 1) {
					photos += $(this).attr("src");
				} else {
					photos += ","+$(this).attr("src");
				}
				count_photos++;
			});
			buildGallery(dir,photos,5000);
		} else if (projector_class == 'home_tiles') {
			return false;
		} else {
			var dir = projector_class;
			// gallery folder exists... list files
			$.ajax({
				url:"/galleries/",
				type:'get',
				data: 'dir='+dir,
				success: function(results) {
					photos = results;
					buildGallery(dir,photos,5000);
				}
			});
		}
	});

	$(".sv_swap_gallery_list li a").click(function(){
		var dir = $(this).attr("class");
		$.ajax({
			url:"/galleries/",
			type:'get',
			data: 'dir='+dir,
			success: function(results) {
				photos = results;
				buildGallery(dir,photos,5000);
				$.scrollTo("#sv_projector", 400);
			}
		});
		return false;
	});

});
