	//
// (C) Trurl McByte
//
var xmlHttp=null;
var tr_search_form;
var holders=false;
var holder_result=null;
var distholder=null;
var holder_status=null;
var search_button=null;
var temp_holder=null;
var prev_holder1=null;
var prev_holder2=null;
var current_offset=0;
var processing=0;
var orderby = new Array();
var lang='ru_RU';
var autosearch=0;
var bobruysk=false;

if(document.getElementById("la2_form_holder")) tr_search_init();


function tr_search_init() {

    tr_search_form=null;
    holders=false;
    holder_result=null;
    distholder=null;
    holder_status=null;
    search_button=null;
    temp_holder=null;
    prev_holder1=null;
    prev_holder2=null;
    current_offset=0;
    processing=0;
    orderby = new Array();


  temp_holder=document.getElementById("la2_form_holder");
  holder_status=temp_holder;
  la2_getForm(lang,document.getElementById("la2_form_holder"));
//  if(!holders) { 
//    holders = holders_init(); 
//  }
  return false;
}


function la2unsort() {
  orderby = new Array();
  la2_search(0);
}

function la2order(field) {
//alert(orderby[field]);
  switch(orderby[field]) {
    case "ASC":
	orderby[field]='DESC';
	break;
    case "DESC":
	delete orderby[field];
	break;
    default:
	orderby[field]='ASC';
  }
  la2_search(0);
}

function la2_subselector(selector) {
  if(processing != 0) {
	xmlHttp.abort();
  }
  var cnt=0;
  var i=0;
  var selected=null;

  for(i=0; i<selector.length; i++){ 
	if(selector.options[i].selected) {
	    cnt = cnt + 1;
	    selected = selector.options[i].value;
	}
  }
  if(cnt == 1) la2_getSelector(document.getElementById('subselector'),'itemsubtype_'+selected);
//  if(cnt == 1 && selected == 1) la2_getSelector(document.getElementById('subselector'),'weapon_metatype');
//  else if(cnt == 1 && selected == 100) la2_getSelector(document.getElementById('subselector'),'armor_body_part');
//  else if(cnt == 1 && selected == 120) la2_getSelector(document.getElementById('subselector'),'jewelry_body_part');
//  else if(cnt == 1 && selected == 140) la2_getSelector(document.getElementById('subselector'),'fun_body_part');
//  else if(cnt == 1 && selected == 200) la2_getSelector(document.getElementById('subselector'),'family');
  else document.getElementById('subselector').innerHTML='';
}

function la2_search(offset)
{

  if(processing != 0) {
	return false;
  }

  current_offset=offset;
  if(search_button != null) search_button.disabled=true;
  holder_status.innerHTML="Processing request...";
  var url="/se/dig";

  var els = tr_search_form.elements; 
  var data=""; var i=0; var y=0;
  for(i=0; i<els.length; i++){ 
//  alert(els[i].type);
    switch(els[i].type){

	    case "select-one" :
		    data = data + (data?"&":"") + els[i].name + "=" + els[i].options[els[i].selectedIndex].value; 
		    break;
	    case "select-multiple" :
		    for(y=0; y < els[i].length; y++ ) {
			if(els[i].options[y].selected) {
			     data = data + (data?"&":"") + els[i].name + "=" + 
			     la2_escape(els[i].options[y].value);
			}
		    }
		    break;
	    case "textarea":
	    case "hidden":
	    case "password":
	    case "text":
		data = data + (data?"&":"") + els[i].name + "=" + la2_escape(els[i].value);
		break;
	    case "submit":
	    case "button":
		break;
	    case "checkbox":
	    case "radio":
		if(els[i].checked==true) data = data + (data?"&":"") + els[i].name + "=true";
		break;
	}
  }
  for(var order in orderby) {
     data = data + (data?"&":"") + "orderby_" + order + "=" + orderby[order];
  }
  data = data + (data?"&":"") + "__search_form=" + tr_search_form.id + "&__offset=" + offset + "&__search_func=la2_search&__ver=0.2";
  //sid="+Math.random();
  processing=1;
  var xmlHttp=GetXmlHttpObject();
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("POST",url,true);
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.send(data);
  return false;
}

