﻿/*
gmaps.js
Contains all the JavaScript related to Google Maps.
*/

var GLOBALmap;
var DEFAULTZOOMLEVEL = 11;

// Represents which vehicle is selected either highlighted in the iframe or highlighted in a marker or both
var iSelectedEntryID = '';
var sQueryString = '';





/*
	For noresults.jsp
	Draws out a blank map
*/
function drawBlankMap(sLongitude , sLatitude, zip){
    GLOBALmarkers = [];
    GLOBALmap = new GMap2(document.getElementById("map2"));
    GLOBALmap.addControl(new GLargeMapControl());
    
    GLOBALmap.addControl(new GMapTypeControl());
    GLOBALmap.enableDoubleClickZoom();
    
    var startPoint = new GLatLng(0,0);
    
    // This will ensure that if a user doesn't see any results they still see a map of their current location.
    if (!sLongitude || !sLatitude || sLongitude == '' || sLatitude == ''){
        if (!zip || zip == ""){
            GLOBALmap.setCenter(new GLatLng(46.4419, -102.1419), 2);
        }else{
            var geocoder = new GClientGeocoder();
            geocoder.getLatLng(zip, function(point){
                if (point){ GLOBALmap.setCenter(point, DEFAULTZOOMLEVEL); };
            });
        };
    }else{
        GLOBALmap.setCenter(new GLatLng(sLatitude , sLongitude), DEFAULTZOOMLEVEL);
        startPoint = new GLatLng(sLatitude,sLongitude);
    };
    
    var latLngBounds = new GLatLngBounds(startPoint, startPoint);
    GLOBALmap.setCenter(latLngBounds.getCenter());
    
    var boundsZoomLevel = GLOBALmap.getBoundsZoomLevel(latLngBounds);
};

/*
  For results.jsp
  Creates a map, and add points to it
*/
function addPoints(){
    if (aDealerships.length > 0){
        GLOBALmap = new GMap2(document.getElementById("map2"));
        GLOBALmap.addControl(new GLargeMapControl());
        
        GLOBALmap.addControl(new GMapTypeControl());
        GLOBALmap.enableDoubleClickZoom();
        GLOBALmap.setCenter(new GLatLng(37.4419, -122.1419), DEFAULTZOOMLEVEL); //map doesn't work without this line
        
        var startPoint = new GLatLng(aDealerships[0].GetAttribute("Latitude") , aDealerships[0].GetAttribute("Longitude"));
        var latLngBounds = new GLatLngBounds(startPoint, startPoint);

        for (var iCount = 0; iCount < aDealerships.length; iCount++){
            var oDealership = aDealerships[iCount];
            var point = new GLatLng(oDealership.GetAttribute("Latitude") , oDealership.GetAttribute("Longitude"));
            
            latLngBounds.extend(point);
            
            var marker = createMarker(point, oDealership.GetAttribute("DealerNo"));
            
            aDealerships[iCount].Marker = marker;
            
            GLOBALmap.addOverlay(marker);
        };
        
        GLOBALmap.setCenter(latLngBounds.getCenter());
        
        var boundsZoomLevel = GLOBALmap.getBoundsZoomLevel(latLngBounds);
        
        if(boundsZoomLevel > DEFAULTZOOMLEVEL){
            GLOBALmap.setZoom(DEFAULTZOOMLEVEL);
        }else{
            GLOBALmap.setZoom(boundsZoomLevel);
        };
    };
};


 
//For results.jsp
/*
	This has been modified to set icon according to dealer
	stock numbers instead of letters followed by numbers.
*/
function createMarker(point, sDealerNo) {
  /*
  var baseLetterIcon = new GIcon();
  baseLetterIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseLetterIcon.iconSize = new GSize(20, 34);
  baseLetterIcon.shadowSize = new GSize(37, 34);
  baseLetterIcon.iconAnchor = new GPoint(9, 34);
  baseLetterIcon.infoWindowAnchor = new GPoint(9, 2);
  baseLetterIcon.infoShadowAnchor = new GPoint(18, 25);
  */

  var baseNumberIcon = new GIcon();
  baseNumberIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  baseNumberIcon.iconSize = new GSize(27, 40);
  baseNumberIcon.shadowSize = new GSize(37, 34);
  baseNumberIcon.iconAnchor = new GPoint(9, 34);
  baseNumberIcon.infoWindowAnchor = new GPoint(9, 2);
  baseNumberIcon.infoShadowAnchor = new GPoint(18, 25);
  
  var icon = new GIcon(baseNumberIcon);
  
  var oDealership = GetDealershipByAttribute("DealerNo" , sDealerNo);

  var numericValue = parseInt(oDealership.GetNumVehicles());

  icon.image='/images/certified-search/google/markers/' + numericValue + '.png';

  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    ShowDealerInventoryMarker(sDealerNo);
  });
 GEvent.addListener(marker, "mouseover", function() {
    ShowDealerInventoryMarker(sDealerNo);
  });  
 GEvent.addListener(marker, "infowindowbeforeclose", function() {
	if (iSelectedEntryID != '') {
		if (document.getElementById('Detail_' + iSelectedEntryID)) { 
			document.getElementById('Detail_' + iSelectedEntryID).style.backgroundColor='#FFFFFF'; 
		}
		if (window.frames[0].document.getElementById('row_' + iSelectedEntryID)) {
			rowOut(window.frames[0].document.getElementById('row_' + iSelectedEntryID));
		}
	}
	iSelectedEntryID = '';
	
  });  
  
  return marker;

}





