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

  $('a#network_tab').click(function() {
  	$('div#trillium_network').slideToggle('normal');
  	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() {
		var this_position = $(this).offset();
		var this_top = parseInt(this_position.top);
		
		var win_height = $(window).height();
		var doc_height = $(document).height();
		
		$('div#job_description_overlay').empty();
		$('div#job_description_overlay').css('position', 'absolute');
		
		
		
		$('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);
				$.scrollTo('0px');
			}
		});
		
		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');
    	}
    	
    }

	
});

;(function($){var m=$.scrollTo=function(b,h,f){$(window).scrollTo(b,h,f)};m.defaults={axis:'xy',duration:parseFloat($.fn.jquery)>=1.3?0:1};m.window=function(b){return $(window).scrollable()};$.fn.scrollable=function(){return this.map(function(){var b=this,h=!b.nodeName||$.inArray(b.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!h)return b;var f=(b.contentWindow||b).document||b.ownerDocument||b;return $.browser.safari||f.compatMode=='BackCompat'?f.body:f.documentElement})};$.fn.scrollTo=function(l,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};if(l=='max')l=9e9;a=$.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=$(k),d=l,p,g={},q=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px)?$/.test(d)){d=n(d);break}d=$(d,this);case'object':if(d.is||d.style)p=(d=$(d)).offset()}$.each(a.axis.split(''),function(b,h){var f=h=='x'?'Left':'Top',i=f.toLowerCase(),c='scroll'+f,r=k[c],s=h=='x'?'Width':'Height';if(p){g[c]=p[i]+(q?0:r-o.offset()[i]);if(a.margin){g[c]-=parseInt(d.css('margin'+f))||0;g[c]-=parseInt(d.css('border'+f+'Width'))||0}g[c]+=a.offset[i]||0;if(a.over[i])g[c]+=d[s.toLowerCase()]()*a.over[i]}else g[c]=d[i];if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],u(s));if(!b&&a.queue){if(r!=g[c])t(a.onAfterFirst);delete g[c]}});t(a.onAfter);function t(b){o.animate(g,j,a.easing,b&&function(){b.call(this,l,a)})};function u(b){var h='scroll'+b;if(!q)return k[h];var f='client'+b,i=k.ownerDocument.documentElement,c=k.ownerDocument.body;return Math.max(i[h],c[h])-Math.min(i[f],c[f])}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

