jQuery(document).ready(function($) {

$("body").addClass("js");				// .js class


// SETTINGS:
// -----------------------------------------------------------------------------------------------


// Thumbnail Buttons
// -----------------------------------------
var enable_thumbnail_buttons		= true;		// Comment out the thumbnail making php file
												// Additional code here to hide js checking for
												// non-existant classes might speed it up a bit.			


// Next & Previous button Navigation
// -----------------------------------------
var enable_next_and_prev_buttons	= true;		// 		
var autohide_next_and_prev_buttons 	= true; 	// 'false' sets them to always show



// Image Number x of y
// -----------------------------------------
var image_numbering 				= false;		


// Loading Cover
// -----------------------------------------
var show_loading_cover				= true;





// BEGIN CODE
// -----------------------------------------------------------------------------------------------

//iPhone checking
var iPhone = '';
var body = $("body#iphone").height();
if (body > 0){ var iPhone = true;}




// SETUP - First positions please!
	


// z-index levels
var z_top     = 99; // Top (used in transition)
var z_current = 98; // The displayed image
var z_initial = 97; // Initial z-index value

var thumb 	= "#thumbnails img"; 		// thumbnail button/img
var gallery = "#gallery";

var current_img_class = "current_image";

$("#gallery img").css("z-index", z_initial);
//$("#img-1").addClass(current_img_class).css("z-index", z_current); 

if (iPhone){ 
	$("#img-0").addClass(current_img_class).css("z-index", z_current);
} else {
	$("#img-1").addClass(current_img_class).css("z-index", z_current);
}
	



// END SETUP







// HOVER THAT THUMB
if ( enable_thumbnail_buttons == true ) {
	$(thumb).css("opacity", "0.7");
	
	$(thumb).hover(function() {
		$(this).stop().animate({ opacity: '1' }, 100, "swing");
	} , function() {
		$(this).stop().animate({ opacity: '0.7' }, 500, "swing");
	});
} // end thumb hover


var next_img_exists = '';
var prev_img_exists = '';
// CLICK THAT THUMB
if ( enable_thumbnail_buttons == true ) {
	$(thumb).click(function() {
		$('.current_thumb').removeClass("current_thumb");
		$(this).addClass("current_thumb"); //current thumb class!
	
		var img = $(this).attr('rel'); // the target img
	
		if ( $(img).hasClass("current_image") == false ) {

		    // let's show the target
		    
		    var was_current = $('.'+current_img_class).attr('id');
		    
			$(img).css({"opacity" : "0", "z-index" : z_top});
			
			$('#'+was_current).removeClass(current_img_class);
			$(img).addClass(current_img_class);
			
			hide_doors();
			
	        $(img).animate({ opacity: '1'}, 300, "linear", function() { // target image now visible
		    	//$('.'+current_img_class).css("z-index", z_initial);
		    	$('#'+was_current).css("z-index", z_initial);
		    	//$('.'+current_img_class).removeClass(current_img_class);
				$(img).css("z-index", z_current);
	    	});		    	
	    	
	    } // end if not current
	}); // end .clickfunction
} // end thumb click



// CREATE NAV BUTTONS
if ( enable_next_and_prev_buttons == true ) {
	// Create the buttons inside gallery, or after/before using .after() / .before() -- I think...!?
	$(gallery).append('<div id="prev"></div><div id="next"></div><div id="prev_wrap"></div><div id="next_wrap"></div><div id="prev_btn" class="gallery_navigation_button"></div><div id="next_btn" class="gallery_navigation_button"></div><div id="loading"></div>');
	
	// SET THEM UP
	$('#prev_wrap').css("left","-55px");
	$('#next_wrap').css("right","-55px");
	$('#prev').css("left","-86px");
	$('#next').css("right","-86px");
	$('#prev_btn').css("left","-86px");
	$('#next_btn').css("right","-86px");
	$('#loading').css("z-index", z_current);

} // end create next/prev btns
















// ANIMATE DOORS



var doorwidth = 47; //55
var doorspeed = 200;

var btnmove = 86; //79
var motion = 'linear';



function show_doors(){
		
		    if ( enable_next_and_prev_buttons == true ) {
	    	    var target = $('.'+current_img_class).attr('rel');
			    var next_img  = '#img-' + ((target * 1) + 1 );
			    var prev_img  = '#img-' + ((target * 1) - 1 );
			    if ( $(next_img).length > 0 ) {										
			    	var next_img_exists = true; // Next image exists!
			    }
			    if ( $(prev_img).length > 0 ) {
			    	var prev_img_exists = true; // Prev image exists:
			    }	    	
	    	} // end if enable_next_and_prev_buttons


			if (next_img_exists){ 
				$("#next_wrap").stop().animate({ right: '0px' }, doorspeed, motion);
				$("#next").stop().animate({ right: '4px' }, doorspeed, motion);
				$("#next_btn").stop().animate({ right: '4px' }, doorspeed, motion);
			} if (prev_img_exists){
				$("#prev_wrap").stop().animate({ left: '0px' }, doorspeed, motion);
				$("#prev").stop().animate({ left: '4px' }, doorspeed, motion);
				$("#prev_btn").stop().animate({ left: '4px' }, doorspeed, motion);
			}
			var next_img_exists = '';
			var prev_img_exists = '';

} //end show_doors

function hide_doors(){

				$("#next_wrap").stop().animate({ right: '-'+doorwidth+'px' }, doorspeed, motion);
				$("#next").stop().animate({ right: '-'+btnmove+'px' }, doorspeed, motion);			
				$("#next_btn").stop().animate({ right: '-'+btnmove+'px' }, doorspeed, motion);			

				$("#prev_wrap").stop().animate({ left: '-'+doorwidth+'px' }, doorspeed, motion);
				$("#prev").stop().animate({ left: '-'+btnmove+'px' }, doorspeed, motion);
				$("#prev_btn").stop().animate({ left: '-'+btnmove+'px' }, doorspeed, motion);
	
} //end hide_doors

//$('#gallery').hover(sliding_doors);
$('#gallery').hover(function(){
	show_doors();
} , function() {
	hide_doors();
});
//sliding_doors();
show_doors();




// NAV BTNS HOVERS
$('#prev_btn').hover(function() {
	$("#prev").css("background-position", "left bottom");
} , function() {
	$("#prev").css("background-position", "left top");
});
$('#next_btn').hover(function() {
	$("#next").css("background-position", "right bottom");
} , function() {
	$("#next").css("background-position", "right top");
}); // end nav hovers











// NEXT & PREV CLICK
$('.gallery_navigation_button').click(function(){
    var current = $('.'+current_img_class).attr('rel');
    
    var this_button = $(this).attr("id");
    //alert(this_button);
    
    if (this_button == "next_btn"){
	    var img = '#img-' + ((current * 1) + 1 );
	    var img_after = '#img-' + ((current * 1) + 2 );	    
    } else {
    	var img = '#img-' + ((current * 1) - 1 );
    	var img_before = '#img-' + ((current * 1) - 2 );
    }
    
    if ( $(img).length > 0 ) {										
		if ( $(img).hasClass("current_image") == false ) {
		    // let's show the target
			$(img).css({"opacity" : "0", "z-index" : z_top});
	        $(img).animate({ opacity: '1'}, 300, "linear", function() { // target image now visible
		    	$('.'+current_img_class).css("z-index", z_initial);
		    	$('.'+current_img_class).removeClass(current_img_class);
				$(img).addClass(current_img_class).css("z-index", z_current);
	    	});
	    	
	    	// Close the doors if there is no next image.
	    	if (this_button == "next_btn" && !( $(img_after).length > 0) ){
	    		//alert("hide right doors!");
	    		$("#next_wrap").stop().animate({ right: '-'+doorwidth+'px' }, doorspeed, motion);
				$("#next").stop().animate({ right: '-'+btnmove+'px' }, doorspeed, motion);			
				$("#next_btn").stop().animate({ right: '-'+btnmove+'px' }, doorspeed, motion);
			} else {
				$("#next_wrap").stop().animate({ right: '0px' }, doorspeed, motion);
				$("#next").stop().animate({ right: '4px' }, doorspeed, motion);
				$("#next_btn").stop().animate({ right: '4px' }, doorspeed, motion);
			} 
			if (this_button == "prev_btn" && !($(img_before).length > 0)){
				//alert("hide left doors!");
				$("#prev_wrap").stop().animate({ left: '-'+doorwidth+'px' }, doorspeed, motion);
				$("#prev").stop().animate({ left: '-'+btnmove+'px' }, doorspeed, motion);
				$("#prev_btn").stop().animate({ left: '-'+btnmove+'px' }, doorspeed, motion);
			} else {
				$("#prev_wrap").stop().animate({ left: '0px' }, doorspeed, motion);
				$("#prev").stop().animate({ left: '4px' }, doorspeed, motion);
				$("#prev_btn").stop().animate({ left: '4px' }, doorspeed, motion);
			}
			var img_after = '';
			var img_before = '';

	    } // end if not current
    }

   //var img = '';

});

	
}); // end document ready.




// REMOVE LOADING IMAGE when images are ready.
jQuery(window).bind("load", function() {
	jQuery("#loading").animate({ opacity: '0' }, 800, "swing", function(){
		jQuery("#loading").css("z-index", "96").css("background", "none");
	});

// if ( image_numbering == true ) { $(".gallery_image_wrapper").animate({ opacity: '0' }, 800, "swing"); }

}); //end

