/* Store Locations Script for Smiggle */

var storeAddress;
$(function(){
	//initialize();
	//findLocation('Carlton','271 Lygon Street Carlton Melbourne VIC 3053 Australia');
	
	$(".store-detail").not(".post").append("<a class='showmap' href='javascript:void(0)'>Show Map</a>");
	//$("#map").css("border","solid 2px red");
	
	$('.showmap').toggle(function() {
		$("#shown").click();
		$(this).html("Hide Map");
		$(this).parent().append("<div id='map'></div>");
		$(this).attr("id","shown");
		
		var storeName = $(this).siblings(".name").html();
		
		var _state = $(this).siblings(".state").html(); if(_state==null) _state="";
		var _postcode = $(this).siblings(".postcode").html(); if(_postcode==null) _postcode="";
		storeAddress = $(this).siblings(".streetaddress").html() +" "+ $(this).siblings(".suburb").html() +" "+ _state +" "+ _postcode +" "+ $(this).siblings(".country").html();
		
		//var storeCoordinates = $(this).siblings(".lat").html() +" "+ $(this).siblings(".long").html();
		var storeCoordinates = new google.maps.LatLng( $(this).siblings(".lat").html(), $(this).siblings(".long").html() )
		
		//findLocation(storeName,storeAddress);
		findLocation(storeName,storeCoordinates);

		
		
	}, function() {
		$(this).attr("id","");
		$(this).html("Show Map");
		$("#map").remove();
	});
	
});



/*************************************************************/
// GOOGLE MAPS API
/*************************************************************/

var map;
var geocoder;
var storeName;

function findLocation(name,address) {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl(1));
	map.enableDoubleClickZoom();
	geocoder = new GClientGeocoder();
  
  	// Create PA marker icon
	var peterIcon = new GIcon();
	peterIcon.image = "/images/misc/storelocaton_marker.png";
	peterIcon.iconSize = new GSize(48, 48);
	peterIcon.iconAnchor = new GPoint(24, 24);
	peterIcon.infoWindowAnchor = new GPoint(24, 24);
	markerOptions = { icon:peterIcon };
	
	//storeName = name;
  	//geocoder.getLocations(address, addAddressToMap);
	
	//point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	marker = new GMarker(address, markerOptions);
	map.addOverlay(marker);
	var windowHTML = "<div class='infowindow'><div class='title'>Smiggle " + name + "</div><div class='body'>" + storeAddress + "</div><br/></div>";
	marker.bindInfoWindowHtml( windowHTML );
	marker.openInfoWindowHtml( windowHTML );
	map.setCenter(new GLatLng( address.lat()+0.001,  address.lng()), 16);
}


// addAddressToMap() is called when the geocoder returns an answer. 
// It adds a marker to the map with an open info window
function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
	alert("Sorry, we were unable to find address for that location.");
  } else {
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	marker = new GMarker(point, markerOptions);
	map.addOverlay(marker);
	var windowHTML = "<div class='infowindow'><div class='title'>Smiggle " + storeName + "</div><div class='body'>" + place.address + "</div><br/></div>";
	marker.bindInfoWindowHtml( windowHTML );
	marker.openInfoWindowHtml( windowHTML );
	map.setCenter(new GLatLng(place.Point.coordinates[1]+0.001, place.Point.coordinates[0]), 16);
  }
}




