var curtain = false;
var currentImage = '';
var image_count = 0;
var current_key = 0;
var current_section = '';

window.onresize = resize;

$(document).ready(function() {

$("#navigation").animate({
	bottom: '-10px'
}, 1200 );
	
	$.localScroll.defaults.axis = 'xy';
	$.localScroll({hash: true});
	
	// setup clicks
	$(".navlink").click(function() {
		if (curtain) {
			$("#curtain").fadeOut(300, function() {
				$("#gallery_container").html('');
				$(".nextprev").removeClass("transparent");
			});
			curtain = false;
		}
		// this works with the chagnes i made in the sprites.js file
		selected = $(this).attr("selected");
		if (selected) {
			$(".nav").removeClass(current_section);
			$(".nav").addClass(selected);
			current_section = selected;
		}
	});
	
	
	$(".contact_input").click(function() {
		if ($(this).val() == "YOUR NAME..." || $(this).val() == "YOUR EMAIL..." || $(this).val() == "YOUR MESSAGE...") {
			$(this).val('');
		}
		$(this).css("backgroundColor","#f6f7f8");
	});
	
	
	$("#send_button").click(function() {	
		name = $("input#name").val();
		email = $("input#email").val();
		message = $("textarea#message").val();
		captcha = $("input#captcha").val();
		error = '';
		
		
		if (name == '' || name == 'YOUR NAME...') {
			$("input#name").css("backgroundColor","yellow");
			error += 'You must enter your name.\r\n';
		}
		
		if (email == '' || email == 'YOUR EMAIL...') {
			$("input#email").css("backgroundColor","yellow");	
			error += 'You must enter your email.\r\n';
		}
		
		
		if (!error) {
		var dataString = 'name='+ name + '&email=' + email + '&message=' + message + '&captcha=' + captcha;  

		$.ajax({  
			type: "POST",  
			url: "contact.php",  
			data: dataString,  
			success: function(msg) {
			
				if (msg == 'success') {  
					$('#contact_form').html("<div id='success'></div>");  
					$('#success').html("<h2>Contact Form Submitted!</h2>")  
					.append("<p>We will be in touch soon.</p>")  
					.hide()  
					.fadeIn(1500, function() {  
						$('#success').append("<img id='checkmark' src='images/check.png' />");  
					});  
				} else if (msg == 'captcha_fail') {
					$("input#captcha").css("backgroundColor","yellow");
					alert('The answer to the math question is incorrect');
				}
			}  
		}); 
		
		} else {
			alert(error);
		} 

  
	
	});
	

	$(".gallery_item").mouseout(function(){
		$(this).animate({ 
       		opacity: 0.3	
       		}, 300 );
	});

	$(".gallery_item").mouseover(function(){
		$(this).animate({ 
       		opacity: 1.0	
       		}, 300 );
	});



	$("#left_image").click(function() {
		$(".nextprev").removeClass('transparent');
		current_key--;
		if (current_key <= 1) {
			current_key = 1;
			$(".leftarrow").addClass('transparent');
		}
		currentImage = "#pic" + current_key;
		$('#gallery_container').scrollTo(currentImage,1000);
	});
	
	$("#right_image").click(function() {
		$(".nextprev").removeClass('transparent');
		current_key++;
		if (current_key >= image_count) {
			current_key = image_count;
			$(".rightarrow").addClass('transparent');			
		}
		currentImage = "#pic" + current_key;
		$('#gallery_container').scrollTo(currentImage,1000);
	});


	generateSprites(".nav", "current-", true, 150, "fade");
	
	$(".lightbox").click(function() {
		currentImage = $(this).attr("link");
		
		
		$("#curtain").fadeIn(150, function(){
			$("#gallery_container").load("gallery.php", { 'project' : currentImage }, function() {
					curtain = true;
					
					setGallery();
										
					$("#gallery_container img").lazyload({
					    // placeholder : "images/grey.gif",
					     container: $("#gallery_container"),
					     threshold: 800,
					     effect: "fadeIn"
					});
					
					
					
					// move to selected image
					$('#gallery_container').scrollTo('#pic1',0,function() {
						// run after positioning
					});
					
					
					// find current image and set image key					
					gallery_object = $(".gallery_image");
					image_count = gallery_object.length;				
/*
					for (i = 0; i < image_count; i++) {
						if ($(gallery_object[i]).attr('link') == currentImage) { 
							current_key = i + 1; 
						}
					}
*/
					current_key = 1;
					if (current_key == image_count) {
						$(".rightarrow").addClass('transparent');							
					}
					if (current_key == 1) {
						$(".leftarrow").addClass('transparent');							
					}
		
					
					
					$.localScroll({
						target: '#gallery_container',
						queue:true,
						//hash:true,
						onBefore:function( e, anchor, $target ){
							// The 'this' is the settings object, can be modified
							
						},
						onAfter:function( anchor, settings ){
							// The 'this' contains the scrolled element (#content)
							
						}
					});
					
					
					// move gallery into view
					$("#gallery_container").css("left",$(".gallery_image").css("width"));
					$("#gallery_container").animate({ 
						left: "0px"
					}, 1000 );
					
					

			});
		});
	});
});


function resize()
{
	setGallery();
	if ($('#curtain').css("display") == 'block') {
		$('#gallery_container').scrollTo(currentImage,0,function() {
			// run after positioning
		});
	}
}

function setGallery()
{
	if ( navigator.appName.indexOf("Microsoft") != -1 ) {
		window_height = document.documentElement.clientHeight;
		window_width  = document.documentElement.clientWidth;
	} else {
		window_height = window.innerHeight;
		window_width  = window.innerWidth;
	}	
	$(".gallery_image").css("width",window_width + "px");
	$(".gallery_image").css("height",window_height + "px");
}



