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


// Marks all the items as selected for the submit button.  
function selectList(sourceList) {
    sourceList = window.document.forms[0].parentList;
    for(var i = 0; i < sourceList.options.length; i++) {
        if (sourceList.options[i] != null)
        sourceList.options[i].selected = true;
    }
    return true;
}

// Deletes the selected items of supplied list.
function deleteSelectedItemsFromList(sourceList) {
    var maxCnt = sourceList.options.length;
    for(var i = maxCnt - 1; i >= 0; i--) {
        if ((sourceList.options[i] != null) && 
            (sourceList.options[i].selected == true)) {
            sourceList.options[i] = null;
        }
    }
}
function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}


function copyAvailableToSelected(fromList,toList,selectWhenFinished) {
    for ( i=0; i<fromList.length ; i++){
        if (fromList.options[i].selected == true ) {
            toList.options[toList.length]= new Option(fromList.options[i].text,
               fromList.options[i].value,null,true );
        }
    }
    if( selectWhenFinished ){
      selectAllOptions(selectWhenFinished);
    }
}
function moveAvailableToSelected(fromList,toList,selectWhenFinished) {
    for ( i=0; i<toList.length; i++)
    {
        if(toList.options[i].value == "")
            toList.options[i] = null;
    }
    for ( i=0; i<fromList.length ; i++){
        if (fromList.options[i].selected == true ) {
          found = false
          for ( j=0; j<toList.length ; j++){
            if (fromList.options[i] == toList.options[j]){
              found = true
            }
          }
          if ( !found ) {
            toList.options[toList.length]= new Option(fromList.options[i].text, fromList.options[i].value,null,true );
          }
        }
    }
    deleteSelectedItemsFromList(fromList);
    if( selectWhenFinished ){
      selectAllOptions(selectWhenFinished);
    }
}
function selectAllOptions(list) {
    for (var i=0; i<list.length ; i++){
        list.options[i].selected = true;
    }
    return true;
}

// shows the survey page when the user clicks on the corresponding tab.
//
// assumes the div id is of the form: survey_page_##
// assumes the class for the tab is current or not_current
// requires prototype.js
	function setSurveyTabCurrent(name,total_pages,tab_id) {
      var tabs = $(tab_id);
      total_pages.times(function(page_num){
        var page = $('survey_page_' + (page_num+1));
        Element.hide(page);
        var tab = $(tab_id + '_' + (page_num+1));	
        });
      var current_page = $('survey_page_'+name);
      Element.show(current_page);
      var current_tab = $(tab_id + '_' + (name));
        current_tab.className = 'selected';
  }

// see app/helpers/welcome_helper.rb for calling from rails/ruby
function link_to_google_map(latitude,longitude,info_html,div,zoom,controls) {
  if (GBrowserIsCompatible()) { 
    GUnload();
    var point = new GLatLng(latitude, longitude); 
    var map = new GMap2(document.getElementById(div)); 
    map.setCenter(point, zoom); 
    if(info_html != ''){
      map.openInfoWindowHtml(point,info_html);
    }
    if( controls ) {
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
    }
  }
}

function slide_show(){
  setTimeout(new Effect.MoveBy('imageBoxInside',0,-300,{duration: 0.3,  transition: Effect.Transitions.slowstop}),50)
}
//    start_slideshow(1, 4, 2000);
    
function start_slideshow_mv(start_frame, end_frame, delay) {
  setTimeout(switch_slides_mv(start_frame,start_frame,end_frame, delay), delay);
}
                            
function switch_slides_mv(frame, start_frame, end_frame, delay) {
  return (function() {
    //Effect.Fade('slideshow' + frame);
    if (frame >= end_frame) { 
      new Effect.MoveBy('imageBoxInside',0,(300*end_frame),{duration: 0,  transition: Effect.Transitions.slowstop})
      frame = start_frame; 
    } else { 
      new Effect.MoveBy('imageBoxInside',0,-300,{duration: 4,  transition: Effect.Transitions.slowstop})
      frame = frame + 1; 
    }
    //setTimeout("Effect.Appear('slideshow" + frame + "');", 850);
    setTimeout(switch_slides_mv(frame, start_frame, end_frame, delay), delay + 850);
  })
}

function start_slideshow(start_frame, end_frame, delay) {
  setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
}
                            
function switch_slides(frame, start_frame, end_frame, delay) {
  return (function() {
    Effect.Fade('slideshow' + frame);
    if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
    setTimeout("Effect.Appear('slideshow" + frame + "');", 850);
    setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + 850);
  })
}
    // observing the window.onload event and doing stuff!
Event.observe(window, 'load', function() {
        var e = $A(document.getElementsByTagName('*')).find(function(e) {
        return (e.tagName.toUpperCase() == 'INPUT' && (e.type == 'text' || e.type == 'password'))
              || e.tagName.toUpperCase() == 'TEXTAREA' || e.tagName.toUpperCase() == 'SELECT';
        });
        if (e) e.focus();
        Nifty("div.rounded_info_box","big transparent");
        Nifty("div.rounded_survey_box","big transparent tl bl");
        Nifty("div.SectionTextQ","big transparent tl bl");
});
 // inserting form fields on the fly
var counter = 0;

function moreFields() {
	counter++;
	var newFields = $('readroot').cloneNode(true);
	newFields.id = '';
	newFields.style.display = 'block';
	var newField = newFields.childNodes;
	for (var i=0;i<newField.length;i++) {
		var theName = newField[i].name
		if (theName)
			newField[i].name = theName + counter;
	}
	var insertHere = $('writeroot');
	insertHere.parentNode.insertBefore(newFields,insertHere);
}
// window.onload = moreFields;
Event.observe(window, 'load', function() {
  if($('noticeExplanation') && $('noticeMessage').innerHTML != "")
    Effect.Fade('noticeExplanation', {delay: 5.0, duration: 3.0});
});
