
var zindex_top = 9000;

function equalHeights(div1,div2,div3) {
	var highestCol = Math.max($('#'+div1).height(),$('#'+div2).height(),$('#'+div3).height());
	$('#'+div1).height(highestCol);
	$('#'+div2).height(highestCol);
	$('#'+div3).height(highestCol);
}

function updateInMyArea() {
  var lat = $.cookie('mlat');
  var lng = $.cookie('mlng');
  if (lat && lng) {
    $('#topmenusearch input[name="lat"]').val(lat);
    $('#topmenusearch input[name="lng"]').val(lng);
    $('#tmmyarea').parent().show();
  } else {
    $('#tmmyarea').parent().hide();
  }
}

function addPub(pub) {
	
  window['contentString_' + pub.pubId] = formatContent(pub);
  var myLatlng = new google.maps.LatLng(pub.pubLatLng.lat, pub.pubLatLng.lng);
  
  	pub.name = pub.name.replace("&amp;", "&");
  
 	safe_name =  encodeURIComponent(pub.name);
	
	if (pub.fapCount > 0) {
		
		var FAPLogo = new google.maps.MarkerImage('/markers/pubs/'+pub.pubId+'.png',
			new google.maps.Size(142,30),
			new google.maps.Point(0,0),
			new google.maps.Point(0,40)
		);
		
	} else if (pub.communityCount > 0) {
		
		var FAPLogo = new google.maps.MarkerImage('/markers/pubs/'+pub.pubId+'.png',
			new google.maps.Size(142,30),
			new google.maps.Point(0,0),
			new google.maps.Point(0,40)
		);
		
	} else {
		
		var FAPLogo = new google.maps.MarkerImage('/markers/pubs/'+pub.pubId+'.png',
			new google.maps.Size(142,30),
			new google.maps.Point(0,0),
			new google.maps.Point(0,40)
		);
		
	}

window['pub.marker_' + pub.pubId] = new google.maps.Marker({
     position: myLatlng,
     map: map,
	 title: pub.name,
	 icon: FAPLogo
  });

  google.maps.event.addListener(window['pub.marker_' + pub.pubId], 'click', function() {
		infowindow.setContent(window['contentString_' + pub.pubId]);
    	infowindow.open(map,window['pub.marker_' + pub.pubId]);
		zindex_top++;
		window['pub.marker_' + pub.pubId].setZIndex(zindex_top);
  });
  
   return pub;
}

function addLocations(location) {
	
 window['location_contentString_' + location.locationId] = formatLocationContent(location);
  var myLatlng = new google.maps.LatLng(location.lat, location.lng);
  
  	if (location.locationTypeId == '9') {
		cachefile = '/markers/attractions/'+location.locationId+'.png';
	}

	if (location.locationTypeId == '1' || location.locationTypeId == '2') {
		cachefile = '/markers/locations/'+location.locationId+'.png';
	}
		  
	if (location.locationTypeId == '7') {
		cachefile = '/markers/stations/'+location.locationId+'.png';
	}
  
	var FAPLogo = new google.maps.MarkerImage(cachefile,
		new google.maps.Size(142,30),
		new google.maps.Point(0,0),
		new google.maps.Point(0,40)
	);
	
	
	window['location.marker_' + location.locationId] = new google.maps.Marker({
     position: myLatlng,
     map: map,
	 title: location.name,
	 icon: FAPLogo
  });

  google.maps.event.addListener(window['location.marker_' + location.locationId], 'click', function() {
		infowindow.setContent(window['location_contentString_' + location.locationId]);
    	infowindow.open(map,window['location.marker_' + location.locationId]);
		zindex_top++;
		window['location.marker_' + location.locationId].setZIndex(zindex_top);
  });

   return;
}


function addLocation(lat,lng,name,type,locationId) {
	
  var myLatlng = new google.maps.LatLng(lat, lng);
  
  	name = name.replace('+',' ');
	name = name.replace('+',' ');
	name = name.replace('+',' ');
	name = name.replace('+',' ');
  
    if (type == '9') {
		cachefile = '/markers/attractions/'+locationId+'.png';
	}

	if (type == '1' || locationId == '2') {
		cachefile = '/markers/locations/'+locationId+'.png';
	}
		  
	if (type == '7') {
		cachefile = '/markers/stations/'+locationId+'.png';
	}
  
	var FAPLogo = new google.maps.MarkerImage(cachefile,
		new google.maps.Size(142,30),
		new google.maps.Point(0,0),
		new google.maps.Point(0,40)
	);

attraction = new google.maps.Marker({
     position: myLatlng,
     map: map,
	 title: name,
	 icon: FAPLogo
  });

attraction.setZIndex(1000);

   return;
}

function show_infowindow(pubId) {
	infowindow.setContent(window['contentString_' + pubId]);
	infowindow.open(map,window['pub.marker_' + pubId]);
	zindex_top++;
	window['pub.marker_' + pubId].setZIndex(zindex_top);
}

function show_location_infowindow(locationId) {
	infowindow.setContent(window['location_contentString_' + locationId]);
	infowindow.open(map,window['location.marker_' + locationId]);
	zindex_top++;
	window['location.marker_' + locationId].setZIndex(zindex_top);
}

function formatContent(pub) {

  var image;

  if (pub.imageLocation != '') {
    image = '/media/' + pub.imageLocation;
  } else {
    image = '/images/awaiting_pub_image.jpg';
  }

  var content = '<div class="map_thumbnails_container"><a href="/Pub/' + pub.pubId + '"><img class="map_thumbnails" src="' + image + '"/></a></div>' 
  content = content + '<div class="map_info"><a href="/Pub/' + pub.pubId + '"><b>'+ pub.name + '</b><br />' + pub.address.town_city + "<br/>";
  content = content + pub.address.postcode + " &nbsp;&nbsp;&nbsp; More...</a><br/>";
  if (pub.fapCount > 0) {
	  if (Math.round(pub.fapAvg) >=0 && Math.round(pub.fapAvg) <=5) {
  	content = content + '<img src="/images/rating_' + Math.round(pub.fapAvg) + '.png" alt="FAP rating" title="FAP rating ' + Math.round(pub.fapAvg) + '" />';
	  } else {
		  content = content + '<img src="/images/rating_none.png" alt="Not rated yet!" title="Not rated yet!" />';
	  }
  } else {
	  	if (pub.communityCount > 0) {
			 if (Math.round(pub.communityAvg) >=0 && Math.round(pub.communityAvg) <=5) {
  			content = content + '<img src="/images/rating_' + Math.round(pub.communityAvg) + '.png" alt="Community rating" title="Community rating ' + Math.round(pub.communityAvg) + '" />';
			 } else {
				 content = content + '<img src="/images/rating_none.png" alt="Not rated yet!" title="Not rated yet!" />';
			 }
		} else {
			content = content + '<img src="/images/rating_none.png" alt="Not rated yet!" title="Not rated yet!" />';
		}
  }
  return content;
}


function formatLocationContent(location) {
	
  content = '<div class="map_info"><a href="/Pub/'+location.url_name+'/' + location.locationId + '"><b>'+ location.descr + '</b><br />';
  content = content + " Go to this location</a><br/>";

  return content;
}