function la2_getSelector(distholder,name) {

  temp_holder=distholder;

  search_button.disabled=true;
  holder_status.innerHTML="Processing request...";

  var url="/se/dig";
  url = url + '/selector/' + name + ".html";
  var data = "__search_form=" + tr_search_form.id + "&__offset=0&__search_func=la2_search&__ver=0.2";

  //sid="+Math.random();
  processing=1;
  xmlHttp=GetXmlHttpObject();
  xmlHttp.onreadystatechange=stateChanged;
//  xmlHttp.open("POST",url,true);
  xmlHttp.open("GET",url,true);
//  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//  xmlHttp.send(data);
  xmlHttp.send(null);
  return false;
}

function la2_getFormRu() {
    return la2_getForm("ru_RU",document.getElementById("la2_form_holder"));
}

function la2_getForm(lang,place) {

  temp_holder=place;

  var url="/se/dig";
  url = url + "/form/" + lang + "/item.html";

  //sid="+Math.random();
  processing=1;
  xmlHttp=GetXmlHttpObject();
  xmlHttp.onreadystatechange=stateChanged;
//  xmlHttp.open("POST",url,true);
  xmlHttp.open("GET",url,true);
//  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//  xmlHttp.send(data);
  xmlHttp.send(null);
  return false;
}

function la2_getById2(id) {
    return la2_getById(id,id,1);
}
function la2_getById3(id,sid) {
    return la2_getById(id,sid,2);
}

function la2_getById(id,did,ph)
{
  if(processing != 0) {
	return false;
  }
  if(ph == 1) {
      distholder=document.getElementById('holder_' + id);
  } else {
      distholder=document.getElementById('sub_' + did + '_holder_' + id);
  }
  eval("prev_holder = prev_holder" + ph + ";");
  if(distholder.innerHTML!='') {
    if(distholder.style.display=='block') {
	distholder.style.display ='none';
    } else {
	distholder.style.display ='block';
    }
    if(prev_holder != null && prev_holder != distholder) {
        prev_holder.style.display='none';
    }
    eval("prev_holder" + ph + " = distholder;");
    if(document.getElementById('item_' + id) != null) {
      return false;
    }
  }
  temp_holder=distholder;
  if(prev_holder != null && prev_holder != distholder) {
	prev_holder.style.display ='none';
  }
  eval("prev_holder" + ph + " = distholder;");

  search_button.disabled=true;
  holder_status.innerHTML="Processing request...";
  var url="/se/dig/item/" + id + ".html";
  var data = "__search_form=" + tr_search_form.id + "&__offset=0&__search_func=la2_search&__ver=0.2";

  //sid="+Math.random();
  processing=1;
  xmlHttp=GetXmlHttpObject();
  xmlHttp.onreadystatechange=stateChanged;
//  xmlHttp.open("POST",url,true);
  xmlHttp.open("GET",url,true);
//  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//  xmlHttp.send(data);
  xmlHttp.send(null);
  return false;
}

function stateChanged() 
{ 
    if (xmlHttp.readyState==1) {
	    processing=1;
	    holder_status.innerHTML='<span style="background: #aaa;color: #000">Loading data...</span>';
    }
    else if (xmlHttp.readyState==2) {
	    processing=1;
	    holder_status.innerHTML="Loaded";
    }
    else if (xmlHttp.readyState==3) {
	    processing=1;
	    holder_status.innerHTML="Prepearing data to show";
    }
    else if (xmlHttp.readyState==4) {
	if (xmlHttp.status==200) {
	    holder_status.innerHTML="Done";
	    if(temp_holder == null) {
		holder_result.innerHTML=xmlHttp.responseText;
	    } else {
		temp_holder.innerHTML=xmlHttp.responseText;
		temp_holder=null;
	    }
	    if(!holders) {
		holder_status=null; 
		temp_holder=null;
		holders = holders_init();
	    }
	} else {
	    holder_status.innerHTML="Error: " + xmlHttp.statusText;
	    if(xmlHttp.statusText=='Unknown') {
		holder_status.innerHTML="";
	    }
	}
	if(search_button != null) search_button.disabled=false;
	temp_holder=null;
	processing=0;

    }
  return false;
}

