/**  Version       : 1.2
  *  Last modified : (K. Breynck) 27.02.2004
  *  Author        : BBDO InterOne Hamburg (J. Leuckel)
  *  Copyright     : BMW Group 2002-2004
  *
  *  Unique scripting solutions that are provided for  BMW Group sites are for use exclusively
  *  within  BMW Group projects.  No other use of these solutions is permitted.
  *
  **/
var scrollerFix      = -90;
var contentParentIds    = new Array();
var contentChildIds          = new Array();
var scrollParentIds          = new Array();
var scrollChildIds           = new Array();
var scrollUpIds              = new Array();
var scrollDownIds            = new Array();
var scrollUpArray            = new Array();
var scrollDownArray          = new Array();
var scrollParentArray        = new Array();
var scrollChildArray         = new Array();
var contentParentArray       = new Array();
var contentChildArray        = new Array();
var baseVisibility           = new Array();
var scrollerHeadlineDynLayer = new Array();
var writeScrollerStyles      = '';
var writeScrollerDivs        = '';
var scrollerDivAmount        = contentParentAttributes.length;
var scrollerArrowLeft;
var scrollerArrowUpTop;
var scrollerArrowDownTop;
var divNum
var documentScroll;
var i;

var horizontalScrollbarPadding = ((typeof myHorizontalScrollbarPadding != "undefined") ? myHorizontalScrollbarPadding : 5);
var verticalScrollbarPadding   = ((typeof myVerticalScrollbarPadding != "undefined")   ? myVerticalScrollbarPadding   : 8);
var scrollbarSpanning          = ((typeof myScrollbarSpanning != "undefined")          ? myScrollbarSpanning          : (verticalScrollbarPadding * (-2)));
var sliderWidth                = ((typeof mySliderWidth != "undefined")                ? mySliderWidth                : 11);
var sliderHeight               = ((typeof mySliderHeight != "undefined")               ? mySliderHeight               : 22);
var arrowSquareSize            = ((typeof myArrowSquareSize != "undefined")            ? myArrowSquareSize            : 13);
var verticalArrowUpPadding     = ((typeof myVerticalArrowUpPadding != "undefined")     ? myVerticalArrowUpPadding     : 0);
var verticalArrowDownPadding   = ((typeof myVerticalArrowDownPadding != "undefined")   ? myVerticalArrowDownPadding   : (verticalArrowUpPadding * (-1)));
var horizontalArrowPadding     = ((typeof myHorizontalArrowPadding != "undefined")     ? myHorizontalArrowPadding     : -1);
var cursorShape                = ((typeof myCursorShape != "undefined")                ? myCursorShape                : "cursorDefault"); // shape of the cursor if it's positioned over the arrows


writeScrollerStyles += '<style type="text/css">\n';

