
function custom_effect(img, div, attr) {
	if(img != 'undefined' && div != 'undefined' && div != '') {
		var d = document.getElementById(div);
		var html = '<img src="' + img + '"';
		if(attr != 'undefined' && attr != '') {
			html += ' ' + attr;
		}
		html += ' />';
		d.innerHTML = html;
	}
	return false;
}


$(document).ready(function(){
	
	// if you click on a thumbnail in the sidebar don't load a new page
	// but load it into the big image on the left
	$(".ngg-gallery-thumbnail a").click(function(event) {
	    $(".main-text img").css({opacity:0});		
	    $(".ngg-gallery-thumbnail a").removeClass('selected');
	    $(this).addClass('selected');
		var big_image = $(this).attr("href");
		$(".main-text img").attr("src", big_image).bind("load", function () { $(this).animate({opacity:1}); });
		event.preventDefault();
	});	
	
	// when the user clicks the prev next arrows make the first thumbnail
	// replace the big image on the left
	var first_image = $(".ngg-visible:first a").attr("href");
	var last_image = $(".ngg-visible:last a").attr("href");
	$(".main-text img").attr("src", first_image);	
	$(".ngg-visible:first a").addClass('selected');
	

	// onload setup next link only, prev should be hidden since there are no permalinks
    var next_link = $(".ngg-visible:eq(1) a").attr("href");
    if (!next_link) {$("#gallery-photo-nav").hide();}
	$("#gallery-photo-nav .next").attr("href", next_link);
	
	
	// next link
	$("#gallery-photo-nav .next").click(function(event) {
	
	    var current_image = $(".main-text img").attr("src");
		$(".main-text img").css({opacity:0});
	
		// update selected state in sidebar
		$(".ngg-gallery-thumbnail a").removeClass('selected');       	    
		
		$(".ngg-visible").each(
            function(index){ 
                this_img = $(this).children('.ngg-gallery-thumbnail').children('a').attr('href');
                if(current_image == this_img){
                    next_img = $(this).nextAll('.ngg-visible').children('.ngg-gallery-thumbnail').children('a').attr('href');
                    if(typeof next_img == 'undefined'){
                        next_img = first_image
                    }                     
                    $(".main-text img").attr("src", next_img).bind("load", function () { $(this).animate({opacity:1}); });
                    $(".ngg-visible a[href="+next_img+"]").addClass('selected');
                 }
            }
        );
		
		if (next_img != first_image) { $("#gallery-photo-nav .prev").show(); }
		event.preventDefault();		
	});
	
	
	
	// prev link 
	$("#gallery-photo-nav .prev").click(function(event) {
	
		var current_image = $(".main-text img").attr("src");
		$(".main-text img").css({opacity:0});
		
	    // update selected state in sidebar
		$(".ngg-gallery-thumbnail a").removeClass('selected');       	    

		// update self (new prev link)
        $(".ngg-visible").each(
            function(index){ 
                nxt_img = $(this).children('.ngg-gallery-thumbnail').children('a').attr('href');
                if(current_image == nxt_img){
                     if(typeof last == 'undefined'){
                         last = last_image;
                     }
                     newname = last;
                     $(".main-text img").attr("src", newname).bind("load", function () { $(this).animate({opacity:1}); });
                     $(".ngg-visible a[href="+newname+"]").addClass('selected');
                 } else {
                    last = nxt_img;
                 }
            }
        );
		
		event.preventDefault();				
	});
		
});
