//утф-8
//show element by id
function show(id){
  var o = document.getElementById(id);
  if (o) o.style.display="";
}
function hide(id){
  var o = document.getElementById(id);
  if (o) o.style.display="none";
}

//function UtopiaPrint() {
//  document.write("<option>" + this.name + "</option>");
//}

function UtopiaCreate(str) {
  var arrayOfStrings = str.split('|');
  //this.print = UtopiaPrint;
  //if (isNaN(parseInt(xxx))) xxx = 0; else xxx = parseInt(xxx);
  this.id = arrayOfStrings[0];
  this.name = arrayOfStrings[1];
  this.year = parseInt(arrayOfStrings[2]);
  this.month = parseInt(arrayOfStrings[3]);
  this.day = parseInt(arrayOfStrings[4]);
  var parcount = (arrayOfStrings.length-5)/2
  this.parameters = new Array(parcount); //!
  this.orders = new Array(parcount); //!
  var j=0
  for (var i=5; i<arrayOfStrings.length-1; i=i+2) {
    this.parameters[j] = parseFloat(arrayOfStrings[i]); //!
    this.orders[j] = parseFloat(arrayOfStrings[i+1]); //!
    j=j+1;
  }
}

function createUtopiaArray() {
  for (var i=0; i<utopiaStringArray.length; i++) {
    utopiaArray[i] = new UtopiaCreate(utopiaStringArray[i]);
    //utopiaArrayTimeSorted[i] = new UtopiaCreate(utopiaStringArray[i]);
    utopiaArrayTimeSorted[i] = utopiaArray[i];
  }
  utopiaArrayTimeSorted.sort(SortUtopiaByTime);
  //for (var i=0; i<utopiaArrayTimeSorted.length; i++) alert(utopiaArrayTimeSorted[i].orders[indexOfTimeParameter] +' / ' +utopiaArrayTimeSorted[i].parameters[indexOfTimeParameter] +' / ' +utopiaArrayTimeSorted[i].name)
  //for (var i=0; i<utopiaArray.length; i++) alert(utopiaArray[i].name);
  //for (var i=0; i<utopiaStringArray.length; i++) alert(utopiaStringArray[i]);
  blink_comment();
}