for (i = 0; i < scrollerDivAmount; i++) {

  if(contentParentAttributes[i]["headlineLayerName"]) {
    scrollerHeadlineDynLayer[i] = DynLayer.getInline(contentParentAttributes[i]["headlineLayerName"]);

    if((scrollerHeadlineDynLayer[i].getY() + scrollerHeadlineDynLayer[i].getHeight() + contentParentAttributes[i]["headlineLayerSpan"]) > (contentParentAttributes[i]["top"])) {
      contentParentAttributes[i]["height"] = contentParentAttributes[i]["height"] - (scrollerHeadlineDynLayer[i].getY() + scrollerHeadlineDynLayer[i].getHeight() + contentParentAttributes[i]["headlineLayerSpan"] - contentParentAttributes[i]["top"]);
      contentParentAttributes[i]["top"]    = scrollerHeadlineDynLayer[i].getY() + scrollerHeadlineDynLayer[i].getHeight() + contentParentAttributes[i]["headlineLayerSpan"];
    }
  }

  contentParentIds[i]  = 'contentParent_'  + i;
  contentChildIds[i]   = 'contentChild_'   + i;
  scrollParentIds[i]   = 'scrollParent_'   + i;
  scrollChildIds[i]    = 'scrollChild_'    + i;
  scrollUpIds[i]       = 'scrollUp_'       + i;
  scrollDownIds[i]     = 'scrollDown_'     + i;

  scrollbarLeft        = contentParentAttributes[i]["left"]    + contentParentAttributes[i]["width"] + horizontalScrollbarPadding;
  scrollbarTop         = contentParentAttributes[i]["top"]     + verticalScrollbarPadding;
  scrollbarHeight      = contentParentAttributes[i]["height"]  + scrollbarSpanning;

  writeScrollerStyles += '#' + contentParentIds[i]  + '{position:absolute; visibility:hidden; left:' + contentParentAttributes[i]["left"] + 'px; top:' + contentParentAttributes[i]["top"] + 'px; width:' + contentParentAttributes[i]["width"] + 'px; height:' + contentParentAttributes[i]["height"] + 'px; z-index:' +  contentParentAttributes[i]["z-index"] + ';     overflow:hidden; clip:rect(0px ' + contentParentAttributes[i]["width"] + 'px ' + contentParentAttributes[i]["height"] + 'px 0px);}\n';
  writeScrollerStyles += '#' + contentChildIds[i]   + '{position:relative;                    left:0px;                                          top:0px;                                         width:' + contentParentAttributes[i]["width"] + 'px;                                                        z-index:' +  contentParentAttributes[i]["z-index"] + ';     overflow:hidden;}\n';
  writeScrollerStyles += '#' + scrollParentIds[i]   + '{position:absolute; visibility:hidden; left:' + scrollbarLeft + 'px;                      top:' + scrollbarTop + 'px;                      width:' + sliderWidth + 'px;                         height:' + scrollbarHeight + 'px;                      z-index:' + (contentParentAttributes[i]["z-index"]+1) + ';  overflow:hidden; background-image:url(' + rootpath + '/common_content/mini/_common/_img/scrollbar_bg_' + contentParentAttributes[i]["fg-color"] + '_' + contentParentAttributes[i]["bg-color"] + '.gif);}\n';
  writeScrollerStyles += '#' + scrollChildIds[i]    + '{position:relative;                    left:0px;                                          top:0px;                                         width:' + sliderWidth + 'px;                         height:'+ sliderHeight + 'px;                          z-index:' + (contentParentAttributes[i]["z-index"]+1) + ';  overflow:hidden;}\n';

  scrollerArrowLeft    = scrollbarLeft  + horizontalArrowPadding;
  scrollerArrowUpTop   = scrollbarTop   - arrowSquareSize + verticalArrowUpPadding;
  scrollerArrowDownTop = scrollbarTop   + scrollbarHeight + verticalArrowDownPadding;

  writeScrollerStyles += '#' + scrollUpIds[i] +   '{position:absolute; visibility:hidden; left:' + scrollerArrowLeft + 'px;top:' + scrollerArrowUpTop + 'px;  width:' + arrowSquareSize + 'px;   height:'+ arrowSquareSize + 'px;  overflow:hidden;z-index:' + (contentParentAttributes[i]["z-index"]+1) + ';}\n';
  writeScrollerStyles += '#' + scrollDownIds[i] + '{position:absolute; visibility:hidden; left:' + scrollerArrowLeft + 'px;top:' + scrollerArrowDownTop + 'px;width:' + arrowSquareSize + 'px;   height:'+ arrowSquareSize + 'px;overflow:hidden;z-index:' + (contentParentAttributes[i]["z-index"]+1) + ';}\n';

  writeScrollerDivs   += '<div name="' + scrollParentIds[i] + '" id="' + scrollParentIds[i] + '" onmouseover="divNum = '+ i +';">\n'
                      +  '<div name="' + scrollChildIds[i] + '"  id="' + scrollChildIds[i] + '" class="cursorHand"><img src="' + rootpath + '/common_content/mini/_common/_img/scroll_slider_' + contentParentAttributes[i]["fg-color"] + '_' + contentParentAttributes[i]["bg-color"] + '.gif" width="' + sliderWidth + '" height="' + sliderHeight + '" border="0" /><br /></div>\n'
                      +  '</div>\n';

  writeScrollerDivs   += '<div name="' + scrollUpIds[i] + '" id="' + scrollUpIds[i] + '" onmousedown="divNum = '+ i +';">\n'
                      +  '<a href="javascript:void(0);" onmousedown="status=\'\';return true;" onmouseover="status=\'\';return true;" onmouseout="status=\'\';return true;" class="setCursor"><img src="' + rootpath + '/common_content/mini/_common/_img/scroll_up_' + contentParentAttributes[i]["fg-color"] + '_' + contentParentAttributes[i]["bg-color"] + '.gif" width="' + arrowSquareSize + '" height="' + arrowSquareSize + '" border="0" /></a><br />\n'
                      +  '</div>\n';

  writeScrollerDivs   += '<div name="' + scrollDownIds[i] + '" id="' + scrollDownIds[i] + '" onmousedown="divNum = '+ i +';">\n'
                      +  '<a href="javascript:void(0);" onmousedown="status=\'\';return true;" onmouseover="status=\'\';return true;" onmouseout="status=\'\';return true;" class="setCursor"><img src="' + rootpath + '/common_content/mini/_common/_img/scroll_down_' + contentParentAttributes[i]["fg-color"] + '_' + contentParentAttributes[i]["bg-color"] + '.gif" width="' + arrowSquareSize + '" height="' + arrowSquareSize + '" border="0" /></a><br />\n'
                      +  '</div>\n';

  baseVisibility[i] = false;
}