function GetXmlHttpObject()
{
  xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }

  if (xmlHttp==null) {
    holder_status.innerHTML="Your browser does not support AJAX!";
    search_form.style.display='none';
    return null;
  }
  return xmlHttp;
}

function holders_init() {
//init placeholders
  if( tr_search_form == null ) { 
    tr_search_form=document.getElementById("la2search_form");
  }
  if ( holder_result == null ) {
    if(document.getElementById('external_search_result')) {
	holder_result=document.getElementById('external_search_result'); 
    }
    else if(document.getElementById('search_result') != null) { 
	holder_result=document.getElementById('search_result'); 
    } else { 
	return alert("Please, put me back to my place."); 
    }
  }
  if ( holder_status == null ) { 
    if(document.getElementById('search_status') != null) { 
	holder_status=document.getElementById('search_status');
    } else {
	holder_status = holder_result;
    }
  }
  if ( search_button == null ) {
    if(document.getElementById('search_button') != null) {
	search_button = document.getElementById('search_button');
    } else {
	for(i=0; i<tr_search_form.elements.length; i++) {
	    if(tr_search_form.elements[i].type=="submit")	search_button = tr_search_form.elements[i];
	}
	if(search_button==null) search_button=document.createElement("button");
    }
  }

  if(autosearch == 0 && tr_search_form.autosearch.value != "") {
	tr_search_form.item_name.value=tr_search_form.autosearch.value;
	autosearch=1;
	processing=0;
	search_button.disabled=false;
        temp_holder=null;
	la2_search(0);
   }

  return true;
}

//var escapeOrig = window.escape;

function la2_escape(str)
{
    var ret = [];
    var trans = [];
    for (var i = 0x410; i <= 0x44F; i++) {
	trans[i] = i - 0x350; // А-Яа-я
    }
    trans[0x401] = 0xA8;    // Ё
    trans[0x451] = 0xB8;    // ё

  // Составляем массив кодов символов, попутно переводим кириллицу
  for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
    if (typeof trans[n] != 'undefined')
      n = trans[n];
    if (n <= 0xFF)
      ret.push(n);
  }
  ret=window.escape(String.fromCharCode.apply(null, ret));
  return ret.replace( new RegExp( "\\+", "g" ), "%2B" );
}


//addon

function clearSel(id) {
        var sel = document.getElementById(id);
	var y = 0;
        for(y=0; y < sel.length; y++ ) {
	    sel.options[y].selected = false; 
	}
//        document.getElementById(id).children().attr('selected', false);
	if(sel.onchange != null) { 
		la2_subselector(sel);
	}
    return false;
}


//
// (C) Trurl McByte
//

var gr_la2_br=true;

function gh_la2_lock(obj,event) {
  obj.onclick = function (e) { return gh_la2_close(obj,e); }
  obj.onmouseout=null;
  return false;
}
function gh_la2_close(obj,event) {
  obj.onclick = function (e) { return gh_la2_lock(obj,e); }
  obj.onmouseout = null; 
  if(obj.parentNode.lastChild.nodeName=="DIV") {
       obj.parentNode.lastChild.style.display='none';
	var m = gh_la2_getMouse(event);
	obj.parentNode.lastChild.style.left=m.X+16+"px";
	obj.parentNode.lastChild.style.top=m.Y+16+"px";
  }
  return false;
}

