/**
 * jQuery gMap
 *
 * @url		http://gmap.nurtext.de/
 * @author	Cedric Kastner <cedric@nur-text.de>
 * @version	1.0.3
 */
(function($)
{
	// Main plugin function
	$.fn.gMap = function(options)
	{		
		// Build main options before element iteration
		var opts = $.extend(true, {}, $.fn.gMap.defaults, options);

		// Iterate each matched element
		return this.each(function()
		{
			// Create new map and set initial options
			var $gmap = new google.maps.Map(this, opts.mapOptions);
			$(this).data('gmap', $gmap);
		});
		
	}
	
	$.fn.gMap.MyMap = null;
	
	$.fn.gMap.addMarker = function($gmap, markers, options) {
		var opts = $.extend(true, {}, $.fn.gMap.defaults, options);
		// Add all map markers
		$.each(markers, function(key, item) {
			var current = null;
			var currentHtml = null;
			
			// Get the options from current marker
			
			var _image = new google.maps.MarkerImage(item.icon.image,
				new google.maps.Size(30, 30),
				new google.maps.Point(0,0),
				new google.maps.Point(0, 30));
			
			var _shadow = new google.maps.MarkerImage("http://www.graffitiartmaps.com.br/wp-content/themes/graffiti/images/shadow.png",
				new google.maps.Size(32, 32),
				new google.maps.Point(0,0),
				new google.maps.Point(1, 31));
			
			var myLatLng = new google.maps.LatLng(item.latitude, item.longitude);
			
			var marker = new google.maps.Marker({
				position: myLatLng,
				map: $gmap,
				title:"Hello World!",
				icon: _image,
				shadow: _shadow
			});
			
			/*if(item.html) {
				var infowindow = new google.maps.InfoWindow({
					content: item.html,
					domready: function() {
						$("#divMapaInterna #box-info .imagem ul").jcarousel({
							visible:1,
							scroll:1,
							initCallback: function() {
								$("#divMapaInterna #box-info .imagem a").unbind("click").nyroModal();	
							}
						});
					}
				});
			}*/
				
			if(item.current == true) {
				var infoBox = new InfoBox({
					latlng: marker.getPosition(), 
					map: $gmap, 
					content: item.html,
					callback: function() {
						$("#divMapaInterna #box-info .imagem ul").jcarousel({
							visible:1,
							scroll:1,
							initCallback: function() {
								$("#divMapaInterna #box-info .imagem a").unbind("click").nyroModal();	
							}
						});
					}
				});
			}
			
			google.maps.event.addListener(marker, 'click', function() {
				if(opts.isHome == true) {
					window.location = item.link;
				}
				else if(opts.isSingle == true){
					var infoBox = new InfoBox({
						latlng: marker.getPosition(), 
						map: $gmap, 
						content: item.html,
						callback: function() {
							$("#divMapaInterna #box-info .imagem ul").jcarousel({
								visible:1,
								scroll:1,
								initCallback: function() {
									$("#divMapaInterna #box-info .imagem a").unbind("click").nyroModal();	
								}
							});
						}
					});
				}					
			});
		});
	}
	
	function bind_click( obj, link ) {
		GEvent.addListener(obj, "click", function() {
			window.location = link;
		});
	}
	
	// Function to check if array or not
	function is_array(input)
	{
		return typeof(input) == 'object' && (input instanceof Array);
  	}
	
	// Set default settings
	$.fn.gMap.defaults =
	{
		latitude:				0,
		longitude:				0,
		markers:				[],
		controls:				[],
		html_prepend:			'<div class="gmap_marker">',
		html_append:			'</div>',
		isHome:					false,
		isSingle:				false,
		mapOptions:
		{
			mapTypeId:				google.maps.MapTypeId.ROADMAP,
			zoom:					2,
			center:					new google.maps.LatLng(34.885931, 9.84375),
			scaleControl: 			true,
			navigationControl: 		true,
			scrollwheel:			true,
			mapTypeControl: 		true,
			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_LEFT
			},
			scaleControl: 			true,
			scaleControlOptions: 
			{
				position: 			google.maps.ControlPosition.TOP_LEFT
			}
		},
		icon:
		{
			image:				"http://www.google.com/mapfiles/marker.png",
			shadow:				"http://www.google.com/mapfiles/shadow50.png",
			iconsize:			[20, 34],
			shadowsize:			[37, 34],
			iconanchor:			[9, 34],
			infowindowanchor:	[9, 2]			
		}
		
	}
	
})(jQuery);