writeScrollerStyles   += '.setCursor{cursor:'+ cursorShape + ';}\n';
writeScrollerStyles   += '</style>';

function fakeSlideUp(directionUp,scrollParent,scrollChild,contentParent,contentChild) {
  var scrollButtonInterval = scrollParent.getHeight() - scrollChild.getHeight();
  var factor = (contentChild.getContentHeight() - contentParent.getHeight())  / scrollButtonInterval;
  var scrollingContentPixels = scrollChild.getY() * factor * (-1);
  if(scrollChild.getY() >= 0) {
    if(scrollChild.getY() > 0) {
      scrollChild.setY(scrollChild.getY() + directionUp);
    }
    contentChild.setY(scrollingContentPixels);
  }
}

function fakeSlideDown(directionDown,scrollParent,scrollChild,contentParent,contentChild) {
  var scrollButtonInterval = scrollParent.getHeight() - scrollChild.getHeight() - 1;
  var factor = (contentChild.getContentHeight() - contentParent.getHeight())  / scrollButtonInterval;
  var scrollingContentPixels = scrollChild.getY() * factor * (-1);
  if(scrollChild.getY() <= scrollButtonInterval) {
    scrollChild.setY(scrollChild.getY() + directionDown);
    contentChild.setY(scrollingContentPixels);
  }
}



