
/**
 * Style sheet differences between IE & Mozilla
 */
var portal_url = 'http://www.buero-newyork.com';
if(document.getElementById) {
  if (!document.all || typeof opera != "undefined") {
    document.write('<style type="text/css">@import url('+portal_url+'/public/other.css);</style>');
  } else {
    document.write('<style type="text/css">@import url('+portal_url+'/public/ie.css);</style>');
  }
}

var FIRST_ARTICLE_ID = "ac0";
var DRAG_CONTAINER_ID = "dragContainer";
var COLUMN_WIDTH = 160;
var TOTAL_BORDER = 137;
var MOUSE_MIN_OFFSET = 1; // Mouse offset to be valid for opening the window.
var IS_IE = document.all && typeof opera == 'undefined';
var HEIGHT_OFFSET = 50; // IE Value for the offset (top & bottom to the total height)
var SCROLL_SPEED_UP = 12;
var SCROLL_SPEED_DOWN = -12;
var SCROLL_MULTIPLIER = 3;
var OFFSET_DOUBLECLICK = 200;
var BREAK_MULTIPLICATOR = 10;
var BOTTOM_CONTENT_CROP = 3;

/**
 * Internal Holders
 */
var elem = null; // Article source
var cont = null; // Article container
var dragCont = null; // DragContainer (container for drag content)
var columns = []; // Columns in a array
var columnsContents = []; // Columnscontent in a array 
var countedColumns = null; // Contains the last counted columns
var currentPos = 0; // Current position of the content
var minPos = 0; // Minimal position available
var startDragMousePos = 2; // y-Position of the mouse by starting to drag
var startDragMousePosX = 2; // xPosition of the mouse by starting to drag
var startDragPos = null; // Position of the content by starting to drag
var isDragging = false;
var isScrolling = false;
var scrollSpeed = 0;
var allowedSelectedTags = "input|textarea|select"; // Allow those tags
var scrollMultiplier = 0;
var tooSmallEntries = 0;
var columnCount = 0;
var columnBin = 1;
var specColumnPos = [160,160];
var keyScrolling = false;
var searchFocus = false;


function getContainer() {
  if(!cont) cont = getFirstElement().parentNode;
  return cont;
}

function getDragContainer() {
  if(!dragCont) dragCont = document.getElementById(DRAG_CONTAINER_ID);
  return dragCont;
}

function countColumns(width) {
  countedColumns = Math.ceil(width/COLUMN_WIDTH);
  return countedColumns;
}

function getFirstElement() {
  if(!elem) elem = document.getElementById(FIRST_ARTICLE_ID);
  return elem;
}

