var current_photo_num = 1;
var intervalID;

jQuery.preloadImages = function(){
	var img_path="/wp-content/themes/mfp2009/images/";
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", img_path+arguments[i]);
	}
}

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

function previousPhoto() {
	// get child count
	var children_count = $("#gallery_nav").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_photo_num > 1){
		previous_num = $("#gallery_nav_button" + current_photo_num).prev().attr("id").replace(/gallery_nav_button/,"");		
	}
	clickAction(previous_num);
}

function clickAction(num) {
	$("#gallery_nav_button" + current_photo_num + " img").removeClass("current");
	$("#photo" + current_photo_num).fadeOut("slow");
	current_photo_num = num;
	$("#gallery_nav_button" + current_photo_num + " img").addClass("current");
	$("#photo" + current_photo_num).fadeIn("slow");		
}

function buildGallery(dir,photos,slide_time) {
	var photo_array = photos.split(",");
	var output = '';
	var photo_count;
	for ( var i in photo_array ) {
		photo_count = parseFloat(i)+1;
		if (dir==false) {
			var path_array = photo_array[i].split("/");
			var permalink = "/blog/"+path_array[0].substring(0,4)+"/"+path_array[0].substring(5,7)+"/"+path_array[0].substring(8);
			output += '<div id="photo'+photo_count+'" class="photo"><a href="'+permalink+'"><img src="/galleries/'+photo_array[i]+'" alt="" /></a></div>'+"\n";
		} else {
			output += '<div id="photo'+photo_count+'" class="photo"><img src="/galleries/'+dir+'/photos/'+photo_array[i]+'" alt="" /></div>'+"\n";
		}
	}
	if (photo_count > 1) {
		if (dir != false) {
			output += '<a id="gallery_b_previous"></a> <a id="gallery_b_next"></a>'+"\n";
		}
		output += '<div id="gallery_nav">'+"\n";
		for(i=1; i<=photo_count; i++) {
			output += '<a id="gallery_nav_button'+i+'"><img src="/wp-content/themes/mfp2009/images/spacer.png" alt="'+i+'" /></a>'+"\n";
		}
		output += '</div> <!-- /#gallery_nav -->'+"\n";
	} else {
		// strip out id="photo1" to keep from animating
		output = output.replace(/id="photo1"/,"");
	}		
	
	//swap show
	$("#gallery").html(output);
	// hide all photos
	$('.photo').hide();
	// if only 1 photo, fade it in
	if (photo_count == 1) {
		$(".photo").fadeIn("slow");
	}
	//stop transitions
	clearInterval(intervalID);
	//"click" the first photo
	current_photo_num = 1;

	// Prepare gallery navigation functionality
	$("#gallery_nav a").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		var num = $(this).attr("id").replace(/gallery_nav_button/,"");
		clickAction(num);
	});
	// Click on "Next Photo"
	$("#gallery_b_next").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		nextPhoto();
	});
	// Click on "Previous Photo"
	$("#gallery_b_previous").click(function() {
		// stop rotation
		clearInterval(intervalID);
		// Do the click action
		previousPhoto();
	});
	clickAction(current_photo_num);
	//start transitions
	intervalID = window.setInterval('nextPhoto()', slide_time);
}

function swapVideo(vimeo_id) {
	$("#video_wrapper").html('<object width="900" height="506"><param name="allowfullscreen" value="true" /><param name="wmode" value="opaque" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+vimeo_id+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=f75a22&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+vimeo_id+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=f75a22&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque" allowscriptaccess="always" width="900" height="506"></embed></object>');
}

$(document).ready(function(){
    // Preload dropdown menu hover images
    $.preloadImages("nav_id_love_to_on.png","nav_gallery_on.png","nav_blog_on.png","nav_events_on.png","nav_info_on.png","nav_contact_on.png","nav_prints_on.png","nav_search_on.png");
    
    // Suckerfish dropdown menus
    $("#nav-one li").hover(
        function(){ $("ul", this).fadeIn("fast"); }, 
        function() { } 
    );
    if (document.all) {
        $("#nav-one li").hoverClass ("sfHover");
    }
    
    // Style <hr>'s consistently
    $("hr").each(function(){
    	var hr_class = $(this).attr("class");
    	$(this).wrap('<div class="'+hr_class+'"></div>');
    });

	// initialize gallery on page load
	$("#gallery").each(function(){
		// dir, title, & photos defined in inline js
		var gallery_class = $(this).attr("class");
		if (gallery_class == "ad") {
			buildGallery(dir,photos,10000);		
		} else if (gallery_class == "announcements") {
			//alert("dir="+dir+"\nphotos="+photos);
			buildGallery(dir,photos,10000);
		} else {
			buildGallery(dir,photos,5000);
		}
	});

	// click nano list item on homepage
	$("#nano_list a").each(function(){
		// get id attribute
		var dir = $(this).attr("id").substring(2);
		var title = $(this).attr("title");
		var photos = $(this).attr("rel");
		$(this).click(function() {
			buildGallery(dir,photos,5000);
			var permalink = '/blog/'+dir.substring(0,4)+'/'+dir.substring(5,7)+'/'+dir.substring(8)+'/';
			//remove "active" class from old <a>
			$("#nano_list a.active").removeClass("active");
			//add "active" class to new <a>
			$(this).addClass("active");
			//swap main title & permalink
			$("#home_content .left h3").html('<a href="'+permalink+'">'+title+'</a>');
		});
	});

	$(".featured_gallery_list li a").each(function(){
		var dir = $(this).attr("id");
		var photos = $(this).attr("rel");
		$(this).click(function() {
			buildGallery(dir,photos,5000);
		});		
	});

	$(".featured_video_list li a").each(function(){
		var vimeo_id = $(this).attr("rel");
		$(this).click(function() {
			swapVideo(vimeo_id);
		});		
	});


	$(".show_hide_b").each(function() {
		var content_id = "#"+$(this).attr("id").replace(/_b/,"");
		$(this).addClass("hide");
		$(this).click(function() {
			// check to see if content is open
			if ($(content_id).css("display") == "none") {
				// it's closed... open it
				$(".show_hide_b").removeClass("show");
				$(".show_hide_b").addClass("hide");
				//$(this).removeClass("hide");
				$(this).addClass("show");
				$(".show_hide_content").slideUp("fast");
				$(content_id).slideDown("fast");
			} else {
				// it's open... close it
				$(this).removeClass("show");
				$(this).addClass("hide");
				$(".show_hide_content").slideUp("fast");
			}
		});
	});
	
	$("ul#med_list li").each(function(){
		var tn_directory = $(this).attr("id");
		$(this).css("background-image","url(/galleries/"+tn_directory+"/med.jpg)");
	});
	
	$("ul#med_list li a").fadeTo("fast",0);
	
	$("ul#med_list li a").hover(
		function(){
			$(this).fadeTo("fast",1);
		},
		function(){
			$(this).fadeTo("fast",0);
		}
	);

});


/* Suckerfish For jquery - by Myles Angell http://be.twixt.us/jquery/suckerFish.php */
$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};    

