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

/* snplotyper.js

Functions related to the Snplotyper SNP tool

Simon Twigger, (c) 2007*/

var $jq = jQuery.noConflict();

$jq(document).ready(function () {
  $jq(function() {
    $jq("#sortable").sortable({ 
  		placeholder: 'ui-state-highlight',
      update: function() {
        $jq.post("/strain_groups/"+$jq(this).attr("group_id")+"/sort", $jq(this).sortable('serialize'));
      } 
    });
		$jq("#sortable").disableSelection();
  });

  $jq(".analysis-group").change(function(){
    $jq.post("/strain_groups/"+$jq(this).attr('gs_id')+"/analysis_group", "group="+$jq(this).val());
  });


  function refreshImage(seconds) {
    $jq.getJSON('/haplotype_images/check/'+$jq("#haplotype-image").attr('analysis'), function(data) {
      if (data.reload) {
        $jq("#progress-bar").hide();
        var timestamp = new Date().getTime();
        $jq("#haplotype-image").attr('src', '/haplotype_images/'+$jq("#haplotype-image").attr('analysis')+'.gif?'+timestamp);
      } else {
        seconds = seconds + 5;
        $jq("#elapsed-time").html("<strong>Elapsed time:</strong> "+seconds+" seconds");
        startTimer(seconds);
      }
    });
  }

  function startTimer(seconds) {
    timer = setTimeout(function() { refreshImage(seconds) }, 5000);
  }

  $jq(document).ready(function() {
    if ($jq("#haplotype-image").length > 0) {
      startTimer(0);
    }
  });

  $jq(document).ready(function() {
    $jq(".view-strains").live("click", function(){
      var position = $jq(this).position();
      left = position.left;
      top = position.top;      
      width = $jq(this).width();

      box = $jq(".strain-listing");
      box.css("top", top);
      box.css("left", (left+width+20));
      box.toggle();
      return false;
    });
  });

/*
  $jq(function() {
  	$jq("#accordion").accordion();
  });

	$jq(function() {
		$jq("#tabs").tabs();
	});
*/
});

function update_groups(item) {
  
}


// toggle the div display status for text boxes and strain lists

function show_sections(section_id, showhide) {
	element_id = 'sec-' + section_id
  text_id = 'sec-text-'+ section_id
  visible = Element.visible(element_id)

	new Effect[visible ? 'BlindUp' : 'BlindDown'](element_id, {duration: 0.25});

  if (showhide) {
	  $(text_id).innerHTML = visible ? "[show]" : "[hide]";
  } else {
    $(text_id).innerHTML = visible ? "+" : "-";
  }

}

// Used on analysis/_edit_analysis_form page to turn on and off radio buttons 
function activate_strain_options(strain_id) {
	var str = "strain_"+strain_id;
  var str_1 = strain_id + '_group_1';
  var str_2 = strain_id + '_group_2';
  var str_primary = 'analysis_primary_strain_id_' + strain_id;
	
	if ($(str).checked) {
		$(str_1).removeAttribute('disabled');
		$(str_2).removeAttribute('disabled');
		$(str_primary).removeAttribute('disabled');
	} else {
		$(str_1).setAttribute('disabled', 'disabled');
		$(str_2).setAttribute('disabled', 'disabled');
		$(str_primary).setAttribute('disabled', 'disabled');
	}
}

function include_all_strains_starting_with(section_letter) {
	var class_name = '.strain_checkbox_'+section_letter;
	var checkbox_name = 'strain_letter_'+section_letter;
	var div_name = 'strains_'+section_letter;
	var boxes;

	if($(checkbox_name).checked) {
    boxes = $jq("#select_region").checkCheckboxes(class_name, true);
  } else {
    boxes = $jq("#select_region").unCheckCheckboxes(class_name, true);
  }

  $jq(boxes).each(function() {
    var str_id = /strain_(.+)/i.exec($jq(this).attr('id'));
  	activate_strain_options(str_id[1]);
   });
}

