

// can't live without x(y)
function x(y){return document.getElementById(y)}

var ivL=[];
var flags = 3; // 1: Hide 18+, 2: Hide variations
var imageList=[];
var dbg = 0; // 0: normal, 1: 18+, 2: sim, 4: del

function setCookie() {
  var date = new Date();
  date.setTime(date.getTime()+(365*24*60*60*1000));
  document.cookie = "cgv_flags="+flags+"; expires="+date.toGMTString()+"; path=/";
  return readCookie() & 128;
}
function readCookie() {
  var nameEQ = "cgv_flags=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) {
      flags = c.substring(nameEQ.length,c.length);
      return flags;
    }
  }
  return flags | 128;
}

function updateSettings() {
  x('h18').innerHTML = flags & 1 ? 'ON' : 'OFF';
  x('h18').style.color = flags & 1 ? '#0F0' : '#F00';
  x('hsim').innerHTML = flags & 2 ? 'ON' : 'OFF';
  x('hsim').style.color = flags & 2 ? '#0F0' : '#F00';
}

function toggleFlag(f) {
  if(flags & f)
    flags -= f;
  else
    flags |= f;
  setCookie();
  updateSettings();
  ivClose();
  showImages();
}


function showImages() {
  if(!imageList || imageList.length == 0)
    return;
  var r='';
  ivL = [];
  for(var i=0; i<imageList.length; i++) {
    if(!dbg && imageList[i].flags && imageList[i].flags & flags)
      continue;
    var w = Math.floor(imageList[i].td.split('x')[0]);
    var h = Math.floor(imageList[i].td.split('x')[1]);
    r += '<a href="'+imageList[i].f+'" onclick="return '+(dbg ? 'debug_toggle' : 'ivView')+'(this)">'
        +'<img src="thumbs/'+imageList[i].f+'" width="'+w+'" height="'+h+'"'
        +(dbg && imageList[i].flags && imageList[i].flags & dbg ? ' class="sel_'+(dbg==1?'h18':dbg==2?'sim':'del')+'"' : '')
        +'></a>';

    var w = Math.floor(imageList[i].fd.split('x')[0]);
    var h = Math.floor(imageList[i].fd.split('x')[1]);
    ivL[ivL.length] = { f: imageList[i].f, w: w, h: h, i: i };
  }
  x('imgv').innerHTML = r;
  x('imgcount').innerHTML = ivL.length;
}


function ivView(what) {
  what = what && what.href ? what : this;
  var u=what.href;
  var w=0;var h=0;
  d = x('ivview');
  u = u.substring(u.length-8, u.length); // xxx.jpg

 // fix prev/next links (if any)
  for(var i=0;i<ivL.length;i++)
    if(ivL[i].f == u) {
      w = ivL[i].w;
      h = ivL[i].h;
      x('ivnext').style.visibility = ivL[i+1] ? 'visible' : 'hidden';
      x('ivnext').href = ivL[i+1] ? ivL[i+1].f : '#';
      x('ivprev').style.visibility = ivL[i-1] ? 'visible' : 'hidden';
      x('ivprev').href = ivL[i-1] ? ivL[i-1].f : '#';
    }

 // calculate dimensions
  var ww = typeof(window.innerWidth) == 'number' ? window.innerWidth : document.documentElement.clientWidth;
  var wh = typeof(window.innerHeight) == 'number' ? window.innerHeight : document.documentElement.clientHeight;
  var st = typeof(window.pageYOffset) == 'number' ? window.pageYOffset : document.body && document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
  if(w+100 > ww || h+70 > wh) {
    x('ivfull').href = u;
    x('ivfull').innerHTML = w+'x'+h;
    x('ivfull').style.visibility = 'visible';
    if(w/h > ww/wh) { // width++
      h *= (ww-100)/w;
      w = ww-100;
    } else { // height++
      w *= (wh-70)/h;
      h = wh-70;
    }
  } else
    x('ivfull').style.visibility = 'hidden';
  var dw = w;
  var dh = h+20;
  dw = dw < 200 ? 200 : dw;

 // update document
  d.style.display = 'block';
  x('ivimg').innerHTML = '<img src="'+u+'" onclick="ivClose()" onload="document.getElementById(\'ivimgload\').style.top=\'-400px\'" style="width: '+w+'px; height: '+h+'px" />';
  d.style.width = dw+'px';
  d.style.height = dh+'px';
  d.style.left = ((ww - dw) / 2 - 10)+'px';
  d.style.top = ((wh - dh) / 2 + st - 20)+'px';
  x('ivimgload').style.left = ((ww - 100) / 2 - 10)+'px';
  x('ivimgload').style.top = ((wh - 20) / 2 + st)+'px';
  return false;
}

