 function createMarker(point,html,img,hint) {
		 
		 	var somfyIcon = new GIcon(G_DEFAULT_ICON);
			
			if (img != "")
			{
		 		somfyIcon.image = img;
				var marker = new GMarker(point, {icon:somfyIcon, title: hint});
			}
			else
			{
				var marker = new GMarker(point, {title: hint});
			}
			
			GEvent.addListener(marker, "click", function() {
			  //alert(html);
			  marker.openInfoWindowHtml(html);
			});
			return marker;
}
// global js object
var map;	
function createMap(dealers,partnerships,labels){ 
	
	if (GBrowserIsCompatible()) {
		
		// create the instance of GMap
		map = new GMap2(document.getElementById("map"));
		
		map.addControl(new GLargeMapControl());
		// add map/satellite toggle 
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng('0','0'), 17);
		//gogglemaps V1 map.centerAndZoom(new GPoint('0','0'), 0);
		
		var bounds = new GLatLngBounds();
		var _lbShowDetails = 'Show Details';
		var _lbAddress = 'Address ';
		var _lbCoordinates = 'Coordinates'; 
		if(labels) {
			_lbShowDetails = labels['showDetails'];
			_lbAddress = labels['address'];
			_lbCoordinates = labels['coordinates'];
		}
			
		for (var i in dealers.result.DEALERS){
			var obj = dealers.result.DEALERS[i];
			var mylat = "";
			var mylng ="";
			var img = "";
			var point = new GLatLng(obj.lat,obj.lng);
			var point_pos = new String(obj.lat).indexOf(".");
			//if(point_pos > 0) mylat = "";
			
				// create the marker 
				//var marker = new GMarker(point);
				var html_marker='<table><tr><td><table style="text-align: left; width: 260px;" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td width="90px;"><span style="font-weight: bold;">'+_lbCoordinates+':</span></td><td></td></tr><tr><td>'+_lbAddress+':</td><td><span><b>'+obj.name+'</b></span></td></tr>';
				
				/*
				if(obj.address1 !='') html_marker+='<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';
				if(obj.address2 !='') html_marker+='<tr><td></td><td>'+obj.address2+'</td></tr>';
				if(obj.zipcode !='') html_marker+='<tr><td></td><td>'+obj.city+' '+obj.state+' '+obj.zipcode+'</td></tr>';
				*/
				
				html_marker+=formatAddress(obj, labels);
				
				
				html_marker+='<tr><td></td><td><a href="javascript:showDealerDetails('+obj.id+')">'+_lbShowDetails+'</a></td></tr>';
				//if(obj.logo !='') html_marker+='<tr><td></td><td><img src="/common/img/library/dealerLogos/75x75/'+obj.logo+'" alt="'+obj.logo+'"></td></tr>';
				html_marker+='</td></tr></table>';							
				
				if(partnerships){
					if(obj.partnership1 == 1) {
						img = partnerships['1'].smallIcon;	
					}
					else if(obj.partnership2 == 1) {
						img = partnerships['2'].smallIcon;	
					}
					else if(obj.partnership3 == 1) {
						img = partnerships['3'].smallIcon;	
					}
					else if(obj.partnership4 == 1) {
						img = partnerships['4'].smallIcon;	
					}
					else if(obj.partnership5 == 1) {
						img = partnerships['5'].smallIcon;	
					}					
				}
				var displayNumber = parseInt(i)+1;
				var marker = createMarker(point, html_marker,img,displayNumber+'-'+obj.name);
				
				
				// Add the marker as an overlay 
				map.addOverlay(marker); 
				
				bounds.extend(point);
				
		}
		
			map.setZoom(map.getBoundsZoomLevel(bounds));
	
			  // ===== determine the centre from the bounds ======
			 map.setCenter(bounds.getCenter());
	}
	else
	{
		document.getElementById("map").innerHTML = "dealerlocator.googlemaps.GMapsBrowserKO" ;
	}

}

function zoomDealer(latitude,longitude) {
	var point = new GLatLng(latitude,longitude);
	map.setCenter(point, 17);
	var bounds = new GLatLngBounds();
	bounds.extend(point);
	map.setZoom(map.getBoundsZoomLevel(bounds)-4);
	document.getElementById("map").scrollIntoView();
}

