onload = function() {
  if (cart) {
    cart.scroll();
    cart.updateCart();
  }
  if (intranetEnabled) {
    intranet.overlay.update();
  }
  if (document.getElementById('newsSubHeader')) { WYSIWYG.attach('newsSubHeader', small); }
  if (document.getElementById('newsText')) { WYSIWYG.attach('newsText', small); }
  if (document.getElementById('bookText1')) { WYSIWYG.attach('bookText1', small); }
  if (document.getElementById('bookText2')) { WYSIWYG.attach('bookText2', small); }
  if (document.getElementById('bookText3')) { WYSIWYG.attach('bookText3', small); }
  if (document.getElementById('aboutBook')) { WYSIWYG.attach('aboutBook', small); }
  if (document.getElementById('quoteText')) { WYSIWYG.attach('quoteText', small); }
  if (document.getElementById('quoteHeadline')) { WYSIWYG.attach('quoteHeadline', small); }
  if (document.getElementById('intranet.books.add.readPartOfBookTextarea')) { WYSIWYG.attach('intranet.books.add.readPartOfBookTextarea', small); }
}
onscroll = function() {
  if (!document.getElementById('readfrombooktext') || (document.getElementById('readfrombooktext') && document.getElementById('readfrombooktext').style.visibility!='visible')) {
    cart.scroll();
  }
  intranet.overlay.update();
}
onresize = function() {
  if (cart) {
    if (!document.getElementById('readfrombooktext') || (document.getElementById('readfrombooktext') && document.getElementById('readfrombooktext').style.visibility!='visible')) {
      cart.scroll();
    }
  }
  if (intranet) {
    intranet.overlay.update();
  }
}

document.onkeydown = function() {
  if (event.keyCode=='27') {
    if (document.getElementById('readfrombooktext') && document.getElementById('readfrombooktext').style.visibility=='visible') {
      books.hideBookPart();
    }
    if (document.getElementById('alert') && document.getElementById('alert').style.visibility == 'visible') {
      intranet.alert.hide();
    }
  }
  if (event.keyCode=='13') {
    if (document.getElementById('alert') && document.getElementById('alert').style.visibility == 'visible') {
      intranet.alert.hide();
    }
  }
}

intranetEnabled = false;

document.onmousemove = function(event) {
  if (intranetEnabled) {
    intranet.trackbar.moving(event);
  }
  if (document.getElementById('pictureEditor_marker') && document.getElementById('pictureEditor_marker').style.visibility == 'visible') {
    intranet.pictureEditor.selectionMove((window.event.clientX ? window.event.clientX : event.pageX), (window.event.clientY ? window.event.clientY : event.pageY));
  }
}

document.onmouseup = function() {
  intranet.trackbar.movingStop(event);
//  if (document.getElementById('dictionarySearchFieldList')) { document.getElementById('dictionarySearchFieldList').style.visibility = 'hidden'; }
  if (event) {

    tel = (event.srcElement ? event.srcElement : (event.toElement ? event.toElement : event.target));

    if (event && tel && (tel.id == 'searchWord')) {
      if (document.getElementById('swsearch') && document.getElementById('swsearch').value > 0) {
        if (document.getElementById('dictionarySearchFieldList')) { document.getElementById('dictionarySearchFieldList').style.visibility = 'visible'; }
      }
    } else {
      if (document.getElementById('dictionarySearchFieldList')) { document.getElementById('dictionarySearchFieldList').style.visibility = 'hidden'; }
    }

    if (event && tel && (tel.id != 'newsSearchField' && !tel.id.match('newsSearchPages_') && tel.id != 'ordabokasida2')) {
      if (document.getElementById('searchFieldMain')) { document.getElementById('searchFieldMain').style.visibility = 'hidden'; }
    }
    if (event && tel) {
      if (tel.id.match('newsSearchPages') || tel.className.match('searchFieldResultList') || tel.id == 'ordabokasida2' || tel.id == 'bookSearchField' || tel.id == 'searchFieldMain2' || tel.id == 'bookSearchFieldButton' || tel.id == 'searchFieldListBottom') {
      } else {
        if (document.getElementById('searchFieldMain2')) { document.getElementById('searchFieldMain2').style.visibility = 'hidden'; }
      }
    }
  }
}

banners = new Object;
banners.lastBannerId = '';
banners.maxBannerId = '';

