/**  Version       : 1.3
  *  Last modified : (K. Breynck) 28.02.2008
  *  Author        : BBDO InterOne Hamburg (K. Breynck)
  *  Copyright     : BMW Group 2002-2008
  *
  *  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.
  *
  **/

serverRelativeUrl = location.href.substr(location.href.split("?")[0].substr(0,(location.href.split("?")[0].lastIndexOf(location.host) + location.host.length)).length, location.href.length);

function writeMiniFrame (miniFrameLayer,frameWidth,frameHeight,frameColor,framePosH,framePosV,frameZIndex,frameBorder,alwaysOpen) {
  var myMiniFrameLayer;

  if (miniFrameLayer != null) {
    myMiniFrameLayer = DynLayer.getInline(miniFrameLayer);

    if ((framePosH != null) && (framePosV != null)) { myMiniFrameLayer.setLocation(framePosH,framePosV); }
    if (frameZIndex != null)                        { myMiniFrameLayer.setZIndex(frameZIndex); }
  }

  var thisBorder       = (frameBorder != null) ? frameBorder : 10;
  var miniFrameString  = '<table cellpadding="0" cellspacing="0" width="' + (frameWidth+1) + '" height="' + (frameHeight+1) + '" border="0">';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += ((!alwaysOpen) ? '<td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>' : '<td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>');
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + thisBorder + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + (frameHeight-(thisBorder*2)) + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '" colspan="3"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + thisBorder + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td width="' + thisBorder + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + thisBorder + '" height="1" /></td>';
      miniFrameString += '    <td width="' + (frameWidth-(thisBorder*2)) + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + (frameWidth-(thisBorder*2)) + '" height="1" /></td>';
      miniFrameString += '    <td width="' + thisBorder + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + thisBorder + '" height="1" /></td>';
      miniFrameString += '    <td width="1"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '</table>';
  return miniFrameString;
}

function writeMiniJCWFrame (miniFrameLayer,frameWidth,frameHeight,frameColor,framePosH,framePosV,frameZIndex,frameBorder,alwaysOpen,flagPos,disableLogo) {
  var myMiniFrameLayer;

  if (miniFrameLayer != null) {
    myMiniFrameLayer = DynLayer.getInline(miniFrameLayer);

    if ((framePosH != null) && (framePosV != null)) { myMiniFrameLayer.setLocation(framePosH,framePosV); }
    if (frameZIndex != null)                        { myMiniFrameLayer.setZIndex(frameZIndex); }
  }

  myFlagPos = ((flagPos) ? flagPos : 0);

  var thisBorder       = (frameBorder != null) ? frameBorder : 10;
  var miniFrameString  = '<table cellpadding="0" cellspacing="0" width="' + (frameWidth+1) + '" height="' + (frameHeight+1) + '" border="0">';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += ((!alwaysOpen) ? '<td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>' : '<td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>');
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + thisBorder + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td>';
      miniFrameString += '      <div style="position: relative; left: 0; top: 0; height: 100%; width: 100%; overflow: hidden;">';
      if (!disableLogo){
        miniFrameString += '        <img style="position: absolute; right:9px; bottom: 31px;" src="'+ rootpath + '/_common/_img/mini_jcw_logo.gif" width="96" heigth="17">';
      }
      miniFrameString += '        <div style="position: absolute; left:-' + myFlagPos + 'px; bottom: 0; width:' + frameWidth + 'px; height:20px; background: transparent url('+ rootpath + '/_common/_img/bg_jcw_checker_flag.gif) repeat-x scroll left bottom;"></div>';
      miniFrameString += '      </div>';
      miniFrameString += '    </td>';
      miniFrameString += '    <td bgcolor="' + frameColor + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + (frameHeight-(thisBorder*2)) + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td bgcolor="' + frameColor + '" colspan="3"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '    <td><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="' + thisBorder + '" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '  <tr>';
      miniFrameString += '    <td width="' + thisBorder + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + thisBorder + '" height="1" /></td>';
      miniFrameString += '    <td width="' + (frameWidth-(thisBorder*2)) + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + (frameWidth-(thisBorder*2)) + '" height="1" /></td>';
      miniFrameString += '    <td width="' + thisBorder + '"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + thisBorder + '" height="1" /></td>';
      miniFrameString += '    <td width="1"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
      miniFrameString += '  </tr>';
      miniFrameString += '</table>';
  return miniFrameString;
}


