/**
 * jQuery overview plugin. Attach it to #Suppliers (the entire page, preferrably <body>).
 */
(function($){
	$.fn.overview = function(){
		var self = this
		self.map = $('#map',self);
		self.marker ='';
		self.content = $('#infoWindowContent',self)
		self.address = $('#infoWindowContent .address',self).text();
		//console.log('address: '+self.address);
		var markup = $('#infoWindowContent',self).html();
		
		self.initMap = function(showInfo){
			self.map.jmap('init', {mapZoom: 12, mapControlSize: "small",language:"en", mapCenter:[34.0522,-118.242798]},function(el, options){
				self.el = el;
				$(self.el).jmap("searchAddress", {address: self.address}, function(options, point){
					$(self.el).jmap('moveTo',{mapCenter:[point.y, point.x]});
					$(self.el).jmap('addMarker', {pointLatLng:[point.y, point.x], addMarker: true, pointHTML: markup},function(marker,options){
						self.Marker = marker;
						$(self.el).jmap('triggerMarker',marker,function(){
								//console.log('triggered');
								$('#getdirections',self).click(function(){
									var from = $('.From', self).val();
									if(from){
										$(el).jmap('triggerMarker',self.Marker);
										self.getDirections(from);
									}else{
										alert('You must enter an address to start from');
									}
									return false;
							});
						});
					});
				});
			});
		}
		
		
		self.getDirections = function(from){
			//console.log(from);
			$(self.map).animate({height:'200px'},function(){
				self.map.jmap('resize',function(){
						//console.log(from+"::"+self.address);
						$(self.el).jmap("searchDirections", {fromAddress: from, toAddress: self.address, directionsPanel:"directions"},function(){
								$('#directions',self).show();
								$(self.content).show();
						});
				});
			});
			
		}
        
		//time to run
		self.initMap();
	}
})(jQuery);