banners.scrollFrontpage = function(id,t,t2) {

  if (banners.lastBannerId != id) {

    if (banners.lastBannerId && document.getElementById('bannerButton_'+banners.lastBannerId)) {
      document.getElementById('bannerButton_'+banners.lastBannerId).src='images/banners/frontpage_dot.png';
    }
    if (document.getElementById('bannerButton_'+id)) {
      document.getElementById('bannerButton_'+id).src='images/banners/frontpage_dot_on.png';
    }

    if (document.getElementById('bannerLink') && document.getElementById('bannerLink_'+id)) {
      if (document.getElementById('bannerLink_'+id).value != '') {
        document.getElementById('bannerLink').href = document.getElementById('bannerLink_'+id).value;
      } else {
        document.getElementById('bannerLink').href = 'javascript:void(0);';
      }
    }

    if (document.getElementById('bannerLink') && document.getElementById('bannerLink_'+id)) {
      if (document.getElementById('bannerLink_'+id).value != '') {
        document.getElementById('bannerLink').target = document.getElementById('bannerTarget_'+id).value;
      } else {
        document.getElementById('bannerLink').target = '';
      }
    }

    if ((banners.lastBannerId == 1 && id == banners.maxBannerId) && banners.maxBannerId > 2) {
      if (t2) {
        document.getElementById('frontpageBanner1').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+banners.lastBannerId).name+')';
        document.getElementById('frontpageBanner2').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+id).name+')';
        banners.scrollFrontpageScrolling(1);
      } else {
        document.getElementById('frontpageBanner2').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+banners.lastBannerId).name+')';
        document.getElementById('frontpageBanner1').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+id).name+')';
        banners.scrollFrontpageScrolling(2);
      }
    } else if ((banners.lastBannerId < id || (banners.lastBannerId == banners.maxBannerId && id == 1))) {
      if (t == 1 || t == undefined) {
        document.getElementById('frontpageBanner1').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+banners.lastBannerId).name+')';
        document.getElementById('frontpageBanner2').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+id).name+')';
        banners.scrollFrontpageScrolling(1);
      } else {
        document.getElementById('frontpageBanner2').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+banners.lastBannerId).name+')';
        document.getElementById('frontpageBanner1').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+id).name+')';
        banners.scrollFrontpageScrolling(2);
      }
    } else {
      document.getElementById('frontpageBanner2').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+banners.lastBannerId).name+')';
      document.getElementById('frontpageBanner1').style.backgroundImage='url(images/banners/frontpageBanner/'+document.getElementById('bannerButton_'+id).name+')';
      banners.scrollFrontpageScrolling(2);
    }

    banners.lastBannerId = id;

  }

}

banners.sfscTimer = '';
banners.sfscW = 0;
banners.sfscMw = 526;
banners.autoRot = '';

banners.scrollFrontpageScrolling = function(t) {
  if (t == 1) {

    document.getElementById('frontpageBanner1').style.backgroundPosition='-'+banners.sfscW+'px 0px';
    document.getElementById('frontpageBanner2').style.backgroundPosition=(banners.sfscMw-banners.sfscW)+'px 0px';

    if (banners.sfscW < banners.sfscMw) {
      banners.sfscTimer = setTimeout("banners.scrollFrontpageScrolling("+t+");",100);
      banners.sfscW = parseInt(banners.sfscW)+100;
    } else {
      document.getElementById('frontpageBanner1').style.backgroundPosition='-'+banners.sfscMw+'px 0px';
      document.getElementById('frontpageBanner2').style.backgroundPosition='0px 0px';
      clearTimeout(banners.sfscTimer);
      banners.sfscW = 0;
    }

  } else {

    document.getElementById('frontpageBanner2').style.backgroundPosition=banners.sfscW+'px 0px';
    document.getElementById('frontpageBanner1').style.backgroundPosition=(banners.sfscW-banners.sfscMw)+'px 0px';

    if (banners.sfscW < banners.sfscMw) {
      banners.sfscTimer = setTimeout("banners.scrollFrontpageScrolling("+t+");",100);
      banners.sfscW = parseInt(banners.sfscW)+100;
    } else {
      document.getElementById('frontpageBanner2').style.backgroundPosition=banners.sfscMw+'px 0px';
      document.getElementById('frontpageBanner1').style.backgroundPosition='0px 0px';
      clearTimeout(banners.sfscTimer);
      banners.sfscW = 0;
    }

  }
}

banners.frontpageBannerMove = function(d,t) {

  if (d == 'right') {
    if (parseInt(banners.lastBannerId)+1 <= banners.maxBannerId) {
      banners.scrollFrontpage((parseInt(banners.lastBannerId)+1),t);
    } else {
      banners.scrollFrontpage(1,t);
    }
  } else {
    if (parseInt(banners.lastBannerId)-1 >= 1) {
      banners.scrollFrontpage((parseInt(banners.lastBannerId)-1),t);
    } else {
      banners.scrollFrontpage(banners.maxBannerId,t);
    }
  }

}


banners.setAutoRot = function() {
  clearTimeout(banners.autoRot);
  banners.autoRot = setTimeout("banners.automaticRotation()",8000);
}


