var RL_use_jsrs = (bw) ? !bw.op : false;
var RL_jsrs_debug = false;

function RL_OTB_change_state(id_site, id_article, index) {
  RL_deleteOrSave(id_site, id_article, 0, index, 1);
}

function RL_deleteOrSave(id_site, id_article, stimestamp, index, type) {
  var action;
  if (type==1) action = (index>=0 && (OTB_states[index]>250)) ? 'save' : 'delete';
  else         action = (index>=0) ? deleteOrSave[index] : 'delete';
  var params = Array();
  params['RL_article'] = id_site+'+'+id_article;
  if (stimestamp) params['RL_article'] += '+'+stimestamp;
  if (type==1) {
    params['RL_OTB_list']     = document.form.RL_OTB_list.value;
    params['RL_OTB_artstate'] = OTB_states[index];
  } else
    params['LBuck_archives']  = document.form.LBuck_archives.value;
  params['RL_deleteOrSave_index'] = index;

  if (action == 'save') {
    if ((document.form.LBuck_archives.value &&
	 document.form.LBuck_archives.value != '0') ||
	type==1) {

      if (type==1) document.form.action_performed.value = 'RL_OTB_change_state';
      else         document.form.action_performed.value = 'RL_save_article';
      if (RL_use_jsrs && id_site && id_article) {
	document.form.action_count.value = parseInt(document.form.action_count.value) + 1;
	if (type==1) {
	  jsrsCall(JSRS_TARGET,RL_OTB_callback,params,RL_jsrs_debug);
	} else {
	  deleteOrSave[index] = 'delete'; //update deleteOrSave-array
	  jsrsCall(JSRS_TARGET,RL_callback,params,RL_jsrs_debug);
	}
      } else if (id_site && id_article) {
	document.form.RL_article.value = id_site + ' ' + id_article + ' ' + stimestamp;
	submit_form();
      }
    } else {
      alert(gettext('RL_JS_PLEASE_SELECT_ARCHIVE_TO_SAVE_IN'));
    }
  }

  else if (action == 'delete') {
    if (document.form.PB_browsing)
      document.form.PB_browsing.value='true';

    if (type==1) document.form.action_performed.value = 'RL_OTB_change_state';
    else         document.form.action_performed.value = 'RL_delete_article';
    if (RL_use_jsrs && id_site && id_article) {
      document.form.action_count.value = parseInt(document.form.action_count.value) + 1;
      if (type==1) {
	jsrsCall(JSRS_TARGET,RL_OTB_callback,params,RL_jsrs_debug);
      } else {
	deleteOrSave[index] = 'save'; //update deleteOrSave-array
	jsrsCall(JSRS_TARGET,RL_callback,params,RL_jsrs_debug);
      }
    } else if (id_site>=0 && id_article>=0) {
      document.form.RL_article.value = id_site + ' ' + id_article;
      if (id_site==0 && id_article==0 && type!=1) {
	if(!confirm(gettext('RL_JS_DO_YOU_REALLY_WANT_TO_EMPTY_ARCHIVE')))
	  return false;
	/* if the arraow is pointing on some other archive: */
	if (document.form.LBuck_active_id.value!=document.form.LBuck_archives.value) {
	  document.form.LBuck_archives_mode.value = 1;
	  update_archives_mode(document.form.LBuck_active_id.value);
	}
      }
      submit_form();
    }
  } else {
    alert("Unknown action: '"+action+"'");
  }
}

function RL_saveAll() {
  if (document.getElementById('RL_art_id')) {
    document.form.action_performed.value = 'RL_save_all';
    submit_form();
  }
}

