
// requires jQuery

// substitute for onload, add as many as you want
//$(document).ready(function(){});


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// menu to highlight progress status in standards list (rm=list_stanards)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function pro_highlight_menu(){

//    $("#pro_highlight_menu_loc").bind("click", function(e){

//                                         var el = $("#pro_highlight_menu");
//                                         var offset = $("#pro_highlight_menu_loc").offset();
                                        
//                                         if( $(this).hasClass("visible") ) {
//                                            el.css("display", "none");
//                                            $(this).removeClass("visible")
//                                         } else {
                                           
//                                            // hide other menu
//                                            $("#qual_highlight_menu").css("display", "none");
//                                            $("#qual_highlight_menu_loc").removeClass("visible")

//                                            el.css("display", "block"); // make visible
//                                            el.css("position", "absolute");

//                                            //el.appendTo('body'); 
//                                            el.css("top", offset.top+20); 
//                                            el.css("left", offset.left); 
//                                            $(this).addClass("visible")
//                                         }
                                        
//                                      });

// }

// $(document).ready(pro_highlight_menu);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// menu to highlight qualitative status in standards list (rm=list_stanards)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function qual_highlight_menu(){

//    $("#qual_highlight_menu_loc").bind("click", function(e){

//                                          var el = $("#qual_highlight_menu");
//                                          var offset = $("#qual_highlight_menu_loc").offset();
                                         
//                                          if( $(this).hasClass("visible") ) {
//                                             el.css("display", "none");
//                                             $(this).removeClass("visible")
//                                          } else {
                                          
//                                             // hide other menu
//                                             $("#pro_highlight_menu").css("display", "none");
//                                             $("#pro_highlight_menu_loc").removeClass("visible")

//                                             el.css("display", "block");
//                                             el.css("position", "absolute");
//                                             //el.appendTo('body'); 
//                                             el.css("top", offset.top+20); 
//                                             el.css("left", offset.left); 
//                                             $(this).addClass("visible")
//                                          }
                                         
//                                       });

// }

// $(document).ready(qual_highlight_menu);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////



// install status highlighter (click on NS to highlight all NS in standards list)
// $(document).ready(function(){

//                      var classes = ['.qual_stat_wrong_direction',
//                                     '.qual_stat_warning',
//                                     '.qual_stat_on_target',
//                                     '.qual_stat_close_target',
//                                     '.qual_stat_slowly_right_direction',
//                                     '.pro_stat_not_started',
//                                     '.pro_stat_in_progress',
//                                     '.pro_stat_ready_review',
//                                     '.pro_stat_program_review_complete',
//                                     '.qual_stat_none_selected',
//                                     '.qual_stat_needs_improvement',
//                                     '.qual_stat_serious_reconsideration'];

//                      for ( var i = 0; i < classes.length; i++ ){
//                         var c = classes[i];
//                         $(c).bind("click", function(e){
//                                      c = '.' + e.target.className; // class from event
//                                      $(c).css("color","red");
//                                      $(c).css("padding","1px 1px 1px 1px");
//                                      $(c).css("border","solid 1px red");

//                                      // close the menus
//                                      $("#pro_highlight_menu").css("display", "none");
//                                      $("#pro_highlight_menu_loc").removeClass("visible");
//                                      $("#qual_highlight_menu").css("display", "none");
//                                      $("#qual_highlight_menu_loc").removeClass("visible");


//                                      // turn off others
//                                      for ( var j = 0; j < classes.length; j++ ){
//                                         if (c != classes[j]){
//                                            $(classes[j]).css("color","black");
//                                            $(classes[j]).css("border","none");
//                                         }
//                                      }
                                  

//                                   });
//                      }

// });


// cookie utilities
function setCookie(c_name,value,expiredays){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate);
}

function getCookie(c_name) {
   if (document.cookie.length>0){
     c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1){ 
         c_start=c_start + c_name.length+1;
         c_end=document.cookie.indexOf(";",c_start);
         if (c_end==-1) c_end=document.cookie.length;
         return unescape(document.cookie.substring(c_start,c_end))
      } 
   }
   return null;
}