banners.automaticRotation = function() {
  if (banners.maxBannerId == 2) {
    banners.frontpageBannerMove('right',1);
  } else {
    banners.frontpageBannerMove('right');
  }
  banners.autoRot = setTimeout("banners.automaticRotation()",8000);
}

news = new Object();

news.print = function(id) {
  if (document.getElementById('newsPrint') && id) {
    document.getElementById('newsPrint').src = 'modules/pages/newsPrint.php?id='+id;
  }
}


news.showImgGallery = function(id,title) {

  intranet.overlay.show();

  if( typeof( window.pageYOffset ) == 'number' ) {
    st = window.pageYOffset;
    sl = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    st = document.body.scrollTop;
    sl = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    st = document.documentElement.scrollTop;
    sl = document.documentElement.scrollLeft;
  }

  if (document.getElementById('upperbottom')) { document.getElementById('upperbottom').style.visibility='hidden'; }
  if (document.getElementById('bottom')) { document.getElementById('bottom').style.visibility='hidden'; }

  intranet.overlay.show();
  intranet.overlay.update();
  document.getElementById('readfrombooktitle').title = title;
  document.getElementById('readfrombooktitle').innerHTML = (title.length > 50 ? title.substr(0,47)+"..." : title);

  xmlHttp_loadImgGallery = ajaxObj();

  xmlHttp_loadImgGallery.onreadystatechange=function() {
    if(xmlHttp_loadImgGallery.readyState==4) {
      if (xmlHttp_loadImgGallery.responseText) {
        document.getElementById('readfrombookinnertext').innerHTML = xmlHttp_loadImgGallery.responseText;
        document.getElementById('readfrombooktext').style.visibility='visible';
        document.getElementById('quote').style.visibility='hidden';
        document.getElementById('quote').style.position='absolute';
        scroll(0,0);
      } else {
        document.getElementById('quote').style.visibility='visible';
        document.getElementById('quote').style.position='relative';
        intranet.overlay.hide();
      }
    }
  }

  xmlHttp_loadImgGallery.open("GET","modules/post/imageGallery.php?g="+id,true);
  xmlHttp_loadImgGallery.send(null);

}


news.sendtofriend = function(id) {

  intranet.overlay.show();

  if( typeof( window.pageYOffset ) == 'number' ) {
    st = window.pageYOffset;
    sl = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    st = document.body.scrollTop;
    sl = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    st = document.documentElement.scrollTop;
    sl = document.documentElement.scrollLeft;
  }

  if (document.getElementById('upperbottom')) { document.getElementById('upperbottom').style.visibility='hidden'; }
  if (document.getElementById('bottom')) { document.getElementById('bottom').style.visibility='hidden'; }

  intranet.overlay.show();
  intranet.overlay.update();

  var title = 'Send einum vini';

  document.getElementById('readfrombooktitle').title = title;
  document.getElementById('readfrombooktitle').innerHTML = (title.length > 50 ? title.substr(0,47)+"..." : title);

  xmlHttp_sendToFriend = ajaxObj();

  xmlHttp_sendToFriend.onreadystatechange=function() {
    if(xmlHttp_sendToFriend.readyState==4) {
      if (xmlHttp_sendToFriend.responseText) {
        document.getElementById('readfrombookinnertext').innerHTML = xmlHttp_sendToFriend.responseText;
        document.getElementById('readfrombooktext').style.visibility='visible';
        document.getElementById('quote').style.visibility='hidden';
        document.getElementById('quote').style.position='absolute';
        scroll(0,0);
      } else {
        document.getElementById('quote').style.visibility='visible';
        document.getElementById('quote').style.position='relative';
        intranet.overlay.hide();
      }
    }
  }

  xmlHttp_sendToFriend.open("GET","modules/post/sendtofriend.php?id="+id,true);
  xmlHttp_sendToFriend.send(null);

}


news.changeTab = function(id,sida,g) {

  xmlHttp_changeTab = ajaxObj();

  xmlHttp_changeTab.onreadystatechange=function() {
    if(xmlHttp_changeTab.readyState==4) {
      if (xmlHttp_changeTab.responseText) {
        document.getElementById('fregnir_news_new').innerHTML = xmlHttp_changeTab.responseText;

        if (document.getElementById('newsArchive')) {
          ju = '';
          if (document.getElementById('journalistUser')) { ju = document.getElementById('journalistUser').value; }
          news.loadArchive(g,ju,id);
        }

        if (document.getElementById('lastBannerId')) {
          banners.lastBannerId = document.getElementById('lastBannerId').value;
          banners.maxBannerId = document.getElementById('maxBannerId').value;
          banners.autoRot = setTimeout("banners.automaticRotation()",8000);
        }

      }
    }
  }

  xmlHttp_changeTab.open("GET","/modules/post/newsTabs.php?cat="+id+"&sida="+sida+"&grein="+g,true);
  xmlHttp_changeTab.send(null);

}


