/*
 * Load google map js dynamically
 */
ASC.apply(Page, 
{
	callGoogleMap:function()
	{
		if(!ASC.getEl("googleMapScript"))
		{
			this.scriptEl = document.createElement("script");
			this.scriptEl.type = "text/javascript";
			this.scriptEl.id  = "googleMapScript";
			this.scriptEl.src  = this.googleMapUrl+'&callback=Page.scriptLoadedCallback';
			document.getElementsByTagName("head")[0].appendChild(this.scriptEl);
		}
		else
		{
			this.mc = this;
			this.createScriptCallback();
		}
	},
	scriptLoadedCallback:function()
	{
		this.mc = this;
		google.load("maps", "2.100", {"callback" : this.createScriptCallback});
	},
	createScriptCallback:function()
	{
		if(!ASC.getEl("pdMarkerScript"))
		{
			var pdMarkerEl = document.createElement("script");
			pdMarkerEl.type = "text/javascript";
			pdMarkerEl.src  = ASC.cfg.getJsPath()+"/pdmarker/pdmarker.js"; 
			pdMarkerEl.id = "pdMarkerScript";
			pdMarkerEl.onreadystatechange =	Page.pdMarkerLoadedIE;
			pdMarkerEl.onload = function(){Page.pdMarkerLoaded();}
			
			document.getElementsByTagName("head")[0].appendChild(pdMarkerEl);
		}else
		{
			Page.pdMarkerLoaded();
		}
	},
	pdMarkerLoadedIE:function()
	{
		if(this.readyState == "complete" || this.readyState == "loaded")
		{
			Page.pdMarkerLoaded();
		}
	},
	pdMarkerLoaded:function()
	{
		Page.initMap();
	}
});
/*
 * ASC.Chrysler.FAD.DealersMap
 *
 */