function getPosition(obj) {
  var pos = {x:0,y:0};
  while (obj.offsetParent){
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return pos;
}

function addColumn(column) {
  var pos = getPosition(column);
  column.yPosition = pos.y;
  column.xPosition = pos.x;
  column.uid = columnCount++;
  columnBin = columnBin*2;
  column.bin = columnBin;
  column.childNodes[0].formerPos = 0;
  columns.push(column);
}

function createNeededColumns(count) {
  if(columns.length == 0) {
    var col = getFirstElement();
    col.style.height = "";
    col.style.overflow = "";
    addColumn(col);
  }
  if(columns.length < count) {
    var pos = columns.length * COLUMN_WIDTH;
    for (i=columns.length; i < count; i++, pos+=COLUMN_WIDTH) {
      var col = elem.cloneNode(true);
      col.id = "ac"+i;
      col.className = "artCol";
      col.style.left = pos+"px";
      cont.appendChild(col);
      addColumn(col);
    }
  }
}

// IE Specific Code
//var updateMinPos;
var updatePosition;
if(IS_IE) {
  /*updateMinPos = function() {
    var windowHeight = getDragContainer().offsetHeight;
    var pos = currentPos;
    var result;
    tooSmallEntries = 0;
    minPos = -columns[0].offsetHeight;
    for (var i = 0; i< countedColumns; i++) {
      result = windowHeight-columns[i].yPosition-HEIGHT_OFFSET;
      if(result > 0) {
        minPos += result;
      } else {
        tooSmallEntries = tooSmallEntries | columns[i].bin;
      }
    }
    minPos -= BOTTOM_CONTENT_CROP;
  }*/
  updatePosition = function() {
    var windowHeight = getDragContainer().offsetHeight;
    var pos = currentPos;
    for (var i = 0; i< countedColumns; i++) {
      var column = columns[i];
      if((tooSmallEntries & column.bin) <= 0) {
        var elem = column.childNodes[0];
        var curPos = elem.formerPos;
        var effectivePos = curPos+(pos-curPos)/column.breakSpeed;
        elem.formerPos = effectivePos;
        elem.style.top = effectivePos+"px";
        pos -= (windowHeight-column.yPosition-HEIGHT_OFFSET);
      }
    }
  }
} else {
  /*updateMinPos = function() {
    var windowHeight = getDragContainer().offsetHeight;
    var pos = currentPos;
    tooSmallEntries = 0;
    minPos = -columns[0].offsetHeight;
    for (var i = 0; i< countedColumns; i++) {
      var pos = 0;
      if(specColumnPos[i]) {
         pos = specColumnPos[i];
      }
      if(windowHeight-pos-TOTAL_BORDER > 0) {
        minPos += windowHeight-columns[i].offsetTop;
      } else {     
        tooSmallEntries = tooSmallEntries | columns[i].bin;
      }
    }
    minPos -= BOTTOM_CONTENT_CROP;
  }*/
  updatePosition = function() {
    var windowHeight = getDragContainer().offsetHeight;
    var pos = currentPos;
    for (var i = 0; i< countedColumns; i++) {
      var column = columns[i];
      if((tooSmallEntries & column.bin) <= 0) {
        var elem = column.childNodes[0];
        var curPos = elem.formerPos;
        var effectivePos = curPos+(pos-curPos)/column.breakSpeed;
        elem.formerPos = effectivePos;
        elem.style.top = effectivePos+"px";
        pos -= (windowHeight-column.offsetTop);
      }
    }
  }
}

function updateSize() {
  var windowWidth = getDragContainer().offsetWidth;
  createNeededColumns(countColumns(windowWidth));
  startDragMousePos = 0;
  startDragMousePosX = COLUMN_WIDTH;
  setPosition(currentPos);
  updateDragBreak(true);
  updatePosition();
  //updateMinPos();
}

function setPosition(pos) {
  //if(pos < minPos) {
  //  pos = minPos;
  //}
  if(pos > 0) {
    pos = 0;
  }
  if(pos != currentPos) {
    currentPos = pos;
  }
}

function getColumnBreak(pos)  {
  //alert();
  if(!startDragMousePos) return 1;
  return Math.abs((Math.round((Math.abs(startDragMousePosX-pos-COLUMN_WIDTH/2))/COLUMN_WIDTH))*BREAK_MULTIPLICATOR)+1;
}
/*
function getColumnBreak(pos)  {
  if(!startDragMousePos) return 1;
  return Math.abs((Math.round((Math.abs(startDragMousePosX-pos-COLUMN_WIDTH/2))/COLUMN_WIDTH))*BREAK_MULTIPLICATOR)+1;
}*/


function updateDragBreak() {
  var offset = getDragContainer().offsetLeft;
  for(var i = 0; i < countedColumns; i++) {
    columns[i].breakSpeed = getColumnBreak(columns[i].offsetLeft+offset);
  }
}

function startDrag(e) {
  if(!isScrolling) {
    startDragPos = currentPos;
    startDragMousePos = e ? e.clientY : event.clientY;
    startDragMousePosX = e ? e.clientX : event.clientX;
    isDragging = true;
    updateDragBreak(false);
  }
}

function stopDrag() {
  isDragging = false;
}

function startScrollUp() {
  if(!isDragging) {
    stopScrollMultiplicator();
    startDragMousePosX = 2;
    startDragMousePos = 2;
    scrollSpeed = SCROLL_SPEED_UP;
    isScrolling = true;
  }
}

function startScrollMultiplicator() {
  scrollMultiplier = SCROLL_MULTIPLIER;
}

function stopScrollMultiplicator() {
  scrollMultiplier = 1;
}

function startScrollDown() {
  if(!isDragging) {
    stopScrollMultiplicator();
    startDragMousePos = 2;
    startDragMousePosX = 2;
    scrollSpeed = SCROLL_SPEED_DOWN;
    isScrolling = true;
  }
}

function stopScroll() {
  isScrolling = false;
}

function drag(e) {
  e = e ? e : event;
  var mousePos = e.clientY;
  if(isDragging) {  
    if(typeof e.button != "undefined" && e.button == 0 && document.all) {
      stopDrag();
    } else {
      setPosition(startDragPos+mousePos-startDragMousePos);
    }
  }
}

function init() {
  if(typeof document.getElementById != "undefined"
     && getFirstElement() != null) {

    getContainer().style.top = "0px";
    window.onresize = updateSize;
    document.body.onmousemove = drag;
    
    document.body.onmousewheel = function(delta) {
      setPosition(currentPos-event.wheelDelta);
    }
    document.body.ondrag = document.body.onstartdrag = document.onselectstart = function() {
      return false;
    }
    document.onmousedown = function(e) {
      startDrag(e);
      if (typeof e != 'undefined' && allowedSelectedTags.indexOf(e.target.tagName.toLowerCase())==-1)
        return false;
    }
    document.onmouseup = function(e) { 
      stopDrag();
      return true;
    }
    updateSize();
    updateDragBreak(false);
    window.setInterval("update();", 13);

    // Safari position actualisation.
    // window.setTimeout("updateMinPos();", 50);
    
    
    if( document.captureEvents ) {
        //non IE
        if( Event.KEYUP ) {
            //NS 4, NS 6+, Mozilla 0.9+
            document.captureEvents( Event.KEYUP );
        }
    }
    /* this next line tells the browser to detect a keyup
    event over the whole document and when it detects it,
    it should run the event handler function 'alertkey' */
    document.onkeyup = alertkey;
    
  }
}

function initLink(that, id) {
  that.onmouseover = new Function("");
  that.startX = -1;
  that.startY = -1;
  that.doOpen = false;
  that.onclick = function() {
    return false;
  }
  that.onmousedown = function(e) {
    this.startX = e ? e.clientX : event.clientX;
    this.startY = e ? e.clientY : event.clientY;
    this.doOpen = true;
    return false;
  }
  that.onmousemove = function(e) {
    var ev = e ? e : event;
    var x = ev.clientX;
    var y = ev.clientY;
    if((    this.startX + MOUSE_MIN_OFFSET < x || this.startX - MOUSE_MIN_OFFSET > x 
         || this.startY + MOUSE_MIN_OFFSET < y || this.startY - MOUSE_MIN_OFFSET > y)
       && this.startX != -1 && this.startY != -1){
      this.doOpen = false;
    }
  }
  that.onstopdrag = that.ondrag = that.onstartdrag = function() {
    return false;
  }
  that.onmouseup = function(e) {
    var ev = e ? e : event;
    if(this.doOpen && ev.button != 2) {
      openContent(this, id);
    }
    stopDrag();
    this.startX = -1;
    this.startY = -1;
    return false;
  }
}

function update() {
  if(isScrolling) {
    setPosition(currentPos-(scrollSpeed*scrollMultiplier));
  }
  updatePosition();
}

function openContent(obj, name) {
  var win = window.open(obj.getAttribute('href'), name, "scrollbars=yes,resizable=yes, width=620,height=440");
  if(win) {
    // Popup Blocker should not cause a error
    win.focus();
  }
  return false;
}


//NOW CREATE THE EVENT HANDLER FUNCTION TO PROCESS THE EVENT
function alertkey(e) {
    if( !e ) {
        //if the browser did not pass the event information to the
        //function, we will have to obtain it from the event register
        if( window.event ) {
            //DOM
            e = window.event;
        } else {
            //TOTAL FAILURE, WE HAVE NO WAY OF REFERENCING THE EVENT
            return;
        }
    }
    if( typeof( e.which ) == 'number' ) {
        //NS 4, NS 6+, Mozilla 0.9+, Opera
        e = e.which;
    } else if( typeof( e.keyCode ) == 'number'  ) {
        //IE, NS 6+, Mozilla 0.9+
        e = e.keyCode;
    } else if( typeof( e.charCode ) == 'number'  ) {
        //also NS 6+, Mozilla 0.9+
        e = e.charCode;
    } else {
        //TOTAL FAILURE, WE HAVE NO WAY OF OBTAINING THE KEY CODE
        return;
    }
    
    if(e==32 && !searchFocus) {
        if(keyScrolling) {
            stopScroll();
            keyScrolling = false;
        } else {
            startScrollUp();
            keyScrolling = true;
        }
    }    
}

function setSearchFocus (par) {
    searchFocus = par;
}

function refresh()
{
    window.location.reload( false );
}