/* Marker Content */

// Shows the marker that displays the dealership inventory (with pagination)
function ShowDealerInventoryMarker(sDealerNo , iPageNum) {

	// Close old marker window if it's still open
	if (iSelectedEntryID != '') {
		 GetDealershipByAttribute("DealerNo" , sDealerNo).GetAttribute("Marker").closeInfoWindow();
	} 

	if (iPageNum == undefined) { iPageNum = 1; }

	var oDealership = GetDealershipByAttribute("DealerNo" , sDealerNo);

	var aCarsToShow = Array();
	
	// Get all cars that are in this dealership
	for (var iCount = 0; iCount < aVehicles.length; iCount++) {
		if (aVehicles[iCount].GetAttribute("DealerNo") == sDealerNo) {
			aCarsToShow[aCarsToShow.length] = aVehicles[iCount];
		}
	}
	
	// Pagination setup
	var NUM_RESULTS_PER_PAGE = 10;
	var iBeginIndex = (NUM_RESULTS_PER_PAGE * (iPageNum - 1));
	var iEndIndex = (iBeginIndex + NUM_RESULTS_PER_PAGE) - 1;
	if (iEndIndex > aCarsToShow.length) {
		iEndIndex = aCarsToShow.length - 1;
	}

	var sHTMLContent = '';
	sHTMLContent += "<div>" + "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"background-color:#FFFFFF;width:325px;height: 300px;\">" + "<tr class=\"mapEntry\">" + "<td colspan=\"2\"><span style=\"font-weight:bold;\">" + oDealership.GetAttribute("Name") + "</span><br /><span style=\"color:gray\">" + oDealership.Phone + "</span></td>" + "</tr>" + "<tr valign=\"top\">" + "<td>" + "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"background-color:#FFFFFF;width:200px;\" >";
	for (var iCount = iBeginIndex; iCount <= iEndIndex; iCount++) {
	    sHTMLContent += "<tr class=\"results\" valign=\"top\">" + "<td id=\"Detail_" + aCarsToShow[iCount].GetAttribute("EntryID") + "\" onClick=\"document.location='inventory-vehicle-details.aspx?entryId=" + aCarsToShow[iCount].GetAttribute("EntryID") + "&" + sQueryString + "'; TrackDetail('frommapmodelyear'); \" onmouseover=\"ChangeDetailVehicle('" + aCarsToShow[iCount].GetAttribute("EntryID") + "' , '" + sDealerNo + "');\" >" + aCarsToShow[iCount].GetAttribute("Title") + " - " + aCarsToShow[iCount].GetAttribute("Price") + "</td>" + "</tr>";
	}
	sHTMLContent += "</table>" + "</td>" + "<td id=\"MarkerCarDetail" + sDealerNo + "\"></td>" + "</tr>" + "</table>" + "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"background-color:#FFFFFF;width:325px;\">" + "<tr class=\"smallcopy\">" + "<td colspan=\"2\" align=\"center\">";
	
	// Pagination at bottom of page
	if (iBeginIndex > 0) {
		sHTMLContent += "<a href=\"javascript: ShowDealerInventoryMarker(" + sDealerNo + " , " + (iPageNum - 1) + ");\">Previous</a> | ";
	} else {
		sHTMLContent += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}

	sHTMLContent += (iBeginIndex + 1) + '-' + (iEndIndex + 1) + ' of ' + (aCarsToShow.length);

	if (iEndIndex < (aCarsToShow.length - 1)) {
		sHTMLContent += " | <a href=\"javascript: ShowDealerInventoryMarker(" + sDealerNo + " , " + (iPageNum + 1) + ");\">Next</a>";
	} else {
		sHTMLContent += "<span style=\"color: #ffffff;\">Previous</span>";
	}
	sHTMLContent += "</td></tr></table></div>";

	oDealership.GetAttribute("Marker").openInfoWindowHtml(sHTMLContent);
	
	// Activate first vehicle in list
	ChangeDetailVehicle(aCarsToShow[iBeginIndex].GetAttribute("EntryID") , sDealerNo);

}


