function MarkElement(Element){
  WIdth=Element.offsetWidth;
  Height=Element.offsetHeight;
  Element.className='js_switchs';
  Element.innerHTML= Element.innerHTML+'<span style="position:absolute;"><img style="position:relative;left:-'+(3+WIdth)+'px;top:'+(Height+2)+'px;" alt="" src="/static/img/fb/letter_here.gif" width="'+(WIdth+6)+'" height="10" border="0"/></span>';
}
function MarkElementInSelector(Element){
  WIdth=Element.offsetWidth;
  Height=Element.offsetHeight;
  Element.className='js_switchs';
  Element.innerHTML= Element.innerHTML+'<span style="position:absolute;"><img style="position:relative;left:-'+(WIdth-1)+'px;top:'+(Height+2)+'px;" alt="" src="/static/img/fb/letter_here.gif" width="'+(WIdth+2)+'" height="10" border="0"/></span>';
}

function OpenTable(){
  if (screen.width > 800){
    document.write('<table border="0" cellpadding="0" cellspacing="0"><tr><td><img alt="" src="/static/img/z.gif" width="430" height="1" border="0"/><br/>');
  }
}
function CloseTable(){
  if (screen.width > 800){
    document.write('</td></tr></table>');
  }
}

function SplitAuthors(AuthorsList,Prefix,LetterN,MaxItems){
  var LettersGroup={};
  var Keys=new Array();
  for (i=0;i < AuthorsList.length;i++){
    var AuthName=document.getElementById('a_'+AuthorsList[i]).innerHTML.charAt(LetterN).replace(/[.,;"'``–\-\(\)\*\^:;]/,'');
		AuthName=AuthName?AuthName:'а';
    if (!LettersGroup[AuthName]){
      LettersGroup[AuthName] = new Array();
      Keys.push(AuthName);
    }
    LettersGroup[AuthName].push(AuthorsList[i]);
  }

  var FinalKeys = new Array();
  for (i=0;i < Keys.length;i++){

    var KeyAuthors=LettersGroup[Keys[i]];
    var ThisKeyAuthors=KeyAuthors.length;
    var NextKeyAuthors=Keys[i+1]?LettersGroup[Keys[i+1]].length:0;
    var Key1=Keys[i].replace(/[.,;"'``–\-\(\)\*\^:;]/,'').toLowerCase();

    var Title=Prefix+Key1;
    while (NextKeyAuthors > 0 && (ThisKeyAuthors + NextKeyAuthors <= MaxAuthorsInList || ThisKeyAuthors < 4 || NextKeyAuthors < 4)){
      i++;
      Title=Prefix+Key1+'–'+Prefix+Keys[i].replace(/[.,;"'``–\-\(\)\*\^:;]/,'').toLowerCase();
      for (i1=0;i1 < LettersGroup[Keys[i]].length;i1++){
        KeyAuthors.push(LettersGroup[Keys[i]][i1]);
      }
      KeyName2=Keys[i];
      ThisKeyAuthors=KeyAuthors.length;
      NextKeyAuthors=Keys[i+1]?LettersGroup[Keys[i+1]].length:0;
    }
    FinalKeys.push({title:Title.replace(/[аa]+–/i,'–'),items:KeyAuthors});
  }
  var i=0;
  while (i < FinalKeys.length){
    if (FinalKeys[i].items.length > SplitUpon){
      var ItemToSplit=FinalKeys[i];
      FinalKeys.splice(i,1);
      var NewKeys=SplitAuthors(ItemToSplit.items,Prefix+ItemToSplit.title.charAt(LetterN),LetterN+1,MaxAuthorsInList);
      for (i1=0;i1 < NewKeys.length;i1++){
        FinalKeys.splice(i,0,NewKeys[i1]);
        i++;
      }
    } else {
      i++;
    }
  }
  return FinalKeys;
}

function RewriteLetters(Selected){
  var HeadHTML='';
  for (i=0;i < AllKeys.length;i++){
    HeadHTML+='<nobr><a href="javascript:SwitchAuthLetter('+i+')" class="js_switch" id="swither_'+i+'">'+AllKeys[i].title+'</a>&#160;&#160;</nobr> ';
  }
  LoadingDiv.innerHTML=HeadHTML+'<br/><br/>';
  MarkElementInSelector(document.getElementById('swither_'+Selected));
}

var PrevLetter;
function SwitchAuthLetter(Selected){
  if (PrevLetter){
    ShowHideSet(PrevLetter-1,'none');
  }
  ShowHideSet(Selected,'block');

  RewriteLetters(Selected);
  PrevLetter=Selected+1;
}

function ShowHideSet(SetID,Display){
  for (i=0;i < AllKeys[SetID].items.length;i++){
    var AUthDiv=document.getElementById('auth_'+AllKeys[SetID].items[i])
    AUthDiv.style.display=Display;
    if (screen.width > 800 && AUthDiv.className != 'auth_float'){
      AUthDiv.className='auth_float';
    }
  }
}

function ShowPopupNote(ID){
	HNote=document.getElementById(ID);
	if (!HNote){alert('Note body is missing!');return;}
	HNote.style.display=HNote.style.display == "block"?'none':'block';
}
