//
//  Sets to the click-to-activate in place editor
//
//  Args:
//  element  - The id of the element to edit
//  type     - The form id applied 
//  item     - The method to be called on the Ajax_IPE object
//  id       - the DB id of the item to edit
//  callback - A javascript method to call when the edit is complete.
//             The callback takes two arguments 1 - the element from this call & the id from this call
//function IG_IPE_single(element,type,item,id,callback) {
//  IG_InPlaceEditor(element,type,item,id,1,callback);
//}
//function IG_IPE(element,type,item,id,callback) {
//  IG_InPlaceEditor(element,type,item,id,4,callback);
//}

//function IG_InPlaceEditor(element,type,item,id,rows,callback) {
//   new Ajax.InPlaceEditor(element,'/ajax/IPE/'+item+'?id=' + id,{formId: type,okText: 'Save',cancelText: 'Cancel', rows:rows, cols:40, onComplete: myCallback});
//   function myCallback(x){
//     updateUndo();
//     if (typeof(callback) != 'undefined'){
//        callback(element,id);
//     }
//   }
//}
function updateUndo(){
  Ajax_send("/ajax/undo/get",null,updateUndoCallback);
}
function updateUndoCallback(data){
  if (typeof(data.undo_count) != undefined && $('undo-area')){
     if (data.undo_count>0){
        $('undo-link').setAttribute('title', "Undo "+data.undo_message);
        $('undo-link').show(); 
        $('undo-disabled-link').hide(); 
     }
  }  
}



var CurrentResultPage=1;
function previousResultsPage(){
  if (CurrentResultPage==1){return;}
  $('ResultPage'+(CurrentResultPage-1)).style.display='';
  $('ResultPage'+(CurrentResultPage)).style.display='none';
  CurrentResultPage--;
  $('next_results_page').style.display='';
  if (CurrentResultPage==1){$('previous_results_page').style.display='none';} 
}
function nextResultsPage(){
  if (CurrentResultPage==MaxResultPage){return;} 
  $('ResultPage'+(CurrentResultPage+1)).style.display='';
  $('ResultPage'+(CurrentResultPage)).style.display='none';
  CurrentResultPage++;
  $('previous_results_page').style.display='';
  if (CurrentResultPage==MaxResultPage){$('next_results_page').style.display='none';} 
}

if($('search') !== null){ google.load('search', '1'); }

function SetQueryBox(query){
  if(document.getElementsByName("search")[0] != null){
     document.getElementsByName("search")[0].value = query;
  }else{
     document.forms[0].elements[0].value = query;   
  }
}

function UpdateSubmitValue(){
  var search_form = document.forms[0];
  var found_search_box = false;
  for (var i = 0; i < search_form.elements.length; i++){
      if (search_form.elements[i].name == "search"){
         var q = search_form.elements[i].value;
         found_search_box = true;
      }
  }
  if (!found_search_box){
     var q = document.getElementsByName("search")[0].value;
  }
  document.getElementById('result-query-input').value = q;
}

function AddOption(query){
  var select = $('result-query-previous');
  var optn = document.createElement("OPTION");
  optn.text = query;
  optn.value = query;
  var options_length = select.options.length;
  var all_options = [];
  for(var i = 0; i < options_length; i++){
     all_options[i] = select.options[i].text;
  }
  for(var k = 0; k < options_length; k++){
     select.remove(0);
  }
  select.options.add(optn);
  for(var j = 0; j < options_length; j++){
     var old_opt = document.createElement("OPTION");
     old_opt.text = all_options[j];
     old_opt.value = all_options[j];
     select.options.add(old_opt);
  }
  return false;
}

function OnLoad() {
   OnSearchStarting = function(sc, searcher, query) {
   AddOption(query);
}

// Create a search control
var searchControl = new google.search.SearchControl();
searchControl.setSearchStartingCallback(this, OnSearchStarting);

var searchOptions = new google.search.SearcherOptions();
searchOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

var drawOptions = new google.search.DrawOptions();
drawOptions.setSearchFormRoot(document.getElementById("searchForm"));

// Add in a full set of searchers
searchControl.addSearcher(new google.search.WebSearch(), searchOptions);

// tell the searcher to draw itself and tell it where to attach
searchControl.draw(document.getElementById("searchResults"), drawOptions);

// execute an inital search
searchControl.execute("{/literal}{$current_result_query}{literal}");
}

if($('search') != null){ google.setOnLoadCallback(OnLoad); }


function editGuider(guider_id){
    $("guider-" + guider_id).style.display = "none";
    $("guider-" + guider_id + "-edit").style.display = $("guider-header").style.display;
}
function cancelGuiderEdit(guider_id){
    $("guider-" + guider_id + "-edit").style.display = "none";
    $("guider-" + guider_id).style.display = $("guider-header").style.display;
    return false;
}