function initialiseScroller() {
  _UserAgent();

  for (i = 0; i < scrollerDivAmount; i++) {
    scrollUpArray[i]      = DynLayer.getInline(scrollUpIds[i]);
    scrollDownArray[i]    = DynLayer.getInline(scrollDownIds[i]);
    scrollParentArray[i]  = DynLayer.getInline(scrollParentIds[i]);
    scrollChildArray[i]   = DynLayer.getInline(scrollChildIds[i], scrollParentArray[i]);
    contentParentArray[i] = DynLayer.getInline(contentParentIds[i]);
    contentChildArray[i]  = DynLayer.getInline(contentChildIds[i], contentParentArray[i]);
  }

  for (i = 0; i < scrollerDivAmount; i++) {
    if(contentParentArray[i].getHeight() < contentChildArray[i].getHeight()) {
      baseVisibility[i] = true;
    }
    else {
      baseVisibility[i] = false;
      scrollUpArray[i].setVisible(0);
      scrollDownArray[i].setVisible(0);
      scrollParentArray[i].setVisible(0);
    }
    if (contentParentAttributes[i]["visibility"]) {
      setScrollerVisibility(i,1);
    }
  }

  for (i = 0; i < scrollerDivAmount; i++) {
    DragEvent.enableDragEvents(scrollChildArray[i]);
    DragEvent.setDragBoundary(scrollChildArray[i], {left:0, right:0, top:0, bottom:0});
  }

  for (i = 0; i < scrollerDivAmount; i++) {
    scrollChildArray[i].addEventListener(evt);
    /* uncomment the entry below if you want to enable scrolling by clicking in the scrollbar (scrollParent_n) */
    /* scrollParentArray[i].addEventListener(scrollParentEvent); */

    scrollUpArray[i].addEventListener(scrollUpEvent);
    scrollDownArray[i].addEventListener(scrollDownEvent);
  }

}

var evt={
  ondragmove:function(e) {
    if (ie) {
//      documentScroll = document.body.scrollTop;
      documentScroll = 	  scrollerFix;

    }
    else if (safari) {
      documentScroll = 0;
    }
    else{
//      documentScroll = window.pageYOffset;
      documentScroll = scrollerFix;
}

    var mousePositionInscrollChild = e.getPageY() - scrollParentArray[divNum].getPageY() + documentScroll - (scrollChildArray[divNum].getHeight()/2);
    var scrollButtonInterval = scrollParentArray[divNum].getHeight() - scrollChildArray[divNum].getHeight();
    if(mousePositionInscrollChild >= 0) {
      if(mousePositionInscrollChild >= scrollButtonInterval) {
        var rest = mousePositionInscrollChild - scrollButtonInterval;
        scrollChildArray[divNum].setY(mousePositionInscrollChild - rest);
      }
      else{
        scrollChildArray[divNum].setY(mousePositionInscrollChild);
      }
    }
    var factor = (contentChildArray[divNum].getContentHeight() - contentParentArray[divNum].getHeight())  / scrollButtonInterval;
    var scrollingContentPixels = scrollChildArray[divNum].getY() * factor * (-1);
    contentChildArray[divNum].setLocation(0,scrollingContentPixels);
  }
}

var scrollParentEvent={
  onclick : function(e) {
    if (ie) {
//      documentScroll = document.body.scrollTop;
	  documentScroll = 	  scrollerFix;
    }
    else if (safari) {
      documentScroll = 0;
    }
    else{
//      documentScroll = window.pageYOffset;
				documentScroll = scrollerFix;
    }
    var mousePositionInscrollChild = e.getPageY() - scrollParentArray[divNum].getPageY() + documentScroll - (scrollChildArray[divNum].getHeight()/2);
    var scrollButtonInterval = scrollParentArray[divNum].getHeight() - scrollChildArray[divNum].getHeight();
    if(mousePositionInscrollChild >= scrollButtonInterval) {
      var rest = mousePositionInscrollChild - scrollButtonInterval;
      scrollChildArray[divNum].setY(mousePositionInscrollChild - rest);
    }
    else{
      scrollChildArray[divNum].setY(mousePositionInscrollChild);
    }
    var factor = (contentChildArray[divNum].getContentHeight() - contentParentArray[divNum].getHeight())  / scrollButtonInterval;
    var scrollingContentPixels = scrollChildArray[divNum].getY() * factor * (-1);
    contentChildArray[divNum].setLocation(0,scrollingContentPixels);
  }
}

