  var ice_mode = false;
  var ice_div="";
  var ice_bar_hidden = false;
  var eicFrm= "#ice_frm";
  var eicTable= "icc_item";

  $(document).ready(function() {
$.log (".ready eicbar.js #8");

    /* eic bar */

/* security on toolbar */
     $('#ice_bar #oSecurity').change(function() {
//$.log ("new val:"+this.value);
//      var rez = prompt(this.value+": What is your new level", "1"); 
        var r5parms="r5parms=set:parents:us:"+this.value;   
        fnAjax (r5parms,"",_reload);
      });
     
    $('#eicBtnHide').click(function() {
      if (ice_bar_hidden) {
        $('.btn_ice').fadeToggle("hider");
        ice_bar_hidden=false;
        hideTxt='Hide In Context Editor';
      } else {
        $('.btn_ice').fadeToggle("hider");
        ice_bar_hidden=true;
        hideTxt='Show In Context Editor';
      }
      $('#eicBtnHide em').html(hideTxt); // ?????????
      return false;
    });

      $('#eicBtnLogout').click(function() {
$.log ("eicBtnLogout");
        window.location ="admin/inc/logouteic.php";
        return false;
      });

      $('#eicBtnCMS').click(function() {
$.log ("eicBtnCMS");
        window.location ="admin/index.php";
        return false;
      });
/*
    $('.btn_ice').live("hover", function() {
        $(this).next().find(".content, .iceTitle").addClass("ice_hilite");
      },
      function() {
         $(this).next().find(".content, .iceTitle").removeClass("ice_hilite");
      }
    );
*/

$(".btn_ice").live("mouseover mouseout", function(event) {
  var e = $(this).next();
  if ( event.type == "mouseover" ) {
    e.find(".iceContent, .iceTitle, .iceSubtitle").addClass("ice_hilite");
    oldColor=e.find(".content").css("background-color");
    oldImg=e.find(".content").css("background-image");
    e.find(".content").css("background-color","#8ddeff");
    e.find(".content").css("background-image","none");
  } else {
    e.find(".iceContent, .iceTitle, .iceSubtitle").removeClass("ice_hilite");
    e.find(".content").css("background-color",oldColor);
    e.find(".content").css("background-image",oldImg);
  }
});



//#ice_div .content:hover {
//background-color: #8ddeff


  var oThis; /* active edit object clicked on */

    /* click means user wants to edit */
    $('.txt_eic').click(function() {
      oThis=this;
      grabLive(oThis);
    });
    
    
    /* refresh form with db */
    $('#btnEICrestore').click(function() {
      grabRec (oThis,"frm");
      return false;
    });

  /* form to HTML, no db save */
    $('#btnEICpreview').click(function() {
      o=grabForm(eicFrm);
      $(oThis).find(".txt_edit").html(o.text);
      $('#btnEICcancel').trigger("click");
      return false;
    });

    /* save form to db */
    $('#btnEICsave').click(function() {
      o=grabForm(eicFrm);
      $(oThis).find(".txt_edit").html(o.text);

    // write to db
      var r5parms="r5parms=u:"+eicTable+":id:"+o.id+"&";

       sCereal=$(eicFrm+" input:not(:checkbox,:password),"+eicFrm+" select[name!=tablePages_length],textarea").serialize();      
          sCereal+="&"+$(eicFrm+" input:checkbox").fieldSerialize(false); // false: include unchecked; normal serialize ignores, but fieldSerialize fails on ckEdit
          r5parms+=sCereal;

$.log (r5parms); ////
/* store the values from the form input box, then send via ajax below */
          $.ajax({
            type: "POST",
            url: sURL,
  
            data: r5parms,
            error: function(fld) {
              $.log(fld);
alert ("ajax error in eicbar @ line 97.\n"+r5parms+"\n \n"+fld);
            },
            success: function(sJson){
$.log (sJson);
$.log ("success sJson eicbar.js 100:");

          x=parseInt(fnOnReturn2(sJson));
          if (x!=o.id) {

// ~FAIL={You have an error in your SQL syntax; 
                var err=sJson.split("}");
alert ("ERROR!\n"+err[0].substr(7)); 
          }
              
            }
          })

      $('#btnEICcancel').trigger("click");
      return false;
    });

  });


/* returns object with fields and contents */
function grabForm (frm) {
    var oFlds = new Object();
    $(frm+' input, '+frm+' textarea').each(function (i) {
      var tag=this.name;
      oFlds[tag]=$(this).val();
  });
  return oFlds;
}

/*  */
function grabLive (oThis) {
  var fld = new Object();
  level=(oThis.id).split(":")
  fld.id=level[0].substr(1);
  fld.security_level=level[1];
  fld.text=$(oThis).find(".txt_edit").html();  
  fillForm(eicFrm,fld);
  $('#btn_icebox').trigger("click");          
  return;
}

// grab db record   
function grabRec (oThis,caller) {
    level=(oThis.id).split(":")
    theID=level[0].substr(1);

    flds="title,text,security_level"
$.log (flds);
      
      var r5parms="r5parms=g:"+eicTable+">"+flds+":id:"+theID;
      $.ajax({
        type: "POST",
        url: sURL,
        data: r5parms,
        error: function (fld) {
$.log("eicbar.js g error","error");
$.log (fld.statusText);
alert ("error @79 eicbar.js:"+fld.statusText); //////////
          },
          success: function(sJson) {
$.log("eicbar get:"+sJson);
          if (sJson.indexOf("FAIL")>0) {
    alert (sJson); ///////////////////////////
          } else {  
            if (sJson.indexOf("<b>Warning</b>:")>0 || sJson.indexOf("<b>Fatal error</b>:")>0){
            //  alert ("cms.js\n"+sJson);
            }
            var fld = JSON.parse(sJson); //$.log (fld);
            fillForm(eicFrm,fld);
            if (caller!="frm") {
              $('#btn_icebox').trigger("click");          
            }
          }
        }
        
      });
      
    }
    
    
/* pass form id and fld object
  all flds with matching field names will be updated
  if rs is passed, flag changed
*/
function fillForm(frm, fld, rs) {
$.log("fillForm fld");
$.log(fld);

  // populate the form; first, inputs
  $(frm+' input, '+frm+' textarea').each(function (i) {
      if (fld[this.name]){
$.log ("fld: "+[this.name],"error");
        if (fld[this.name].raw != undefined) {
/* ?? sVal=(fld[this.name]+""!="")? fld[this.name][0] : "%%"; */
$.log (fld[this.name]); ////////
          sVal=fld[this.name].val;
          sValRaw=fld[this.name].raw;
          $(this).attr("data-raw",sValRaw);
          $(this).attr("data-prior",sVal);
        } else {
          sVal=fld[this.name];
          sValOrig="";
        }
      } else {
        sVal="";
      }
      $(this).val(sVal);
    });

    s=$(frm+" select").attr("id");
    $("#"+s).val(fld.security_level);
  return;
}