// hide all the display
//var area_style = document.getElementById(name).style;
//area_style.display = "none"; // to cleanly, completely close                                                             
//area_style.height = 0 + "px";
// $(document).ready(function(){ // because need to load before page is displayed

// 		     hide_all_sorts_but('standards_table_no_sorted');
                     
// 		     $(".th_standard").click(
// 			function(e){
// 			   hide_all_sorts_but('standards_table_no_sorted');
// 			}
// 		     );
                     
// 		     $(".th_progress").click(
// 			function(e){
// 			   hide_all_sorts_but('standards_table_progress_sorted')
// 			}
// 		     );
                     
// 		     $(".th_qualitative").click(
// 			function(e){
// 			   hide_all_sorts_but('standards_table_qualitative_sorted')
// 			}
// 		     );
                     
// 		     $(".th_last_updated").click(
// 			function(e){
// 			   hide_all_sorts_but('standards_table_updated_sorted')
// 			}
// 		     );
//                   });


// debugging tool to show the props of an element
function show_props(element){
   for (property in element) 
      document.write(property+' ---> '+element[property]+'<BR>');
}


// find the position of an object on the page
function findPos(obj) {
   var curleft = curtop = 0;
   if (obj.offsetParent) {
      curleft = obj.offsetLeft
      curtop = obj.offsetTop
      while (obj = obj.offsetParent) {
	 curleft += obj.offsetLeft
	 curtop += obj.offsetTop
      }
   }
   return [curleft,curtop];
}



//upload pane, position and reveal on click
function install_upload_pane() {
   var pane_element = "supporting_data_docs_upload_pane";
   var pane_activator = "supporting_data_docs_upload";
   var form="supporting_data_docs_form";

   // if page doesnt have element than skip this
   if (!$(pane_activator)){return}


   $(pane_activator).observe("click", function(e){
         
                                pane_style = document.getElementById(pane_element).style;

                                // figure out position we were called from, so we can
                                // pop up the div in a predictable place
                                activator = document.getElementById(pane_activator);
                                activator_pos = findPos(activator);
                                activator_left = activator_pos[0];
                                activator_top =  activator_pos[1];
                                
                                if (getCookie('upload_pane_visible') == 1){
	                           pane_style.display = "none"; 
	                           setCookie('upload_pane_visible', 0);
                                }else{
                                   
	                           // position pane
	                           pane_style.left = activator_left + 5 + 'px';
	                           pane_style.top  = activator_top  - 305 + 'px';
                                   
	                           // make visible
	                           pane_style.display = "block"; 
                                   
	                           // remember we made visible, next click removes
	                           setCookie('upload_pane_visible', 1);
                                }
                                
                             }
                            );
   
   // close widget
   $("close_widget").observe("click", function(e){
                                pane_style = document.getElementById(pane_element).style;
                                pane_style.display = "none"; 
                                setCookie('upload_pane_visible', 0);
                             }
                            );
   
}


document.observe('dom:loaded', function(){
                    install_upload_pane()
                 });




// mouseover tooltip handler using AJAX, attaches to css classes
function install_tooltip(css_class, content){

   // build tip html
   var html = 
      '<p class="internal_header">guide:</p>' + 
      content.guide + 
      "<br />" + 
      '<p class="internal_header">upoaded by:</p>' + 
      content.author;

   // install tooltip, loop because there might be more than one of each class
   var elements = $$("." + css_class);
   for(var i = 0; i < elements.length; i++){
      var element = elements[i]
      new Tip(element, html, {className:"eaccred", delay:0.0})
   }

}



// utility function to pop up a window, if in a <a> dont forget to return false
// to stop main window from reloading.
function popup(URL, w, h){
   
   var winpops=window.open(URL, "", "width=" + w + ",height=" + h + ", scrollbars=yes, resizable=yes");
   
}


