var prev_link = "prev";
var next_link = "next";
var page = 1;
var box = "box_";
var slide;

function $(id) {
  return document.getElementById(id);
}

function fade(n, vector) {
  var s = -0.1;
  var o = 1;
  
  var t = setInterval ( function() {    
    o += s;
    $(box + n).style.opacity = o;
    $(box + n).style.filter = 'alpha(opacity=' + o*100 + ')';

    if (o <= 0) { 
      $(box + n).style.visibility = "hidden";
      if (noabs) $(box + n).style.position = "absolute";
      n += vector;      
      if (n < 1) { var i = 1; while ($(box + i)) i++; n = i - 1; }
      if (!$(box + n)) n = 1;
      page = n;
      if (noabs) $(box + n).style.position = "static";
      $(box + n).style.visibility = "visible";    
      s*= -1; 
    }
    if (o >= 1) { $(box + n).style.filter = ""; clearInterval(t); }
  }, 30 );
}

function move(to) {
  if (!$(box + to)) return false;
  page = to;
  var b = $(box + to);
  
  var desired = 460 - (b.width / 2);
  var current = b.offsetLeft;
  var shift = -(current - desired);
  
  var end = slide + shift;
  
  var dir = 20;
  if (shift < 0) dir *= -1;
  
  var t = setInterval ( function() {
    slide += dir;
    $(box + "1").style.marginLeft = slide + "px";
    if (dir < 0) {
      if ((slide + dir) <= end) {
        clearInterval(t);
        $(box + "1").style.marginLeft = end + "px";
        slide = end;
      }
    } else {
      if ((slide + dir) >= end) {
        clearInterval(t);
        $(box + "1").style.marginLeft = end + "px";
        slide = end;
      }
    }
  }, 20);
  
}


function prev() { 
  if (noabs == false) {
    fade(page, -1);
   } else {
    move(page - 1);
   } 
}

function next() { 
  if (noabs == false) {
    fade(page, 1);
   } else {
    move(page + 1);
   } 
}

function winDim () {
    if( typeof( parent.window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = parent.window.innerWidth;
    myHeight = parent.window.innerHeight;
  } else if( parent.document.documentElement && ( parent.document.documentElement.clientWidth || parent.document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = parent.document.documentElement.clientWidth;
    myHeight = parent.document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = parent.document.body.clientWidth;
    myHeight = parent.document.body.clientHeight;
  }
  return new Array(myWidth, myHeight);
}

if (noabs == false)
  window.onresize = function () {
    var d = winDim();
    if (d[0] <= 1100) { $(prev_link).style.left = 0 } else { $(prev_link).style.left = "-40px"; }
    if (d[0] <= 1100) { $(next_link).style.right = 0 } else { $(next_link).style.right = "-40px"; }
  }



var i = 2;
var d = winDim();

$("prev").onclick = function() { prev(); return false; }
$("next").onclick = function() { next(); return false; }

if (noabs == true) {  
  var pd = parent.document;
  pd.getElementById('iframe').width = 982;
  pd.getElementById('iframe').height = 602;
  pd.getElementById('iframe').style.left = ((d[0] - 980) / 2) + "px";
  pd.getElementById('iframe').style.top = ((d[1] - 580) / 2) + "px";

  $("exit").onclick = function () { pd.getElementById('iframe').style.display = pd.getElementById('blind').style.display = "none"; return false; }

  $("print").onclick = function () {
    $(box + "1").style.marginLeft = 0;
    window.print();    
    setTimeout(function() { $(box + "1").style.marginLeft = slide + "px"; }, 200);
  }
  setTimeout(function () { slide = (460 - ($(box + "1").getAttribute("width") / 2)); $(box + "1").style.marginLeft = slide + "px"; }, 1);  
  pd.getElementById('iframe').style.display = "block";
} else {
  while ($(box + i)) {
    $(box + i).style.opacity = 0;
    $(box + i).style.position = "absolute";
    $(box + i).style.top = 0;
    $(box + i).style.left = 0;
    $(box + i).style.visibility = "hidden";
    ++i;  
    }
  var as = document.getElementsByTagName("a");
  for (i = 0; i < as.length; i++)
    if (as[i].rel) as[i].onclick = function () { $("iframe").src = this.href; $("blind").style.display = 'block'; $("prev").style.visibility = $("next").style.visibility = "hidden"; return false; }
  if (d[0] <= 1024) { $(prev_link).style.left = 0 } else { $(prev_link).style.left = "-40px"; }
  if (d[0] <= 1024) { $(next_link).style.right = 0 } else { $(next_link).style.right = "-40px"; }
  $("wrapper").onmouseover = function () { $("prev").style.visibility = $("next").style.visibility = "visible"; }
  $("header").onmouseover = function (e) { $("prev").style.visibility = $("next").style.visibility = "hidden"; }
  $("footer").onmouseover = function (e) { $("prev").style.visibility = $("next").style.visibility = "hidden"; }
}


