/* counter that gets incremented each iteration */
var fs_counter = 0;
/* how long each array element shows */
var fs_stamina = 4000; /* 2 seconds */
/* how long to pause between fading in the next element */
var fs_intermission = 0; 

$(document).ready(function(){

	// Photo gallery carousel
	$("#thumb-ul-wrapper").jCarouselLite({
		// auto: 5000, 
		speed: 600,
	    btnNext: "#thumb-nav-next",
	    btnPrev: "#thumb-nav-prev",
		easing: "easeOutQuart",
		visible: 7,
		scroll: 7
	});
	
	// Swap out the photo gallery image on thumbnail click
	$('div#thumb-ul-wrapper ul li a').click(function() {

		var img_id = this.id;
		var img_id_split = img_id.split('-');
		var width = img_id_split[0];
		var height = img_id_split[1];
		var left_margin = img_id_split[2];
		var top_margin = img_id_split[3];
		img_id = img_id_split[4];
		
		// Set the active thumbnail
		$('div#thumb-ul-wrapper ul li a').removeClass('current');
		$(this).addClass('current');
		
		// Generate and populate the selected image
		var new_img = '<img src="/t_images/'+img_id+'" width="'+width+'px" height="'+height+'px" style="margin: -'+top_margin+'px 0 0 -'+left_margin+'px" alt="">';
		var photo_author = $(this).children('img').attr('alt');
		new_img += '<span id="photo-author">Photo by: '+photo_author+'</span>';
		$('div#photo-gallery div.large-photo').html(new_img);
		$('#photo-author').css('opacity', .5);

		return false;
	});
	
	$('div#thumb-ul-wrapper ul li a:first').click();
	
	// Submit handler for the upload password form
	$('form#gallery-upload-code-form').submit(function() {
		$.ajax({
			dataType: 'jsonp',
			data: 'pw='+$('#gallery-upload-code-input').val(),
			jsonp: 'jsonp_callback',
			url: '/photo_upload/upload-password.php',
			success: function (json) {
				if(json.status == 'error') {
					$('span#gallery-upload-msg').removeClass('good');
					$('span#gallery-upload-msg').addClass('error');
				} else if(json.status == 'success') {
					$('span#gallery-upload-msg').removeClass('error');
					$('span#gallery-upload-msg').addClass('good');
					$('a#launch-upload-photos').removeClass('hide');
					$('form#gallery-upload-code-form').addClass('hide');
				}
				$('span#gallery-upload-msg').html(json.msg);
				$('span#gallery-upload-msg').show();
			}
		});
		return false;
	});
	
	// Submit handler for the upload form
	$('#photo-upload-form').submit(function() {
		$('input.submit').addClass('submit-loading');
		$('input.submit').removeClass('submit');
		$('input.submit-loading').val('Uploading...');
	});
});

/* swaps the content to the next element in the array,
then fades in the content */
function fadeInFS() {
    /* reset the counter if we have reached the end */
    if(fs_counter == fs_fadeContent.length) fs_counter = 0;

	// Load the next image
	var load_next = ((fs_counter + 1) == fs_fadeContent.length) ? 0 : (fs_counter + 1);
	$('#photostack-ss-pl').html(fs_fadeContent[load_next]);

    /* swap the html content */
    $('#ss_fadeshow').html(fs_fadeContent[fs_counter]);

    /* then fade in the next element */
    $('#ss_fadeshow').fadeIn('slow', function() {
		setTimeout("fadeOutFS()",fs_stamina);
	});
}

/* fades out the current element, increments the counter,
then loops back to the previous function */
function fadeOutFS() {
    /* fade out the current element */
    $('#ss_fadeshow').fadeOut('slow', function() {
	    /* increment the counter */
	    ++fs_counter;

	    /* wait...then fade in the next element */
	    setTimeout("fadeInFS()",fs_intermission);
	});
}

