var places = {
    config: {},
    init: function(){
        places.el.wrapper = $('#places');
		// no places
        if (!places.el.wrapper.length) 
            return false;
		// no google maps
        if (typeof(google.maps) !== 'object') 
            return false;
        
        places.el.wrapper.hide();
        // create map
        places.createMap();
        // find items and create locations
        places.el.items = places.el.wrapper.children('.item');
        places.el.items.each(function(){
            places.createLocation($(this));
        });
    },
    createMap: function() {
	var pmc = $('#placesMapCenter').text();
	var pos = "";
	var zoom = parseInt($('#placesMapZoom').text());
	
	if (pmc != undefined && pmc.indexOf("|") >= 0) {
		var arr = pmc.split("|");
		pos = arr[0];
		zoom = (parseInt(arr[1]) <= 0) ? zoom : parseInt(arr[1]);	
	} else {
		pos = pmc;
	}
	 
        // get gps coords
        var latlng = places.parseGps(pos);
        // define center of map	
        var mapCenter = new google.maps.LatLng(latlng[0], latlng[1]);
        // define map options
        var mapOptions = {
            zoom: zoom,
            center: mapCenter,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
			      disableDefaultUI: false,
			      mapTypeControl: false,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                position: google.maps.ControlPosition.TOP_RIGHT
            },
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.ZOOM_PAN,
                position: google.maps.ControlPosition.TOP_RIGHT
            },
            scaleControl: true,
            scaleControlOptions: {
                position: google.maps.ControlPosition.TOP_LEFT
            }  
        };
        // create map from options and center
        places.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    },
    createLocation: function(el){
        var locoptions = {};
		var autoOpen = parseInt($('#placesMapAutoOpen').text());

        var ico = "gmap-logo.png";
		var markerImage = new google.maps.MarkerImage(
			'/public/images/gmap-logo.png',
			new google.maps.Size(20, 19),
			new google.maps.Point(0,0),
			new google.maps.Point(0,0)
		);
		
        var loc = el.find('.location').text();
	
        // get gps coords
        var latlng = places.parseGps(loc);
        // create gps location
        locoptions.markerPos = new google.maps.LatLng(latlng[0], latlng[1]);
		
        // create marker
        locoptions.marker = new google.maps.Marker({
            position: locoptions.markerPos,
            title: el.find('.title').text(),
            icon: markerImage,
            map: places.map
        });
		
		// html of info window
		var text = '<div class="map_content"><div class="bodyContent">' + el.html() + '</div></div>';
		
		// create info window
		var infowindow = new google.maps.InfoWindow({
			content: text
		});
		
		if (autoOpen == 1) {
			infowindow.open(places.map,locoptions.marker);
		}

		// open info box
		google.maps.event.addListener(locoptions.marker, 'click', function() {
			infowindow.open(places.map,locoptions.marker);
		});
		
        // store link to array
        places.locations[places.locations.length] = locoptions;
    },
    parseGps: function(text){
        return text.replace(/\s/, '').split(/,|;/);
    },
    el: {},
    locations: []
};

