/**
 * @author Łukasz Puła
 * @copyright Enzo Sp. z o.o., http://enzo.pl
 */

jQuery(document).ready(init);

function initMapsBookmarks(){
	initMapsGoogle();
	
	var tableCells = jQuery('#servicePoints li');
	var tableCellsContent = jQuery('#servicePoints .cellContent');
	tableCellsContent.append('<a class="showOnMap" href="#servicePoints">pokaż na mapie</a>');
	var showOnMap = jQuery('#servicePoints .showOnMap');
	
	tableCellsContent.bind('click', function(){
		tableCells.removeClass('selected');
		$(this).parent().addClass('selected');
		
		var coordinates = $(this).find('.mapCoordinates').text().split(',');
		if (coordinates[0]) {
			var id = $(this).attr('id');
			var centerPoint = new GLatLng(parseFloat(coordinates[0]),parseFloat(coordinates[1]));	
			var googleMarker = new GMarker(centerPoint,icon); 
			googleMap.addOverlay(googleMarker);			
			googleMap.setCenter(centerPoint, 15);
		
			mapBubble = '<div class="bubble">'+$('#'+id).html()+'</div>';	
	
			if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
				googleMarker.openInfoWindowHtml(mapBubble, {maxWidth: 200});
				GEvent.addListener(googleMarker, "click", function(){
					googleMarker.openInfoWindowHtml(mapBubble, {maxWidth: 200});
				});
			} else {
				googleMarker.openExtInfoWindow(
					googleMap,
					"googleInfoWindow",
					mapBubble,
					{beakOffset: 3}
				);
				GEvent.addListener(googleMarker, 'click' , function(){ 
					googleMarker.openExtInfoWindow(
						googleMap,
						"googleInfoWindow",
						mapBubble,
						{beakOffset: 3}
					); 
				}); 
			}			
		} else {
			alert('Nie podałeś współrzędnych punktu serwisowego');
		}
	});
	
	tableCellsContent.hover(function(){
		var showOnMap = $(this).find('.showOnMap');
		showOnMap.show();
	}, function(){
		var showOnMap = $(this).find('.showOnMap');
		showOnMap.hide();
	})
	
}

/**
 * initializes maps google. there were problems between height of maps google and
 * hiding elements which contains those maps  
 */
function initMapsGoogle(){
	icon = new GIcon();  
	icon.image = "http://turbo-rotor.pl/wp-content/themes/turborotor/images/mapMarker.png";
	icon.iconSize = new GSize(32, 33);   
	icon.iconAnchor = new GPoint(8,33);
	if (!(jQuery.browser.msie && jQuery.browser.version == '6.0')) {
		icon.infoWindowAnchor = new GPoint(136, 0);
	} else {
		icon.infoWindowAnchor = new GPoint(15, 30);
	}

	jQuery('#mapBox').empty();
	if (GBrowserIsCompatible()) {
		googleMap = new GMap2(document.getElementById('mapBox'));
		var ui = new GMapUIOptions();
			ui.maptypes = {normal:true, physical:true, satellite: true};
			ui.zoom = {};
			ui.controls = {largemapcontrol3d:true, maptypecontrol:false};
			ui.keyboard = false;
		googleMap.setUI(ui);
	}
	
	initMarkers();

}


function initMarkers(){
    mapBubble = new Array();
    mapID = new Array();
    
    jQuery(".cellContent").each(function(i) {	
    	var coordinates = jQuery(this).find('.mapCoordinates').text().split(',');
    	var centerPoint = new GLatLng(parseFloat(coordinates[0]),parseFloat(coordinates[1]));
    	var googleMarker = new GMarker(centerPoint,icon);		
    	googleMap.addOverlay(googleMarker);
    	googleMap.setCenter(new GLatLng(51.75424, 19.442138), 6);
    
    
        	mapBubble[i] = '<div class="bubble">'+jQuery(this).html()+'</div>';
        	mapID[i]=jQuery(this).attr('id');
    
        		GEvent.addListener(googleMarker, 'click' , function(){ 
        			
              googleMap.setCenter(centerPoint, 15);
              googleMarker.openExtInfoWindow(
        				googleMap,
        				"googleInfoWindow",
        				mapBubble[i],
        				{beakOffset: 3}
        			); 
        			jQuery('#servicePoints li').removeClass('selected');
        			jQuery('#'+mapID[i]).parent().addClass('selected');
        		});
    
      });
}

function init(){
	initMapsBookmarks();
}