var popupManager = {};
    popupManager.anonymus = [];

function centerPopup(popupName,popupUrl,popupWidth,popupHeight,myWidth,myHeight,myScrollbar) {
	if(!myWidth)     {myWidth = 10;}
	if(!myHeight)    {myHeight = 50;}
	if(!myScrollbar) {myScrollbar = 0;}

	var popupLeft = (window.screen.width/2)  - (popupWidth/2 + myWidth);
	var popupTop  = (window.screen.height/2) - (popupHeight/2 + myHeight);

  if (isString(popupName) && (popupName != "")) {
    popupManager[popupName] = window.open(popupUrl,popupName,("toolbar=no,location=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=no,width=" + popupWidth + ",height=" + popupHeight + ",left=" + popupLeft + ",top=" + popupTop + ",screenX=" + popupLeft + ",screenY=" + popupTop));
    if(popupManager[popupName] != null) {
      popupManager[popupName].focus();
    }  
  } else {
    popupManager.anonymus[popupManager.anonymus.length] = window.open(popupUrl,popupName,("toolbar=no,location=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=no,width=" + popupWidth + ",height=" + popupHeight + ",left=" + popupLeft + ",top=" + popupTop + ",screenX=" + popupLeft + ",screenY=" + popupTop));
    if(popupManager.anonymus[popupManager.anonymus.length-1] != null) {
      popupManager.anonymus[popupManager.anonymus.length-1].focus();
    }  
  }
}

function justifyPopup(popupName,popupUrl,popupWidth,popupHeight,myScrollbar,myLeft,myTop) {
  if(!myScrollbar) {myScrollbar = 0;}
  if(!myLeft)      {myLeft      = 0;}
  if(!myTop)       {myTop       = 0;}

  var popupLeft = ((document.all) ? window.screenLeft + myLeft : window.screenX);
  var popupTop  = ((document.all) ? window.screenTop  + myTop  : window.screenY);

  if (isString(popupName) && (popupName != "")) {
    popupManager[popupName] = window.open(popupUrl,popupName,("toolbar=no,location=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=no,width=" + popupWidth + ",height=" + popupHeight + ",left=" + popupLeft + ",top=" + popupTop + ",screenX=" + popupLeft + ",screenY=" + popupTop));
    if(popupManager[popupName] != null) {
      popupManager[popupName].focus();
    }  
  } else {
    popupManager.anonymus[popupManager.anonymus.length] = window.open(popupUrl,popupName,("toolbar=no,location=no,status=no,menubar=no,scrollbars=" + myScrollbar + ",resizable=no,width=" + popupWidth + ",height=" + popupHeight + ",left=" + popupLeft + ",top=" + popupTop + ",screenX=" + popupLeft + ",screenY=" + popupTop));
    if(popupManager.anonymus[popupManager.anonymus.length-1] != null) {
      popupManager.anonymus[popupManager.anonymus.length-1].focus();
    }  
  }
}

function toolTipInit() { // Call this function to initialise the tool tips. The parameter must be a list of names(String) for all 'toolTipImageLayers'.
  toolTipImgLyr = new Array();

  for(var i=0; i<arguments.length; i++) {
    toolTipImgLyr[arguments[i]] = DynLayer.getInline(arguments[i]);
  }

  toolTipDynLayer      = DynLayer.getInline('toolTipLayer');
}

