// 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();
var browser = new browserStats();


// **Modified 10.2003 SConnelly - changed path, added variable

var cssPath = "/crossbrand/uconnect/'+brand+'/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
function wrap( argApp, argTracking, argFamily, argModel ) {

  argApp = argApp.toLowerCase();
  var sizedWin = false;
  
  //if( argApp == "cdl" ) argApp = "dealer";
  
  var launchUrl = "/bridge/index.html?";
  if( argTracking != '' ) launchUrl += argTracking + "&";
  launchUrl += "app=" + argApp + "&family=" + argFamily + "&model=" + argModel;
  
  if( argApp == "dealer" || argApp == "edmunds" || argApp == "fulfillment" || argApp == "gaq" || argApp == "gap" || argApp == "incentives" || argApp == "newinventory" || argApp == "preownedinventory" ) {
    sizedWin = true;
    var winWidth = 618, winHeight = 452;
    if( argApp == "edmunds" || argApp == "gap" || argApp == "preownedinventory" ) {
      var winWidth = 790, winHeight = ( window.screen.availHeight - 130 );
    }
    var 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( sizedWin ) {
    window.open( launchUrl, "universal", options );
  } else {
    window.location = launchUrl;
  }
}

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;
}