/*
 * Copyright (c) 2002-2007 Julien WILK (http://LDDb.com/)
 * Not to be modified/reused without authorization.
 */
 
document.onkeydown = function (E)
{
  var r;
  if(typeof(E)=='undefined')
    r = event.keyCode;
  else
    r = E.keyCode;

  switch(r)
  {
    case 13:
      if(notes_in_focus >= 0)
        $("describe_"+notes_in_focus).click();
      break;

    case 27: //ESC in Notes
      if(notes_in_focus >= 0)
        display_notes('Collection','hide','notes',notes_in_focus,'describe');
    break;
  }
}

function calendar(id)
{
	if($("cal_"+id).style.display == 'none')
	  Effect.Fade('cal_'+id+'_img',{to:0.2,duration:0.5})
	return showCalendar(id,'%Y-%m-%d');
}

function update_date(id,value)
{
	if($("cal_"+id).value != value)
	{
    $("cal_"+id).style.display = 'none';
    $("cal_"+id).value = value;
    $("updating_cal_"+id).style.display = '';
    sajax_update_date(id,value,update_date_cb);
  }
}

function update_date_cb(id)
{
	$("updating_cal_"+id).style.display = 'none';
	Effect.Appear("cal_"+id);
}

function show_controls(id,target)
{
  $(target+"_"+id).style.display = 'block';
}

function hide_controls(id,target)
{
  $(target+"_"+id).style.display = 'none';
}

function show_status(id)
{
  $("status_star_"+id).style.display = 'none';
  $("status_"+id).style.display = '';
}

function hide_status(id)
{
  $("status_"+id).style.display = 'none';
  $("status_star_"+id).style.display = '';
}

function confirm()
{
  window.location.reload();
}

function delete_collection(id)
{
  $("updating").style.display = '';
  sajax_delete_collection(id,confirm);
}

function delete_wishlist(id)
{
  $("updating").style.display = '';
  sajax_delete_wishlist(id,confirm);
}

function duplicate_collection(id)
{
  $("updating").style.display = '';
  sajax_duplicate_collection(id,confirm);
}

function move_to_collection(id)
{
  $("updating").style.display = '';
  sajax_move_to_collection(id,confirm);
}

function renew_wishlist(id)
{
  $("updating").style.display = '';
  sajax_renew_wishlist(id,renew_wishlist_cb);
  //sajax_renew_wishlist(id,confirm);
}

function renew_wishlist_cb(id_tr)
{
// shortcut to toggle_notify_cb, same code! 
  toggle_notify_cb(id_tr);
}

function toggle_notify(id,status,tr_class)
{
// TEMP
  $("updating").style.display = '';

//alert("request: "+status+" "+tr_class);
  $("notify_star_"+id).style.display = 'none';
  $("notify_updating_"+id).style.display = '';

//TEMP
  sajax_toggle_notify(id,status,tr_class,toggle_notify_cb);
//  sajax_toggle_notify(id,status,tr_class,confirm);
}

function toggle_notify_cb(id_tr)
{
  var param = id_tr.split('|');
  var id = param[0];
  var tr_class = param[1];
  var tr = param[2];

  $("wishlist_full_"+id).className = "contents_"+tr_class;

// Does not work with IExplorer -> tr is read-only
  //$("wishlist_full_"+id).innerHTML = tr;
  try{
  	$("wishlist_full_"+id).innerHTML = tr;
  	slider_init(id,'wishlist');
    $("updating").style.display = 'none';
  }
  catch(E){ //IExplorer...
    $("updating").style.display = 'none';
    $("loading").style.display = '';
    confirm();
  }
}

function update_note_cb(id)
{
  if(id)
  {
    if($("NOTES_NEW_"+id).value)
      $("notes_"+id).innerHTML = $("NOTES_NEW_"+id).value;
    else
      $("notes_"+id).innerHTML = '';

    $("NOTES_OLD_"+id).value = $("NOTES_NEW_"+id).value;

    $("describe_updating_"+id).style.display = 'none';
    $("describe_updated_"+id).style.display = '';
    //setTimeout("display_notes('Collection','hide','notes',"+id+",'describe')", 750);
    new Effect.Fade("describe_updated_"+id,{duration:1.0,queue:'end',afterFinish:function(){display_notes('Collection','hide','notes',id,'describe');} });
  }
  new Effect.Fade("updating",{duration:2.0});
}

function update_note(id,target)
{
  var old_note, new_note;

  old_note = $("NOTES_OLD_"+id).value;
  new_note = $("NOTES_NEW_"+id).value;

  if(new_note!=old_note)
  {
    $("updating").style.display = '';

    $("describe_"+id).style.display = 'none';
    $("describe_updating_"+id).style.display = '';
    sajax_update_note(id,old_note,new_note,target,update_note_cb);
  }
  else
    display_notes('Collection','hide','notes',id,'describe');
}

	
function update_price(id)
{
  var old_price, new_price;
  
  old_price = $("PRICE_OLD_"+id).value;
  new_price = $("PRICE_NEW_"+id).value;

  if(new_price!=old_price)
  {
    switch(IsNumeric(new_price))
    {
      case -1:
        $("PRICE_NEW_"+id).value = old_price;
      break;
  
      case 0:
        // TEMP
        // $("updating").style.display = '';
        $("loading").style.display = '';

        $("notify_star_"+id).style.display = 'none';
        $("notify_updating_"+id).style.display = '';
        sajax_update_price(id,0,update_price_cb);
      break;
  
      case 1:
        // TEMP
        // $("updating").style.display = '';
        $("loading").style.display = '';

        $("notify_star_"+id).style.display = 'none';
        $("notify_updating_"+id).style.display = '';
        sajax_update_price(id,new_price,update_price_cb);
      break;
    }
  }
}

function update_price_cb(id_value)
{
  var param = id_value.split(':');
  var id = param[0];
  var value = param[1];

  $("notify_updating_"+id).style.display = 'none';
  $("notify_updated_"+id).style.display = '';
  setTimeout("$(\"notify_updated_"+id+"\").style.display = 'none';$(\"notify_star_"+id+"\").style.display = '';", 750);
  $("PRICE_NEW_"+id).value = value;
  $("PRICE_OLD_"+id).value = value;

  new Effect.Fade("updating");
}

/************************************/

function IsNumeric(strString)
//  check for valid numeric strings	
{
  var strValidChars = "0.123456789";
  var strChar;
  
  if (strString.length == 0) return 0;
  
  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
      return -1;
    }
  }
  return 1;
}
