// JavaScript Document

// preload mouseover images at runtime
function preloadImg() {
  if (document.images) { 
    if(!document.p) document.p=new Array();
    var i,j=document.p.length; 
    for(i=0; i<preloadImg.arguments.length; i++) {
      if (preloadImg.arguments[i].indexOf("#")!=0) { 
        document.p[j]=new Image; 
        document.p[j++].src=preloadImg.arguments[i];
      }
    }
  }
}

//open new browser window
function openwindow(url,name,prop) {
  window.open(url,name,prop);
}

//show/hide selected border for form inputs
function show_border(help_field, form_field) {
  $(form_field + "_" + help_field).style.borderColor = '#999999';
}

function hide_border(help_field, form_field) {
  $(form_field + "_" + help_field).style.borderColor = '#cccccc';
}


// Zapoint Multiple Selector helper
function multiple_selector_toggler(obj_name, obj_field) {
  link_el  = $(obj_name + '_' + obj_field + '_link');
  input_el = $(obj_name + '_' + obj_field); 
  if(link_el.className.split(' ').include('selected_highlight')) {
    link_el.removeClassName('selected_highlight');
    input_el.value = 0;
  } else {
    link_el.addClassName('selected_highlight');
    input_el.value = 1;
  }
  return false;
}

// Zapoint Multiple Selector helper - for the multiline version
function multiple_selector_toggler_multiline(obj_name, obj_field) {
  link_el  = $(obj_name + '_' + obj_field + '_link');
  div_el   = $('multiline_selector_' + obj_name + '_' + obj_field);
  input_el = $(obj_name + '_' + obj_field); 
  if(link_el.className.split(' ').include('selected_highlight')) {
    link_el.removeClassName('selected_highlight');
    div_el.className = 'multiline_row';
    input_el.value = 0;
  } else {
    link_el.addClassName('selected_highlight');
    div_el.className = 'multiline_row_selected';
    input_el.value = 1;
  }
  return false;
}

// COMPA

function answered(answer_key, total_count, question_index) {
	eval("completed_answers." + answer_key + "=true")
	completed_count = completed_answers.keys().size()
	if (total_count == completed_count) {
		$('remaining_questions').hide();
		$('center_link').show();
		new Effect.Pulsate('center_link');
	} else {
		$('remaining_questions_count').innerHTML = (total_count - completed_count).toString();
	}
	$('numerical_question_link_'+question_index).addClassName('strikethrough');
}

function switch_questions(from, to) {
	if (from != to) {
		cur_question = to;
		new Effect.BlindUp('question_' + from);
		new Effect.BlindDown('question_' + to);
	}
}

function save_answer_score(answer_id, score) {
	new Ajax.Request('/account/ajax_update_answer_score/'+answer_id+'?score='+score, {asynchronous:true, evalScripts:true});
	$('answer_'+answer_id+'_status').innerHTML = 'A&#x219;teapt&#x103; pu&#x21B;in...'
	$('answer_'+answer_id+'_status').show();
}

function init_slider(answer_id, current) {
	new Control.Slider('handle_'+answer_id, 'track_'+answer_id,{range:$R(0,400),
    	/* values:[0,50,100,150,200], */
		increment:2,
		sliderValue:current*4,
    	onSlide:function(v){$('debug_'+answer_id).innerHTML=Math.ceil(v*100/400) + '%'},
    	onChange:function(v){save_answer_score(answer_id, Math.ceil(v*100/400))}});
}

function toggle_summary(compa_id) {
	compas = $$('.dummy_compa');
	compas.each(function(compa) {
		id=compa.id.split('_')[1]
		if (compa.id == "summary_" + compa_id) {
          $(compa).show();
		  $('compa_'+id).addClassName('compa_selected');
		  $('hide_summary_link_'+id).show();
		  $('show_summary_link_'+id).hide();
        } else {
          $(compa).hide();
		  $('compa_'+id).removeClassName('compa_selected');
		  $('hide_summary_link_'+id).hide();
		  $('show_summary_link_'+id).show();
        }
    });
}