function writeToolTip(myToolTipImageLayer, myToolTipText, myTooltipMaxWidth, myTooltipModes) {
  if(typeof(toolTipDynLayer) != "undefined") {
  
    var myOffset       = 0;             // Offset of the tool tip layer to the mouse sensitive tool tip image.
    var myBgSpanH      = 5;             // Horizontal span between the the tool tip text to its background border.
    var myBgSpanV      = 5;             // Vertical span between the tool tip text to its background border.
  
    var toolTipTimeOut = 250;
    var activateToolTipLyr;             // Variable for the timeout handling
  
    var newToolTipDynLayerPosX;         // New horizontal position of the tool tip.
    var rightToolTipDynLayerPosX;       // Standard horizontal position of the tool tip, if the browser window is not wide enough.
    var leftToolTipDynLayerPosX;        // Alternative horizontal position of the tool tip, if the browser window is not wide enough.
    var newToolTipDynLayerPosY;         // New vertical position of the tool tip.
    var upToolTipDynLayerPosY;          // Standard vertical position of the tool tip, if the browser window is not wide enough.
    var downToolTipDynLayerPosY;        // Alternative vertical position of the tool tip, if the browser window is not wide enough.
    var toolTipBottomPos;               // Vertical position of the right border of the tool tip.
  
    var toolTipRightPos;                // Hirizontal position of the right border of the tool tip.
  
    var newToolTipContent;
  
    var toolTipDynLayerHeight;
    var toolTipDynLayerWidth  = ((myTooltipMaxWidth) ? myTooltipMaxWidth : 202);
  
    var toolTipImageDynLayer  = toolTipImgLyr[myToolTipImageLayer];
  
    toolTipDynLayer.setVisible(0);
  
    if (myToolTipText) {
  
        newToolTipContent  = '<table cellpadding="0" cellspacing="0" width="' + (toolTipDynLayerWidth) + '" border="0">';
        newToolTipContent += '  <tr>';
        newToolTipContent += '    <td valign="top" width="' + (toolTipDynLayerWidth) + '" colspan="3" bgcolor="#000000"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
        newToolTipContent += '  </tr>';
        newToolTipContent += '  <tr>';
        newToolTipContent += '    <td valign="top" width="1" bgcolor="#000000"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
        newToolTipContent += '    <td valign="top" width="' + (toolTipDynLayerWidth-2) + '">';
  
        newToolTipContent += '      <table cellpadding="0" cellspacing="0" width="' + (toolTipDynLayerWidth-2) + '" border="0" bgcolor="#ffffe1">';
        newToolTipContent += '        <tr>';
        newToolTipContent += '          <td valign="top" width="' + (toolTipDynLayerWidth-2) + '" colspan="3"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + (toolTipDynLayerWidth-2) + '" height="' + (myBgSpanV-2) + '" /></td>';
        newToolTipContent += '        </tr>';
        newToolTipContent += '        <tr>';
        newToolTipContent += '          <td valign="top" width="' + myBgSpanH + '"  ><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + myBgSpanH + '" height="1" /></td>';
        newToolTipContent += '          <td valign="top" width="' + (toolTipDynLayerWidth-(2*myBgSpanH)-2) + '" class="color000000"><span class="font11line13">' + myToolTipText + '</span></td>';
        newToolTipContent += '          <td valign="top" width="' + myBgSpanH + '"  ><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + myBgSpanH + '" height="1" /></td>';
        newToolTipContent += '        </tr>';
        newToolTipContent += '        <tr>';
        newToolTipContent += '          <td valign="top" colspan="3"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="' + (toolTipDynLayerWidth-2) +  '" height="' + (myBgSpanV-2) + '" /></td>';
        newToolTipContent += '        </tr>';
        newToolTipContent += '      </table>';
  
        newToolTipContent += '    </td>';
        newToolTipContent += '    <td valign="top" width="1" bgcolor="#000000"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
        newToolTipContent += '  </tr>';
        newToolTipContent += '  <tr>';
        newToolTipContent += '    <td valign="top" colspan="3" bgcolor="#000000"><img src="'+ rootpath + '/_common/_img/1x1_trans.gif" width="1" height="1" /></td>';
        newToolTipContent += '  </tr>';
        newToolTipContent += '</table>';
  
      toolTipDynLayer.setHTML(newToolTipContent);
      toolTipDynLayerHeight = toolTipDynLayer.getContentHeight();
  
      toolTipRightPos  = toolTipImageDynLayer.getX() + toolTipImageDynLayer.getWidth()  + toolTipDynLayerWidth  + myOffset;
      toolTipBottomPos = toolTipImageDynLayer.getY() + toolTipImageDynLayer.getHeight() + toolTipDynLayerHeight + myOffset;
  
      rightToolTipDynLayerPosX = toolTipRightPos  - toolTipDynLayerWidth;
      downToolTipDynLayerPosY  = toolTipBottomPos - toolTipDynLayerHeight;
      leftToolTipDynLayerPosX  = toolTipImageDynLayer.getX() - toolTipDynLayerWidth  - myOffset;
      upToolTipDynLayerPosY    = toolTipImageDynLayer.getY() - toolTipDynLayerHeight - myOffset;
  
      switch (myTooltipModes) {
      case "1": // down right (if possible)
        newToolTipDynLayerPosX = (((toolTipRightPos)  < dynapi.document.getWidth()) ? rightToolTipDynLayerPosX   : ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX ));
        newToolTipDynLayerPosY = (((toolTipBottomPos) < dynapi.document.getHeight())  ? downToolTipDynLayerPosY  : ((upToolTipDynLayerPosY < 0)   ? 0 : upToolTipDynLayerPosY ));
        break;
      case "2": // down left (if possible)
        newToolTipDynLayerPosX = ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX);
        newToolTipDynLayerPosY = ((toolTipBottomPos < dynapi.document.getHeight())  ? downToolTipDynLayerPosY  : ((upToolTipDynLayerPosY < 0) ? 0 : upToolTipDynLayerPosY ));
        break;
      case "3": // top left (if possible)
        newToolTipDynLayerPosX = ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX);
        newToolTipDynLayerPosY = ((upToolTipDynLayerPosY < 0) ? 0 : upToolTipDynLayerPosY);
        break;
      case "4": // top right align to image top (if possible)
        newToolTipDynLayerPosX = ((toolTipRightPos < dynapi.document.getWidth()) ? rightToolTipDynLayerPosX : ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX ));
        newToolTipDynLayerPosY = ((upToolTipDynLayerPosY < 0) ? 0 : upToolTipDynLayerPosY);
        newToolTipDynLayerPosY += toolTipDynLayerHeight + 4;
        break;
      case "MINIOpenNights": // Special case for the module "MINI Open Nights" 
        newToolTipDynLayerPosX = ((toolTipRightPos < dynapi.document.getWidth()) ? rightToolTipDynLayerPosX : ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX ));
        newToolTipDynLayerPosY = ((upToolTipDynLayerPosY < 0) ? 0 : upToolTipDynLayerPosY);
        newToolTipDynLayerPosY += toolTipDynLayerHeight + 44;
        break;
      default: // top right (if possible)
        newToolTipDynLayerPosX = ((toolTipRightPos < dynapi.document.getWidth()) ? rightToolTipDynLayerPosX : ((leftToolTipDynLayerPosX < 0) ? 0 : leftToolTipDynLayerPosX ));
        newToolTipDynLayerPosY = ((upToolTipDynLayerPosY < 0) ? 0 : upToolTipDynLayerPosY);
        break;
      }
  
      toolTipDynLayer.setX(newToolTipDynLayerPosX);
      toolTipDynLayer.setY(newToolTipDynLayerPosY);
  
      activateToolTipLyr   = window.setTimeout("toolTipDynLayer.setVisible(1)",toolTipTimeOut);
  
    } else {
      clearTimeout(activateToolTipLyr);
      toolTipDynLayer.setHTML('');
      toolTipDynLayer.setX(-1000);
      toolTipDynLayer.setY(-1000);
      toolTipDynLayer.setVisible(0);
    }
  }
}

function loadMainImage(imgPath, imageName) {
  var preload = new Image();
  preload.src = imgPath;
  document.images[imageName].src = preload.src;
}