// setup for the tinymce textareas
if (typeof(tinyMCE) != "undefined") _init_tinyMCE();

function _init_tinyMCE(){
   
   // big version
   tinyMCE.init({
                   editor_selector : "mceEditorLarge",
                   content_css : "/style/styles.css",
                   mode : "textareas",
	           theme : "advanced",
	           plugins : "save,autosave,print",
	           theme_advanced_buttons1 : "save,print,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,undo,redo,link,unlink",
	           theme_advanced_buttons2 : "",
	           theme_advanced_buttons3 : "",
	           theme_advanced_toolbar_location : "top",
	           theme_advanced_toolbar_align : "left",
	           extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	           width : "925px",
	           height : "280px"
	        });

   // no save button
   tinyMCE.init({
                   editor_selector : "mceEditorNoSave",
                   content_css : "/style/styles.css",
                   mode : "textareas",
	           theme : "advanced",
	           plugins : "autosave,print",
	           theme_advanced_buttons1 : "print,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,undo,redo,link,unlink",
	           theme_advanced_buttons2 : "",
	           theme_advanced_buttons3 : "",
	           theme_advanced_toolbar_location : "top",
	           theme_advanced_toolbar_align : "left",
	           extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	           width : "925px",
	           height : "280px"
	        });
   
   // small version
   tinyMCE.init({
                   editor_selector : "mceEditorSmall",
                   content_css : "/style/styles.css",
                   mode : "textareas",
	           theme : "advanced",
	           plugins : "save,autosave,print",
	           theme_advanced_buttons1 : "save,print,bold,italic,underline,separator,strikethrough,justifyleft,justifycenter,justifyright, justifyfull,bullist,undo,redo,link,unlink",
	           theme_advanced_buttons2 : "",
	           theme_advanced_buttons3 : "",
	           theme_advanced_toolbar_location : "top",
	           theme_advanced_toolbar_align : "left",
	           extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	           width : "750px",
	           height : "280px"
	        });
   
}


// activate a tinymce editor
function setTextareaToTinyMCE(sEditorID) {
   var oEditor = document.getElementById(sEditorID);
   tinyMCE.execCommand('mceAddControl', true, sEditorID);
   return;
}

// deactivate a tinymce editor
function unsetTextareaToTinyMCE(sEditorID) {
   tinyMCE.execCommand('mceRemoveControl', true, sEditorID);
   return;
}


// reusable ajax function.  mosty used to update a status radio button
// see calls in templates for examples
function ajax_status_widget(obj, form){
   
   // get cgi script path (in URL) so we can reuse this 
   // function for multiple scripts
   url = document.location.href;
   match = url.match(/^(.*?)\?/);
   script_path = match[1];
      
   function activate_spinner(){
      if ($("spinner")){$("spinner").remove()};
      spin_html = '<span id="spinner"><img src="/images/ajax_loading_laser.gif" />&nbsp;Saving';
      $("saving").insert(spin_html);
   }

   function deactivate_spinner(){
      if ($("spinner")){$("spinner").remove()};
   }

   function failure_alert(transport){
      alert("Couldn't save choice, status code: " + transport.status);

   }


   function on_success(transport) {

      var stat = transport.status;

      // we can get here with a status of 0 also, which seems to be what
      // browser gets from a dead server, nice, so lets get more picky
      if ( stat == 0 ) { 
         failure_alert(transport)
      }

   }


   new Ajax.Request(script_path, {
                       method: 'get',
                       parameters: $(form).serialize(),
                       onCreate: function(transport){activate_spinner() },
                       // FF and Opera say dead server is status 0, which runs onSuccess,
                       // because prototype calls 2xx and 0 both success codes
                       onSuccess: on_success,
                       // ie says dead server is status 12029, which makes prototype run onFailure
                       onFailure: function(transport){failure_alert(transport)},
                       onComplete: function(transport){ deactivate_spinner() },
                       onException: function(transport, exception){alert("ajax exception :" + exception)}
                    });
   

}

