/* javascript for Quest Online */

/* have to hard-code path information to the roll-over images
   because of asset tracking. revisit this when/if you adjust
   the location of the quest content */
// $cmignore
var questpath = '/about/pubs/quest/wrapper/';
// but in the cms do not use this...
if(window.location.hostname.indexOf('cms.fhcrc.org')>-1||window.location.hostname.indexOf('cms-dev.fhcrc.org')>-1){
  questpath = '';
}
// $/cmignore


/* treat the search box when it gains focus */
function focusSearchBox(){
  document.getElementById('SearchQuestTextBox').style.color='black';
  if( document.getElementById('SearchQuestTextBox').value=='Search Quest' ){
      document.getElementById('SearchQuestTextBox').value='';
  }
}

/* handle the image roll-over for the search button */
function focusSearchBoxArrow(){
    if(document.images){ document.getElementById('SearchQuestArrow').src = questpath + "img/buttons/Button_Arrow_Over.gif";}
    
}
function blurSearchBoxArrow(){
    if(document.images){ document.getElementById('SearchQuestArrow').src= questpath + 'img/buttons/Button_Arrow_Static.gif'; }
}

/* handle the image roll-over for the subscribe */
function focusSubscribeButton(){
    if(document.images){ document.getElementById('QuestSubscribeButton').src= questpath + 'img/buttons/Button_Subscribe_Over.gif'; }
}
function blurSubscribeButton(){
    if(document.images){ document.getElementById('QuestSubscribeButton').src= questpath + 'img/buttons/Button_Subscribe_Static.gif'; }
}

/* handle the image roll-over for the submit comment button */
function focusCommentBoxArrow(){
    if(document.images){ document.getElementById('AddCommentArrow').src= questpath + 'img/buttons/Button_Arrow_Over.gif'; }
}
function blurCommentBoxArrow(){
    if(document.images){ document.getElementById('AddCommentArrow').src= questpath + 'img/buttons/Button_Arrow_Static.gif'; }
}

/* rotate amongst all the value ads on page load.  */
function rotateValueAdd(o){
  // o is the ValueAdd container div. check how many <div> elements are in it.
  var adds = o.getElementsByTagName('div'); 
  var truncate=0; // how many elements should I remove from the adds array because there is no <img> tag within? (this happens sometimes because of CMS wysiwyg)
  for(var j=0;adds.length&&j<adds.length;j++){
    if(adds[j].getElementsByTagName('img').length==0){
      truncate++;
    }
  }
  var num_add = adds.length-truncate;
  if(num_add<2) return; /* no need to rotate for only one value add, it is already visible */
  var rnd = Math.floor(Math.random()*(num_add)); //alert(rnd);
  for(var i=0;i<num_add;i++){
    adds[i].style.display= (i==rnd ? 'block' : 'none') ; // reveal just the value add matching the random number
  }  
}

/* initialize the page after it loads */
function initPage(){
  if(document.getElementById('SearchQuestTextBox'))
     addEvent( document.getElementById('SearchQuestTextBox'), 'focus', focusSearchBox );
  if(document.getElementById('SearchQuestArrow')){
     addEvent( document.getElementById('SearchQuestArrow'), 'mouseover', focusSearchBoxArrow );
     addEvent( document.getElementById('SearchQuestArrow'), 'mouseout', blurSearchBoxArrow );  
  }
  if(document.getElementById('QuestSubscribeButton')){
     addEvent( document.getElementById('QuestSubscribeButton'), 'mouseover', focusSubscribeButton );
     addEvent( document.getElementById('QuestSubscribeButton'), 'mouseout', blurSubscribeButton );  
  } 
  if(document.getElementById('AddCommentArrow')){
     addEvent( document.getElementById('AddCommentArrow'), 'mouseover', focusCommentBoxArrow );
     addEvent( document.getElementById('AddCommentArrow'), 'mouseout', blurCommentBoxArrow );  
  }
  if(document.getElementById('AddCommentText')){
     addEvent( document.getElementById('AddCommentText'), 'mouseover', focusCommentBoxArrow );
     addEvent( document.getElementById('AddCommentText'), 'mouseout', blurCommentBoxArrow );  
  }
  if(document.getElementById('ValueAdd'))
     rotateValueAdd(document.getElementById('ValueAdd'));
     
}
addEvent( window, 'load', initPage );