/** Google map instance */
var map = false;
/** If true users will be displayed (distributors with dtype == 'user') */
var show_users = true;
/** If true distributors will be displayed (distributors with dtype == 'distributor') */
var show_distributors = true;
/** If true representatives will be displayed (distributors with dtype == 'representative') */
var show_representatives = true;
/** Array of markers */
var markers = {'user':[],'distributor':[],'representative':[]};

// Mariusz loves python-style decorator-patterns!!
function onMarkerClick(marker, item){
	return function(){
		var html = "<h3>"+item.name+"</h3><p>";
			if(item.telephone !== ""){ html += "Tel: "+item.telephone+"<br/>";}
			if(item.fax !== ""){ html += "Fax: "+item.fax+"<br/>";}
			if(item.website !== ""){ 
				if(item.website.substr(0,8) !== 'https://' || item.website.substr(0,7) != 'http://'){
					item.url = 'http://'+item.website;
				} else {
					item.url = item.website;
				}
				html += "Website: <a href="+item.url+">"+item.website+"</a><br/>";
			}
			if(item.name1 !== "" && item.name1 !== null && item.name1 !== undefined){ html += '<br/>'+item.name1+'<br/>';}
			if(item.email !== "" && item.email !== null && item.email !== undefined){ html += '<a href="mailto:'+item.email+'">'+item.email+'</a><br/>';}
			if(item.name2 !== "" && item.name2 !== null && item.name2 !== undefined){ html += '<br/>'+item.name2+'<br/>';}
			if(item.email2 !== "" && item.email2 !== null && item.email2 !== undefined){ html += '<a href="mailto:'+item.email2+'">'+item.email2+'</a><br/>';}
			if(item.name3 !== "" && item.name3 !== null && item.name3 !== undefined){ html += '<br/>'+item.name3+'<br/>';}
			if(item.email3 !== "" && item.email3 !== null && item.email3 !== undefined){ html += '<a href="mailto:'+item.email3+'">'+item.email3+'</a><br/>';}
			if(item.name4 !== "" && item.name4 !== null && item.name4 !== undefined){ html += '<br/>'+item.name4+'<br/>';}
			if(item.email4 !== "" && item.email4 !== null && item.email4 !== undefined){ html += '<a href="mailto:'+item.email4+'">'+item.email4+'</a><br/>';}
			if(item.name5 !== "" && item.name5 !== null && item.name5 !== undefined){ html += '<br/>'+item.name5+'<br/>';}
			if(item.email5 !== "" && item.email5 !== null && item.email5 !== undefined){ html += '<a href="mailto:'+item.email5+'">'+item.email5+'</a><br/>';}
			
		html+="</p><p>";
			if(item.address1 !== "" && item.address1 !== null && item.address1 !== undefined){ html += item.address1+"<br/>";}
			if(item.address2 !== "" && item.address2 !== null && item.address2 !== undefined){ html += item.address2+"<br/>";}
			if(item.address3 !== "" && item.address3 !== null && item.address3 !== undefined){ html += item.address3+"<br/>";}
			if(item.address4 !== "" && item.address4 !== null && item.address4 !== undefined){ html += item.address4+"<br/>";}
			if(item.address5 !== "" && item.address5 !== null && item.address5 !== undefined){ html += item.address5+"<br/>";}
			if(item.address6 !== "" && item.address6 !== null && item.address6 !== undefined){ html += item.address6+"<br/>";}
			if(item.address7 !== "" && item.address7 !== null && item.address7 !== undefined){ html += item.address7+"<br/>";}
			if(item.country !== "" && item.country !== null && item.country !== undefined){ html += item.country+"<br/>";}
		html+="</p>";
		
		$('#map_column .box_interior').html(html);
		$('#map_column .box_title_bar').html(item.dtype.charAt(0).toUpperCase() + item.dtype.substr(1).toLowerCase());
	}
}