function ivClose() {
  if(x('ivview')) {
    x('ivview').style.display = 'none';
    x('ivview').style.top = '-5000px';
    if(x('ivimgload'))
      x('ivimgload').style.top = '-400px';
    x('ivimg').innerHTML = '';
  }
  return false;
}


function DOMLoad(y){var d=0;var f=function(){if(d++)return;y()};
  if(document.addEventListener)document.addEventListener("DOMCont"
  +"entLoaded",f,false);document.write("<script id=_ie defer src="
  +"javascript:void(0)><\/script>");document.getElementById('_ie')
  .onreadystatechange=function(){if(this.readyState=="complete")f()
  };if(/WebKit/i.test(navigator.userAgent))var t=setInterval(
  function(){if(/loaded|complete/.test(document.readyState)){
  clearInterval(t);f()}},10);window.onload=f;
}

DOMLoad(function() {
  if(!x('menubar')) {
    var d = document.createElement('div');
    d.innerHTML = '<table id="menubar"><tr>'
    +' <td id="settings">Settings</td>'
    +' <td id="h18l">Hide 18+:</td>'
    +' <td id="h18">ON</td>'
    +' <td id="hsiml">Hide similar:</td>'
    +' <td id="hsim">OFF</td>'
    +'</tr>';
    document.body.insertBefore(d, document.body.childNodes[0]);
  }
  readCookie();
  updateSettings();
  x('h18').onclick  = x('h18l').onclick  = function() { toggleFlag(1) };
  x('hsim').onclick = x('hsiml').onclick = function() { toggleFlag(2) };
  if(x('imgv')) {
    var d = document.createElement('div');
    d.id = 'ivview';
    d.innerHTML = '<b id="ivimg"></b><br />'
      +'<a href="#" id="ivfull">&nbsp;</a>'
      +'<a href="#" onclick="return ivClose()" id="ivclose">close</a>'
      +'<a href="#" onclick="return ivView(this)" id="ivprev">&lt;- previous</a>'
      +'<a href="#" onclick="return ivView(this)" id="ivnext">next -&gt;</a>';
    document.body.appendChild(d);
    d = document.createElement('b');
    d.id = 'ivimgload';
    d.innerHTML = 'Loading...';
    document.body.appendChild(d);
    showImages();
  }
});


function debug() {
  flags = 0;
  var e = document.createElement('div');
  e.id = 'debuginfo';
  document.body.appendChild(e);
  x('debuginfo').style.display = 'block';
  debug_upd();
  updateSettings();
  showImages();
}

function debug_upd() {
  var r = (!dbg ? 'normal' : '<a href="javascript:dbg=0;showImages();debug_upd()">normal</a>')
  +' | '+(dbg == 1 ? '18+' : '<a href="javascript:dbg=1;showImages();debug_upd()">18+</a>')
  +' | '+(dbg == 2 ? 'sim' : '<a href="javascript:dbg=2;showImages();debug_upd()">sim</a>')
  +' | '+(dbg == 4 ? 'del' : '<a href="javascript:dbg=4;showImages();debug_upd()">del</a>')
  +' ------- select <a href="javascript:debug_sel(1)">all</a> | <a href="javascript:debug_sel(0)">none</a>'
  x('dbglinks').innerHTML = r;

  var i;  r = '';
  for(i=0; i<imageList.length; i++)
    r += (r?' ':'')+imageList[i].f+','+imageList[i].flags;
  x('updimgs').value = r;
}

function debug_toggle(what) {
  what = what && what.href ? what : this;
  var u = what.href;
  u = u.substring(u.length-8, u.length); // xxxx.jpg
  var img = what.getElementsByTagName('img')[0];
  var gj=0;
  for(var i=0;i<ivL.length;i++)
    if(ivL[i].f == u)
      gi = ivL[i].i;

  if(imageList[gi].flags & dbg) {
    imageList[gi].flags -= dbg;
    img.className = '';
  }
  else {
    imageList[gi].flags += dbg;
    img.className = dbg == 1 ? 'sel_h18' : dbg == 2 ? 'sel_sim' : 'sel_del';
  }

  debug_upd();
  return false;
}

function debug_sel(on) {
  for(var i=0;i<imageList.length;i++) {
    if(on && !(imageList[i].flags & dbg))
      imageList[i].flags += dbg;
    if(!on && imageList[i].flags & dbg)
      imageList[i].flags -= dbg;
  }
  showImages();
  debug_upd();
}

