// Version       : 1.0
// Last modified : 18.08.2003
// Author        : BBDO InterOne Hamburg (P. Seliger)
// Copyright     : BMW Group 2002-2003

// 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.


// this function returns an array of teaser-objects according to the pages pathname where the teasers have to occour; 
function getBigTeaserArray() {
  splitSearchstring();
  var contentPath = "";
  var subjectMatch = false;
  var subject = "";
  if(query.contentPath){
    contentPath = query.contentPath.toLowerCase();
    for (subject in bigTeasersMap) {
      if (subject != "home") {
        if (contentPath.indexOf(bigTeasersMap[subject].toLowerCase()) >= 0) {
          subjectMatch = true;
          break;
        }
      }
    }
    if (!subjectMatch) {
      subject = "home";
    }
  }else{
    subject = "home";
  }
  return bigTeasers[subject];
}
var bigTeaserArray = getBigTeaserArray();


// preloading & extending teaser objects within "bigTeaserArray" by some properties;
function preloadBigTeaserImages() {
  var imageObject = null;
  var imageSource = "";
  var mouseoverSource = "";
  for (var i=0;i<bigTeaserArray.length;i++) {
    imageSource = bigTeaserArray[i].imageSource;
    if ((imageSource.search(/\.gif$/i) >= 1) || (imageSource.search(/\.jpg$/i) >= 1) || (imageSource.search(/\.png$/i) >= 1)) {
      bigTeaserArray[i].img = new Object();
      imageObject = bigTeaserArray[i].img;
      imageObject.regular = new Image();
      imageObject.regular.src = genericpath + imageSource;
      mouseoverSource = bigTeaserArray[i].mouseoverSource;
      if (mouseoverSource && (mouseoverSource != "")) {
        if ((mouseoverSource.search(/\.gif$/i) >= 1) || (mouseoverSource.search(/\.jpg$/i) >= 1) || (mouseoverSource.search(/\.png$/i) >= 1)) {
          imageObject.over = new Image();
          imageObject.over.src = genericpath + mouseoverSource;
          imageObject.name = /\/([^\/\.]*)\.[a-zA-Z]{3,}$/.exec(imageSource)[1].toLowerCase().replace(/_/g,""); // image-objekte mit mouseover-effekt benoetigen einen namen; // image objects featuring a mouseover effect do need a name;
          addJpgToPictureList(imageObject.name,imageObject.regular.src,imageObject.over.src); // fuegt images, die einen mouseover-effekt haben sollen, als "activeImageObj"-objekte der liste "pictures" hinzu; // adds images that are supposed to show a mouseover effect as "activeImageObj"-objects to the "pictures"-list;
        }
      }
    }
  }
}
preloadBigTeaserImages(); // preload now;


// show & rotate;
function keepBigTeaserRotating(pointer) {
  clearTimeout(timeoutIdRotation);
  var canBlend = ((pointer) ? (true) : (false));
  pointer = ((pointer) ? ((pointer >= bigTeaserArray.length) ? (0) : (pointer)) : (0));
  var imageObject = bigTeaserArray[pointer].img;
  var teaserContent = "";
  if (imageObject) {
    var reference = bigTeaserArray[pointer].link;
    var hrefTarget = bigTeaserArray[pointer].target;
    hrefTarget = ((hrefTarget && (hrefTarget != "")) ? (hrefTarget) : ((reference.search(/^\s*http:\/\//) >= 0) ? ("_blank") : ((reference.search(/^\s*javascript:/) >= 0) ? ("_self") : ("_top"))));
    var mouseEvent = ((imageObject.over) ? (' onmouseover="breakRotation();mouseOver(\'' + imageObject.name + '\',\'bigTeaser\');top.status=\'' + bigTeaserArray[pointer].statusText + '\';return true" onmouseout="mouseOut(\'' + imageObject.name + '\',\'bigTeaser\');continueRotation(' + pointer + ');top.status=\'\';return true"') : (' onmouseover="top.status=\'' + bigTeaserArray[pointer].statusText + '\';return true" onmouseout="top.status=\'\';return true"'));
    var teaserLink = ((hrefTarget == "_top") ? (genericpath + reference) : (reference));
    teaserContent = '<a href="' + teaserLink + '" target="' + hrefTarget + '"' + mouseEvent + '><img src="' + imageObject.regular.src + '" ' + ((imageObject.name) ? ('name="'+imageObject.name+'" ') : ('')) + 'width="' + imageObject.regular.width + '" height="' + imageObject.regular.height + '" style="filter:blendTrans(Duration=1, Transition=2)" border="0" /></a>';
  } else if ((bigTeaserArray[pointer].imageSource.search(/\.swf$/i) >= 1) || (bigTeaserArray[pointer].imageSource.search(/\.swf\?/i) >= 1)) { // nach "http://www.bmw.com/generic/_common/js/.swf" duerfen auch noch parameter aufgefuehrt werden; // "http://www.bmw.com/generic/_common/js/.swf" also can be followed by some parameters;
    var imageSource = bigTeaserArray[pointer].imageSource;
    if ((imageSource.search(/[a-zA-Z0-9_]*\=http\:\/\//gi) == -1) && ((imageSource.search(/\?/) >= 0) && (imageSource.substring(imageSource.search(/\?/)).search(/\//) >= 0))) {
      imageSource = genericpath + imageSource.replace(/([a-zA-Z0-9_]*\=)([a-zA-Z0-9_\/]*)/gi,"$1"+genericpath+"$2");
    } else {
      imageSource = genericpath + imageSource;
    }
    teaserContent = ''+
    '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + bigTeaserArray[pointer].width + '" height="' + bigTeaserArray[pointer].height + '" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'+
    '<param name="movie" value="' + imageSource + '">'+
    '<param name="play" value="true">'+
    '<param name="loop" value="true">'+
    '<param name="quality" value="high">'+
    '<param name="bgcolor" value="' + bigTeaserArray[pointer].bgColor + '">'+
    '<param name="menu" value="false">'+
    '<embed src="' + imageSource + '" width="' + bigTeaserArray[pointer].width + '" height="' + bigTeaserArray[pointer].height + '" play="true" loop="true" bgcolor="' + bigTeaserArray[pointer].bgColor + '" quality="high" menu="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'+
    '</object>';
  }
  if (teaserContent != "") {
    writeIntoLayer("bigTeaser",teaserContent);
    if (bigTeaserArray.length > 1) {
      timeoutIdRotation = setTimeout("keepBigTeaserRotating("+(++pointer)+")",rotationDelay);
      rotationTimeBegin = new Date();
    }
  }
}
var timeoutIdRotation = null;

// interrupts or continues teaser rotation;
function breakRotation() {
  if (bigTeaserArray.length > 1) {
    rotationTimeLeft = rotationDelay - ((new Date()) - rotationTimeBegin);
    clearTimeout(timeoutIdRotation);
    mouseoverTimeBegin = new Date();
  }
}

function continueRotation(pointer) {
  if (bigTeaserArray.length > 1) {
    mouseoverTimeEnd = new Date();
    if ((mouseoverTimeEnd - mouseoverTimeBegin) >= rotationTimeLeft) {
      keepBigTeaserRotating(++pointer);
    } else {
      timeoutIdRotation = setTimeout("keepBigTeaserRotating("+(++pointer)+")",(rotationTimeLeft-(mouseoverTimeEnd-mouseoverTimeBegin)));
    }
  }
}

var rotationTimeBegin = 0;
var rotationTimeLeft = 0;
var mouseoverTimeBegin = 0;
var mouseoverTimeEnd = 0;
