// browser detection
function browserStats() {
  var ua = " " + navigator.userAgent.toLowerCase();
  this.nn = ua.indexOf( "mozilla" ) > 0;
  ua.indexOf( "compatible" ) > 0 ? this.nn = false : "";
  this.ie = ua.indexOf( "msie" ) > 0;
  this.version = parseInt( navigator.appVersion );
  this.mac = ua.indexOf( "mac" ) > 0;
  this.ie ? ua.indexOf( "msie 5" ) > 0 ? this.version = 5 : "" : "";
  this.ie ? ua.indexOf( "msie 6" ) > 0 ? this.version = 6 : "" : "";
  return this;
}

var b = new browserStats();


// browser/platform font size cascade
// Removed for global Nav changes -- WPO 38872
/*var cssPath = prefix + "css/", cssFile;
//alert(" PERFRIX " +prefix);
//var cssPath = "/css/", cssFile;
b.mac && b.nn && b.version >= 5 ? cssFile = "half_size_smaller.css" : b.mac && b.version >= 5 || !b.mac && b.ie || b.nn && b.version >= 5 ? cssFile = "one_size_smaller.css" : cssFile;
cssFile ? document.write( '<link rel="stylesheet" href="' + cssPath + cssFile + '" type="text/css" />' ) : "";
*/

// netscape browser resize fix
if( b.nn ) {
    widthCheck = window.innerWidth;
    heightCheck = window.innerHeight;
    window.onresize = resizeFix;
}

function resizeFix() {
    widthCheck != window.innerWidth || heightCheck != window.innerHeight ? window.location.reload( true ) : "";
}

// div img rollovers

//Universal image swap function
function swapImg(argImageName, argImageSrc, argDivArray){
  var divArray = "" ;
  //Check to see if the target image is in a div or layer, then switch images.
  if (document.layers && argDivArray){
    for( var i = 0; i < argDivArray.length; i++ ) {
     divArray += 'document.' + argDivArray[i] + '.';
    }
     eval(divArray + 'document.' + argImageName + '.src ="' + argImageSrc + '";');
     return true;
  }else{
    document[argImageName].src = argImageSrc;
  }
}


//MSRP access function: uses /crossbrand/msrpus/jmsrpus.js
function getMSRP(argVehicleID,argModelID) {
  for( i=0; i<models.length; i++ ) {
    if ( models[i].vehicle == argVehicleID && models[i].id == argModelID ) {
          return '$' + models[i].msrp + '.00'; // do any additional formatting here.
    }
  }
  return "Please see your dealer for pricing.";
}

//Universal function to get parameter from url srting.
function getParameter( argParam ) {
  var queryString = new String( location.search.substring(1, location.search.length) );
  var params = queryString.split( "&" );
  var val = "";
  if( argParam ) {
   for(i=0;i<params.length;i++) {
    if( params[i].split( "=" )[0] == argParam )
     val = params[i].split( "=" )[1];
   }
   return val;
  }
}

//Universal function for no cache
function nocacheRandom() {
  var axel = Math.random() + "";
  var ord = axel * 1000000000000000000;
  return ord;
}

// generate random number within range passed in
function generateRandomNumber( argRange ) {
  return 1 + Math.round( Math.random() * ( argRange - 1 ) );
}

//Universal Window Function\
function flexWin(argUrl,argScrollbar,argWidth,argHeight,argWinName,argOptions,argXpos,argYpos,argCloseWin){
 //Set defaults for new window - All options on by default except scrollbars
  var winName = argWinName ? argWinName : "flexWin" + nocacheRandom();
  //If you pass "all" for window options, it will turn on everything except scroll bar.
  if(argOptions == "all"){
  	argOptions = "history=1,toolbar=1,location=1,directories=1,status=1,menubar=1,resizable=1";
  }
  var windowOptions = argOptions ? argOptions : "history=0,toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0";
  var winWidth = argWidth ? argWidth : 800;
    windowOptions += ",width=" + winWidth;
  var winHeight = argHeight ? argHeight : 600;
    windowOptions += ",height=" + winHeight;
  var scrollbar = argScrollbar ? ",scrollbars=yes" : ",scrollbars=no";
    windowOptions += scrollbar;
 //Position window
  var winXpos = argXpos ? argXpos : ( screen.availWidth - winWidth ) / 2;
    windowOptions += ",left=" + winXpos + ",screenX=" + winXpos;
  var winYpos = argYpos ? argYpos : ( ( screen.availHeight - winHeight ) / 2 ) - 40;
    windowOptions += ",top=" + winYpos + ",screenY=" + winYpos;
 //Open Window
 //alert(winName);
  window.open(argUrl,winName,windowOptions);
//Close window if asked to
  argCloseWin ? window.close() : "";
}