function RL_callback(param, callId) {
  //alert(callId+" "+param);
  var index = param[1];
  var archive = param[2];
  var id = document.form.LBuck_archives.value;
  var val = 1;

  if (index && archive) {
    if (param[0] == 'RL_save_article') {
      //update is_in_baskets-array:
      if (is_in_baskets[index]!='') is_in_baskets[index] += ',';
      is_in_baskets[index] += archive;
      val = 1;
    } else if (param[0] == 'RL_delete_article') {
      //update is_in_baskets-array:
      var tmp_arr = is_in_baskets[index].split(',');
      var tmp_str = '';
      for (var i=0; i < tmp_arr.length; i++) {
	if (tmp_arr[i] != archive) {
	  if (tmp_str!='') tmp_str += ',';
	  tmp_str += tmp_arr[i];
	}
      }
      is_in_baskets[index] = tmp_str;
      val = -1;
    }
    RL_toggle_save('RL_img'+index,is_in_baskets[index],index);
    if (eval("document.form.LBuck_archives_numart_"+id)) {
      eval("document.form.LBuck_archives_numart_"+id+".value = parseInt(document.form.LBuck_archives_numart_"+id+".value) + "+val);
      LBuck_empty_archives(id);
    }
  }
}

function RL_OTB_callback(param, callId) {
  var action = param[0];
  var otb_list = param[1];
  var updated = param[2];

  if (updated) {
    updated = updated.split(",");
    for (var i=0; i<updated.length; i++) {
      data = updated[i].split("|");
      RL_OTB_toggle_save(action,data[0],data[1]);
    }
  }
}

function RL_saveList() {
  document.form.LBuck_active_id.value = 0;
  document.form.action_performed.value = 'RL_save_list';
  submit_form();
}

function RL_viewComments(id_site,id_article) {
  document.form.RL_article.value = id_site + ' ' + id_article;
  document.form.action_performed.value = 'RL_view_comments';
  submit_form();
}

function RL_sendMail(id_site,id_article) { //send whole result (id_site=0, id_article=0) or one article on mail
  document.form.RL_article.value = id_site + ' ' + id_article;
  document.form.action_performed.value = 'RL_send_mail';
  submit_form();
}

function RL_printArticle(id_site,id_article) { //print whole result (id_site=0, id_article=0) or one article
  document.form.RL_article.value = id_site + ' ' + id_article;
  document.form.action_performed.value = 'RL_print_article';
  submit_form();
}

function RL_sendSMS(id_site,id_article,stimestamp) {
  if (id_site>0 && id_article>0 && stimestamp>0) {
    document.form.RL_article.value = id_site + ' ' + id_article + ' ' + stimestamp;
    document.form.action_performed.value = 'RL_send_sms';
    submit_form();
  }
}

function RL_OTB_edit(id_list) {
  document.form.RL_OTB_list.value = id_list;
  document.form.action_performed.value = 'RL_OTB_edit';
  submit_form();
}

function RL_OTB_print(id_rules) {
  window.open('http://otb.opoint.com/otb_parser.php?id_rule='+id_rules+'&mode=print','otb_parser','width=800,height=600,resizable=1,scrollbars=1,menubar=0');
}

function RL_OTB_toggle_identical() {
  document.form.action_performed.value = 'RL_OTB_identical';
  submit_form();
}

function RL_OTB_toggle_deleted() {
  document.form.action_performed.value = 'RL_OTB_deleted';
  submit_form();
}

function RL_OTB_exit() {
  document.form.action_performed.value = 'RL_OTB_exit';
  submit_form();
}


function RL_toggle_all_save() {
  for (var i = 1; i < is_in_baskets.length; i++) {
    RL_toggle_save("RL_img"+i, is_in_baskets[i], i);
  }
}

function RL_toggle_save(id_tag, baskets, index) {
  var img = document.getElementById(id_tag);

  if (document.form && document.form.LBuck_archives) {
    var id_basket = document.form.LBuck_archives.value;
    //alert(baskets.indexOf(id_basket)+' baskets:'+baskets+' id:'+id_basket+' tag:'+id_tag);
    if (baskets.indexOf(id_basket) >= 0) {
      if (img) {
	img.src = IMG_PATH+'archives_delete.gif';
	img.title = gettext('GLOB_REMOVE_FROM_ARCHIVE');
	//addEvent(img, "mouseover", show_status(gettext('GLOB_REMOVE_FROM_ARCHIVE')));
	deleteOrSave[index] = 'delete';
      }
    } else {
      //img.style.visibility = 'visible';
      if (img) {
	img.src = IMG_PATH+'archives_black.gif';
	img.title = gettext('GLOB_SAVE_IN_ARCHIVE');
	//addEvent(img, "mouseover", show_status(gettext('GLOB_SAVE_IN_ARCHIVE')));
	deleteOrSave[index] = 'save';
      }
    }
  }
}