// Called when a user rolls over a vehicle in the dealer inventory marker
// This will trigger the highlights to change and the small vehicle detail content area to change
// We need to pass in the dealer number because the <div> that this function replaces content for
//	needs to have a unique name (in this case based on the dealer number). Since there are so many bubbles
//	if all of them have a div called "MarkerCarDetail", this function might overwrite content for the wrong
//	div. Making them all unique solves this problem.
function ChangeDetailVehicle(iEntryID , sDealerNo) {

	var sHTMLContent = '';
	
	var oVehicle = GetVehicleByAttribute("EntryID" , iEntryID);
	
	sHTMLContent += "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"background-color:#FFFFFF;width:175px;height: 220px;border: 1px solid #CCCCCC;\"><tr class=\"smallcopy\" valign=\"top\"><td style=\"height: 85px; vertical-align: top;\" valign=\"top\">";
	// Show image if there is one
	if (oVehicle.GetAttribute("ImageLink") == "") {
	} else if ( (oVehicle.GetAttribute("ImageLink").indexOf("eshopping") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("automobiles") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("staging") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("autosdev") > 0) ) {
		sHTMLContent += "<img src=\"" + oVehicle.GetAttribute("ImageLink") + "\" style=\"height:105px;width:140px;vertical-align:top;\" border=\"0\" />" + "<img src=\"/images/trans.gif\" style=\"height:75px;width:1px;\" border=\"0\" />";
	} else {
		sHTMLContent += "<img src=\"" + oVehicle.GetAttribute("ImageLink") + "\" style=\"height:75px;width:100px;\" border=\"0\" />";
	}
sHTMLContent += "</td>" + "</tr>" + "<tr class=\"smallcopy\" valign=\"top\">" + "<td style=\"height: 115px;\">" + "<b>" + oVehicle.GetAttribute("Title") + "</b>" + "<br />" + oVehicle.GetAttribute("Transmission") + "<br />" + oVehicle.GetAttribute("ExtColor") + "<br />" + oVehicle.GetAttribute("Price") + "<br />" + oVehicle.GetAttribute("Mileage") + "</td>" + "</tr>" + "<tr class=\"smallcopy\" valign=\"top\">" + "	<td style=\"height: 20px;\"><a href=\"inventory-vehicle-details.aspx?entryId=" + oVehicle.GetAttribute("EntryID") + "&" + sQueryString + "\" onclick=\"javascript:TrackDetail('frommapmoreinfo');\">More Info</a></td>" + "</tr>" + "</table>";
	
	// If there is a previously selected entry, turn off the highlight (both in the iframe and the 
	//  dealer inventory list)
	if (iSelectedEntryID != '') {
		if (document.getElementById('Detail_' + iSelectedEntryID)) {
			document.getElementById('Detail_' + iSelectedEntryID).style.backgroundColor='#FFFFFF'; 
		}
		if (window.frames[0].document.getElementById('row_' + iSelectedEntryID)) {
			rowOut(window.frames[0].document.getElementById('row_' + iSelectedEntryID));	
		}
	}
	// Highlight entry in iframe and in dealer inventory list
	rowOver(window.frames[0].document.getElementById('row_' + iEntryID));
	if (document.getElementById('Detail_' + iEntryID)){
	    document.getElementById('Detail_' + iEntryID).style.backgroundColor='#CCCCCC'; 
	};

	// Replace the small vehicle detail area in the marker with content for the new vehicle
	// Every "MarkerCarDetail" div has a unique id (based on dealer number)
	if (document.getElementById('MarkerCarDetail' + sDealerNo)){
	    document.getElementById('MarkerCarDetail' + sDealerNo).innerHTML = sHTMLContent;
	};

	iSelectedEntryID = iEntryID;

}



