var winHeight = 0;
var winWidth = 0;
var contentLeft = 0;
var contentTop = 0;
var jw_left = 347;
var jw_top = 60;
var jw_width = 350;
var jw_height = 320;
var jw2_left = 26;
var jw2_top = 63;
var jw2_width = 120;
var jw2_height = 160;

// CALCULATE

function calculateWindow() {
  winHeight = (document.all) ? document.body.clientHeight: window.innerHeight;
  winWidth = (document.all) ? document.body.clientWidth : window.innerWidth;
  contentLeft = Math.round((winWidth - 751)/2);
  contentTop = Math.round((winHeight - 441)/2);
  jw_left = contentLeft + 347;
  jw_top = contentTop + 60;
  jw2_left = contentLeft + 26;
  jw2_top = contentTop + 63;
}

// INIT

function init() {
 
  layerSetBgColor("bgDiv","#07152B");
  layerSetX("bgDiv",contentLeft);
  layerSetY("bgDiv",contentTop);
  layerSetX("metanavDiv",contentLeft + 33);
  layerSetY("metanavDiv",contentTop + 320);
  layerSetX("mainnavtopDiv",contentLeft + 297);
  layerSetY("mainnavtopDiv",contentTop + 7);
  layerSetX("mainnavbottomDiv",contentLeft + 297);
  layerSetY("mainnavbottomDiv",contentTop + 417);
  layerSetX("subnavDiv",contentLeft + 185);
  layerSetY("subnavDiv",contentTop + 99);
  
  if (layerExists("nansenDiv")) {
    layerSetX("nansenDiv",contentLeft + 7);
    layerSetY("nansenDiv",contentTop + 7);
  }  
  if (layerExists("hpDiv")) {
    layerSetX("hpDiv",contentLeft + 300);
    layerSetY("hpDiv",contentTop + 260);
  } 
  if (layerExists("langDiv")) {
    layerSetX("langDiv",contentLeft + 215);
    layerSetY("langDiv",contentTop + 380);
  }
  if (layerExists("windowDiv")) {
    if (is.ie) document.onmousewheel = shiftUpDown;
    layerSetX("scrollupDiv",jw_left + jw_width + 20);
    layerSetY("scrollupDiv",jw_top);
    layerSetX("scrolldownDiv",jw_left + jw_width + 20);
    layerSetY("scrolldownDiv",jw_top + jw_height - 20);
    layerSetX("windowDiv",jw_left);
    layerSetY("windowDiv",jw_top);
    layerSetSize("windowDiv",jw_width,jw_height);
    layerSetX("contentDiv",0,"windowDiv");
    layerSetY("contentDiv",0,"windowDiv");
    if (layerGetContentHeight('contentDiv','windowDiv') > jw_height) {
      layerShow("scrollupDiv");
      layerShow("scrolldownDiv");
    } else {
      layerHide("scrollupDiv");
      layerHide("scrolldownDiv");
    }
    layerShow("windowDiv");
    layerShow("contentDiv","windowDiv");
  }  
  if (layerExists("window2Div")) {
    layerSetX("scrollup2Div",jw2_left + 55);
    layerSetY("scrollup2Div",jw2_top - 38);
    layerSetX("scrolldown2Div",jw2_left + 55);
    layerSetY("scrolldown2Div",jw2_top + jw2_height + 22);
    layerSetX("window2Div",jw2_left);
    layerSetY("window2Div",jw2_top);
    layerSetSize("window2Div",jw2_width,jw2_height);
    layerSetX("content2Div",0,"window2Div");
    layerSetY("content2Div",0,"window2Div");
    if (layerGetContentHeight('content2Div','window2Div') > jw2_height) {
      layerShow("scrollup2Div");
      layerShow("scrolldown2Div");
    } else {
      layerHide("scrollup2Div");
      layerHide("scrolldown2Div");
    }
    layerShow("window2Div");
    layerShow("content2Div","window2Div");
  }
  
  onresize=function() { window.location.reload()}
}

// MISC FUNCTIONS

function photoPopup(page) {
  photoPop = window.open(page,"photo_popup",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=650,height=550');
  photoPop.focus()
}

// JS WINDOW

function shiftUpDown() {
  if (event.wheelDelta/120 < 0) {
    pageDown()
  } else {
    pageUp();
  }
  return false;
}

var scroll_move = false;
var scroll_up = false;
var scroll_down = false;

function scrollUp() {
  if (scroll_up && (layerGetContentHeight('contentDiv','windowDiv') > jw_height)) {
    if (layerGetY('contentDiv','windowDiv') > -2) {
      layerSetY('contentDiv',0,'windowDiv');
      scroll_up = false;
    } else {
      layerSetY('contentDiv',(layerGetY('contentDiv','windowDiv') + 2),'windowDiv');
      setTimeout("scrollUp()", 15);
    }
  }
}

function scrollDown() {
  if (scroll_down && (layerGetContentHeight('contentDiv','windowDiv') > jw_height)) {
    if (layerGetY('contentDiv','windowDiv') < (-layerGetContentHeight('contentDiv','windowDiv')+jw_height+2)) {
      layerSetY('contentDiv',-layerGetContentHeight('contentDiv','windowDiv')+jw_height,'windowDiv');
      scroll_down = false;
    } else {
      layerSetY('contentDiv',(layerGetY('contentDiv','windowDiv') - 2),'windowDiv');
      setTimeout("scrollDown()", 15);
    }
  }
}

function pageUp() {
  if (layerGetContentHeight('contentDiv','windowDiv') > jw_height) {
    if (layerGetY('contentDiv','windowDiv') > -20) {
      layerSetY('contentDiv',0,'windowDiv');
      scroll_up = false;
    } else {
      layerSetY('contentDiv',(layerGetY('contentDiv','windowDiv') + 20),'windowDiv');
    }
  }
}

function pageDown() {
  if (layerGetContentHeight('contentDiv','windowDiv') > jw_height) {
    if (layerGetY('contentDiv','windowDiv') < (-layerGetContentHeight('contentDiv','windowDiv')+jw_height+20)) {
      layerSetY('contentDiv',-layerGetContentHeight('contentDiv','windowDiv')+jw_height,'windowDiv');
      scroll_down = false;
    } else {
      layerSetY('contentDiv',(layerGetY('contentDiv','windowDiv') - 20),'windowDiv');
    }
  }
}


// JS WINDOW 2

var scroll2_move = false;
var scroll2_up = false;
var scroll2_down = false;

function scroll2Up() {
  if (scroll2_up && (layerGetContentHeight('content2Div','window2Div') > jw2_height)) {
    if (layerGetY('content2Div','window2Div') > -2) {
      layerSetY('content2Div',0,'window2Div');
      scroll2_up = false;
    } else {
      layerSetY('content2Div',(layerGetY('content2Div','window2Div') + 2),'window2Div');
      setTimeout("scroll2Up()", 15);
    }
  }
}

function scroll2Down() {
  if (scroll2_down && (layerGetContentHeight('content2Div','window2Div') > jw2_height)) {
    if (layerGetY('content2Div','window2Div') < (-layerGetContentHeight('content2Div','window2Div')+jw2_height+2)) {
      layerSetY('content2Div',-layerGetContentHeight('content2Div','window2Div')+jw2_height,'window2Div');
      scroll2_down = false;
    } else {
      layerSetY('content2Div',(layerGetY('content2Div','window2Div') - 2),'window2Div');
      setTimeout("scroll2Down()", 15);
    }
  }
}