function RL_OTB_toggle_save(action,index,new_state) { //FIX image title and status text
  if (action == 'RL_OTB_change_state') {
    var id_img = 'RL_OTB_img'+index;
    OTB_states[index] = new_state; //update OTB_states-array

    if (document.getElementById('RL_header_'+index) && document.getElementById(id_img)) {
      var header_tag = document.getElementById('RL_header_'+index);
      var img_tag = document.getElementById(id_img);
      new_state = parseInt(new_state);
      removeClass(header_tag,'');
      switch(new_state) {
      case -1:
	addClass(header_tag,'ListArtHeading_hidden');
	img_tag.src = IMG_PATH+'archives_gray.gif';
	break;
      case 0:
	addClass(header_tag,'ListArtHeading_hidden');
	img_tag.src = IMG_PATH+'archives_bluemark.gif';
	break;
      case 1:
	addClass(header_tag,'ListArtHeading');
	img_tag.src = IMG_PATH+'archives_delete.gif';
	break;
      case 2:
	addClass(header_tag,'ListArtHeading');
	img_tag.src = IMG_PATH+'archives_delete.gif';
	break;
      case 3:
	addClass(header_tag,'ListArtHeading');
	img_tag.src = IMG_PATH+'archives_gray_redmark.gif';
	break;
      case 254:
      case 255:
	addClass(header_tag,'ListArtHeading_hidden');
	img_tag.src = IMG_PATH+'archives_black.gif';
	break;
      }
      RL_OTB_set_title(index,new_state);
    }
  }
}

function RL_OTB_set_title(index,state) {
  document.getElementById('RL_OTB_img'+index).title = OTB_help_txt[state];
}

function RL_ArtInfo(id_site, id_article, stimestamp) {
  /*window.open(MODULES_PATH+'ArticleInfo/ArticleInfo.php?id_site='+id_site+'&id_article='+id_article+'&stimestamp='+stimestamp+'&session_name='+document.form.session_name.value,'ArticleInfo','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1');*/
  window.open('http://ai.opoint.com/art_info.php?id_site='+id_site+'&id_article='+id_article+'&stimestamp='+stimestamp,'ArticleInfo','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1');
}

function RL_SourceInfo(id_source) {
  window.open('http://ose.opoint.com/showfoundarticles.php?id_source='+id_source+'&session_name='+document.form.session_name.value+'&url='+APP_PATH+MODULES_PATH+'ArticleInfo/ArticleInfo.php','SourceInfo','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=1,resizable=1,statusbar=1');
}

function RL_reload() {
  document.form.action_performed.value = 'App_reload';
  submit_form();
}



//////////////////////////////////////////////////////////////////////////

var active_box = null;
var lastA;
var lastB;
var y_pos;
var x_pos;
var orig_x;
var orig_y;