function initialize() {
	// Initialize map
	if(map === false){
		var latlng = new google.maps.LatLng(0.00, 0.00);
		//var latlng = new google.maps.LatLng(45, -35.00);
		var myOptions = {
		  zoom: 1,
		  center: latlng,
		  mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		map = new google.maps.Map(document.getElementById("map_canvas"),
			myOptions);
			
		overlayAdd();
			
	//Clean markers
	}else{
		if(!show_users){
			for(var i = 0; i < markers['user'].length; i++){
				markers['user'][i].setMap(null);
			}
			markers['user'] = [];
		}
		if(!show_distributors){
			for(var i = 0; i < markers['distributor'].length; i++){
				markers['distributor'][i].setMap(null);
			}
			markers['distributor'] = [];
		}
		if(!show_representatives){
			for(var i = 0; i < markers['representative'].length; i++){
				markers['representative'][i].setMap(null);
			}
			markers['representative'] = [];
		}
	}

	for(var i=0; i < distributors.length; i++){
		var item = distributors[i];
		
		if(item === "" || item === undefined || item === false) continue;
		if(item.dtype == 'distributor' && !show_distributors) continue;
		if(item.dtype == 'user' && !show_users) continue;
		if(item.dtype == 'representative' && !show_representatives) continue;
		
		var image;
		if(item.dtype == 'representative' && item.marker_number !== null){
			image = BASE_URI+"application/assets/img/maps/marker"+item.marker_number+'.png';
		}else{
			image = BASE_URI+"application/assets/img/maps/"+item.dtype+'.png';
		}
		
		var marker = new google.maps.Marker({
					map: map, 
					title:item.name,
					position: new google.maps.LatLng(item.lat, item.lon),
					icon: image
		});
		markers[item.dtype].push(marker);

		google.maps.event.addListener(marker, 'click', onMarkerClick(marker, item));
	}
}

function updateFilterButtons(){
	$('.map_filters a').removeClass('sel');
	if(show_users) $('.map_filter_users').addClass('sel');
	if(show_distributors) $('.map_filter_distributors').addClass('sel'); 
	if(show_representatives) $('.map_filter_representatives').addClass('sel');
}


$(document).ready(function(){
	$('input.map_filters').each(function() { $(this).attr('checked', false); });
	updateFilterButtons();
	$('input.map_filters').change(function(){
		var val = $(this).val()
		if(val == 'users'){
			show_users = true;
			show_distributors = !true;
			show_representatives = !true;
		}else if(val == 'distributors'){
			show_users = !true;
			show_distributors = true;
			show_representatives = !true;		
		}else if(val == 'representatives'){
			show_users = !true;
			show_distributors = !true;
			show_representatives = true;
		}
		updateFilterButtons();
		initialize();
	});
	$('.map_filters a').click(function(event){
		if($(this).hasClass('map_filter_users')) show_users = !show_users;
		if($(this).hasClass('map_filter_distributors')) show_distributors = !show_distributors;
		if($(this).hasClass('map_filter_representatives')) show_representatives = !show_representatives;
		updateFilterButtons();
		
		initialize();
		try{
			event.preventDefault();
		}catch(err){}
		return false;
	});
	$('.map_filters a.map_filter_representatives').css('cursor','default');
});

 
// Add the overlay
 
function overlayAdd(){
	return;
    var sw = new google.maps.LatLng(25,-126) ;
    var ne = new google.maps.LatLng(50,-66) ;
    var bounds = new google.maps.LatLngBounds(sw,ne) ;
    var overlay = new ProjectedOverlay(map,BASE_URI+"application/assets/img/maps/us_overlay.png", bounds, {}) ;

	var sw2 = new google.maps.LatLng(50.5,-178) ;
    var ne2 = new google.maps.LatLng(71.7,-129.4) ;
    var bounds2 = new google.maps.LatLngBounds(sw2,ne2) ;
    var overlay2 = new ProjectedOverlay(map,BASE_URI+"application/assets/img/maps/us_alaska.png", bounds2, {}) ;
//    var overlay3 = new ProjectedOverlay(map,BASE_URI+"application/assets/img/maps/transparent.gif", bounds2, {}) ;

}
 