function SortUtopiaByTime (a, b) {
  var x = a.orders[indexOfTimeParameter];
  var y = b.orders[indexOfTimeParameter];
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
//function printUtopiaArray() {
//  for (var i=0; i<utopiaArray.length; i++) {
//    utopiaArray[i].print();
//  }
//}
function makeUtopiaOptions() {
  var utopiaSelection = f.utopiaSelection;
  for (var i=0; i<utopiaArray.length; i++) {
    var o = new Option();
    o.text = utopiaArray[i].name;
    o.value = utopiaArray[i].id;
    utopiaSelection.options[utopiaSelection.options.length] = o;
  }
}


function utopiaChange(index, blink) {
  if (index > -1){
    if (index == lastUtopia)
      return;
    var utopia = utopiaArray[index];
    if (utopia){
      for (var i=0; i<utopia.parameters.length; i++) {
        setScore(i, utopia.parameters[i], utopia.orders[i]+1, index);
      }
      var img  = document.getElementById("i" + index); if (img) img.style.display="block";
      var desc = document.getElementById("d" + index); if (desc) desc.style.display="block";
      if (lastUtopia != -1) {
        var img  = document.getElementById("i" + lastUtopia); if (img) img.style.display="none";
        var desc = document.getElementById("d" + lastUtopia); if (desc) desc.style.display="none";
        if(blink) blink_comment();
      }
      lastUtopia = index;
    }
  }
}

function utopiaChangeByNoteId(note_id) {
  var foundUtopiaIndex = 0;
  if (!isNaN(parseInt(note_id))){
    for (var i=1; i<utopiaArray.length; i++) {
      if (utopiaArray[i].id == note_id) foundUtopiaIndex = i;
    }
  }
  f.utopiaSelection.selectedIndex = foundUtopiaIndex;
  utopiaChange(foundUtopiaIndex, false);
}

function searchForUtopia(index, score) {
  var minDelta = 100;
  var foundUtopiaIndex = -1;
  var found_id = -1
  var blink = false;
  //var oldscore = utopiaArray[f.utopiaSelection.selectedIndex].parameters[index];
  //var direction = (oldscore < score)?'less':'more';
  if (index==indexOfTimeParameter){
    for (var i=0; i<utopiaArrayTimeSorted.length; i++) {
      var delta = Math.abs(utopiaArrayTimeSorted[i].parameters[index] - score);
      if (minDelta > delta) {
        found_id = utopiaArrayTimeSorted[i].id;
        minDelta = delta;
      }
    }
    if (found_id>-1) {
      for (var i=0; i<utopiaArray.length; i++) {
        if (found_id == utopiaArray[i].id) foundUtopiaIndex = i;
      }
    }
    //alert(foundUtopiaIndex);
  }else{
    for (var i=0; i<utopiaArray.length; i++) {
      var delta = Math.abs(utopiaArray[i].parameters[index] - score);
      if (minDelta > delta) {
        foundUtopiaIndex = i;
        minDelta = delta;
      }
    }
  }
  f.utopiaSelection.selectedIndex = foundUtopiaIndex;
  if (foundUtopiaIndex>-1 && lastUtopia>-1 && foundUtopiaIndex!=lastUtopia){
    var last_order = utopiaArray[lastUtopia].orders[index];
    var found_order = utopiaArray[foundUtopiaIndex].orders[index];
    blink = (Math.abs(last_order - found_order)>1)?true:false;
    //if (blink) alert(blink);
  }
  utopiaChange(foundUtopiaIndex,blink);
}

function searchForLessMoreUtopia(index,direction) {
  var minDelta = 100;
  var foundUtopiaIndex = -1;
  var currentUtopiaIndex = f.utopiaSelection.selectedIndex;
  var score = utopiaArray[currentUtopiaIndex].parameters[index];
  var order = utopiaArray[currentUtopiaIndex].orders[index];
  var new_order = -1;
  if (direction == 'more') {
    if (order<utopiaCount-1) new_order = order +1;
  }else{
    if (order>0) new_order = order -1;
  }
  if (new_order>-1) {
    for (var i=0; i<utopiaArray.length; i++) if (utopiaArray[i].orders[index] == new_order) foundUtopiaIndex = i;
  }
  //alert(order +' / ' +foundUtopiaIndex);
  /*
  foundArr = new Array();
  //nejdriv zkusim najit objekty se stejnym scrore, ukladam si do pole jejich indexy v utopiaArray
  for (var i=0; i<utopiaArray.length; i++) if (utopiaArray[i].parameters[index] == score) foundArr.push(i);
  if (direction == 'more') {
    if (foundArr.length > 1){
      //najdu aktualni objekt a vyberu jeho naslednika
      for (var i=0; i<foundArr.length-1; i++) {
        if (foundArr[i] == currentUtopiaIndex) foundUtopiaIndex = foundArr[i+1];
      }
    }
    //pokud jsem nenasel objekt se stejnym skore ktery se nachazi abecedne za aktualnim objektem,
    //pokusim se najit prvni objekt s vetsim skore a zaroven nejmensim rodilem ve skore
    if (foundUtopiaIndex == -1) {
      for (var i=0; i<utopiaArray.length; i++) {
        var delta = utopiaArray[i].parameters[index] - score;
        if (minDelta > delta && delta > 0) {
          foundUtopiaIndex = i;
          minDelta = delta;
        }
      }
    }
  }else{
    if (foundArr.length > 1){
      //najdu aktualni objekt a vyberu jeho predchudce
      for (var i=1; i<foundArr.length; i++) {
        if (foundArr[i] == currentUtopiaIndex) foundUtopiaIndex = foundArr[i-1];
      }
    }
    //pokud jsem nenasel objekt se stejnym skore ktery se nachazi abecedne pred aktualnim objektem,
    //pokusim se najit prvni objekt s mensim skore a zaroven nejmensim rodilem ve skore
    if (foundUtopiaIndex == -1) {
      for (var i=utopiaArray.length-1; i>=0; i--) {
        var delta = score - utopiaArray[i].parameters[index];
        if (minDelta > delta && delta > 0) {
          foundUtopiaIndex = i;
          minDelta = delta;
        }
      }
    }
  }
  */
  if (foundUtopiaIndex > -1) {
    f.utopiaSelection.selectedIndex = foundUtopiaIndex;
    utopiaChange(foundUtopiaIndex,false);
  }
  return false;
}

//////////
function setWidth(index, newWidth, drag, newScore, newOrder, actualUtopia) {
  if (!drag) {
    var par  = document.getElementById("par" + index); 
    if (par) par.style.width = newWidth +'px';
    var val  = document.getElementById("val" + index); 
    if (val) {
      var strVal = '';
      if(index==indexOfTimeParameter){ //time parameter - 9
        var utopia = utopiaArray[actualUtopia ];
        if (utopia){
          strVal = '[' 
          if (isNaN(utopia.year)) {
            strVal = strVal +'- - -';
          }else{
            strVal = strVal +utopia.year;
            if (utopia.month > 0) strVal = strVal +'/' +utopia.month;
            if (utopia.day > 0) strVal = strVal +'/' +utopia.day;
          }
          //strVal = strVal +' |' +newScore +']';
          strVal = strVal +']';
        }
      }else{
        strVal = '[' +newScore.toFixed(2) +']';
      }
      val.innerHTML = strVal;
    }
    var ord  = document.getElementById("ord" + index); 
    if (ord) ord.innerHTML = '[' +newOrder +utopiaOf +utopiaCount +']';
    //if (val) alert(val.innerText);
    
  }
  var slider  = document.getElementById("slider" + index); 
  if (slider) slider.style.left = (newWidth + sliderFinalOffsetLeft)+'px';
}

function setScore(index, newScore, newOrder, actualUtopia) {
  newWidth = newScore / 10 * parWidth;
  //if (index==indexOfTimeParameter) alert(newWidth);
  setWidth(index, newWidth,false,newScore, newOrder, actualUtopia);
}

function dragFunc(obj,evnt) {
  if (dragState != -1) {
    //txt.innerHTML = event.x;
    if (document.all) {
      var newWidth = parseInt(evnt.x) - parFinalOffsetLeft;
    }else{
      var stranka  = document.getElementById("stranka"); 
      var strankaOffsetLeft = 0;
      if (stranka) strankaOffsetLeft = parseInt(stranka.offsetLeft);
      var newWidth = parseInt(evnt.pageX) - parFinalOffsetLeft - strankaOffsetLeft;
    }
    newWidth = Math.max(newWidth, 0);
    newWidth = Math.min(newWidth, parWidth);
    setWidth(dragState, newWidth, true);
    searchForUtopia(dragState, newWidth / parWidth * 10);
  }
}

function dragOn(num) {
  var parGlobal  = document.getElementById("par" + num + "Global"); 
  if (parGlobal) {
    oldStyle = parGlobal.style.cursor;
    parGlobal.style.cursor = "col-resize";
  }
  dragState = num;
}

function dragOff() {
  if (dragState == -1)
    return;
  var parGlobal  = document.getElementById("par" + dragState + "Global"); 
  if (parGlobal) {
    parGlobal.style.cursor = oldStyle;
  }
  dragState = -1;
}

function on_change_keyword_id() {
  if (f.keyword_id.value){
    f.fulltext_an.value = '';
  }
  f.submit();
}

function new_search() {
  f.fulltext_an.value = '';
  f.keyword_id.value = '';
  f.submit();
}

function change_L() {
  f.L.value = (f.L.value == 'EN')?'FR':'EN';
  f.submit();
  return false;
}

function blink_comment() {
  var o = document.getElementById("found-comment"); 
  if (o) {
    if (blink_count<6){
      o.style.backgroundColor = (o.style.backgroundColor=='')?'#A2B8BE':'';
      blink_count++;
      setTimeout("blink_comment()",300)
    }else{
      blink_count = 0;
      o.style.backgroundColor = '';
    } 
  }
}
