ASC.namespace('ASC.Chrysler.FAD.DrivingDirection');
ASC.Chrysler.FAD.DrivingDirection = ASC.extend(Ext.util.Observable,
		{
	    map: null,
	    originalCoordinate:null,
	    destinationCoordinate:null,
	    directionsDesc: null,
	    directions:null,
	    isSuccess:true,
		brand:null,
	    constructor: function (cfg)
		{
			ASC.Chrysler.FAD.DrivingDirection.superclass.constructor.apply(this);
			if (GBrowserIsCompatible())
			{
				if(cfg.map)
				{
					this.map = cfg.map;
				}
				else
				{
					this.map = new GMap2(ASC.getEl(cfg.renderTo).dom); 
				}
				if(cfg.controls&&!cfg.isPrint)
				{
				    for(var i =0;i<cfg.controls.length;i++)
					{
				    	this.map.addControl(cfg.controls[i]);
					}
				}
				if(cfg.isPrint)
				{
					this.map.disableDragging();
					this.map.disableGoogleBar();
				}
				//alert(cfg.original);
				this.directionsDesc        = cfg.directionsDesc;
				this.originalCoordinate	   = cfg.original;
				this.destinationCoordinate = cfg.destination;
				this.brand = cfg.brand;
			}
			this.addEvents({"error":true,"success":true,"checkError":true,"checkSuccess":true,"convertAddress":true});	
			
		},
		changeImage:function(i) {
			var marker = this.directions.getMarker(i);
			this.map.removeOverlay(marker);
			var icon = new GIcon();
			icon.iconSize = new GSize(66, 66);		
			icon.iconAnchor = new GPoint(39, 53);	
			icon.image = ASC.cfg.getImgLocalePath()+"/fad/map_tab_b.png";
			icon.shadow = null;
			var newMarker = new GMarker(marker.getLatLng(), {icon:icon});
	        this.map.addOverlay(newMarker);
	        var obj = this;
			GEvent.addListener(newMarker, "click", function() {
				obj.map.showMapBlowup(newMarker.getLatLng());
			  });
	    },
		getDrivingDirection:function()
		{
			 var desc = ASC.getEl(this.directionsDesc).dom;
			 this.directions = new GDirections(this.map, desc); 
			 this.directions.el = this;
			 var myObj = this;
			//alert(this.originalCoordinate);
			//alert(this.destinationCoordinate);
			// GEvent.addListener(this.directions, "load", this.onGDirectionsLoad);
			 GEvent.addListener(this.directions,"load", function(){
			        setTimeout(function() {
			          // fire event first, then change the default images.
			          myObj.fireEvent("success",myObj);
					  myObj.replaceImage();
					  myObj.trackLinkMetrics();
			          for (var i=0; i < myObj.directions.getNumRoutes()+1; i++) {
			        	  if( i == myObj.directions.getNumRoutes())
			        		  myObj.changeImage(i);
			          }
			        },10);
			      });
			 GEvent.addListener(this.directions, "error", this.handleErrors);
			 this.directions.load(this.originalCoordinate+" to "+this.destinationCoordinate);
		},
		replaceImage:function(){
			var desc = ASC.getEl(this.directionsDesc).dom;
			var div=desc.firstChild;
			var googledir=div.firstChild;
			var copyRight=googledir.lastChild;
			var destination=copyRight.previousSibling;
			var table=destination.firstChild;
			var tbody=table.firstChild;
			var tr=tbody.firstChild;
			var td=tr.firstChild;
			td.removeChild(td.firstChild);
			if(this.brand){
				var divImg =document.createElement("div");
				divImg.className = "logoImage";
				var img1=document.createElement("img");
				img1.src=ASC.cfg.getContextPath() + "/resources/images/brands/"+this.brand.toLowerCase()+".gif";
				divImg.appendChild(img1);
			}
			td.appendChild(divImg);
			var img2=document.createElement("img");
			img2.src=ASC.cfg.getContextPath() + "/resources/images/brands/address_icon.png";
			td.appendChild(img2);
			tr.onclick = function(){ASC.Chrysler.trackLinkMetrics('content_directions','b');};
		},
		trackLinkMetrics:function(){
			var desc = ASC.getEl(this.directionsDesc).dom;
			var div=desc.firstChild;
			var googledir=div.firstChild;
			var dirEl = googledir.firstChild.nextSibling;
			var startPoint = dirEl.firstChild;
			startPoint.onclick = function(){ASC.Chrysler.trackLinkMetrics('content_directions','a');};
			var stepsRows = dirEl.lastChild.lastChild.rows;
			for(var i=0;i<stepsRows.length;i++){
				var stepEl = stepsRows[i];
				stepEl.onclick = function(){ASC.Chrysler.trackLinkMetrics('content_directions',this.stepId+1);};
			}
		},
		clear:function()
		{
			if(this.directions)
				this.directions.clear();
			var desc = ASC.getEl(this.directionsDesc).dom;
			if(desc){
				while (desc.firstChild) {
					  desc.removeChild(desc.firstChild);
				}
			}
				
		}
		,
		test:function()
		{
			var testDirections = new GDirections(null, null); 
			testDirections.el = this;
			 GEvent.addListener(testDirections, "load", this.ontestSuccess);
			 GEvent.addListener(testDirections, "error", this.ontestError);
			 testDirections.load(this.originalCoordinate+" to "+this.destinationCoordinate);
		},
		ontestSuccess:function()
		{
			this.el.fireEvent("checkSuccess",{});
		}
		,
		ontestError:function()
		{
			this.el.fireEvent("checkError",{});
		}
		,
		onGDirectionsLoad:function(e)
		{
			
			this.el.fireEvent("success",this);
		}
		,
		handleErrors:function(e)
		{
			this.el.fireEvent("error",e);
		},
		convertAddressToLatLng:function(address)
		{
			var geo = new GClientGeocoder(); 
			var me = this;
			 geo.getLatLng(address, function (point)
					 {
				 			me.fireEvent("convertAddress",point);
					 });
		}
		
		});