function toggleShow(strA, strB, max, image) {

  document.getElementById('A0').style.visibility = "hidden";

  if (bw.dom) {//is_gecko || is_opera5up
    var divA = document.getElementById(strA);
    var divB = document.getElementById(strB);

    if (lastA) {
      lastA.style.visibility = "hidden";
    }
    if (lastB) {
      removeClass(lastB,"ListArt_highlight");
      addClass(lastB,"ListArt");
    }

    lastA = divA;
    lastB = divB;

    if (!y_pos) {
      y_pos = RL_get_left_position(image);
      x_pos = RL_get_top_position(image);
    }

    if (divB) {
      removeClass(divB,"ListArt");
      addClass(divB,"ListArt_highlight");
    }
    if (divA) {
      divA.style.top  = x_pos;
      divA.style.left = y_pos;
      divA.style.visibility = "visible";
    }
    active_box = divA;
  }

  else if (bw.ie4) { //is_ie5up
    for (i = 1; tmpA = document.all["A"+i]; i++) {
      if (typeof tmpA != 'undefined') {
	tmpA.style.visibility = "hidden";
      }
      if (typeof document.all["B"+i] != 'undefined') {
	removeClass(document.all["B"+i],"ListArt_highlight");
	addClass(document.all["B"+i],"ListArt");
      }
    }

    if (!y_pos) {
      x_pos = RL_get_left_position(image);
      y_pos = RL_get_top_position(image);
    }

    if (typeof document.all[strB] != 'undefined') {
      removeClass(document.all[strB],"ListArt");
      addClass(document.all[strB],"ListArt_highlight");
    }
    if (typeof document.all[strA] != 'undefined') {
      document.all[strA].style.top = y_pos;
      document.all[strA].style.left = x_pos;
      document.all[strA].style.visibility = "visible";
    }
    active_box = document.all[strA];
  }

  else if (bw.ns4) { //is_nav4
    for (i = 1; tmpA = document.layers["A"+i]; i++){
      if (typeof tmpA != 'undefined') {
	tmpA.visibility = "hide";
      }
      if (typeof document.layers["B"+i] != 'undefined') {
	document.layers["B"+i].document.bgColor = "#ffffff";
      }
    }

    if (!y_pos) {
      x_pos = RL_getImagex(image);
      y_pos = RL_getImagey(image);
    }

    if (typeof document.layers[strB] != 'undefined') {
      document.layers[strB].document.bgColor = "#fdfadb";
    }
    if (typeof document.layers[strA] != 'undefined') {
      document.layers[strA].top = y_pos;
      document.layers[strA].left = x_pos;
      document.layers[strA].visibility = "show";
    }
    active_box = document.layers[strA];
  }
  checkscrolled();
}


var RL_pid = null;
function RL_init_checkscroll(id_tag) {
  var el = document.getElementById(id_tag);
  if (el && el.value && parseInt(el.value) > 10) {
    if(bw.ie) {
      document.body.onscroll=new Function("checkscrolled()");
      checkscrolled();
    }
    else RL_pid = setTimeout("checkscrolled()",200); //REMOVE THIS LINE TO HAVE SCROLLING ON FOR EXPLORER ONLY!!
    //setTimeout("checkscrolled()",200); //init
  }
}

function checkscrolled() {
  var i, o;
  if(bw.mac) return; //REMOVE THIS LINE TO HAVE SCROLLING ON THE MAC AS WELL - unstable!
  if(bw.ns4 || bw.ns6 || bw.op5) this.scrollY = window.pageYOffset;
  else this.scrollY=document.body.scrollTop;
  if (!active_box) {active_box = document.getElementById('A0'); orig_x = RL_get_left_position(active_box); orig_y = RL_get_top_position(active_box);}
  this.fromTop  = RL_get_top_position(active_box);
  this.fromLeft = RL_get_left_position(active_box);
  //alert(this.scrollY+ ' '+parseInt(this.fromTop)+' '+parseInt(orig_y));
  if(this.scrollY!=parseInt(this.fromTop)) {
    //for(i=0;i<100;i++){o=document.getElementById('A'+i);if(o && o.){RL_moveIt(o,this.fromLeft,this.scrollY);}else{break;}}
    //for(i=0;o=document.getElementById('A'+i);i++){RL_moveIt(o,this.fromLeft,this.scrollY);}//if(is_visible(o)){}
    if (this.scrollY>=parseInt(orig_y))
      RL_moveIt(active_box,this.fromLeft,this.scrollY);
    else
      RL_moveIt(active_box,this.fromLeft,orig_y);
  }
  if(!bw.ie) {
    if (RL_pid) clearTimeout(RL_pid);
    RL_pid = setTimeout("checkscrolled()",200);
  }
}


function RL_getImagex(imgID) {
  var posX = (eval(imgID).x);
  return posX;
}

function RL_getImagey(imgID) {
  var posY = (eval(imgID).y);
  return posY;
}

function RL_get_left_position(element) {
  if (!element.style.left)
    element.style.left = get_left_position(element);
  return element.style.left;
}

function RL_get_top_position(element) {
  if (!element.style.top)
    element.style.top = get_top_position(element);
  return element.style.top;
}

