
/* put in 'DOM READY' handler */
var gear_menu = make_gear_menu();

function show_gear_menu()
{ 
   //this.src = '/img/gear_picked.png'; 
   gear_menu.current_cue = this.cue_id;
   gear_menu.gear_button_id = this.id;
   bins = new Array();
   o = new Object();
   o.onclick = new Object();
   o.onclick.fn = make_new_bin_shim;
   o.onclick.obj = new Object();
   o.onclick.obj.cue_id = this.cue_id;
   o.text = "New bin...";
   bins.push(o);

   for (var b in bin_data)
   {
      this_bin = bin_data[b];
      o = new Object();
      o.onclick = new Object();
      o.onclick.fn = add_to_bin_shim;
      o.onclick.obj = new Object();
      o.onclick.obj.bin_id = b;
      o.onclick.obj.cue_id = this.cue_id;
      o.text = this_bin.bin_name;
      bins.push(o);
   }

   gear_menu.clearContent();
   if (!standalone && (!user || user.is_guest))
   {
      gear_menu.addItems([
         { text: "Request Info", onclick: {fn: show_info_request_shim }   }
      ]);
   }
   else
   {
      if (!standalone)
      {
         gear_menu.addItems([
            { text: "Request Info", onclick: {fn: show_info_request_shim } }]);
      }
      gear_menu.addItems([
         { text: "Add to bin", 
            submenu: { 
            id: 'submenu1',
            zindex:10,
            itemdata: bins
            }
         }]);

      if (standalone) {
         var item_data = [{text: "Quick Export", onclick: {fn:instant_download_shim} }];
         item_data.push( { 
            text: "Show in " + (is_unix ? "Finder" : "Explorer"), 
            onclick: {fn:show_in_finder} });

         gear_menu.addItems([
            { text: "Export", 
               submenu: { id:'submenu2', itemdata: item_data }
            }
         ]);
      } else if (getInternetExplorerVersion() == -1) {
         var item_data = [{text: (standalone ? "Quick Export" : "Agree & Download"), onclick: {fn:instant_download_shim} }];
         if (standalone)
         {
            item_data.push( { text: "Show in Finder", onclick: {fn:show_in_finder} });
         }
         gear_menu.addItems([
            { text: (standalone ? "Export" : "Quick Download"), 
               submenu: { id:'submenu2', itemdata: item_data }
            }
         ]);
      }
      if (standalone) 
      {
      }

   }
//   gear_menu.render("all_divs");
   gear_menu.render(document.getElementById("all_divs"));
// this is NOT optional!
   gear_menu.cfg.setProperty('context', [this.id, 'tr','br']); 
   gear_menu.cfg.setProperty('submenualignment', ['tr','tl']); 
   gear_menu.show();
}


function add_to_bin_shim(p_sType, p_aArgs, p_oVals)
{ 
   if (!standalone && (!user || user.is_guest))
   {  show_login(true, null);
      return;
   } 
   add_cue_to_bin(p_oVals.bin_id, p_oVals.cue_id); 
}

function make_new_bin_shim(p_sType, p_aArgs, p_oVals)
{  
   if (!standalone && (!user || user.is_guest))
   {  show_login(true, null);
      return;
   } 
   makeNewBin(false, p_oVals.cue_id); 
}

function show_info_request_shim()
{  show_info_request(true, gear_menu.current_cue);
}

function instant_download_shim()
{  instant_download(gear_menu.current_cue);
}

function show_in_finder()
{  
   if (!audio_file_path) {
      showAlert("Your cue location has not been set.  Please set it under 'prefs'");
      return;
   }
   var cue_str = all_cue_data[gear_menu.current_cue].cue_code;
   var re = new RegExp(/^[ ]*([0-9a-z]*[a-z]+)([0-9][^_]+)*(_([0-9]*))[ ]*$/i);
   re.exec(cue_str);

//  e.g. iota/iota2006
   var path = RegExp.$1.toUpperCase() + DIR_SEP + RegExp.$1.toLowerCase() + 
      RegExp.$2;

   var result;
   if (is_unix) {
   result =
      COCOA.showInFinder_fileName_(audio_file_path + DIR_SEP + path, cue_str.toLowerCase()+".mp3");
   } else {
   result =
      window.external.showInFinder(audio_file_path + DIR_SEP + path, cue_str.toLowerCase()+".mp3");
   }

   if (result)
   {  showAlert(result);
   }
}
/*
function gear_hide() 
{  var el = document.getElementById(gear_menu.gear_button_id);
   el.src = '/img/gear_' + 
      (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
}
*/
function make_gear_menu() 
{
   var oMenu = new YAHOO.widget.Menu("gear_menu", 
      { zindex:10 }
   );

   // Since this Menu instance is built completely from script, call the 
   // "render" method passing in the DOM element that it should be 
   // appended to.
    
   oMenu.render(document.getElementById("all_divs"));
//r_menu.render(document.getElementById("all_divs"));
   //oMenu.subscribe("hide", gear_hide);
   oMenu.current_cue = null;
   
   return oMenu;
}


