if (typeof vcc != "object")
	vcc = new Object();
	
if (typeof vcc.dl != "object")
	vcc.dl = new Object();

if (typeof vcc.dl.ws != "object")
	vcc.dl.ws = new Object();

vcc.dl.ws.getDealersByGeoCode = function() {
	if (!vcc.dl.CheckIfPlotDealers())
		return;
	vcc.dl.map.Clear();
	vcc.dl.showLoading();
	var centerLatLong = vcc.dl.map.GetCenter();
	var sFilter = vcc.dl.getFiltering();
	VolvoCars.Web.Sites.WebServices.DealerLocator.GetDealersByGeoCode(vcc.dl.sMarket, vcc.dl.sLanguage, vcc.dl.bMiles, centerLatLong.Latitude, centerLatLong.Longitude, vcc.dl.getMapheightInScaleUnit(), sFilter, vcc.dl.ws.getDealersByGeoCode_CallBack, vcc.dl.ws.handleFailedCallback);
}

vcc.dl.ws.getDealersByGeoCode_CallBack = function(result, eventArgs) {
	eval(result);
	vcc.dl.addDealersToMap();
	vcc.dl.createDealerList(result)
	vcc.dl.hideLoading();
}

vcc.dl.ws.getDrivingInstructions = function(sAddressId, sCityId, sZipId) {
	document.getElementById("driving-error").innerHTML = "";
	var sAddress = document.getElementById(sAddressId).value;
	var sCity = document.getElementById(sCityId).value;
	var sZip = document.getElementById(sZipId).value;
	vcc.dl.showLoadingRoute();
	VolvoCars.Web.Sites.WebServices.DealerLocator.GetDrivingDirectionsStartPoint(vcc.dl.sMarket, vcc.dl.sLanguage, vcc.dl.bMiles, sCity, sAddress, sZip, vcc.dl.ws.getDrivingInstructions_CallBack);
}

vcc.dl.ws.getDrivingInstructions_CallBack = function(result, eventArgs) {
	vcc.dl.hideLoadingRoute();
	document.getElementById("driving-error").innerHTML = "";
	if (result[0] == 0 && result[1] == 0) {
		document.getElementById("driving-error").innerHTML = vcc.dl.getText("ErrorNoDirections");
		return;
	}
	var win = vcc.openWindow("","directions","width=670,height=780,scrollbars=yes,status=yes");
	var sOutput = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
	sOutput += "<html><head>";
	sOutput += "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=7\" />";
	sOutput += "<title>" + vcc.dl.getText("DrivingDirectionsResultHeader") + "</title>";
	sOutput += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
	sOutput += "<style type=\"text/css\">\n";
	sOutput += "@import url(/_layouts/VolvoCars.Web.Sites/CSS/General.css?v=" + vcc.sAppVersion + ");\n";
	sOutput += "@import url(/_layouts/VolvoCars.Web.Sites/CSS/GlobalDealerLocator.css?v=" + vcc.sAppVersion + ");\n";
	if (vcc.bIsRtl)
		sOutput += "@import url(/_layouts/VolvoCars.Web.Sites/CSS/RTL.css?v=" + vcc.sAppVersion + ");\n";
	sOutput += "</style>\n";
	sOutput += "<script src=\"/_layouts/VolvoCars.Web.Sites/JS/functions.js?v=" + vcc.sAppVersion + "\" type=\"text/javascript\"><\/script>\n";
	sOutput += "<script src=\"/_layouts/VolvoCars.Web.Sites/JS/GlobalDealerLocator/GlobalDealerLocator.js?v=" + vcc.sAppVersion + "\" type=\"text/javascript\"><\/script>\n";
	sOutput += "<script type=\"text/javascript\" src=\"http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=" + vcc.dl.mapCulture + "\"><\/script>\n";
	sOutput += "</head>\n";
	sOutput += "<body id=\"dl-popup\">\n";
	sOutput += "<div id=\"top\"><h1>" + vcc.dl.getText("DrivingDirectionsResultHeader") + "</h1><div id=\"logo\"><img src=\"/_layouts/VolvoCars.Web.Sites/Images/logo.gif\" alt=\"Volvo for life\" /></div></div>\n";
	sOutput += "<div id=\"page\">\n";
	sOutput += "<ul class=\"icons top fixfloat\"><li class=\"print\"><a href=\"#\" onclick=\"vcc.printPage();return false;\">" + vcc.dl.getText("DrivingDirectionsResultPrint") + "</a></li></ul>\n";
	sOutput += "<div id=\"map\" style=\"width:620px; height:320px;\"></div>\n";
	sOutput += "<div id=\"directions\"></div>\n";
	sOutput += "<ul class=\"icons bottom fixfloat\"><li class=\"print\"><a href=\"#\" onclick=\"vcc.printPage();return false;\">" + vcc.dl.getText("DrivingDirectionsResultPrint") + "</a></li></ul>\n";
	sOutput += "</div>\n";
	sOutput += "<textarea id=\"directions-textarea\" style=\"display: none;\">";
	sOutput += "vcc.dl.bDirections = true; vcc.dl.bMiles = " + vcc.dl.bMiles + ";";
	sOutput += "vcc.dl.directionsStart = { latitude: " + result[0] + ", longitude: " + result[1] + " };";
	sOutput += "vcc.dl.directionsEnd = { latitude: " + vcc.dl.selectedDealer.geocode.latitude + ", longitude: " + vcc.dl.selectedDealer.geocode.longitude + " };";
	sOutput += "vcc.dl.texts = { DrivingDirectionsResultDirections: \"" + vcc.dl.getText("DrivingDirectionsResultDirections") + "\", DrivingDirectionsResultDistance: \"" + vcc.dl.getText("DrivingDirectionsResultDistance") + "\", DrivingDirectionsResultTime: \"" + vcc.dl.getText("DrivingDirectionsResultTime") + "\", DrivingDirectionsResultTotal: \"" + vcc.dl.getText("DrivingDirectionsResultTotal") + "\" };";
	sOutput += "</textarea>\n";
	sOutput += "</body>\n";
	sOutput += "</html>";
	win.document.open();
	win.document.write(sOutput);
	win.document.close();
}

vcc.dl.ws.handleFailedCallback = function(error) {
	var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();
	vcc.dl.hideLoading();
	vcc.dl.showErrorBox(error.get_message());
}

vcc.dl.ws.cancelAjaxCall = function(request) {
	if (request != null) {
		var executor = request.get_executor();
		if (executor.get_started()) {
			executor.abort();
			vcc.dl.hideLoading();
		}
    }    
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