function RL_moveIt(obj,x,y) { //FIX: IE fails here when this is called to early when loading a page...!
  //alert(obj);
  //alert(obj.style.left + ' ' + obj.style.top);
  //alert('x=' + obj.style.left + ' y=' + y);
  if (obj) {
    if (x && obj.style.left)
      obj.style.left = x;
    if (y && obj.style.top)
      obj.style.top  = y;
  }
  //alert('moved to ('+obj.style.left+','+obj.style.top+')');
}

//function is_visible(element) {
//  if (is_gecko || is_opera5up || is_ie5up) {
//    return (element.style.visibility == "visible");
//  }
//  else if (is_nav4) {
//    return (element.visibility == "show")
//  }
//  return false;
//}

///////////////// ON MOUSE OUT

//var ind;
//var pid;
//
//function toggleHide (i) {
//  clearTimeout(pid);
//  ind = i;
//  //alert(ind);
//  pid = setTimeout('hide()', 1000);
//}
//
//function hide() {
//  if (is_gecko || is_opera5up) {
//    document.getElementById("A"+ind).style.visibility = "hidden";
//    document.getElementById("B"+ind).style.backgroundColor = "#ffffff";
//  }
//
//  else if (is_ie5up) {
//    document.all["A"+ind].style.visibility = "hidden";
//    document.all["B"+ind].style.backgroundColor = "#ffffff";
//  }
//
//  else if (is_ns4) {
//    document.layers["A"+ind].visibility = "hide";
//    document.layers["B"+ind].document.bgColor = "#ffffff";
//
//  }
//}



// COMMENTS:


function C_add(id_site, id_article) {
  if (C_validate(0)) {
    document.form.C_article.value = id_site + ' ' + id_article;
    document.form.action_performed.value = 'C_add';
    submit_form();
  }
}

function C_insert(id_site, id_article) {
  if (C_validate(0)) {
    document.form.RL_article.value = id_site + ' ' + id_article;
    document.form.action_performed.value = 'C_insert';
    submit_form();
  }
}

function C_update(id_site, id_article, id_user, stimestamp) {
  if (C_validate(id_user+'-'+stimestamp)) {
    document.form.action_performed.value = 'C_update';
    document.form.C_id_user.value = id_user;
    document.form.RL_article.value = id_site + ' ' + id_article;
    document.form.C_stimestamp.value = stimestamp;
    document.form.C_updated_comment.value = document.form.elements["C_editcomment_" + id_user + '-' + stimestamp].value;
    submit_form();
  }
}


function C_toggle_edit(id_user, stimestamp) {
  var editField = document.getElementById("C_edit_" + id_user + '-' + stimestamp);
  var functionBarEdit = document.getElementById("comments-functionbar-edit-" + id_user + '-' + stimestamp);
  var functionBarView = document.getElementById("comments-functionbar-view-" + id_user + '-' + stimestamp);
  if(editField.style.display == 'block') {
    // Toggle edit mode OFF
    editField.style.display = 'none';
    editField.nextSibling.style.display = 'block';
    functionBarEdit.style.display = 'none';
    functionBarView.style.display = 'block';
  } else {
    // Toggle edit mode ON
    editField.style.display = 'block';
    editField.nextSibling.style.display = 'none';
    functionBarEdit.style.display = 'block';
    functionBarView.style.display = 'none';
  }
}

function C_delete(id_site, id_article, id_user, stimestamp) {
  if(confirm(gettext('RL_JS_DO_YOU_REALLY_WANT_TO_DELETE_COMMENT'))) {
    document.form.action_performed.value = 'C_delete';
    document.form.RL_article.value = id_site + ' ' + id_article;
    document.form.C_id_user.value = id_user;
    document.form.C_stimestamp.value = stimestamp;
    submit_form();
  }
}

function C_close() {
  document.form.action_performed.value = 'C_close';
  submit_form();
}

function C_showNewComment() {
  var el = document.getElementById('comments-new');
  el.style.display = 'block';

  el = document.getElementById('comments-show-new');
  el.style.display = 'none';
}

function C_hideNewComment() {
  var el = document.getElementById('comments-new');
  el.style.display = 'none';

  el = document.getElementById('comments-show-new');
  el.style.display = 'block';
}