function gh_la2_showtipclick(obj,event,id,holder) {

  if(holder==null) holder=obj.nextSibling;
  if(holder.nodeName=="#text") holder=holder.nextSibling;
  holder=holder.firstChild.firstChild;

  if(holder.nodeName=="DIV" && holder.hasChildNodes()) {

    if(holder.firstChild.style.display=="block") {
  	holder.firstChild.style.display="none";
  	return false;
    } else {
 	holder.firstChild.style.display="block";
  	return false;
    }
  }

  var obj_pid = obj.parentNode;
  var lim = 0;
  var pos = new Object;  pos.X=0;  pos.Y=0;
  while(lim<30) {
      lim = lim + 1;
      if(obj_pid.style && (obj_pid.style.position == 'absolute' || obj_pid.style.position == 'relative')) { 
  	pos.X=parseInt(obj_pid.style.left);
  	pos.Y=parseInt(obj_pid.style.top);
   	break;
      }
      if(obj_pid.parentNode == document) break;
      obj_pid = obj_pid.parentNode;

  }
 

  gh_la2_getObjById (holder,event,id,pos);
  holder.firstChild.style.display="block";

// obj.onclick = function () { 
//	return gh_la2_hidetipclick(obj,holder); 
//  }
  return false;
}

function gh_la2_hidetipclick(obj,holder) {
	holder.style.display='none';
	obj.onclick = function() { holder.style.display='block';
		obj.onclick = function () { return gh_la2_hidetipclick(obj,holder);  }
		return false;
	}
	return false;
}

function gh_la2_close_seek(obj,e) {

  var obj_pid = obj.parentNode;
  var lim = 0;
  while(lim<30) {
      lim = lim + 1;
      if(obj_pid.style && (obj_pid.style.position == 'absolute' || obj_pid.style.position == 'relative')) { 
	obj_pid.style.display="none";
   	break;
      }
      if(obj_pid.parentNode == document) break;
      obj_pid = obj_pid.parentNode;

  }

}
function gh_la2_closetip(btn,id,e) {
	if(document.getElementById('holder_' + id)) {
		var holder = document.getElementById('holder_' + id);
		var obj = document.getElementById('row_item_' + id);
		return gh_la2_hidetipclick(obj,holder);
	} else {
		var holder = btn.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.firstChild;
//		alert(holder.innerHTML)
		gh_la2_close(holder,e);
	}
}


function gh_la2_showtip(obj,event) {
  var tm=null;
  if(obj.parentNode.lastChild.nodeName=="DIV") { 
    obj.parentNode.lastChild.style.display='block';
	var m = gh_la2_getMouse(event);
	obj.parentNode.lastChild.style.left=m.X+16+"px";
	obj.parentNode.lastChild.style.top=m.Y+16+"px";
  } else {
    gh_la2_getObj (obj,event);
  }
  obj.onmouseout = function (e) {
    tm=setTimeout(function(){
	if(obj.parentNode.lastChild.nodeName=='DIV') 
	    obj.parentNode.lastChild.style.display='none'; 
	obj.onclick = function (ev) { return gh_la2_close(obj,ev); }
	obj.onmouseout=null;
    },300);
	return false; 
  }
  obj.parentNode.lastChild.onmouseover = function (e) {
//	clearTimeout(tm);
  }
  obj.onclick = function(e) { return gh_la2_lock(obj,e); }
  return false;
}

function gh_la2_getObj(obj,event) {
  gr_la2_br = (document.all&&!window.opera);
  var result = gh_la2_createlayer(obj.parentNode,event);
  var x = gh_la2_getAtrax(obj,result,result);
  x.xmlHttp.open("GET",obj.href,true);
  x.xmlHttp.send(null);
}

function gh_la2_getObjById(obj,event,id,pos) {
  gr_la2_br = (document.all&&!window.opera);
  var result = gh_la2_createlayer(obj,event,pos);
  var x = gh_la2_getAtrax(obj,result,result);
  var href='/se/dig/item/' +id + '.html';
  x.xmlHttp.open("GET",href,true);
  x.xmlHttp.send(null);
}