function createMapForCustomer(obj, labels) {

	if (GBrowserIsCompatible()) {
		
		// create the instance of GMap
		var map = new GMap2(document.getElementById("map"));		
		map.addControl(new GLargeMapControl());
		// add map/satellite toggle 
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(obj.lat,obj.lng), 17);
		//gogglemaps V1 map.centerAndZoom(new GPoint('0','0'), 0);
		
		var bounds = new GLatLngBounds();		
		
	    var _lbAddress = 'Adress';
	 
	    if(labels) {
	        _lbAddress = labels['address'];
	    }
	    
		var mylat = "";
		var mylng ="";
		var point = new GLatLng(obj.lat,obj.lng);
		//var point_pos = new String(obj.lat).indexOf(".");
		//if(point_pos > 0) mylat = "";				
		
		// create the marker 
		//var marker = new GMarker(point);
		var html_marker='<table><tr><td><table style="text-align: left; width: 260px;" border="0" cellpadding="0" cellspacing="0"><tbody><tr><td>' + _lbAddress + ' :</td><td><span><b>'+obj.name+'</b></span></td></tr>';
		
		/*
		if(obj.hide_street_address != '1'){
			if(obj.address1 !='') html_marker+='<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';
			if(obj.address2 !='') html_marker+='<tr><td></td><td>'+obj.address2+'</td></tr>';
		}
		// if(obj.zipcode !='') html_marker+='<tr><td></td><td>'+obj.city+' '+obj.state+' '+obj.zipcode+'</td></tr>';
		if(obj.city !='') {
			html_marker+='<tr><td></td><td>'+obj.city+' '+obj.state;
			if(obj.hide_street_address != '1'){
				html_marker+=' '+obj.zipcode;
			}
			html_marker+='</td></tr>'
		}
		*/
		
		html_marker+=formatAddress(obj, labels);
		
		html_marker+='</td></tr></table>';							
		
		var marker = createMarker(point, html_marker,"",obj.name);		
		
		// Add the marker as an overlay 
		map.addOverlay(marker); 		
		bounds.extend(point);
		map.setZoom(map.getBoundsZoomLevel(bounds)-4);
		
		// ===== determine the centre from the bounds ======
		map.setCenter(bounds.getCenter());
	}
	else
	{
		document.getElementById("map").innerHTML = "dealerlocator.googlemaps.GMapsBrowserKO" ;
	}
}
function showDealerDetails(id) {
	document.getElementById(id).scrollIntoView();
	$('.dealerBox').css("background-color","");
	document.getElementById(id).parentNode.style.backgroundColor='#F8F8F8';
	//alert(document.getElementById(id).childNodes[0].style.backgroundColor='#cccccc';
	//document.getElementById(id).childNodes[0].style.backgroundColor='#cccccc';
}