function C_validate(id) {
  if (id==0 && document.form.C_signature.value.match(new RegExp("^\s*$"))) {
    alert(gettext('RL_JS_PLEASE_PROVIDE_A_SIGNATURE'));
    return false;
  }
  if ((id==0 && document.form.C_comment.value.match(new RegExp("^\s*$"))) ||
      (id!=0 && document.getElementById('C_editcomment_'+id).value.match(new RegExp("^\s*$")))) {
    alert(gettext('RL_JS_IMPOSSIBLE_TO_SAVE_EMPTY_COMMENT'));
    return false;
  }
  return true;
}


//------------------------------------------------------------------------------------

function ordering_enableDrag(el) {
  //document.getElementById('DEBUGINFO').innerHTML +=
  //"ordering_enableDrag: " + DragPane.hoveredElement + ' ' + el + ' ' + (DragPane.hoveredElement == el) + "<br/>";
  SortableListItem.getInstance(el);
}

function ordering_hover(el) {
  //document.getElementById('DEBUGINFO').innerHTML +=
  //"ordering_hover1: " + DragPane.hoveredElement + ' ' + el + ' ' + (DragPane.hoveredElement == el) + "<br/>";
  if(DragPane.hoveredElement != null) {
    removeClass(DragPane.hoveredElement, "hoveredItem");
    //should be done in DragPane.js:
    //if (DragPane.hoveredElement.id != el.id)
      //removeClass(DragPane.hoveredElement, "focusedItem");
    DragPane.hoveredElement = null;
  }
  addClass(el,"hoveredItem");
  DragPane.hoveredElement = el;

  //SortableListItem.getInstance(el);
}

//FIX: get triggered even if mouse just moves to children nodes
var ordering_view = new Array();
function ordering_hoverOff() {
  //document.getElementById('DEBUGINFO').innerHTML +=
  //"ordering_hoverOff: " + DragPane.hoveredElement + ' ' + "<br/>";
  var elem = document.getElementById('ordering-view');
  if (!ordering_view.length)
    ordering_view = new Array(get_left_position(elem),get_top_position(elem),elem.offsetWidth,elem.offsetHeight);
  //  document.getElementById('DEBUGINFO').innerHTML +=
  //    "data: " + get_left_position(elem) + ',' + get_top_position(elem) + " - " + elem.offsetHeight + ',' + elem.offsetWidth + "<br/>";
  //} else {
  //  document.getElementById('DEBUGINFO').innerHTML +=
  //    "data2: " + mouse_x + ',' + mouse_y + "<br/>";
  //}

  //document.getElementById('DEBUGINFO').innerHTML +=
  //  "left: "+mouse_x+"<"+ordering_view[0]+"="+(mouse_x<=ordering_view[0]) + " | " +
  //  "top: "+mouse_y+"<"+ordering_view[1]+"="+(mouse_y<=ordering_view[1]) + " | " +
  //  "right: "+mouse_x+">"+(ordering_view[0]+"+"+ordering_view[2])+"="+(mouse_x>=(ordering_view[0]+ordering_view[2])) + " | " +
  //  "bottom: "+mouse_y+">"+(ordering_view[1]+ordering_view[3])+"="+(mouse_y>=(ordering_view[1]+ordering_view[3])) + "<br/>";

  if (typeof mouse_x == 'undefined' || typeof mouse_y == 'undefined' ||
      mouse_x<=ordering_view[0] ||
      mouse_y<=ordering_view[1] ||
      mouse_x>=(ordering_view[0]+ordering_view[2]) ||
      mouse_y>=(ordering_view[1]+ordering_view[3])) {
    //document.getElementById('DEBUGINFO').innerHTML +=
    //"OUT: " + DragPane.hoveredElement + " " + (DragPane.hoveredElement ? DragPane.hoveredElement.id : '') + "<br/>";
    if(DragPane.hoveredElement != null){
      removeClass(DragPane.hoveredElement, "hoveredItem");
      removeClass(DragPane.hoveredElement, "focusedItem");
      //alert(DragPane.hoveredElement + ' ' + DragPane.hoveredElement.className);

      //force dragged element to get dropped:
      /*DragHandlers.mouseUp();*/ //does not work in IE??
    }
    DragPane.hoveredElement = null;
  }
}