news.search = function(val,p,t) {

  xmlHttp_searchNews = ajaxObj();

  xmlHttp_searchNews.onreadystatechange=function() {
    if(xmlHttp_searchNews.readyState==4) {
      if (xmlHttp_searchNews.responseText) {
        document.getElementById('searchFieldContents').innerHTML = xmlHttp_searchNews.responseText;
        if (t == 'top') { document.getElementById('searchMoveSelected').value = 0; }
        if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value)) { 
          document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #000000';
        } else if (document.getElementById('searchMoveSelected').value > document.getElementById('searchMoveSelectedMax').value) {
          if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelectedMax').value)) { 
            document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelectedMax').value).style.border='1px solid #000000';
            document.getElementById('searchMoveSelected').value = document.getElementById('searchMoveSelectedMax').value;
          }
        }
      }
    }
  }

  xmlHttp_searchNews.open("GET","/modules/post/searchNews.php?s="+val+"&p="+p,true);
  xmlHttp_searchNews.send(null);

}



news.loadArchive = function(id,usr,cat,parentCat,p) {

  xmlHttp_loadArchive = ajaxObj();

  xmlHttp_loadArchive.onreadystatechange=function() {
    if(xmlHttp_loadArchive.readyState==4) {
      if (xmlHttp_loadArchive.responseText) {
        document.getElementById('newsArchive').innerHTML = xmlHttp_loadArchive.responseText;
      }
    }
  }

  xmlHttp_loadArchive.open("GET","/modules/post/newsArchive.php?id="+id+"&usr="+usr+"&cat="+cat+"&parentCat="+parentCat+"&p="+p,true);
  xmlHttp_loadArchive.send(null);

}


news.searchMove = function(e,t,v) {

    if (e.keyCode == 13) {
      if (t == 1) {
        location.href=document.getElementById('searchFieldResultUrl_'+document.getElementById('searchMoveSelected').value).value;
        e.returnValue = false;
        return false;
      }
    } else if (e.keyCode == 40) {
      //Nišur
      if (t == 1) {
        var ts = parseInt(document.getElementById('searchMoveSelected').value)+1;
        if (document.getElementById('searchFieldResult_'+(ts-1)) && !document.getElementById('searchFieldResult_'+ts)) {
          var ts2 = parseInt(document.getElementById('ordabokasida2').value)+1;
          if (ts2 <= parseInt(document.getElementById('ordabok_tilsamans').innerHTML)) {
            news.search(document.getElementById('bookSearchField').value,ts2,'top');
          }
        } else if (document.getElementById('searchFieldResult_'+ts)) {
          if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value)) {
            document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #cccccc';
          }
          document.getElementById('searchMoveSelected').value = ts;
          document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #000000';
        }
      }
      e.returnValue = false;
      return false;
    } else if (e.keyCode == 38) {
      //Upp
      if (t == 1) {
        var ts = parseInt(document.getElementById('searchMoveSelected').value)-1;
        if (ts >= 0) {
          if (document.getElementById('searchFieldResult_'+ts)) {
            if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value)) {
              document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #cccccc';
            }
            document.getElementById('searchMoveSelected').value = ts;
            document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #000000';
          }
        } else {
          if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value)) {
            document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #cccccc';
          }
          document.getElementById('searchMoveSelected').value = -1;
        }
      }
      e.returnValue = false;
      return false;
    } else if (e.keyCode == 37) {
      //Vinstra
      if (document.getElementById('searchMoveSelected').value > -1) {
        if (t == 1) {
          var ts = parseInt(document.getElementById('ordabokasida2').value)-1;
          if (ts > 0) {
            news.search(document.getElementById('bookSearchField').value,ts);
          }
        }
        e.returnValue = false;
        return false;
      }
    } else if (e.keyCode == 39) {
      //Hųgra
      if (document.getElementById('searchMoveSelected').value > -1) {
        if (t == 1) {
          var ts = parseInt(document.getElementById('ordabokasida2').value)+1;
          if (ts <= parseInt(document.getElementById('ordabok_tilsamans').innerHTML)) {
            news.search(document.getElementById('bookSearchField').value,ts);
          }
        }
        e.returnValue = false;
        return false;
      }
    } else {
      if (document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value)) {
        document.getElementById('searchFieldResult_'+document.getElementById('searchMoveSelected').value).style.border='1px solid #cccccc';
      }
      document.getElementById('searchMoveSelected').value = -1;
      document.getElementById('searchFieldMain2').style.visibility='visible';
      news.search(v,1); 
    }

}