function formatAddress(obj, labels) {
	
	var htmlView='';
	
	switch(obj.address_format)
    {
        case 1:
            if (obj.hide_street_address != '1') {
	            if(obj.address1.length > 0) { htmlView += '<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';}
	            if(obj.address2.length > 0) { htmlView += '<tr><td></td><td>'+obj.address2+'</td></tr>';}
	            if(obj.address3.length > 0) { htmlView += '<tr><td></td><td>'+obj.address3+'</td></tr>';}            	
            }
            
            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                if (obj.hide_street_address != '1') { htmlView += obj.zipcode + ' '; }
                htmlView += obj.city;
                htmlView += '</td><tr>';
            }
            
            if(obj.state !=''){ 
            	htmlView += '<tr><td></td><td>' + obj.state + '</td><tr>'; 
            }

            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        case 2:
            if (obj.hide_street_address != '1') {
            	if(obj.address1.length > 0 || obj.address3.length > 0 || obj.address3.length > 0) 
            	{
            		htmlView += '<tr><td></td><td>';
	                if(obj.address1.length > 0) { htmlView += '<span>'+obj.address1+'<span> ';}
	                if(obj.address2.length > 0) { htmlView += obj.address2+' ';}
	                if(obj.address3.length > 0) { htmlView += obj.address3;}
	                htmlView += '</td></tr>';
            	}               
            }
            
            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                if (obj.hide_street_address != '1') { htmlView += obj.zipcode + ' '; }
                htmlView += obj.city;
                htmlView += '</td><tr>';
            } 

            if(obj.state !='' ){ 
            	htmlView += '<tr><td></td><td>' + obj.state + '</td></tr>';
            }
            
            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        case 3: 
            if (obj.hide_street_address != '1') {
                if(obj.address1.length > 0 || obj.address2.length > 0 || obj.address3.length > 0) 
                {
                    htmlView += '<tr><td></td><td>';
                    if(obj.address1.length > 0) { htmlView += '<span>'+obj.address1+' ';}
                    if(obj.address2.length > 0) { htmlView += obj.address2+' ';}
                    if(obj.address3.length > 0) { htmlView += obj.address3;}
                    htmlView += '</td></tr>';
                }               
            }
            
            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                htmlView += obj.city;
                if (obj.hide_street_address != '1') { htmlView += ', ' + obj.zipcode; }
                htmlView += '</td></tr>';
            }
             
            if(obj.state !='' ){ 
                htmlView += '<tr><td></td><td>' + obj.state + '</td></tr>';
            }
            
            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        case 4: // use for US
            if (obj.hide_street_address != '1') {
                if(obj.address1.length > 0) { htmlView += '<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';}
                if(obj.address2.length > 0) { htmlView += '<tr><td></td><td>'+obj.address2+'</td></tr>';}
                if(obj.address3.length > 0) { htmlView += '<tr><td></td><td>'+obj.address3+'</td></tr>';}               
            }
            
            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                
                if ( obj.city !='' ) {
                    htmlView += obj.city + ', ';
                }
                
                if ( obj.state !='' ) {
                    htmlView += obj.state + ' ';
                }

                if (obj.hide_street_address != '1') {
                    if ( obj.zipcode !='' ) { htmlView += obj.zipcode; }
                }
                
                htmlView += '</td><tr>';
            } 

            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        case 5:
            if (obj.hide_street_address != '1') {
                if(obj.address1.length > 0 || obj.address2.length > 0 || obj.address3.length > 0) 
                {
                    htmlView += '<tr><td></td><td>';
                    if(obj.address1.length > 0) { htmlView += '<span>'+obj.address1+' ';}
                    if(obj.address2.length > 0) { htmlView += obj.address2+' ';}
                    if(obj.address3.length > 0) { htmlView += obj.address3;}
                    htmlView += '</td></tr>';
                }               
            }
            
            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                htmlView += obj.city;
                if (obj.hide_street_address != '1') { htmlView += ' ' + obj.zipcode; }
                htmlView += '</td></tr>';
            }
             
            if(obj.state !='' ){ 
                htmlView += '<tr><td></td><td>' + obj.state + '</td></tr>';
            }
            
            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        case 6:
            if (obj.hide_street_address != '1') {
                if(obj.address1.length > 0) { htmlView += '<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';}
                if(obj.address2.length > 0) { htmlView += '<tr><td></td><td>'+obj.address2+'</td></tr>';}
                if(obj.address3.length > 0) { htmlView += '<tr><td></td><td>'+obj.address3+'</td></tr>';}               
            }
            
            if(obj.state !='' ){ 
                htmlView += '<tr><td></td><td>' + obj.state + '</td></tr>';
            }

            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                if (obj.city != '') { htmlView += obj.city; }
                if (obj.hide_street_address != '1') { 
                	if (obj.zipcode != '') { htmlView += ', ' + obj.zipcode; }
                }
                htmlView += '</td><tr>';
            } 

            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
        default: 
            if (obj.hide_street_address != '1') {
                if(obj.address1.length > 0) { htmlView += '<tr><td></td><td><span>'+obj.address1+'</span></td></tr>';}
                if(obj.address2.length > 0) { htmlView += '<tr><td></td><td>'+obj.address2+'</td></tr>';}
                if(obj.address3.length > 0) { htmlView += '<tr><td></td><td>'+obj.address3+'</td></tr>';}               
            }

            if(obj.zipcode !='' || obj.city !='' ){
                htmlView += '<tr><td></td><td>';
                if (obj.hide_street_address != '1') { 
                    if (obj.zipcode != '') { htmlView += obj.zipcode + ' '; }
                }
                htmlView += obj.city;
                htmlView += '</td><tr>';
            } 

            
            if(obj.state !='' ){ 
                htmlView += '<tr><td></td><td>' + obj.state + '</td></tr>';
            }

            if(obj.country !='' ){ htmlView += '<tr><td></td><td></td><tr>'; }
            break;
    }
    


    var _lbPhone = 'Phone';
    var _lbFax = 'Fax';
 
    if(labels) {
        _lbPhone = labels['phone'];
        _lbFax = labels['fax'];
    }

    var exp=new RegExp("'","g");

    if(obj.phone.length > 0) { 
    	htmlView += '<tr><td>' + _lbPhone + ' :</td>';
    	htmlView += '<td>' + obj.phone.replace(exp,"-") + '</td>';
    	htmlView += '</tr>';
    }
    if(obj.fax.length > 0) { 
    	htmlView += '<tr><td>' + _lbFax + ' :</td>';
        htmlView += '<td>' + obj.fax.replace(exp,"-") + '</td>';
        htmlView += '</tr>';
    }

    return htmlView;
} 




