$(document).ready(function() {
  
  $('#menu_jobs a, #frm_keywords, #link_startsearch').click(function() {
  	$.scrollTo('558px', 1200, {easing:'easeInOutQuad'});
  	return false;
  });
  
  $('#frm_keywords').focus(function() {
  	$.scrollTo('558px', 1200, {easing:'easeInOutQuad'});
  	return false;
  });
  
  $('.job_listing').css('opacity', 0);

  animateJobs();

	var drop_loc_open = 0;
	
	$('#drop_loc').live('click', function() {
		
		if ( drop_loc_open == 1 ) {
			
			$(this).removeClass('drop_loc_active');
			$('#drop_loc_options').hide();
			drop_loc_open = 0;
		
		} else {
			
			$(this).addClass('drop_loc_active');
			$('#drop_loc_options').show();
			drop_loc_open = 1;
			
		}
		
	});
	
	$('#drop_loc a:not(.inact)').live('click', function() {
		var thetext = $(this).html();
		var thevalue = $(this).attr('href');
		$('#frm_drop_loc_value').val(thevalue);
		$('#drop_loc .selected').html(thetext);
		$('.drop_loc_active').removeClass('drop_loc_active');
		$('.act').removeClass('act');
		$(this).addClass('act');
		$('#drop_loc_options').hide();
		drop_loc_open = 0;
		Cufon.replace('#drop_loc .selected', { fontFamily: 'Lubalin', hover: true });
		$('#search_results').html('<img src="system/themes/dylan/images/ajax-loader2.gif" style="margin-left: 6px;" />');
		$.get("ajax.php", { action: 'livesearch', theregion: thevalue },
			  function(data){
					if ( data == 'We found no jobs for your search, please try again.' ) {
			  		$('#search_results').append('<div class="job_listing"><div class="job_box_mid"><div class="job_box_top"><div class="job_box_bot"><p style="margin-left: 6px;">'+data+'</p><div class="clear"></div></div></div></div></div>');
			  		$('#load_more_jobs_wrap').hide();
			  		$('.searched_for').html('Nothing');
			  	} else if ( data == 'We found no more jobs for your search.' ) {
			  		$('#search_results').append('<div class="job_listing"><div class="job_box_mid"><div class="job_box_top"><div class="job_box_bot"><p style="margin-left: 6px;">'+data+'</p><div class="clear"></div></div></div></div></div>');
			  		$('#load_more_jobs_wrap').hide();
			  		$('.searched_for').html('Nothing');
			  	} else {
						$('#search_results').html(data);
						$('.job_listing').css('opacity', 0);
						animateJobs();
						if ( thetext != 'All Jobs' ) {
			  				$('.searched_for').html('Jobs in '+thetext);
						} else {
							$('.searched_for').html('All Jobs');
						}
					}
					Cufon.replace('h3.searchedfortext', { fontFamily: 'Lubalin', hover: true });
			  });
		return false;
	});
	
	$('#drop_loc a.inact').live('click', function() {
		return false;
	});
	
	var viewportwidth;
	var viewportheight;
	 
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	if (typeof window.innerWidth != 'undefined')
	{
	    viewportwidth = window.innerWidth,
	   	viewportheight = window.innerHeight
	}
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	else if (typeof document.documentElement != 'undefined'
	    && typeof document.documentElement.clientWidth !=
	    'undefined' && document.documentElement.clientWidth != 0)
	{
	    viewportwidth = document.documentElement.clientWidth,
	    viewportheight = document.documentElement.clientHeight
	}
	 
	// older versions of IE
	 
	else
	{
	    viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	    viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	viewportwidth = viewportwidth-1;
	
	var _docHeight = document.height || document.body.offsetHeight;

	$('.jobdetaillink').live('click', function() {
		var jobid = $(this).attr('id');
		jobid = jobid.replace('getjobdetail_', '');
		$('#popup_overlay').show();
		$('#popup_overlay').css('width', viewportwidth);
		$('#popup_overlay').css('height', _docHeight);
		$('.job_detail_'+jobid).fadeIn('fast');
		return false;
	});
	
	$('#popup_overlay, .sub_pop_close').live('click', function() {
		
		$('.job_detail').fadeOut('fast');
		$('#popup_overlay').hide();
		return false;
		
	});
	
	$('.value_link').live('click', function() {
		$('.numbers_link').removeClass('numbers_link_on');
		$('.value_link').addClass('numbers_link_on');
		$('.numbers_panel').hide();
		$('.numbers_value').show();
		return false;
	});
	
	$('.projects_link').live('click', function() {
		$('.numbers_link').removeClass('numbers_link_on');
		$('.projects_link').addClass('numbers_link_on');
		$('.numbers_panel').hide();
		$('.numbers_projects').show();
		return false;
	});
	
	$('.staff_link').live('click', function() {
		$('.numbers_link').removeClass('numbers_link_on');
		$('.staff_link').addClass('numbers_link_on');
		$('.numbers_panel').hide();
		$('.numbers_staff').show();
		return false;
	});
	
	$('.hours_link').live('click', function() {
		$('.numbers_link').removeClass('numbers_link_on');
		$('.hours_link').addClass('numbers_link_on');
		$('.numbers_panel').hide();
		$('.numbers_hours').show();
		return false;
	});
	
});

function animateJobs() {
	
	var del = 0;
  
  $('.job_listing').each(function(i) {
		$(this).delay(del).animate({ opacity: 1 }, { duration: 500, queue: true }); 
  	del = del+100;
  });
  
}

function animateNewJobs(newjobs) {
	
	var del = 0;
  
  $(newjobs).each(function(i) {
		$(this).delay(del).animate({ opacity: 1 }, { duration: 500, queue: true }); 
  	del = del+100;
  });
  
}

function getWindowSize() {
	return {
		scrollLeft: $(window).scrollLeft(),
		scrollTop: $(window).scrollTop(),
		width: $(window).width(),
		height: $(window).height()
	};
}

