// 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
/*var cssPath = prefix + "css/", cssFile;

b.mac && b.nn && b.version >= 5 || b.nn && b.version >= 5 ? cssFile = "half_size_smaller.css" : b.mac && b.version >= 5 || !b.mac && b.ie ? cssFile = "one_size_smaller.css" : cssFile;
cssFile ? document.write( '<link rel="stylesheet" href="' + cssPath + cssFile + '" type="text/css" />' ) : "";
//alert(cssFile);
//Global 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 ) : "";}

//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 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;
  }
}

//function to get msrp for model
function getMSRP(argModelId) {
  for( i=0; i<models.length; i++ ) {
    if( models[i].id == argModelId ) {
      return '$' + models[i].msrp; // do any additional formatting here.
    }
  }
  return "See dealer for pricing.";
}
		
//function to get starting @ price for family.
function getStartingAtPrice(argVehicle){
  if(argVehicle == "grand_caravan_cv"){
    for( i=0; i<models.length; i++ ) {
      if(models[i].id == argVehicle) {
        return 'Starting @ $' + models[i].msrp + '**'; // do any additional formatting here.
      }
    }
  }else{
    for( i=0; i<models.length; i++ ) {
      if(models[i].vehicle == argVehicle) {
        if(models[i].msrp == ""){
          return "See dealer for pricing.";
        }else{
          return 'Starting @ $' + models[i].msrp + '**'; // do any additional formatting here.
        } 
      }
    }
  }
}
  
//Universal function to take user to a page in main window
function targetMainWindow(argUrl) {
  if (window.blur){
	self.blur();
	}
  if (window.opener.closed){
    window.open(argUrl,"newWin");
   }else{
    window.opener.location = argUrl;
   }
}

//Universal function for no cache
function nocacheRandom() {
  var axel = Math.random() + "";
  var ord = axel * 1000000000000000000;
  return ord;
}

//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 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 = "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;
    }
    //alert(launchUrl);
    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;
}