//universal popup
var wrapMsg = "";
wrapMsg += 'The contents in the current "Build Your Own" window will be erased   \n';
wrapMsg += 'and a new session will be started. Click OK to continue and restart,\n';
wrapMsg += 'or Cancel to retain the contents of the window.';
var pandeWindow = null;
var universalWindow = null;

function wrap(argApp, argTracking, argFamily, argModel) {
  argApp = argApp.toLowerCase();
  var launchUrl = "/bridge/index.html?";
  if( argTracking != '' ) launchUrl += argTracking + "&";
  launchUrl += "app=" + argApp + "&family=" + argFamily + "&model=" + argModel;

  if( argApp == "priceequip" ) {
    if( b.ie && b.mac && b.version < 5 ) {
      openit( argApp, launchUrl );
    } else {
      if( pandeWindow && !pandeWindow.closed ) {
        if( confirm( wrapMsg ) ) {
          pandeWindow.focus();
          window.pandeWindow.location = launchUrl;
        } else {
          pandeWindow.focus();
        }
      } else {
        openit( argApp, launchUrl );
      }
    }
  } else {
    openit( argApp, launchUrl );
  }
  
  // nested function
  function openit( argApp, launchUrl ) {
    var winWidth = 618, winHeight = 452;
    if( argApp == "priceequip" || argApp == "myfolder" || argApp == "vehicleselector" || argApp == "vehiclespecs" || argApp == "preownedinventory" || argApp == "gap" )
      winWidth = 790;
    if( window.screen && argApp == "priceequip" || argApp == "myfolder" || argApp == "vehicleselector" || argApp == "vehiclespecs" || argApp == "preownedinventory" || argApp == "gap" )
      winHeight = ( window.screen.availHeight - 130 );
    var options="";
    if (argApp == "virtualtour" ) {
      //special cases for virtual tour...override window size and options (none)
      winWidth = 720;
      winHeight = 420;
    } else {
      options+= "resizable,scrollbars,toolbar,";
    }
    options += "width=" + winWidth + ",height=" + winHeight;
    if( window.screen ) {
      var xPos = ( screen.availWidth - winWidth ) / 2;
      var yPos = ( ( screen.availHeight - winHeight ) / 2 ) - 40;
      options += ",left=" + xPos + ",screenX=" + xPos;
      options += ",top=" + yPos + ",screenY=" + yPos;
    }
   if( argApp == "priceequip" ) pandeWindow = window.open( launchUrl, "priceequip", options );
     else if( argApp == "dealer" ) window.location = launchUrl;
     else universalWindow = window.open( launchUrl, "universal", options );
  }
  // end nested function
}

function askWin() {
  var winWidth = 618, winHeight = 452;
  var options = "resizable,scrollbars,toolbar,"
    options += "width=" + winWidth + ",height=" + winHeight;
  if( window.screen ) {
      var xPos = ( screen.availWidth - winWidth ) - 20;
      var yPos = ( ( screen.availHeight - winHeight ) / 2 ) - 40;
      options += ",left=" + xPos + ",screenX=" + xPos;
      options += ",top=" + yPos + ",screenY=" + yPos;
    }
  window.open( "", "askWindow", options );
  return true;
}

// enable measurement parameters
function promolink(argURL,argType) {
	window.location= argURL + '?context=' + context + '&type='+ argType;
}