var scrollUpEvent={
  onmousedown : function() {
    id = setInterval('fakeSlideUp(-1,scrollParentArray[divNum],scrollChildArray[divNum],contentParentArray[divNum],contentChildArray[divNum])',10);
  },
  onmouseup : function() {
    if (typeof id != 'undefined') {
      clearInterval(id);
    }
  },
  onmouseout : function() {
    if (typeof id != 'undefined') {
      clearInterval(id);
    }
  }
}

var scrollDownEvent={
  onmousedown : function() {
    id = setInterval('fakeSlideDown(1,scrollParentArray[divNum],scrollChildArray[divNum],contentParentArray[divNum],contentChildArray[divNum])',10);
  },
  onmouseup : function() {
    if (typeof id != 'undefined') {
      clearInterval(id);
    }
  },
  onmouseout : function() {
    if (typeof id != 'undefined') {
      clearInterval(id);
    }
  }
}

/**  ========================================================
  *  The following functions manipulates the scrollable layer
  *  ========================================================
  **/

// Enables/Disables the visibility of a scrollable layer 
function setScrollerVisibility(scrollerNumber,scrollerValue) {
  if(baseVisibility[scrollerNumber]) {
    scrollUpArray[scrollerNumber].setVisible(scrollerValue) ;
    scrollDownArray[scrollerNumber].setVisible(scrollerValue) ;
    scrollParentArray[scrollerNumber].setVisible(scrollerValue) ;
  }
  contentParentArray[scrollerNumber].setVisible(scrollerValue) ;
  contentParentAttributes[scrollerNumber]["visibility"] = scrollerValue;
}

// Set the position(horizontal and vertical) of a scrollable layer
function setScrollerPosition(scrollerNumber,scrollerPosX,scrollerPosY) {
  var posXDistance = scrollerPosX - contentParentArray[scrollerNumber].getX();
  scrollUpArray[scrollerNumber].setX(     (scrollUpArray[scrollerNumber].getX()      + posXDistance));
  scrollDownArray[scrollerNumber].setX(   (scrollDownArray[scrollerNumber].getX()    + posXDistance));
  scrollParentArray[scrollerNumber].setX( (scrollParentArray[scrollerNumber].getX()  + posXDistance));
  contentParentArray[scrollerNumber].setX((contentParentArray[scrollerNumber].getX() + posXDistance));

  var posYDistance = scrollerPosY - contentParentArray[scrollerNumber].getY();
  scrollUpArray[scrollerNumber].setY(     (scrollUpArray[scrollerNumber].getY()      + posYDistance));
  scrollDownArray[scrollerNumber].setY(   (scrollDownArray[scrollerNumber].getY()    + posYDistance));
  scrollParentArray[scrollerNumber].setY( (scrollParentArray[scrollerNumber].getY()  + posYDistance));
  contentParentArray[scrollerNumber].setY((contentParentArray[scrollerNumber].getY() + posYDistance));
}

// Set the "Z-Index" of a scrollable layer
function setScrollerZIndex(scrollerNumber,scrollerValue) {
  scrollUpArray[scrollerNumber].setZIndex(scrollerValue);
  scrollDownArray[scrollerNumber].setZIndex(scrollerValue);
  scrollParentArray[scrollerNumber].setZIndex(scrollerValue);
  contentParentArray[scrollerNumber].setZIndex(scrollerValue);
  contentChildArray[scrollerNumber].setZIndex(scrollerValue + 1);
  scrollChildArray[scrollerNumber].setZIndex(scrollerValue + 1);
}

// Writes new content into an existing scrollable layer
function setScrollerHTML(scrollerNumber,scrollerContent) {
  contentChildArray[scrollerNumber].setHTML(scrollerContent);
  initialiseScroller();
  contentChildArray[scrollerNumber].setY(0);
  scrollChildArray[scrollerNumber].setY(0);
}

dynapi.onLoad(initialiseScroller);

document.writeln(writeScrollerStyles);
document.writeln(writeScrollerDivs);
