/*
java script to support corallosphere.org
Ken Johnson , Natural History Museum, London
version 260809
*/

   function toggle (direction, display) {
   return function() {
   var self = this;
   var ul = $("ul", this);
   if( ul.css("display") == display && !self["block" + direction] ) {
   self["block" + direction] = true;
   ul["slide" + direction]("slow", function() {
   self["block" + direction] = false;
   });
   }
   };
   }
 
   function citeStyleWin (citation) {
     var content = "<html><head><link rel=stylesheet type=text/css href=/css/csphere.css></head><body><span class=citeWinText>Please cite this document from Corallosphere.org as:</span><span class=citeWinText>"+ citation +"</span><span class=citeWinClose><a href='javascript:self.close()'>close window</a></span></body></html>";
     var winc = window.open("", "Citation", "width=500,height=150,scrollbars=yes,menubar=0,toolbar=0");
     winc.document.open();
     winc.document.writeln(content);
     winc.document.close();
     }

var  citation ="Morsch,%20S.%20in%20prep.,%20Araucanastrea%20Morsch,%201996,%20p.%20678.%20In:%20Cairns,%20S.%20D.,%20Baron-Szabo,%20R.,%20Budd,%20A.F.,%20Lathuili%C3%A8re,%20B.,%20Roniewicz,%20E.,%20Stolarski,%20J.,%20and%20Johnson,%20K.G.%20(eds.),%20Corallosphere,%20%20world%20wide%20web%20site%20at%20http://obsidian.nhm.ac.uk/cpshere/taxon/1609,%20last%20visited%20on%2026%20Aug%202009.";



/* try a new kind of popup window for citations */

var popupStatus = 0;
function loadPopup(){  
 //loads popup only if it is disabled  
 if(popupStatus==0){  
 $("#backgroundPopup").css({  
 "opacity": "0.7"  
 });  
 $("#backgroundPopup").fadeIn("slow");  
 $("#citationPopup").fadeIn("slow");  
 popupStatus = 1;  
 }  
 }

 //disabling popup with jQuery magic!  
  function disablePopup(){
 //disables popup only if it is enabled  
 if(popupStatus==1){  
  $("#backgroundPopup").fadeOut("slow");  
  $("#citationPopup").fadeOut("slow");  
  popupStatus = 0;  
 }  
 }

  //centering popup  
function centerPopup(){  
  //request data for centering  
  var windowWidth = document.documentElement.clientWidth;  
  var windowHeight = document.documentElement.clientHeight;  
  var popupHeight = $("#citationPopup").height();  
  var popupWidth = $("#citationPopup").width();
 
  //centering  
  $("#citationPopup").css({  
   "position": "absolute",  
   "top": windowHeight/2-popupHeight/2,  
   "left": windowWidth/2-popupWidth/2  
  });  
  //only need force for IE6  
    
  $("#backgroundPopup").css({  
  "height": windowHeight  
  });  
    
  }

// this is autocomplete stuff - to call code in jquery.autocomplete.js
//bad hack -- need to figure out how to pass control names  . . .
//270809

function findValueValidTaxon(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the ID
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
        //need to get sValue back to form . .
        $("#validtaxid").val(sValue);
        
}

function selectItemValidTaxon(li) {
	findValueValidTaxon(li);
}

function findValueHomoTaxon(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the ID
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
        //need to get sValue back to form . .
        $("#homotaxid").val(sValue);
        
}

function selectItemHomoTaxon(li) {
	findValueHomoTaxon(li);
}

function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}

//find a taxon field 


//CODE FOR DISTRIBUTION CONTROLS
 
   function addRowFossilDistribution()
{
       var index=document.topform.fdindex.value;
        var tbody = document.getElementById("distributiontable").getElementsByTagName("tbody")[0];
        var row = document.createElement("tr");
                row.setAttribute("id", "fossil_"+index);
                var cell2 = document.createElement("td");
                var ms=multiselect("fdistfrom_"+index,0,"ppepochs");      
                cell2.appendChild(ms);
                var cell3 = document.createElement("td");
                        cell3.innerHTML = "-";
                var cell4 = document.createElement("td");
                var ms=multiselect("fdistto_"+index,0,"ppepochs");      
                cell4.appendChild(ms);
                
               var cell5 = document.createElement("td");
                var ms=multiselect("fdistgeog_"+index+"[]",4,"fossilgeog");      
                cell5.appendChild(ms);
                

                var celld = document.createElement("td");
                         var delbutton = document.createElement("input");
                            delbutton.setAttribute("value","Delete");
                            delbutton.setAttribute("type","button");
                            delbutton.setAttribute("onclick","deleteRow(this.parentNode.parentNode.rowIndex)");
                        celld.appendChild(delbutton);

        row.appendChild(cell2);
        row.appendChild(cell3);        
        row.appendChild(cell4);
       row.appendChild(cell5);
        row.appendChild(celld);
        tbody.appendChild(row);
	   index++;
	  document.topform.fdindex.value=index;
}

function multiselect(name,size,options) {
         var ms= document.createElement("select");
          ms.setAttribute("name",name);
         var options= multioptions(options);
        
          if (size>0) {
            ms.setAttribute("multiple","")
            ms.setAttribute("size",size);
          }
        
         
         for (var i=0; i < options.d.length; i++) {
                     var op = document.createElement("option");
                     op.innerHTML = options.d[i].value;
                      ms.appendChild(op);
        }
        return ms;
}

function multioptions(type){
var mm;
if (type=="ppepochs") mm= {"d": [  {"value": ""},  {"value": "Pleistocene"},  {"value": "Pliocene"},  {"value": "Miocene"}, 
 {"value": "Oligocene"},  {"value": "Eocene"},  {"value": "Late Cretaceous"},  {"value": "Early Cretaceous"},  {"value": "Late Jurassic"}, 
 {"value": "Middle Jurassic"},  {"value": "Late Triassic"}, {"value": "Middle Triassic"},  {"value": "Early Triassic"}] };    

if (type=="fossilgeog") mm= {"d": [  {"value": ""},  {"value": "Western Europe"},  {"value": "Southern Europe"},  {"value": "Central Europe"}, 
 {"value": "Eastern Europe"},  {"value": "North Africa"},  {"value": "Subsaharan Africa"},  {"value": "Northern Asia"},  {"value": "Central Asia"}, 
 {"value": "South Asia"},  {"value": "East Asia"}, {"value": "Australasia"},  {"value": "Antarctica"},
 {"value": "Melanesia"},  {"value": "Micronesia"}, {"value": "Polynesia"},  {"value": "North America"}, 
 {"value": "Central America"},  {"value": "Caribbean"}, {"value": "South America"}]  };    

return eval(mm);
}


function deleteRow(i)
{
  document.getElementById('distributiontable').deleteRow(i)
}

function getTaxon() {
   if ($("#validtaxid").val() >0 ) {
   $("#mainform").attr("action", "taxon/"+ $("#validtaxid").val());
   }
   }
