$( document ).ready( function() {
    lightbox();	
} );

function lightbox() {
	var links = $('a[rel^=lightbox]');
	var overlay = $('<div id="overlay" style="display: none"></div>');
	var container = $('<div id="container" style="display: none"></div>');
	var loading = $('<div id="loading"></div>');

$( 'body' ).append( overlay ).append( container );
container.show().css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -6 + 'px', 'margin-top': 0, 'margin-left': 0} ).hide();

set_close_button = function(){
	$("#cancel").click( function( c ) {
	    c.preventDefault();
	    overlay.add( container ).fadeOut( 'normal' );
		container.children().remove();
		$( window ).scrollTop(0);
	});
	$(".close").click( function( c ) {
	    c.preventDefault();
	    overlay.add( container ).fadeOut( 'normal' );
		container.children().remove();
		$( window ).scrollTop(0);
	});
}



links.each( function( index ) {
    var link = $( this );
    link.click( function( c ) {
        c.preventDefault();
		container.show().css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -7 + 'px', 'margin-top': 0, 'margin-left': 0} ).hide();
        open_project( link.attr( 'title' ) ); // the title is the project id for the server
    } );
} );

$("#contact").click( function( c ) {
        c.preventDefault();
		container.show().css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -7 + 'px', 'margin-top': 0, 'margin-left': 0} ).hide();
		overlay.fadeIn( 'normal',function(){
		loadContact();
	    } );
} );

$("#login").click( function( c ) {
        c.preventDefault();
		container.show().css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -7 + 'px', 'margin-top': 0, 'margin-left': 0} ).hide();
		overlay.fadeIn( 'normal',function(){
		loadLogin();
	    } );
} );

$("#about").click( function( c ) {
        c.preventDefault();
		container.show().css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -7 + 'px', 'margin-top': 0, 'margin-left': 0} ).hide();
		overlay.fadeIn( 'normal',function(){
		loadAbout();
	    } );
} );

$(".back_top a:link").click( function( c ) {
        c.preventDefault();
		$( window ).scrollTop(0);
} );

var open_project = function( p_id ) { // p_id is the project id for the server
	overlay.fadeIn( 'normal',function(){
		loadProject( p_id );
    } );
}

var loadProject = function(p_id) {
	var project_id = p_id;
	$.ajax({
		type: "GET",url: "projects_scroll.php", data: "id=" + project_id, dataType: "html",
		beforeSend: function(){container.append( loading );},
		success: function(data){
			$("#container").html(data);
			$( window ).scrollTop(0);
			container.fadeIn( 'normal' );
			set_close_button();},
		complete: function(){$('#loading').remove();},
	}); // closed ajax function
} // closed loadProject function

var loadContact = function() {
	$.ajax({
		type: "GET",url: "contact.php", dataType: "html",
		beforeSend: function(){container.append( loading );},
		success: function(data){
			$("#container").html(data);
			$( window ).scrollTop(0);
			container.fadeIn( 'normal' );
			set_close_button();
			contact_form();},
		complete: function(){$('#loading').remove();},
	}); // closed ajax function
} // closed loadProject function

var loadLogin = function() {
	$.ajax({
		type: "GET",url: "login.php", dataType: "html",
		beforeSend: function(){container.append( loading );},
		success: function(data){
			$("#container").html(data);
			$( window ).scrollTop(0);
			container.fadeIn( 'normal' );
			set_close_button();
			login_form();},
		complete: function(){$('#loading').remove();},
	}); // closed ajax function
} // closed loadProject function

var loadAbout = function() {
	$.ajax({
		type: "GET",url: "about.php", dataType: "html",
		beforeSend: function(){container.append( loading );},
		success: function(data){
			$("#container").html(data);
			$( window ).scrollTop(0);
			container.fadeIn( 'normal' );
			set_close_button();},
		complete: function(){$('#loading').remove();},
	}); // closed ajax function
} // closed loadProject function

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

$(window).resize(function() {
	container.css( {'top':'25px', 'left': Math.round( ($( window ).width() - 915 ) / 2 ) -7 + 'px', 'margin-top': 0, 'margin-left': 0} );
});

$("#logout").click( function( c ) {
	c.preventDefault();
	logout();
} );

} // closed lightbox function

function contact_form(){
	$("#contact_submit").submit(function() {
		var error_msg = new Array; 
		var submitted = $('#submitted').attr('value');

		if($('#EMAIL').attr('value') == ''){
			error_msg.push('email');
		}else{
			var email = $('#EMAIL').attr('value'); 
		}
		if($('#SUBJECT').attr('value') == ''){
			error_msg.push('subject');
		}else{
			var subject = $('#SUBJECT').attr('value');
		}
		if($('#MESSAGE').attr('value') == ''){
			error_msg.push('message');
		}else{
			var message = $('#MESSAGE').attr('value');
		}
		if(error_msg.length){
			var msg = "";
			for (i=0;i<(error_msg.length);i++){
				msg = msg + ", " + error_msg[i];
			}
			$('.message').html("Please fill in " + (msg.substring(2)) + ".");
			$('.message').css( {'color':'#b60000'} );
		}else{
	    	$.ajax({  
	        	type: "POST",  
	        	url: "contact_process.php",  
	        	data: "EMAIL="+ email +"& SUBJECT="+ subject +"& MESSAGE="+ message +"& submitted="+ submitted,  
	        	success: function(data){
					email_success = data;
				},
				complete: function(){
					if(email_success == "true"){
						$('.message').html('Thanks for your message.');
						$('form').clearForm();
						$('.message').css( {'color':'#005aff'} );
					}else if(email_success == "false"){
						alert( "Your email did not work.");
					}
				},
	    	});  //ajax
		} //if errors close
		return false;
	}); //submit
} //contact_form function

function login_form(){
	$("#login_submit").submit(function() {
		var error_msg = new Array; 
		var submitted = $('#submitted').attr('value');

		if($('#USER').attr('value') == ''){
			error_msg.push('user name');
		}else{
			var user = $('#USER').attr('value'); 
		}
		if($('#PASS').attr('value') == ''){
			error_msg.push('password');
		}else{
			var password = $('#PASS').attr('value');
		}
		if(error_msg.length){
			var msg = "";
			for (i=0;i<(error_msg.length);i++){
				msg = msg + ", " + error_msg[i];
			}
			$('.message').html("Please fill in " + (msg.substring(2)) + ".");
			$('.message').css( {'color':'#b60000'} );
		}else{
	    	$.ajax({  
	        	type: "POST",  
	        	url: "login_process.php",  
	        	data: "USER="+ user +"& PASS="+ password +"& submitted="+ submitted,  
	        	success: function(data){
					login_success = data;
				},
				complete: function(){
					if(login_success == "true"){
						$('.message').html('Thanks for loging-in.');
						$('form').clearForm();
						window.location = "http://www.aaronmadethis.com/site_v4";
					}else if(login_success == "false"){
						$('.message').html('User name or password not found.');
						$('.message').css( {'color':'#b60000'} );
						$('form').clearForm();
					}
				},
	    	});  //ajax
		} //if errors close
		return false;
	}); //submit
} //login_form function

function logout(){
	$.ajax({
		type: "POST",
		url: "logout.php",  
		success: function(data){
			logout_success = data;
		},
		complete: function(){
			if(logout_success == "true"){
				window.location = "http://www.aaronmadethis.com/site_v4";
			}else if(logout_success == "false"){
				alert( "There was a problem. Please try again.");
			}
		},
	});  //ajax
} //logout function