// Show vehicle detail marker (from iframe list)
function ShowVehicleDetail(iEntryID){
    // Don't reload popup if it's already selected
	if ( (iSelectedEntryID != '') && (iSelectedEntryID == iEntryID) ) return;
	
	var oVehicle = GetVehicleByAttribute("EntryID" , iEntryID);
	
	var oDealership = oVehicle.GetDealership();

	var sHTMLContent = '';

	sHTMLContent += '<div><table border=\"0\" cellpadding=\"2\" cellspacing=\"0\" style=\"background-color:#FFFFFF;width:300px;\"><tr class=\"mapEntry\"><td>';
	
	if (oVehicle.GetAttribute("ImageLink") == "") {
		sHTMLContent += '&nbsp;';
	} else if ( (oVehicle.GetAttribute("ImageLink").indexOf("eshopping") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("automobiles") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("staging") > 0) || (oVehicle.GetAttribute("ImageLink").indexOf("autosdev") > 0) ) {
		sHTMLContent += "<img src=\"" + oVehicle.GetAttribute("ImageLink") + "\" style=\"height:105px;width:140px;\" border=\"0\" />";
	} else {
		sHTMLContent += "<img src=\"" + oVehicle.GetAttribute("ImageLink") + "\" style=\"height:75px;width:100px;\" border=\"0\" />";
	}

sHTMLContent += "</td>" + "<td style=\"padding-left:15px;\"><span style=\"font-weight:bold;\">" + oDealership.GetAttribute("Name") + "</span><br /><span style=\"color:gray\">" + oDealership.Phone + "</span></td>" + "</tr>" + "<tr class=\"mapEntry\">" + "<td colspan=\"2\">" + oVehicle.GetAttribute("Title") + "</td>" + "</tr>" + "<tr class=\"mapEntry\">" + "<td colspan=\"2\">" + oVehicle.GetAttribute("Transmission") + "</td>" + "</tr>" + "<tr class=\"mapEntry\">" + "<td colspan=\"2\">" + oVehicle.GetAttribute("ExtColor") + "</td>" + "</tr>" + "<tr class=\"mapEntry\" style=\"font-weight:normal\">" + "<td colspan=\"2\">" + oVehicle.GetAttribute("Price") + "</td>" + "</tr>" + "<tr class=\"mapEntry\" style=\"font-weight:normal\">" + "<td colspan=\"2\">" + oVehicle.GetAttribute("Mileage") + "</td>" + "</tr>" + "<tr class=\"mapEntry\" style=\"font-weight:normal\">" + "<td colspan=\"2\"><a href=\"inventory-vehicle-details.aspx?entryId=" + oVehicle.GetAttribute("EntryID") + "&" + sQueryString + "\" onclick=\"javascript:TrackDetail('frommap');\">View vehicle details</a></td>" + "</tr>" + "<tr class=\"mapEntry\" style=\"font-weight:normal\">" + "<td colspan=\"2\"><a href=\"javascript: ShowDealerInventoryMarker(" + oDealership.GetAttribute("DealerNo") + ");\">View similar models at this dealer</a></td>" + "</tr>";

	sHTMLContent = sHTMLContent.replace(/undefined/gi,'');

	oDealership.GetAttribute("Marker").openInfoWindowHtml(sHTMLContent);
	
	rowOver(window.frames[0].document.getElementById('row_' + iEntryID));

	iSelectedEntryID = iEntryID;

}



// Dealership overview marker
function GetDealershipOverviewHTML(sDealerNo) {

	var sReturn = '';
	
	var oDealership = GetDealershipByAttribute("DealerNo" , sDealerNo);

	sReturn += '<div>'
	+ '<table border="0" cellpadding="2" cellspacing="0" style="background-color:#FFFFFF;width:250px;">'
	+ '<tr class="mapEntry">'
	+ '<td><a href="javascript: ShowDealerInventoryMarker(' + sDealerNo + ');" >Click to view vehicle(s)</a> at <br /><span style="font-weight:bold;">' + oDealership.GetAttribute("Name") + '</span> <span style="color:#ff0000">' + oDealership.GetAttribute("Phone") + '</span></td>';
	+ '</tr>'
	+ '</table>'
	+ '</div>';
	
	return sReturn;

}


