/**
 * jQuery gMap
 *
 * @url		http://gmap.nurtext.de/
 * @author	Cedric Kastner <cedric@nur-text.de>
 *          Modified by Berk Demirkır <bdemirkir@mixoffice.com>
 * @version	1.0.1
 */
 var mgr;
(function($)
{
	// Main plugin function
	$.fn.gMap = function(options)
	{
		// Check if the browser is compatible with Google Maps
		if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;
		
		// Build main options before element iteration
		var opts = $.extend({}, $.fn.gMap.defaults, options);
    
		// iterate and reformat each matched element
		return this.each(function()
		{
			// Create new map and set initial options
			$gmap = new GMap2(this);
			$.fn.mapObj = $gmap;
			
			// Try to center to the first marker
			if (!opts.latitude && !opts.longitude)
			{
				// Check for at least one marker
				if (is_array(opts.markers) && opts.markers.length >= 1)
				{
					// Center to the first marker
					opts.latitude  =  opts.markers[0].latitude;
					opts.longitude =  opts.markers[0].longitude;
				}
				else
				{
					// Center Earth :)
					opts.latitude  = 39.113014;
					opts.longitude = 35.288086;
					opts.zoom = 6;
					
				}
				
			}
			
			$gmap.defaultLatLng = new GLatLng(opts.latitude, opts.longitude);
			$gmap.defaultZoom = opts.zoom;
			// Center the map and set the maptype
			$gmap.setCenter($gmap.defaultLatLng, $gmap.defaultZoom);
			$gmap.setMapType(opts.maptype);
			
			// Check for custom map controls
			if (opts.controls.length == 0)
			{
				// Default map controls
				$gmap.setUIToDefault();
			}
			else
			{
				// Add custom map controls
				for (var i = 0; i < opts.controls.length; i++)
				{
					// Eval is evil - I know. ;)
					eval('$gmap.addControl(new ' + opts.controls[i] + '());');
				}
				
			}
						
			// Check if scrollwheel should be enabled when using custom controls
			if (opts.scrollwheel == true && opts.controls.length != 0) { $gmap.enableScrollWheelZoom(); }
			
			// Create new icon and set initial options
			gicon = new GIcon();
			gicon.image = opts.icon.image;
			gicon.shadow = opts.icon.shadow;
            
            gicon2 = new GIcon();
            gicon2.image = opts.icon.image2;
            gicon2.shadow = opts.icon.shadow;
			
			// Additional options to check for
			gicon.iconSize = gicon2.iconSize = (is_array(opts.icon.iconsize)) ? new GSize(opts.icon.iconsize[0], opts.icon.iconsize[1]) : opts.icon.iconsize;
			gicon.shadowSize = gicon2.shadowSize = (is_array(opts.icon.shadowsize)) ? new GSize(opts.icon.shadowsize[0], opts.icon.shadowsize[1]) : opts.icon.shadowsize;
			gicon.iconAnchor = gicon2.iconAnchor = (is_array(opts.icon.iconanchor)) ? new GPoint(opts.icon.iconanchor[0], opts.icon.iconanchor[1]) : opts.icon.iconanchor;
			gicon.infoWindowAnchor = gicon2.infoWindowAnchor = (is_array(opts.icon.infowindowanchor)) ? new GPoint(opts.icon.infowindowanchor[0], opts.icon.infowindowanchor[1]) : opts.icon.infowindowanchor;
			
            mgr = new MarkerManager($gmap, {trackMarkers:true});
            mgr.repopulate = function(bayi, servis) {
                mgr.clearMarkers();
			    // Add all map markers
			    for (var j = 0; j < opts.markers.length; j++)
			    {
				    // Get the options from current marker
				    marker = opts.markers[j];
				    
				    // IE doesn't want to see commas at the end of arrays! But we may add with this check
				    if(!marker) continue;
                    if( (bayi == 1 && servis == 0 && marker.bayi == 0) ||
                        (bayi == 0 && servis == 1 && marker.servis == 0))
                    {
                            continue;
                    }
				    
				    // Create a new marker on the map
                    if(marker.bayi == 0 && marker.servis == 1) {
				        gmarker = new GMarker(new GPoint(marker.longitude, marker.latitude), gicon2);
                    } else {
                        gmarker = new GMarker(new GPoint(marker.longitude, marker.latitude), gicon);
                    }
				    
                    // Add overlay if marker was created and check if popup should be shown when map is loaded
                    if (gmarker) { mgr.addMarker(gmarker, marker.minZoom ? marker.minZoom : 1, marker.maxZoom ? marker.maxZoom : 17); }
                    if (marker.popup == true) { gmarker.openInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append); }
                    if (marker.zoom) {
                        gmarker.zoom = marker.zoom;
						if(marker.city)
							gmarker.city = marker.city;
							
                        GEvent.addListener(gmarker, "click", function () {
                            if(this.zoom == 10 && this.city) {
								$('#city').val(this.city).change();
							} else {
								$gmap.setCenter(this.getLatLng(), this.zoom);
							}
                        });
                    }
                    
				    // Only display info window if the marker contains a description
				    if (marker.html)
				    {
                        // Bind the info window to marker
                        gmarker.bindInfoWindowHtml(opts.html_prepend + marker.html + opts.html_append);
				    }
			    }
			    mgr.refresh();
            };
            mgr.repopulate(1, 1);
		});
		
	}
	
	// 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,
		zoom:					6,
		markers:				[],
		controls:				[],
		scrollwheel:			true,
		maptype:				G_NORMAL_MAP,
		html_prepend:			'<div class="gmap_marker">',
		html_append:			'</div>',
		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);