ASC.namespace('ASC.Chrysler.FAD.DealersMap');
ASC.Chrysler.FAD.DealersMap = ASC.extend(Ext.util.Observable,{
	moreInfoOffsetTop: 365,
	moreInfoOffsetLeft: 40,
	infoOffsetTop: 65,
	infoOffsetLeft: -45,
	sniMenu: null,
	dealerCardShowed:"false",
	dealerCardDealerCode:"",
	constructor: function (cfg)
	{
		ASC.Chrysler.FAD.DealersMap.superclass.constructor.apply(this);
		ASC.apply(this, cfg);
		this.addEvents({"clickMarker":true});
		if (GBrowserIsCompatible())
		{
			this.map = new GMap2(ASC.getEl(this.renderTo).dom); 
			this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom);
		 
			if( this.isPrint ){
				this.map.disableDragging();
				this.map.disableGoogleBar();
			}else{
				for(var i =0;i<this.controls.length;i++)
				{
					this.map.addControl(this.controls[i]);
				}
			}		
		}
		this.map.dealersMapObj = this;
		this.initMarkeOrderNumTpl();
		this.initMoreDealerInfoTpl();
		GEvent.addListener(this.map, 'zoomend',this.onZoomEnd);
		var me = this;
		Ext.getBody().on('click', function () { 
			if (me.sniMenu) {
				me.sniMenu.hide();
			} 
		});	
		if( this.isPrint ){
			this.map.disableDragging();
			this.map.disableGoogleBar();
		}
	},
	update:function(cfg)
	{
		ASC.apply(this, cfg);
	},
	onZoomEnd: function(oldLevel,newLevel)
	{ 
		/*
		 * CHRDEV-2472
		 * need to recalculate zoom level if "Return to the last result" action triggered.
		 */
		var center = this.getCenter();
		var lat = this.dealersMapObj.lat;
		var lng = this.dealersMapObj.lng;
		if( center.x == lng && center.y == lat )
			this.zoomToMarkers();
		if(this.dealersMapObj.dealerList) {
			for(var i = 0;i <this.dealersMapObj.dealerList.length;i++)
			{
				var marker = this.getMarkerById(i);
				if(marker)
				{
					marker.hideSecondTooltip();
					marker.showSecondTooltip();
				}		
			}
		}
		if(this.dealersMapObj.directionDealer){
			var marker = this.getFirstMarker();
			if(marker){
				marker.hideSecondTooltip();
				marker.showSecondTooltip();
			}
		}
	},
	/*
	 *  to display pdMarker properly on map
	 */
	reZoom: function(){
		for(var i = 0;i <this.dealerList.length;i++)
		{
			var marker = this.map.getMarkerById(i);
			if(marker)
			{
				marker.hideSecondTooltip();
				marker.showSecondTooltip();
			}		
		}
	},
	setMarkersCursor:function(style){
		for(var i = 0;i <this.dealerList.length;i++){
			var marker = this.map.getMarkerById(i);
			if(marker)
			{
				var elSecToopTipName = "ttobj_sec_"+marker.getId();
				var elSecToopTip = ASC.getEl(elSecToopTipName);
				elSecToopTip.dom.previousSibling.style.cursor=style;
			}		
		}
	},
	setMarkerCursor:function(marker,style){
		var elSecToopTipName = "ttobj_sec_"+marker.getId();
		var elSecToopTip = ASC.getEl(elSecToopTipName);
		elSecToopTip.dom.previousSibling.style.cursor=style;
	},
	/* show all dealer info push pin on the map */
	pushDealerPin: function()
	{		
		if(this.map && this.dealerList && this.dealerList.length)
		{   
			this.clearAllMarker();
			for(var i=0;i<this.dealerList.length;i++)
			{
				var marker = new PdMarker(new GLatLng(this.dealerList[i].latitude, this.dealerList[i].longitude));	
				var icon = marker.getIcon();
				icon.iconSize = new GSize(66, 66);		
				icon.iconAnchor = new GPoint(39, 53);	
				icon.image = ASC.cfg.getImgLocalePath()+"/fad/map_tab.png";
				icon.shadow = null;
				this.map.addOverlay(marker);
				marker.setId(i);
				marker.setTooltipHiding(true);
				marker.setOpacity(100);
				marker.el = this;
				marker.dealerCode = this.dealerList[i].dealerCode;
				marker.dealerData = this.dealerList[i];	
				var number = this.pageSize*(this.pageIndex -1)+(i+1);
				this.setMarkerOrderNumber(marker,number);
				GEvent.addListener(marker, "click", function() {
						this.el.fireEvent("clickMarker",{markerId:this.getId(),dealerCode:this.dealerCode});				
					}
				);
		
			}
			this.map.zoomToMarkers();
			this.reZoom();
			if(this.isPrint)
				this.setMarkersCursor("default");
		}
	},
	getMarkerIdByDealerCode:function(dealerCode)
	{
		for(var i=0;i<this.dealerList.length;i++)
		{
			if(this.dealerList[i].dealerCode == dealerCode)
			{
				return i;
			}
		}
	},
	clearAllMarker: function()
	{
		var marker = this.map.getFirstMarker();
		if(marker){
			do
			{
				marker.remove();
			}while
				(marker=this.map.getFirstMarker())
		}
	},
	setMarkerOrderNumber:function(marker,number)
	{
		
		marker.offsetTop = this.infoOffsetTop;
		marker.offsetLeft = this.infoOffsetLeft;
		var html = this.markerOrderNumTpl.applyTemplate({
			num: number,
			markerId:marker.getId()
		});			
		marker.hideSecondTooltip();
		marker.setSecondTooltip(html);
		marker.showSecondTooltip();
		//Safari use scientific math(e+008)
		/*
		 * -1.36577e+008 which equals 136577000
		 * Need to add 1000 to become 136576000, then it will convert into -1.36576e+008
		 */
		try{
			marker.secondTooltipObject.style.zIndex = marker.getMarkerZIndex() +1000;
		}catch(ex){
			//alert(ex.description);fix ie6 issue;
		}
	},
	setDirectinMarker:function(dealer)
	{
		this.initMarkeDirectionTpl();
		var marker = new PdMarker(new GLatLng(dealer.latitude,dealer.longitude));	
		var icon = marker.getIcon();;
		icon.iconSize = new GSize(66, 66);		
		icon.iconAnchor = new GPoint(39, 53);	
		icon.image = ASC.cfg.getImgLocalePath()+"/fad/map_tab.png";
		icon.shadow = null;
		this.map.addOverlay(marker);
		marker.offsetTop = this.infoOffsetTop;
		marker.offsetLeft = this.infoOffsetLeft;
		var html = this.markeDirectionTpl.applyTemplate({});			
		marker.hideSecondTooltip();
		marker.setSecondTooltip(html);
		marker.showSecondTooltip();
		//Use setTimeour to fix ie6 issue
		this.setMarkerCursor(marker,"default");
		var zIndex = marker.getMarkerZIndex() +1;
		if( Ext.isSafari ){
			zIndex = -1;//In order to support older safari version.
		}
		try{
			marker.secondTooltipObject.style.zIndex = zIndex;
		}catch(ex){
			//alert(ex.description);fix ie6 issue;
		}
	},
	initMarkeOrderNumTpl:function()
	{
		this.markerOrderNumTpl = new Ext.XTemplate(	
						'<div class="matches">',
						'<span class="matches_Num">{num}</span>',
						'</div>'
		  );
	},
	initMarkeDirectionTpl:function()
	{
		this.markeDirectionTpl = new Ext.XTemplate(	
				'<div class="matches">',
				'<span class="matches_Num">B</span>',
				'</div>'
				);
	},
	initMoreDealerInfoTpl: function()
	{
		this.moreDealerInfoTpl = new Ext.XTemplate(
			'<div class="bCard" onmousedown="Page.stopEvent(event);" ondblclick="Page.stopEvent(event);">',				    	
				'<div class="top">&nbsp;</div>',
				'<table class="card" border="0" cellpadding="0" cellspacing="0">',
			    	'<tr>',
			    		'<td class="leftEdge">&nbsp;</td>',				    		
				    	'<td class="{[this.getCssStyle("body")]}">',
				    		'<tpl if="! values.isPrint">',
				    			'<div class="card_close"><a href="javascript:Page.dealersMap.closeDealerCard({id});"><span>{[this.translate("fad_findDealers_btnClose")]}</span></a></div>',
				    		'</tpl>',
				    		'<div style="height:360px;">',
				    		'<table class="tblDealer" border="0" cellpadding="0" cellspacing="0">',
					    		'<tr>',
						    		'<td class="dealerOverview">',
						    			'<label class="dealerName">{values.dealer.name}</label>',					    			
					    				'<tpl if="this.isValidDistance(distance)">',
					    					'<div class="dealerDistance">{distance} {[this.translate("fad_findDealers_distanceDesc")]}</div>',
					    				'</tpl>',
//					    				'<tpl if="values.dealer.fiveStar == \'Y\'">',
//			                        		'<div class="smallFiveStar"><img src="{[this.getFiveStarImage()]}"/></div>',
//			                        	'</tpl>',
//			                        	'<tpl if="values.dealer.fiveStar != \'Y\'">',
//		                        			'<div class="smallFiveStar">&nbsp;</div>',
//		                        		'</tpl>',
			                        	'<div class="dealerInfo" title="{[values.dealer.address1]}">{values.dealer.address1:this.subAndLowerCase}</div>',
			                        	'<div class="dealerInfo">{values.dealer.city:this.lowerCase}, {values.dealer.state}</div>',
			                        	'<div class="dealerInfo">{[this.formatZip(values.dealer.zip)]}</div>',
										'<div class="dealerInfo">{[this.formatPhone(values.dealer.salesPhone)]}</div>',	
			                        	'<tpl if="! values.isPrint">',
			                        	 	//'<div class="listitem_preferred" id="setPreferredDealer{values.dealer.dealerCode}" style="display:{[this.isPreferredDealerExist()? "none":"block"]};"><a class="direction" href="javascript:ASC.Chrysler.trackLinkMetrics(\'dealer_specific_info\',\'set_preferred_dealer\'); Page.setAsPreferredDealer(\'{values.dealer.dealerCode}\');">{[this.translate("fad_findDealers_setPreferredDealer")]}</a></div>',
			                        	 	//'<div class="listitem_preferred" id="changeOrclearPreferredDealer{values.dealer.dealerCode}" style="display:{[this.isPreferredDealerExist()? "block":"none"]};"><a href="javascript:Page.setAsPreferredDealer(\'{values.dealer.dealerCode}\');ASC.Chrysler.trackLinkMetrics(\'dealer_specific_info\',\'change_preferred_dealer\');">{[this.translate("fad_findDealers_changeOrclearPreferredDealer")]}</a></div>',
			                        		'<div><a class="gdirection" href="javascript:Page.callDirectionView(\'{[values.dealer.dealerCode]}\');" name="&lpos=card_content&lid=card_get_directions_{[values.dealer.dealerCode]}">{[this.translate("fad_get_directions")]}</a></div>',
			                        	'</tpl>',
			                        	//'<tpl if="!this.isParticipatingDealer(values.dealer) && !values.isPrint">',
			                        	//'<div class="card_viewDealerSite"><a href="moreDealerInfo.do?dealerCode={[values.dealer.dealerCode]}&fromFadZip={[this.formatZip(values.dealer.zip)]}&zipDistance={distance}&city={[values.dealer.city]}&state={[values.dealer.state]}">{[this.translate("fad_view_dealers_website")]}</a></div>',
		    							//'</tpl>',
		    							'<tpl if="this.isParticipatingDealer(values.dealer) && !values.isPrint">',
		                        			'<div class="card" onclick="javascript:Page.trackCardDealerWebsite(\'{values.dealer.dealerCode}\',\'CARD_WEB\');"><a id="card_web_{values.dealer.dealerCode}" class="card_viewDealerSite" href="{[values.dealer.url]}" target="_blank">{[this.translate("fad_view_dealers_website")]}</a></div>',
		                        		'</tpl>',
		                        		'<tpl if="!this.isParticipatingDealer(values.dealer)">',
	                        				'<div class="card">&nbsp;</div>',
	                        			'</tpl>',
						    		'</td>',
							    	'<td id="dealerServices" rowspan="2">', 
								    	'<div class="card_instoreservice">{[this.translate("fad_instore_service")]}</div>',
								    	'<div>',
							     	 		'<tpl if="values.dealer.hasSDTruck == \'Y\'"><div>{[this.translate("fad_findDealers_ramChassisCabs")]}</div></tpl>',
//							     	 		'<tpl if="values.dealer.fiveStar == \'Y\'"><div>{[this.translate("fad_findDealers_fiveStar")]}</div></tpl>',
							     	 		'<tpl if="values.dealer.hasSpanishPersonnel == \'Y\'"><div>{[this.translate("fad_findDealers_seHablaEspanol")]}</div></tpl>',
							     	 		'<tpl if="values.dealer.hasBusinessLink == \'Y\'"><div>{[this.translate("fad_findDealers_businessLink")]}</div></tpl>',
							     	 		'<tpl if="values.dealer.hasSprinter == \'Y\'"><div>{[this.translate("fad_findDealers_sprinter")]}</div></tpl>',
                                            '<tpl if="values.dealer.dealerSitePointer == \'3\'"><div>{[this.translate("fad_findDealers_onlineService")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasShuttle == \'Y\'"><div>{[this.translate("fad_findDealers_shuttle")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasExpressLube == \'Y\'"><div>{[this.translate("fad_findDealers_expressLube")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasCPOV == \'Y\'"><div>{[this.translate("fad_findDealers_cpov")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasSatService == \'Y\'"><div>{[this.translate("fad_findDealers_satService")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasFleetElite == \'Y\'"><div>{[this.translate("fad_findDealers_fleetElite")]}</div></tpl>',
                                            '<tpl if="values.dealer.hasRentalService == \'Y\'"><div>{[this.translate("fad_findDealers_rentalService")]}</div></tpl>',
								    	'</div>',
						    		'</td>',
					    		'</tr>',
					    		'<tr>',
					    			'<td class="dealerOverview">',
					    				'<div class="blank">&nbsp;</div>',
								    	'<table class="tblDealerBrands" border="0">',
		                        		'<tr>',
		                        			/*'<tpl if="values.hotDeals == \'Y\'"><td align="center"><img src="{[this.getImgLocalePath()]}/fad/fad_map_icon_09.gif"/></td></tpl>',*/
					                		'<tpl if="values.dealer.isDodge == \'Y\'"><td align="center"><img src="{[this.getAllImgLocalePath()]}/fad/ico_bcard_dodge.gif" /></td></tpl>',
                                            '<tpl if="values.dealer.isRam == \'Y\'"><td align="center"><img src="{[this.getAllImgLocalePath()]}/fad/ico_bcard_ram.gif" /></td></tpl>',
		                					'<tpl if="values.dealer.isChrysler == \'Y\'"><td align="center"><img src="{[this.getAllImgLocalePath()]}/fad/ico_bcard_chrysler.gif" /></td></tpl>',
		                					'<tpl if="values.dealer.isJeep == \'Y\'"><td align="center"><img src="{[this.getAllImgLocalePath()]}/fad/ico_bcard_jeep.gif" /></td></tpl>',
		                					'<tpl if="values.dealer.isFiat == \'Y\'"><td align="center"><img src="{[this.getAllImgLocalePath()]}/fad/ico_bcard_fiat.gif" /></td></tpl>',
			                			'</tr>',
			                			'</table>',
						    		'</td>',
					    		'</tr>',
					    		'<tr>',
					    			'<td colspan="2" class="dealerOverview"><label class="dealerName"></label></td>',
					    		'</tr>',
					    		'<tr height="120px">',
						    		'<td class="dealerOverview1">',
						    			'<tpl if="this.shouldShowUsedDealerInfo(values.dealer.address1,values.dealer.usedAddress1)">',
							    			'<div class="blank">&nbsp;</div>',
								    		'<label class="dealerName">'+ASC.message("fad_findDealers_preownedsaleslocation")+'</label>',					    			
						    				'<tpl if="this.isValidDistance(distance)">',
						    					'<div class="dealerDistance">{distance} {[this.translate("fad_findDealers_distanceDesc")]}</div>',
						    				'</tpl>',
						    				'<tpl if="!this.isValidDistance(distance)">',
						    					'<div class="dealerDistance">&nbsp;</div>',
					    					'</tpl>',
				                        	'<div class="dealerInfo" title="{[values.dealer.usedAddress1]}">{values.dealer.usedAddress1:this.subAndLowerCase}</div>',
				                        	'<tpl if="this.validCS(values.dealer.usedCity,values.dealer.usedState)">',
				                        		'<div class="dealerInfo">{values.dealer.usedCity:this.lowerCase}, {values.dealer.usedState}</div>',
			                        		'</tpl>',
				                        	'<tpl if="!this.validCS(values.dealer.usedCity,values.dealer.usedState)">',
				                        		'<div class="dealerInfo">&nbsp;</div>',
				                        	'</tpl>',
				                        	'<tpl if="this.validStr(values.dealer.usedZip)">',
				                        		'<div class="dealerInfo">{[this.formatZip(values.dealer.usedZip)]}</div>',	
				                        	'</tpl>',
				                        	'<tpl if="!this.validStr(values.dealer.usedZip)">',
				                        		'<div class="dealerInfo">&nbsp;</div>',	
				                        	'</tpl>',
				                        	'<tpl if="this.validStr(values.dealer.usedPhone)">',
				                        		'<div class="dealerInfo">{[this.formatPhone(values.dealer.usedPhone)]}</div>',
				                        	'</tpl>',
				                        	'<tpl if="!this.validStr(values.dealer.usedPhone)">',
			                        			'<div class="dealerInfo">&nbsp;</div>',
			                        		'</tpl>',
				                        	'<tpl if="! values.isPrint">',
				                        		'<div><a class="gdirection" href="javascript:Page.callDirectionView(\'{[values.dealer.dealerCode]}\');" name="&lpos=card_content&lid=card_secondary_get_directions_{[values.dealer.dealerCode]}">{[this.translate("fad_get_directions")]}</a></div>',
				                        	'</tpl>',
		    							'</tpl>',
						    		'</td>',
							    	'<td id="dealerSitePointer">', 
								    	'<tpl if="values.dealer.dealerSitePointer == 3">',
								    		'<tpl if="! values.isPrint">',
									    		'<div class="dealerList" id="nvs_sni" onclick="javascript:Page.trackInventory(\'{[values.dealer.dealerCode]}\',\'SNI\');"><a id="card_sni_{values.dealer.dealerCode}" class="searchInventory" target="_new" href="{[this.getDealerSiteUrl(values.dealer)]}" id="searchNewInventoryLink" >{[this.translate("fad_findDealers_searchNewInventory")]}</a></div>',
									    	'</tpl>',
									    	'<tpl if="!values.dealer.url && ! values.isPrint">',
									    		'<div class="dealerList" onclick="javascript:Page.trackPreOwned(\'{[values.dealer.dealerCode]}\',,\'PRE\');"><a id="card_pre_{values.dealer.dealerCode}" class="searchPreOwned" href="javascript:Page.links.openPreOwenedWindow({[values.dealer.dealerCode]},{[values.distance]});">{[this.translate("fad_findDealers_searchPreOwned")]}</a></div>',
									    	'</tpl>',
									    	'<tpl if="values.dealer.url && ! values.isPrint">',
									    		'<div class="dealerList" onclick="javascript:Page.trackPreOwned(\'{[values.dealer.dealerCode]}\',\'PRE\');"><a id="card_pre_{values.dealer.dealerCode}" class="searchPreOwned" target="_new" href="{[values.dealer.url]}/link?program=cllc&page=USED_INVENTORY" >{[this.translate("fad_findDealers_searchPreOwned")]}</a></div>',
									    	'</tpl>',
								    	'</tpl>',
						    		'</td>',
					    		'</tr>',
					    	'</table>',	
					    	'</div>',
	    				'<td class="rightEdge">&nbsp;</td>',
			    	'</tr>',
		    	'</table>',
				'<div class="{[this.getCssStyle("bottom")]}">&nbsp;</div>',    
			'</div>',
					{			
	    				dealerSitePoint: function(sitePointer, url, dealerCode, distance)
						{
							if(sitePointer==3 && url != ''){
								return '<div class="dealerList"><a target="_new" href="' +url + '/link?program=cllc&page=USED_INVENTORY" onclick="javascript:ASC.Chrysler.trackLinkMetrics(\'used_vehicle_sales_tab\',\'search_preowned\');">'+this.translate("fad_findDealers_searchPreOwned")+'</a></div>';		
							}
							return '<div class="dealerList"><a href="javascript:ASC.Chrysler.trackLinkMetrics(\'used_vehicle_sales_tab\',\'search_preowned\');Page.links.openPreOwenedWindow(\''+dealerCode+'\',\''+distance+'\');">'+this.translate("fad_findDealers_searchPreOwned")+'</a></div>'					
						},
						
						shouldShowUsedDealerInfo:function(address1,address2){
							if( address2 )
								address2 = address2.replace(/(^\s*)/g, "");
							if( ! address2 )
								return false;
							else{
								return address1 != address2;
							}
						},
									
						translate: function (key)
						{
							return ASC.message(key);
						},
						getCssStyle: function (css)
						{
							if(Ext.isIE)
								return css+"_ie";
							else
								return css;
						},
						
						getImgLocalePath: function(){
							return ASC.cfg.getImgLocalePath();
						},
						getExternalUrlPath: function(){
							return  ASC.cfg.getBridgeUrl();
						},
						getFiveStarImage: function(){
				    		 return ASC.cfg.getContextPath() + "/resources/images/all/en/fivestar.gif";
				    	},
						subAndLowerCase : function(str){
							/* There is no requirement for this at the moment,
							 * so I have removed it as it was causing odd output 
							 *  and didn't really make sense as well with the current logic
							 * Sept 5th, 2009 -> Kieran Murphy 
							 * 
							 * if(str.length > 18)
							{
								str = str.toLowerCase();
								return str.substr(0,21) + '...';
							}
							else
							{
								return str.toLowerCase();
							}*/
							return str;
						},
						validCS:function(city,state){
							if( this.validStr(city) && this.valid(state))
								return true;
							else
								return false;
						},
						validStr:function(str){
							if(str)
								str = str.replace(/(^\s*)/g, "");
							if(str)
								return true;
							else
								return false;
						},
						lowerCase: function(str){
							if(str)
								return str.toLowerCase();
							else
								return "";
						},
						subStr: function (str)
						{
							/*
							 * There is no requirement for this at the moment,
							 * so I have removed it as it was causing odd output 
							 *  and didn't really make sense as well with the current logic
							 * Sept 5th, 2009 -> Kieran Murphy
							 * 
							if(str.length > 21)
							{
								return str.substr(0,21) + '...';
							}
							else
							{
								return str;
							}*/
							return str;
						},
						formatZip: function(zip){
							return ASC.util.formatZipPostalCode(zip);
						},
						getInfo:function(star,end,info){
							var infoReturn = "&nbsp;";
							if(star!=end){
								if(info!=""){
									if(info!="-")
										infoReturn = this.translate(info);
									else
										infoReturn = info;
								}else{									
									infoReturn = end;
								}
							}
							return infoReturn;
						},
						getAllImgLocalePath: function(){
							return ASC.cfg.getContextPath() + "/resources/images/all/en";
						},
						getContextPath: function() {
							return ASC.cfg.getContextPath();
						},
						
						/**
						 * Returns true of it's a participating dealer that has a website
						 * @param {Object} dealer
						 */
						isParticipatingDealer: function (dealer) {
							return dealer.dealerSitePointer == '3'; 
						},
						
						getDealerSiteUrl: function (dealer) {
							var brand = Page.brand;
							var brandCode = "C";
							switch(brand.toUpperCase())
							{
								case  "JEEP" :
									brandCode = "J";
									brand = "Jeep";
									break;
								case "DODGE":
									brandCode = "D";
									brand = "Dodge";
									break;
                                case "RAM":
									brandCode = "T";
									brand = "Ram";
									break;
                                case "FIATUSA":
                                	brandCode = "X";
									brand = "fiatusa";
									break;
								default:
									brandCode = "C";
									brand = "Chrysler";
							}
							if(dealer.url){
								var url = dealer.url + '/link?program=cllc&page=NEW_INVENTORY&make='+brand;
								return url;
							}else{
								var zipCode = ASC.Cookies.get('zipcode');
								var url = ASC.cfg.getBrandHostnameUrl(brandCode) + '/sni/inventorySearch.do?dealerCode='+dealer.dealerCode+'&zipCode='+zipCode;
								return url;
							}
						},
						
						isValidDistance: function(distance){
							if( distance && distance !== 'null'){
								return true;
							}
							return false;
						},
						formatPhone: function(phone){
							return ASC.Chrysler.formatPhoneNumberUseHyphen(phone);
						}
						/*isPreferredDealerExist:function(){
				    		var preferredDealerCode = ASC.Chrysler.getPreferredDealer();
				    		if(preferredDealerCode && preferredDealerCode != 'null')
				    			return true;
				    		else
				    			return false;
				    	}*/
					});		
		this.moreDealerInfoTpl.compile();	
	},
	showDealerCard: function(id,dealerInfo,isPrint)
	{		
		var marker = this.map.getMarkerById(id);
		if(!marker)
		{
			return;
		}
		if(this.clickedMarker)
		{
			this.closeDealerCard(this.clickedMarker.getId());
		}
		var p = this.map.fromLatLngToContainerPixel(marker.getPoint());
		p.x +=45;
		p.y -= 165;
		var pp = this.map.fromContainerPixelToLatLng(p);
		this.panTo(pp);
		var top = 32;
		marker.offsetTop = this.moreInfoOffsetTop+top;
		marker.offsetLeft = this.moreInfoOffsetLeft+133;
		marker.hideTooltip();
		var moreInfoHtml = this.moreDealerInfoTpl.applyTemplate({
			dealer: dealerInfo.dealer,
			id: id,
			distance: marker.dealerData.distance,
			hotDeals: marker.dealerData.hotDeals,
			isPrint:isPrint
		});	
		marker.setTooltip(moreInfoHtml);
		marker.showTooltip();
		marker.display(false);
		marker.hideSecondTooltip();
		marker.isMoreDealerInfo = true;
		this.clickedMarker = marker;
		//this.buildFloatingMenu(dealerInfo.dealer);
		this.dealerCardShowed = "true";
		this.dealerCardDealerCode = dealerInfo.dealer.dealerCode;
	},
	panTo: function(p)
	{		
		this.map.panTo(p);
	},
	closeDealerCard: function(id)
	{
		if (this.sniMenu) {
			this.sniMenu.hide();
		}
		var marker = this.map.getMarkerById(id);
		if(!marker)
		{
			return;
		}
		marker.offsetTop = this.infoOffsetTop;
		marker.offsetLeft = this.infoOffsetLeft;
		marker.setTooltip("");
		marker.hideTooltip();
		marker.showSecondTooltip();
		marker.display(true);
		this.dealerCardShowed = "false";
		this.dealerCardDealerCode = "";
	},
	buildFloatingMenu:function(dealer)
	{
		/*if (this.sniMenu) {
    		this.sniMenu.destroy();
    		delete this.sniMenu;
    	}*/
		var elNvsSni = ASC.getEl('nvs_sni');
    	
    	if(elNvsSni){
    		//ASC.getEl('nvs_sni').on('click',this.toggleFloatingMenu,this,{type:'sni',dealerCode:dealerCode,anchorTarget:ASC.getEl('nvs_sni') });
    		if(this.multipleBrands(dealer)){
		   		this.sniMenu = new ASC.Chrysler.InterimDealer.SNIPanel({cls: 'a-sniPanel a-fad',style:'clear:both;text-align:center;'}, dealer,elNvsSni);    		
		   		var me = this;
		   		me.sniMenu.hide();
		   		elNvsSni.on('click', function (evt, el) {	
					me.sniMenu.anchorTo(elNvsSni.dom, 'br', [me.sniMenu.el.getWidth() * -1, 0]);
					if (me.sniMenu.isVisible()){
						me.sniMenu.hide();
					}else{
						me.sniMenu.show();
					}	
					evt.stopPropagation();	
				});
			}else{
    			var url;
    			if(dealer.isJeep=='Y'){
    				if(dealer.url != '' && dealer.dealerSitePointer == 3){
    					url = dealer.url + '/link?program=cllc&page=NEW_INVENTORY&make=Jeep';
    				}else{
    					url = ASC.cfg.getBrandHostnameUrl('J') + '/sni/inventorySearch.do?dealerCode='+dealer.dealerCode+'&zipCode='+ASC.getEl('zipCode2').getValue();
    				}    				    				
    			}else if(dealer.isChrysler=='Y'){
    				if(dealer.url != '' && dealer.dealerSitePointer == 3){
    					url = dealer.url + '/link?program=cllc&page=NEW_INVENTORY&make=Chrysler';
    				}else{
    					url = ASC.cfg.getBrandHostnameUrl('C') + '/sni/inventorySearch.do?dealerCode='+dealer.dealerCode+'&zipCode='+ASC.getEl('zipCode2').getValue();
    				}    				    				
    			}else if(dealer.isDodge=='Y'){
    				if(dealer.url != '' && dealer.dealerSitePointer == 3){
    					url = dealer.url + '/link?program=cllc&page=NEW_INVENTORY&make=Dodge';
    				}else{
    					url = ASC.cfg.getBrandHostnameUrl('D') + '/sni/inventorySearch.do?dealerCode='+dealer.dealerCode+'&zipCode='+ASC.getEl('zipCode2').getValue();
    				}    				    				
    			}else if(dealer.isRam=='Y'){
    				if(dealer.url != '' && dealer.dealerSitePointer == 3){
    					url = dealer.url + '/link?program=cllc&page=NEW_INVENTORY&make=Ram';
    				}else{
    					url = ASC.cfg.getBrandHostnameUrl('T') + '/sni/inventorySearch.do?dealerCode='+dealer.dealerCode+'&zipCode='+ASC.getEl('zipCode2').getValue();
    				}
    			}
    			ASC.getEl('searchNewInventoryLink').dom.href = url;
    			ASC.getEl('searchNewInventoryLink').dom.target="_new";
    		}    	
    	}
	},
	multipleBrands: function (dealer)
	{
		var numOfBrands = 0;
		if(dealer.isJeep=='Y'){
			numOfBrands++;
		}
		if(dealer.isChrysler=='Y'){
			numOfBrands++;
		}
		if(dealer.isDodge=='Y'){
			numOfBrands++;
		}
        if(dealer.isRam=='Y'){
			numOfBrands++;
		}
		return numOfBrands > 1 ? true : false;
	}
});

