/** Version       : 1.2
  * Last modified : (K. Breynck) 02.03.2004
  * Author        : BBDO InterOne Hamburg (W. Rauch)
  * 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 actualFramesetPath;
var contentFrameSource;

var defaultPage      = "content.jsp"; //Default name for the default content-Page
var framesetPage     = "index.jsp";   //Name of the Frameset-Page(s)
var initContentURL;
var allowedDomain    = new Array();
    allowedDomain[0] = "http://www.mini.com";

var domainIsAllowed;

function checkForFrameset(indexDistanceIndicator) {
  var myPath = self.location.href.split("?")[0];
  var myName = myPath;
  var newLocation ="";

  if(!(parent.frames.length > 2)) {
    if(!isNaN(parseInt(indexDistanceIndicator))) { // Use the indexDistanceIndicator to locate the nearest higher-level frameset
      for (i=0; i <= parseInt(indexDistanceIndicator); i++) {
        myPath = myPath.substr(0,myPath.lastIndexOf("/"));
      }
      myPath     += "/";
      myName      = myName.substring(myPath.length, myName.length);
      newLocation = myPath + framesetPage + "?content=" + myName + escape(self.location.search);
    } else { // Extract the filename and append it to the frameset page in that folder
      myName = myName.substr(myName.lastIndexOf("/")+1,myName.length);
      myPath = self.location.href.split("?")[0];
      myPath = myPath.substr(0,myPath.lastIndexOf("/")+1);
      newLocation = myPath + framesetPage + "?content=" + myName + escape(self.location.search);
    }
    self.location.href = newLocation;
  }
}

function checkNscForFrameset(framesetPath) {
  var tempUrl  = self.location.href.split("://")[0] + "://";
      tempUrl += escape(self.location.href.substr(tempUrl.length,self.location.href.length));
  if(!(parent.frames.length > 2)) {
    top.location.href = framesetPath + '?content=' + tempUrl;
  }
}

function getFramesetPath() {
  var tempSubstr;
  if(location.host) {  // server
    tempSubstr = location.href.substr(location.href.split("?")[0].substr(0,(location.href.split("?")[0].lastIndexOf(location.host) + location.host.length)).length, location.href.length);
    tempSubstr = tempSubstr.split("?")[0].substr(0,(tempSubstr.split("?")[0].lastIndexOf("/")+1));
  } else { // local
    tempSubstr = location.href.split("?")[0];
    tempSubstr = tempSubstr.substr(0,(tempSubstr.lastIndexOf("/")+1));
  }
  return tempSubstr;
}

function pageHandler() {
  contentFramePath      = ((contentFramePath.indexOf(location.host) != -1) ? contentFramePath.split(top.location.host)[1] : contentFramePath);
  actualFramesetPath    = ((contentFramePath.indexOf("://") != -1) ? "" : getFramesetPath());
  contentFrameParam     = (((!location.query.content) && (location.search != "")) ? location.search : contentFrameParam);
  contentFramePath      = (((contentFramePath.indexOf("://") != -1)) ? contentFramePath : ((contentFramePath.lastIndexOf(actualFramesetPath) != -1) ? contentFramePath.substr((contentFramePath.lastIndexOf(actualFramesetPath) + actualFramesetPath.length), contentFramePath.length) : contentFramePath));
  contentFrameSource    = ((flashCheckIsActive) ? flashCheckSource + "?contentFrameSource=" + actualFramesetPath + contentFramePath + escape(contentFrameParam) : actualFramesetPath + contentFramePath + contentFrameParam);

  if(location.query.content) {
    if(location.query.content.indexOf("://") != -1) {
      for (var i=0; i < allowedDomain.length; i++) {
        if(location.query.content.indexOf(allowedDomain[i]) != -1) {
          domainIsAllowed = true;
          break;
        }
      }
      if(domainIsAllowed) {
        initContentURL = ((flashCheckIsActive) ? flashCheckSource + "?contentFrameSource=" + escape(location.query.content) : location.query.content);
      } else {
        if(contentFrameSource != defaultPage) {
          initContentURL = contentFrameSource;
        } else {
          initContentURL = defaultPage;
        }
      }
    } else {
      initContentURL = ((flashCheckIsActive) ? flashCheckSource + "?contentFrameSource=" + actualFramesetPath + escape(location.query.content) : actualFramesetPath + location.query.content);
    }
  } else {
    if(contentFrameSource != defaultPage) {
      initContentURL = contentFrameSource;
    } else {
      initContentURL = defaultPage;
    }
  }
}
