$(function() {

	$("#nav").lavaLamp({
    	fx: "backout",
        speed: 700,
        click: function(event, menuItem) {
        	/* return false; */
        }
    });

	
	/*
	 * Everytime the 'contact' link in the main nav menu is clicked,
	 * the dark overlay and contact form will fade in and if the
	 * gallery div exists, it fades to 30%
	 */
	$('a#engage_contact').click(function() {
		show_contact_form();
		return false;
	});
	
	/*
	 * Everytime the dark overlay is visible and clicked,
	 * it fades out and returns the page to a normal view
	 */
	$('div#contact_bg_overlay').click(function() {
		hide_contact_form();
	});
	
	$('.launch_job_details').livequery('click', function() {
		$('div#job_description_overlay').empty();
		$('div#job_description_bg_overlay').fadeIn('normal');
		$('div#job_description_overlay').fadeIn('normal');
		
		var assignment_id = $(this).attr('href');
		
		$.ajax({
			url: '/ajax/get_job_description.php',
			type: 'POST',
			async: true,
			data: {
				aid: assignment_id
			},
			dataType: 'html',
			success: function(data) {
				$('div#job_description_overlay').empty();
				$('div#job_description_overlay').append(data);
			}
		});
		
		return false;
	});
	
	$('div#job_description_bg_overlay').click(function() {
		$('div#job_description_bg_overlay').fadeOut('fast');
		$('div#job_description_overlay').fadeOut('fast');
		$('div#job_description_overlay').empty();
	});
	
	$('div#job_description_overlay a.close_window').livequery('click', function() {
		$('div#job_description_bg_overlay').fadeOut('fast');
		$('div#job_description_overlay').fadeOut('fast');
		$('div#job_description_overlay').empty();
		return false;
	});
	
	$('div#contact_overlay a.close_window').livequery('click', function() {
		$('div#contact_bg_overlay').fadeOut('fast');
		$('div#contact_overlay').fadeOut('fast');
		$('div#gallery').css('opacity', '1.0');
		return false;
	});
	
	$('div#affiliate_overlay a.close_window').livequery('click', function() {
		$('div#affiliate_bg_overlay').fadeOut('fast');
		$('div#affiliate_overlay').fadeOut('fast');
		$('div#gallery').css('opacity', '1.0');
		return false;
	});
	
	$('a#engage_affiliates').click(function() {
		show_affiliates();
		return false;
	});
	
	/********** SUPPORTING FUNCTIONS **********/
	
	
	function show_contact_form() {
		$('div#contact_bg_overlay').fadeIn('normal');
		$('div#contact_overlay').fadeIn('normal');
		$('div#gallery').css('opacity', '0.3');
	}
	
	function show_affiliates() {
		$('div#affiliate_bg_overlay').fadeIn('normal');
		$('div#affiliate_overlay').fadeIn('normal');
		$('div#gallery').css('opacity', '0.3');
	}
	
	function hide_contact_form() {
		$('div#contact_bg_overlay').fadeOut('fast');
		$('div#contact_overlay').fadeOut('fast');
		$('div#gallery').css('opacity', '1.0');
	}
	
	function hide_affiliates() {
		$('div#affiliate_bg_overlay').fadeOut('fast');
		$('div#affiliate_overlay').fadeOut('fast');
		$('div#gallery').css('opacity', '1.0');
	}
	
	/********** CONTACT FORM **********/
	
	$('div#contact_overlay input[type=submit]').attr('disabled', 'disabled');
	
	$('#FirstName').livequery('keyup', function() {
		if($(this).val() != '') {
			indicate_success('FirstName');
		} else {
			indicate_error('FirstName');
		}
	});
	
	$('#LastName').livequery('keyup', function() {
		if($(this).val() != '') {
			indicate_success('LastName');
		} else {
			indicate_error('LastName');
		}
	});
	
	$('#PhoneNumber').livequery('keyup', function() {
		if($(this).val() != '') {
			indicate_success('PhoneNumber');
		} else {
			indicate_error('PhoneNumber');
		}
	});
	
	$('#EmailAddress').livequery('keyup', function() {
		if(IsEmail($(this).val())) {
			indicate_success('EmailAddress');
		} else {
			indicate_error('EmailAddress');
		}
	});
	
	$('#Country').livequery('change', function() {
		
			$.ajax({
				url: '/ajax/get_regions.php',
				type: 'POST',
				async: true,
				data: {
					cid: $('#Country').val()
				},
				dataType: 'html',
				success: function(data) {
					$('#Region').empty();
					$('#Region').append(data);
				}
			});
		
	});
	
	$('#RequestsComments').livequery('keyup', function() {
		if($(this).val() != '') {
			indicate_success('RequestsComments');
		} else {
			indicate_error('RequestsComments');
		}
	});
	
	$('#Purpose').livequery('change', function() {
		if($(this).val() != '') {
			indicate_success('Purpose');
		} else {
			indicate_error('Purpose');
		}
	});
	
	function indicate_success(myid) {
		$('#'+myid).parent().children('img').remove();
		$('#'+myid).parent().append('<img src="/images/icons/accept.png" />');
	}
	
	function indicate_error(myid) {
		$('#'+myid).parent().children('img').remove();
		$('#'+myid).parent().append('<img src="/images/icons/error.png" />');
	}
	
	function IsEmail(email) {
    	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (regex.test(email)) return true;
        else return false;
    };
    
    $('#contact_form input, #contact_form textarea').livequery('keyup', function() {
    	ready_for_submit();
    });
    
     $('#contact_form select').livequery('change', function() {
    	ready_for_submit();
    });
    
    function ready_for_submit() {
    	var cntreq = $('#contact_form label.required').length;
    	var cntok = $('#contact_form img[src$=accept.png]').length;

    	if(cntreq == cntok) {
    		$('div#contact_overlay input[type=submit]').removeAttr('disabled');
    	} else {
    		$('div#contact_overlay input[type=submit]').attr('disabled', 'disabled');
    	}
    	
    }

	
});