function TrackDetail(Key) {
    s.linkTrackVars = 'prop26,prop27,prop37';
    switch (Key) {
        case 'fromlist':
            s.pageName = 'GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            s.channel = 'GOOGLE CUSTOM SEARCH';
            s.prop37 = 'GOOGLE CUSTOM SEARCH:GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            CallSDotTL(s, true, 'GOOGLE CUSTOM SEARCH:SEE DETAILS LINK', 'o');
            break;
        case 'frommap':
            s.pageName = 'GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            s.channel = 'GOOGLE CUSTOM SEARCH';
            s.prop37 = 'GOOGLE CUSTOM SEARCH:GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            CallSDotTL(s, true, 'GOOGLE CUSTOM SEARCH:VIEW VEHICLE DETAILS LINK', 'o');
            break;
        case 'frommapmodelyear':
            s.pageName = 'GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            s.channel = 'GOOGLE CUSTOM SEARCH';
            s.prop37 = 'GOOGLE CUSTOM SEARCH:GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            CallSDotTL(s, true, 'GOOGLE CUSTOM SEARCH:VEHICLE MODEL AND YEAR TEXT LINK', 'o');
            break;
        case 'frommapmoreinfo':
            s.pageName = 'GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            s.channel = 'GOOGLE CUSTOM SEARCH';
            s.prop37 = 'GOOGLE CUSTOM SEARCH:GOOGLE CUSTOM SEARCH RESULTS ABT TRIAL 2 CREATIVE 927';
            CallSDotTL(s, true, 'GOOGLE CUSTOM SEARCH:MORE INFO ABOUT VEHICLE LINK', 'o');
            break;
    }
}



/* Dealerships */

var aDealerships = Array();

function Dealership(iMapIndex , sName , sPhone , sDealerNo , sLatitude , sLongitude) {
	this.MapIndex = iMapIndex;
	this.Name = sName;
	this.Phone = sPhone;
	this.DealerNo = sDealerNo;
	this.Latitude = sLatitude;
	this.Longitude = sLongitude;
	
	this.GetAttribute = function(sAttributeName) {

		var sReturn = '';
		eval ("sReturn = this." + sAttributeName);
		return sReturn;

	}
	
	this.GetNumVehicles = function() {
	
		var iTotalVehicles = 0;

		for (var iCount = 0; iCount < aVehicles.length; iCount++) {
			if (aVehicles[iCount].GetAttribute("DealerNo") == this.GetAttribute("DealerNo")) {
				iTotalVehicles++;
			}
		}
	
		return iTotalVehicles;
	
	}

}



function AddDealership(iMapIndex , sName , sPhone , sDealerNo , sLatitude , sLongitude) {

	// Check to ensure that dealership hasn't already been added
	for (var iCount = 0; iCount < aDealerships.length; iCount++) {
		if (aDealerships[iCount].GetAttribute("DealerNo") == sDealerNo) {
			return;
		}
	}
	
	var oDealership = new Dealership(iMapIndex , sName , sPhone , sDealerNo , sLatitude , sLongitude);
	aDealerships[aDealerships.length] = oDealership;

}


function GetDealershipByAttribute(sAttributeName , sValue) {

	for (var iCount = 0; iCount < aDealerships.length; iCount++) {
		if (aDealerships[iCount].GetAttribute(sAttributeName) == sValue) {
			return aDealerships[iCount];
		}
	}
	
	return null;

}





/* Vehicles */

var aVehicles = Array();

function Vehicle(iEntryID , iDealershipID , sImageLink , sTitle , sTransmission , sExtColor , sPrice , iMileage , sModel) {
	this.EntryID = iEntryID;
	this.DealerNo = iDealershipID;
	this.ImageLink = sImageLink;
	this.Title = sTitle;
	this.Transmission = sTransmission;
	this.ExtColor = sExtColor;
	this.Price = sPrice;
	this.Mileage = iMileage;
	this.Model = sModel;
	this.Marker = '';

	this.GetAttribute = function(sAttributeName) {

		var sReturn = '';
		eval ("sReturn = this." + sAttributeName);
		return sReturn;

	}
	
	this.GetDealership = function() {

		return oDealership = GetDealershipByAttribute("DealerNo" , this.GetAttribute("DealerNo"));

	}

}


function AddVehicle(iEntryID , iDealershipID , sImageLink , sTitle , sTransmission , sExtColor , sPrice , iMileage , sModel) {

	var oVehicle = new Vehicle(iEntryID , iDealershipID , sImageLink , sTitle , sTransmission , sExtColor , sPrice , iMileage , sModel);
	aVehicles[aVehicles.length] = oVehicle;

}

function GetVehicleByAttribute(sAttributeName , sAttributeValue) {

	for (var iCount = 0; iCount < aVehicles.length; iCount++) {
		if (aVehicles[iCount].GetAttribute(sAttributeName) == sAttributeValue) {
			return aVehicles[iCount];
		}
	}
	
	return null;

}