function gh_la2_getAtrax(obj,holder_status,holder_result)
{
  var atrax = new Object;
  atrax.ajax=1;
  atrax.processing=0;
  atrax.holder_status = holder_status;
  atrax.holder_result = holder_result;
  atrax.xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    atrax.xmlHttp=new XMLHttpRequest();
    }
  catch (e) {
    // Internet Explorer
    try {
      atrax.xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e) {
      atrax.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }

  if (atrax.xmlHttp==null) {
    atrax.holder_status.innerHTML="Your browser does not support AJAX!";
    atrax.ajax=0;
  }
  atrax.xmlHttp.onreadystatechange=function () 
  { 
    if (atrax.xmlHttp.readyState==1) {
	    atrax.processing=1;
		atrax.holder_status.innerHTML = 'Loading data...';
    }
    else if (atrax.xmlHttp.readyState==2) {
	    atrax.processing=1;
	    atrax.holder_status.innerHTML="Loaded";
    }
    else if (atrax.xmlHttp.readyState==3) {
	    atrax.processing=1;
	    atrax.holder_status.innerHTML="Prepearing data to show";
    }
    else if (atrax.xmlHttp.readyState==4) {
	if (atrax.xmlHttp.status==200) {
	    atrax.holder_status.innerHTML="Done";
	    atrax.holder_result.innerHTML=atrax.xmlHttp.responseText;

	} else {
	    atrax.holder_status.innerHTML="Error: " + atrax.xmlHttp.statusText;
	}
	atrax.processing=0;
    }
    else {
      atrax.holder_status.innerHTML="Error: " + atrax.xmlHttp.statusText;
      atrax.holder_result.innerHTML=atrax.xmlHttp.responseText;
      obj.appendChild(atrax.holder_result);

	}
  }

  return atrax;
}

function gh_la2_getMouse(e) {
 var m = new Object;
 if(e==null) e = window.event;
 m.X=0;
 m.Y=0;
 if(document.all&&!window.opera) {
    m.X = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    m.Y = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
 } else {
    m.X = e.pageX;
    m.Y = e.pageY;
 }
// alert('X='+m.X+'|Y='+m.Y);
 return m;
}

function gh_la2_createlayer(obj,e,pos) {
var m = gh_la2_getMouse(e); 
var p = gh_la2_findPos(obj);

var tooltip = document.createElement("div");
 tooltip.style.position = "absolute";
 tooltip.style.zIndex = 0;
 tooltip.style.left =  'auto';// p.X + 16 + "px";
 tooltip.style.top = 'auto';//m.Y - p.Y/2 + 16 + "px";
 obj.appendChild(tooltip);
 tooltip.style.left = tooltip.offsetLeft + "px"; 
 tooltip.style.top = tooltip.offsetTop + "px"; 


// 	if(pos && pos.X) {
 //		tooltip.style.left= m.X - pos.X + 16 + "px";
 //		tooltip.style.top = m.Y - pos.Y + 16 + "px";
//	} else {
// 		tooltip.style.left=m.X+16+"px";
// 		tooltip.style.top=m.Y+16+"px";
//	}
 return tooltip;

}


function gh_la2_mover(header,tooltip) {
  header.style.cursor="move";
  header.onmousedown = function (e) {
    var m = gh_la2_getMouse(e);
    tooltip.style.zIndex = parseInt(tooltip.style.zIndex) + 1;
    var dragXoffset = m.X - parseInt(tooltip.style.left);
    var dragYoffset = m.Y - parseInt(tooltip.style.top);
    document.onmouseup = function () { 
	document.onmousemove=null;
	document.onmouseup=null;
        tooltip.style.zIndex = parseInt(tooltip.style.zIndex) - 1;
    }
    document.onmousemove = function (em) {
	var m = gh_la2_getMouse(em); 
	tooltip.style.left = m.X - dragXoffset + 'px';
	tooltip.style.top = m.Y - dragYoffset + 'px';
	return false;
    }
    return false;
  }	
}

function gh_la2_findPos(obj) {
        var lobj = obj;
	var m = new Object;
	m.X = 0;
	m.Y = 0;
	if (lobj.offsetParent) {
		m.X = lobj.offsetLeft;
		m.Y = lobj.offsetTop;
		while (lobj = lobj.offsetParent) {
			m.X = m.X + lobj.offsetLeft;
			m.Y = m.Y + lobj.offsetTop;
		}
	}
	return m;
}
////
