
var secogroup = window.secogroup = window.secogroup || {};


secogroup.GoogleMap = function(options)
{
	this.options = $.extend({
		element: '.dealer-map',
		zoom: 1
	}, options);
	
	this.$element = null;
};

secogroup.GoogleMap.prototype.init = function()
{
	this.$element = $(this.options.element);
	this.geocoder = new google.maps.Geocoder();
	this.googlemap = new google.maps.Map(this.$element.get(0), {
		zoom: this.options.zoom,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	this.$addressBar = $('.side-dealers');
	this.showAddresses();
	
	this.$addressBar.find('li').bind('click', $.proxy(this.clickAddress, this));
	
	this.googlemap.setCenter(new google.maps.LatLng(0, 0));
	this.googlemap.setZoom(1);
};

secogroup.GoogleMap.prototype.zoomTo = function(address)
{
	this.geocoder.geocode(
		{
			address: address
		}, 
		$.proxy(function(results, status)
		{
			if(status == google.maps.GeocoderStatus.OK) 
			{
				this.googlemap.setCenter(results[0].geometry.location);
			}
		}, this));
};

secogroup.GoogleMap.prototype.showAddress = function(address)
{
	this.geocoder.geocode(
		{
			address: address
		}, 
		$.proxy(function(results, status)
		{
			if(status == google.maps.GeocoderStatus.OK) 
			{
				if(results[0])
				{
					this.googlemap.setCenter(results[0].geometry.location);
					this.googlemap.setZoom(15);
					new google.maps.Marker({
						map: this.googlemap, 
						position: results[0].geometry.location
					});
					
					//console.log(results[0].formatted_address);
					
					//var url = "http://maps.google.com/?ll=" + results[0].geometry.location + "&z=" + this.googlemap.getZoom() + "&t=" + this.googlemap.getMapTypeId() + "&q=" + results[0].formatted_address;
					//this.$element.after('<p><a href="' + url + '" target="_blank" class="small">Zvětšit mapu</a></p>');
				}				

			}
		}, this));
};

secogroup.GoogleMap.prototype.showAddresses = function()
{
	var addresses = this.addresses = [];
	this.$addressBar.find('li').each(function(i){
		var location =  $(this).attr('data-position').split(',');
		addresses.push({
			zipCode: $(this).find('.zipCode').text(),
			city: $(this).find('.city').text(),
			state: $(this).find('.state').text(),
			dealerName: $(this).find('.dealerName').text(),
			location: new google.maps.LatLng(location[0], location[1])
		});
	});
	
	var markerIcon = new google.maps.MarkerImage(BASE_HREF + 'img/seco-marker.png', new google.maps.Size(21, 32), new google.maps.Point(0,0), new google.maps.Point(10,32));
	var markerIconShadow = new google.maps.MarkerImage(BASE_HREF + 'img/seco-marker-shadow.png', new google.maps.Size(40, 30), new google.maps.Point(0,0), new google.maps.Point(8,24));
	for(var i = 0; i < addresses.length; i++)
	{		
		 
		$.proxy(function()
		{
			var ii = i;
			var address = addresses[i];
			var addressGeo = address.zipCode + ' ' + address.city + ', ' + address.state;
//*							
			address.marker = new google.maps.Marker({
				map: this.googlemap, 
				position: address.location,
				icon: markerIcon,
				shadow: markerIconShadow
			});
			
			address.infoWindow = new google.maps.InfoWindow({
				position:  address.location,
				content: '<p style="margin-bottom: 5px;"><strong>' + address.city + '</strong></p>' + '<p style="margin-bottom: 5px;">' + address.dealerName + '</p>'
			});
			
			google.maps.event.addListener(address.marker, 'click', $.proxy(function() {
					this.showAddress(ii);
				}, this));
//*/

			/*
			this.geocoder.geocode(
				{
					address: addressGeo
				}, 
				$.proxy(function(results, status)
				{
					if(status == google.maps.GeocoderStatus.OK) 
					{
						if(results[0])
						{
							this.googlemap.setCenter(results[0].geometry.location);
							this.googlemap.setZoom(15);
							
							address.location = results[0].geometry.location;
							address.marker = new google.maps.Marker({
								map: this.googlemap, 
								position: results[0].geometry.location
							});
							
							address.infoWindow = new google.maps.InfoWindow({
								position: results[0].geometry.location,
								content: '<p style="margin-bottom: 5px;"><strong>' + address.city + '</strong></p>' + '<p style="margin-bottom: 5px;">' + address.dealerName + '</p>'
							});
							
							google.maps.event.addListener(address.marker, 'click', $.proxy(function() {
	    						address.infoWindow.open(this.googlemap, address.marker);
	  						}, this));
						}				
					}
				}, this));
				
				//*/
		}, this)();
	}
};

secogroup.GoogleMap.prototype.showAddress = function(i, zoom)
{
	var $this = this.$addressBar.find('li').removeClass('active').eq(i);	
	$this.addClass('active');
	
	var address = this.addresses[i];
	
	if(zoom)
	{
		this.googlemap.setCenter(address.location);
		this.googlemap.setZoom(14);
	}
	else
	{
		var scroll = $this.position().top - 100 + this.$addressBar.scrollTop();
		console.log(scroll);
		this.$addressBar.stop().animate({scrollTop: scroll}, 200);	
	}
	
	if(this.activeAddress) this.addresses[this.activeAddress].infoWindow.close();
	
	if(address.infoWindow) address.infoWindow.open(this.googlemap, address.marker);
	
	this.activeAddress = i;
};

secogroup.GoogleMap.prototype.clickAddress = function(event)
{
	var i = this.$addressBar.find('li').index(event.currentTarget);
	this.showAddress(i, true);
};

