;(function($) {

$.pluginCore('plugin.moreLoader', {
	options: {
		url: false,
		indicator: '<span class="loadingIndicator">Loading...</span>'
	},
	_create: function() {
		var self = this;

		if (!self.options.url) {
			self.options.url = self.widget().attr('href');
		}
	},
	_init: function() {
		var self = this;
		self.widget().click(function() {
			self.load();
			return false;
		});
	},
	load: function() {
		var self = this;
		
		var indicator = $(self.options.indicator);
		self.widget().hide().after(indicator);
		$.get(self.options.url, function(content) {
			self.widget().replaceWith(content);
			$(document).pluginLoader();
			FB.XFBML.parse();
			indicator.remove();
		});
	}
}); 

$.pluginCore('plugin.submitButtonIndicator', {
	options: {
		indicator: '/img/indicators/h_small.gif',
		text: 'Processing...'
	},
	_create: function() {
	},
	_init: function() {
		var self = this;
		self.element.closest('form').submit(function() {
			self.loadIndicator();
		});
		self.element.click(function() {
			self.loadIndicator();
		});
	},
	loadIndicator: function() {
		var self = this;
		if (self.replaced) {
			return;
		}
		var base = '';
		if (__appBase) {
			base = __appBase;
		}
		self.replaced = true
		
		var replacement = '<span class="text">' + self.options.text + '</span>';
		if (self.options.indicator) {
			replacement = '<img src="'+base+self.options.indicator+'"/>' + replacement;
		}
		replacement = '<span class="indicator">' + replacement + '</span>';
		self.element.hide().after(replacement);
	}
});

$.pluginCore('plugin.googleMap', {
	options: {
		mapOptions: {
			zoom: 4,
			center: new google.maps.LatLng(37.09024, -95.71289100000001),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		},
		markerOptions: {
		},
		clusterMarkers: false, //true,
		clustererOptions: {
			maxZoom: 15,
			gridSize: 50
		},
		markers: [],
		autoCenter: true,
		maxZoom: 10
	},
	_create: function() {
		var self = this;

		self._createMap(self.element.get(0));
	},
	_init: function() {
		var self = this;

		self._initMap();

		self._trigger('init');
	},
	_createMap: function(element) {
		var self = this;

		self.map = new google.maps.Map(element, self.options.mapOptions);
		self.bounds = new google.maps.LatLngBounds();
		if (self.options.clusterMarkers) {
			self.clusterer = new MarkerClusterer(self.map, [], self.options.clustererOptions);
		}	
	},
	_initMap: function() {
		var self = this;
		
		self.markers = [];

		self.addMarkers(self.options.markers);
	},
	_addMarker: function(options) {
		var self = this;

		var marker = new google.maps.Marker(options);

		if (options.info) {
			var infowindow = new google.maps.InfoWindow({
    				content: options.info
			});

			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(self.map, marker);
			});	

		}
		
		self.markers.push(marker);
		if (marker.getPosition()) {
			self.bounds.extend(marker.getPosition());
		}
		if (self.options.autoCenter) {
			self.autoCenter();
		}
		if (self.options.clusterMarkers) {
			self.clusterer.addMarker(marker);
		}
		return marker;
	},
	addMarker: function(options, callback) {
		var self = this;

		$.extend(options, self.options.markerOptions);
		$.extend(
			options,
			{
				'map': self.map
			}
		);

		if (options.address) {
			self.geocode(options.address, function(result) {
				if (result) {
					options.position = new google.maps.LatLng(result.lat, result.lon);
					var marker = self._addMarker(options);
				
					if (callback && $.isFunction(callback)) {
						callback(marker);
					}
				}	
			});
		}
		else {
			var marker = self._addMarker(options);
			
			if (callback && $.isFunction(callback)) {
				callback(marker);
			}	
		}	
	},
	addMarkers: function(markers) {
		var self = this;

		for(var i in markers) {
			self.addMarker(markers[i]);
		}
	},
	removeMarkers: function() {
		var self = this;

		for(var i in self.markers) {
			self.markers[i].setMap(null);
		}
		self.markers = [];
		self.bounds = new google.maps.LatLngBounds();
		if (self.options.clusterMarkers) { 
			self.clusterer.clearMarkers();
		}
	},
	setCenter: function(location) {
		var self = this;
		if (!self.markers.length && !location) {
			address = 'USA';
		}
		
		if (location && location[0] && location[1]) {
			self.map.setCenter(new google.maps.LatLng(location[0], location[1]));
		}
		else if ($.type(location) == 'string') {
			self.geocode(location, function(res) {
				if (res) {
					self.map.setCenter(new google.maps.LatLng(res.lat, res.lon));
				}
			});
		}
		else if ($.type(location) == 'object') {
			self.map.setCenter(location);
		}
		else {
			self.autoCenter();
		}
	},
	setZoom: function(zoom) {
		var self = this;

		self.map.setZoom(zoom);
	},
	geocode: function(address, callback) {
		var self = this;
	
		if (!self.geocoder) {
			self.geocoder = new google.maps.Geocoder();
		}

		self.geocoder.geocode( {'address': address}, function(results, status){
			if (callback && $.isFunction(callback)) {
				var response = null;
				if (status == 'OK' && results[0]) {
					response = {
						value: results[0].formatted_address,
						lat: results[0].geometry.location.lat(),
						lon: results[0].geometry.location.lng()
					}
				}
				callback(response);
			}
		});
	},
	autoCenter: function() {
		var self = this;
		if (!self.bounds.isEmpty()) {
			self.map.fitBounds(self.bounds);
		}
		if (self.options.maxZoom) {
			var zoom = self.map.getZoom();
			if (zoom>self.options.maxZoom) {
				self.setZoom(self.options.maxZoom);
			}
		}
	}
});

$.pluginCore('plugin.limitTextarea', {
	options: {
		limit: 0,
		counter: null
	},
	_create: function() {
		var self=this;

		self.element
			.keyup(function(){self._edit();})
			.keydown(function(){self._edit();})
			.focus(function(){self._edit();})
			.live('input paste', function(){self._edit();});
		self._edit();

	},
	_edit: function(){
		var self=this;

		if(self.element.val().length > self.options.limit){
			self.element.val(self.element.val().substr(0, self.options.limit));
		}
		
		if ( self.options.counter ) {
			$(self.options.counter).html(self.getRemainingCount());
		}
	},
	getRemainingCount: function() {
		var self=this;
		return self.options.limit - self.element.val().length;
	},
	getMaxLength: function() {
		var self=this;

		return parseInt(self.element.attr(self.options.attribute));
	}
});


})(jQuery);

