jQuery.noConflict();
jQuery(function($){
    var frontend = {
		strokeColor: ["#ff0000","#00ffff","#ff00ff"],		
		init: function(){
			this.homeMap();
			this.routeMap();
			this.roundedCorners();
		},
		
		homeMap: function(){
			if ($('body.homepage').length && $('#googleMap').length) {
				var pathData = new Array();
				var latlngbounds = new google.maps.LatLngBounds();
				
				//console.log(pathData[0][4]['lat']);
				
				var latlng = new google.maps.LatLng(50.6, 4.1);
				var myOptions = {
					zoom: 8,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.HYBRID
				};
				
				var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
				
				
				$('.coordinates').each(function(i)
				{
					pathData[i] = frontend.getCoordinatesAsArray($(this).text());
		
					if (pathData[i]) 
					{
						var coordinates = [];
						for (var j = 0; j < pathData[i].length; j++) 
						{
							latlngbounds.extend(new google.maps.LatLng(pathData[i][j]['lat'], pathData[i][j]['lon']));
							coordinates[j] = new google.maps.LatLng(pathData[i][j]['lat'], pathData[i][j]['lon']);
						}
						
						//console.log(coordinates);
						
						var path = new google.maps.Polyline({
    						path: coordinates,
    						strokeColor: frontend.strokeColor[i],
    						strokeOpacity: 1.0,
    						strokeWeight: 3
  						});
						path.setMap(map);
					}
				});
				
				map.fitBounds(latlngbounds);
				map.setCenter(latlngbounds.getCenter());
				
			}
		},
		
		routeMap: function(){
			if ($('body.routepage').length && $('#googleMap').length) {
				
				var coordinatesAr = frontend.getCoordinatesAsArray($('#coordinates').text());
				var coordinates = [];
				
				var latlngbounds = new google.maps.LatLngBounds();
  				for ( var i = 0; i <coordinatesAr.length; i++ )
  				{
    				latlngbounds.extend(new google.maps.LatLng(coordinatesAr[i]['lat'],coordinatesAr[i]['lon']));
					coordinates[i] = new google.maps.LatLng(coordinatesAr[i]['lat'],coordinatesAr[i]['lon']);
  				}
				
				var center = latlngbounds.getCenter();
			
				var myOptions = {
					zoom: 12,
					center: center, 
					mapTypeId: google.maps.MapTypeId.HYBRID
				};
				
				var map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
  				map.fitBounds(latlngbounds);
				
				var path = new google.maps.Polyline({
    				path: coordinates,
    				strokeColor: "#FF0000",
    				strokeOpacity: 1.0,
    				strokeWeight: 3
  				});
				path.setMap(map);
			}
		},
		
		getCoordinatesAsArray:function(value)
		{
			var coordinates = value.split(')(');
			var cleaned = new Array();
			var latLng = "";
			if (coordinates.length > 1) 
			{
			 	for(var i = 0; i<coordinates.length; i++)
				{
					cleaned[i] = new Array();
					latlng = coordinates[i].toString().split(",");
					cleaned[i]['lat'] = $.trim(latlng[0].replace('(', ''));
					cleaned[i]['lon'] = $.trim(latlng[1].replace(')', ''));
				}
				return cleaned;
			}
			
		},
		
		roundedCorners:function(value)
		{
			if(!$.support.opacity)
			{
				$('#content, #routes li, a.button, ul.sortList a, #mainNav a').corner('5px');
                                $('#memberNav a').corner('5px bottom');
			}
		}
	}
    frontend.init();
});
