// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


if (Prototype.Browser.IE) {
	Prototype.Browser.IEVersion = parseFloat(navigator.appVersion.split(';')[1].strip().split(' ')[1]);
}

function showCart(num){
	if(num > 0){
		new Effect.Appear('job_cart')	
	}else{
		//$('job_cart').style.display ='none'
	}
}

function ensure_contact_us_is(view_state){
	panel_name = 'contact_panel';
	tab_name = 'contact_tab'
	current_display = $(panel_name).getStyle('display')
	current_display = (current_display == 'block')? 'expanded' : 'collapsed'
	if (view_state != current_display){
		slide_toggle($(tab_name))
	}
	
	
}

function add_if_nonexistant(id){
	new Ajax.Request('/welcome/add_to_cart/'+id, {asynchronous:true, evalScripts:true}); return false;
}

function slide_toggle(tab){
	set = tab.id.substr(0,tab.id.indexOf("_tab"));
	panel = $(set+"_panel");
	control = $(set+"_control");
	new Effect.toggle(panel,'slide');
	if(control.className.indexOf('up') >= 0){
		control.className = 'control down';
	}else{
		control.className = 'control up';
	}
}

function select_states(current_params){
	var checkboxes = $$('.state_checkbox')
	var states = new Array()
	checkboxes.each(function(c){
		if(c.checked){
			states.push(c.value)
		}
	});	
	location.href = '/welcome/update_states?multi_state='+states.join(',')+'&id='+current_params;
	
	
}

function ui_helpers(){
	//navigation
	$$('.chg_header .nav')[0].childElements().last().addClassName('last');
	$$('.chg_footer .nav').each(function(navObj){
		navObj.childElements().last().addClassName('last');
	})
	$$('.sf-menu').first().childElements().last().addClassName('last');
	if($('.prev_page') != undefined){
		$$('.prev_page')[0].update('<span class="arrow">&lsaquo;</span>&nbsp;Previous');
		$$('.next_page')[0].update('Next&nbsp;<span class="arrow">&rsaquo;</span>');
	}	
	$('CareerResources').addClassName('selected');
}


function clear_default_search_text(){
	var $default_text = $('search_field').value;
	$('search_field').onfocus = function(){if(this.value==$default_text){this.value='';this.className='text focused';}};
	$('search_field').onblur = function(){if(this.value==''){this.value=$default_text;this.className='text subtle';}};
}
function validate_search(d){
	if($('search_field').value==d){
		$('search_field').focus();
		alert(d);
	}else{
		$('specialty_search').submit();
	}
}

function job_action_view(target,state){
	//alert(target+" : "+state);
	try{$(target+"_show").hide(); // hiding: link at bottom of details page or job snippet.
		$(target+"_hide").hide(); // hiding: cart instructions at bottom of details page 
		$(target+"_"+state).show(); // showing: cart instructions at bottom of details (a bit redundant aren't we?) 
		
		$(target+"_icon").show(); // the little cart image next to the add to cart link gets hidden now. WHY?
		if (state == 'show') {
			$('add_button').show();
			$('add_button_lower').show();
			$('return_button').hide();
			$('return_button_lower').hide();			
		}
		else {
			$('add_button').hide();
			$('add_button_lower').hide();
			$('return_button').show();
			$('return_button_lower').show();
		}
	}
	catch(e1){var errors = e1}
}

function dax_control(this_dax,effect_type) {
	Effect.toggle(this_dax,effect_type,{duration:.5});
}

//Warn user if they are abandoning an incomplete form
changed = false;
function initialize_form_checker(form){
	for (i=0; i<$(form).elements.length; i++){
		$(form).elements[i].onchange = function(){
			changed = true;
			if (form == "contact_widget"){
				$(form).name = form;
			}
		};
	}
}
window.onbeforeunload = function(){
	//Other text in the dialogue CANNOT be changed. They are browser specific.
	if (changed == true) return "You have started filling out a form without submitting it.";
}


function toggle_feedback(){
	//contact_select appears above the feedback window in IE6 and the only way to solve it is to toggle it.
	//for more info: http://archivist.incutio.com/viewlist/css-discuss/91442
	if (Prototype.Browser.IEVersion==6){
		if ($('candidate[specialty]') != null){
			$('candidate[specialty]').toggle();
		}
	}
	//extra -5 is to fix IE6 bug;
	var pos=($('feedback_form').cumulativeOffset().left<(document.viewport.getWidth()-5))?-235:0;
	$('feedback_form').morph('right:'+pos+'px',{duration:0.5});
}

function toggle_email_a_colleague(){
	Effect.toggle('email_a_colleague','appear',{duration:0.5});
}

function reset_eac(){
	$('eac_thanks').hide();
	$('referral_candidate_name').value = "";
	$('referral_candidate_email').value = "";
	$('eac_body').show();
}

function reset_email_colleague_errors(){
	var error_elements = $$("#data_set #email_a_colleague input.fieldWithErrors");
	if (error_elements.length > 0){
		error_elements.each(function(t){
			t.className = "text";
		});
	}
}

function clinical_capabilities_downloader(select_element){
	if(select_element.selectedIndex>0){
		var pdf_link = 'http://content.chghealthcare.com/ch_lt/ext_clinical_capabilities/'+select_element.value;
		$('download_pdf').style.display="inline";
		$('download_pdf_button').onclick= function(){window.open(pdf_link)};
	} else {
		$('download_pdf').hide();
	}
}

function state_check(state){
	if ($(state).checked){
		$(state).checked = false;
	}else{
		$(state).checked = true;
	}
}

function expand(obj){
	obj.style.height="300px"
}

function omniture_cart_functions(action,count,job_id){
	if(action== 'add'){
		oevent = 'scAdd'
		if(count==1){oevent = oevent+',scOpen'} // if the count is 1 they are adding the first job
	}else{ // action == remove
		oevent = 'scRemove'
	}
	products = ';'+job_id
	
	if ($debug_mode != true) {
		s = s_gi($reporting_suite); // load the reporting suite
		s.linkTrackVars="events,products"; 
		s.linkTrackEvents=oevent;
		s.events = oevent
		s.products= products
		s.tl(true,'o',action);
	}
	
}

function omniture_track_click(oevent,name){
	s = s_gi($reporting_suite); // load the reporting suite
	s.linkTrackVars="eVar9,events"; 
	s.linkTrackEvents=oevent;
	s.events = oevent
	s.eVar9 = name
	s.tl(true,'o',name);
}
