function GoogleMap(opts) {
	this._map = false;
	this._center = false;
	this._mapHolder = false;
	this._allMarkers = [];
	this._infoWindow = false;
	this._allPolygons = [];
	this._directionsDisplay = new google.maps.DirectionsRenderer();
	this._directionsService = new google.maps.DirectionsService();
	
		var scope = this;
	
		this._center = new google.maps.LatLng(opts.centerLat,opts.centerLong);
	
		this._mapHolder = (opts._mapHolder) ? opts._mapHolder : this._mapHolder;
	
		var mapOptions_ = {};
			mapOptions_['zoom'] = (opts.zoom) ? opts.zoom : 17;
			mapOptions_['center'] = this._center;
			mapOptions_['mapTypeId'] = (opts.mapType) ? opts.mapType : google.maps.MapTypeId.ROADMAP;
			mapOptions_['disableDefaultUI'] = opts.disableDefaultUI;
			this._map = new google.maps.Map(scope._mapHolder.get(0),mapOptions_);			
			
			//load the markers passed in with the initating options
			for(i in opts.markers) {
				this.loadMarker(opts.markers[i].lat,opts.markers[i].lng,opts.markers[i].icon,opts.markers[i].title,opts.markers[i].infoWindow);
			}
			
			this._infoWindow = new google.maps.InfoWindow({content:"<div id='infoWindow'>Hi I'm an Info Window</div>",position:this._center,zIndex:110});
}

GoogleMap.prototype.loadMarker = function(lat,lng,icon,title,html) {
		var scope = this;
		
		var MarkerOptions_ = {};
			if(title) { MarkerOptions_['title'] = title; }
			if(icon) { MarkerOptions_['icon'] = icon; MarkerOptions_['flat'] = true; }
			MarkerOptions_['map'] = this._map;
			MarkerOptions_['position'] = new google.maps.LatLng(lat,lng);
	
		var newMarker_ = new google.maps.Marker(MarkerOptions_);
			this._allMarkers.push(newMarker_);
			newMarker_.setMap(this._map);

			if(!html) { return newMarker_; }
			
			google.maps.event.addListener(newMarker_,'click',function() {
					InfoOptions_ = {};
						InfoOptions_['offset'] = new google.maps.Size(10,0);
						InfoOptions_['content'] = html;
					scope._infoWindow.setOptions(InfoOptions_);
					scope._infoWindow.open(scope._map,newMarker_);
			});
		
		return newMarker_;
}

GoogleMap.prototype.resizeInfoWindow = function() {
		var infoWindow = $('#infoWindow');
		infoWindow.parent().parent().css('height','250px').parent().css('height','300px');
}

GoogleMap.prototype.loadPolygon = function(opts) {
		var o = this;
		var newPolygon_ = new google.maps.Polygon(opts);
		if($.inArray(opts.zIndex,$.soldLots) != -1) { newPolygon_.setOptions({fillColor:'#e72929',fillOpacity:0.8}); newPolygon_.sold = true;}
			newPolygon_.setMap(this._map);
			this._allPolygons.push(newPolygon_);
			google.maps.event.addListener(newPolygon_,'mouseover',function(e) { 
				$.each(o._allPolygons,function(index,val) {  if(!val.sold) { val.setOptions({fillOpacity:0}); } }); 
				this.setOptions({fillOpacity:0.8});
				$.GMapSlider.moveTo(parseInt(this.d.zIndex)); 
			});
}

GoogleMap.prototype.getDirections = function(opts) {
		var o = this;
		this._directionsService.route(opts,function(res,status) {
			if(status == google.maps.DirectionsStatus.OK) {	
				if(res.routes.length == 2) { res.routes[0] = res.routes[1]; }
				o._directionsDisplay.setDirections(res);
				/*o._directionsDisplay.setOptions({markerOptions:{icon:$.themeURL+'/images/star.png'}});*/
				o._directionsDisplay.setOptions({markerOptions:{visible:true}});
				var time_ = res.routes[0].legs[0].duration.text;
				var distance_ = res.routes[0].legs[0].distance.text;
				/*$('#time').animate({opacity:0},250,function() { $(this).html(distance_ + ' - about ' + time_).animate({opacity:1},250); });*/
				o._directionsDisplay.setMap(o._map);
			}
		});
}

GoogleMapOpts = {
	_mapHolder : $('#gmap'),
	centerLat : 47.31645,
	centerLong : -52.8157,
	mapType : google.maps.MapTypeId.ROADMAP,
	zoom : 12,
	disableDefaultUI : true,
	markers : [
		{
			lat : 47.3099,
			lng : -52.7879,
			title : "The Bread & Cheese Country Inn"
		}
	]
}

var SmallMap = new GoogleMap(GoogleMapOpts);

if($('#gmap2').length) { 
	delete GoogleMapOpts.markers;
	GoogleMapOpts._mapHolder = $('#gmap2');
	GoogleMapOpts.disableDefaultUI = false;
	
	var LargeMap = new GoogleMap(GoogleMapOpts);
	
	
	//load stars on carbonear, st. john's, and bristol hill
	var BreadAndCheese = LargeMap.loadMarker(47.3099, -52.7879,false,"The Bread & Cheese Country Inn");
	var StJohnsAirport = LargeMap.loadMarker(47.61415, -52.7436,false,"St. John's Airport");
	var PortAuxBasques = LargeMap.loadMarker(47.58169, -59.13957,false,"Port Aux Basques Ferry");
	var Argentia = LargeMap.loadMarker(47.29004, -53.99190,false,"Argentia Ferry");
	
	StJohnsAirport.setVisible(false);
	PortAuxBasques.setVisible(false);
	Argentia.setVisible(false);
	
	$('a.directions').bind('click',function(e) {
		e.preventDefault();

		//get the origin position
		switch($(this).attr('id')) {
			case "airport": var origin_ = StJohnsAirport.getPosition();	break;
			case "portauxbasques": var origin_ = PortAuxBasques.getPosition();	break;
			case "argentia":	var origin_ = Argentia.getPosition();	break;
		}
		
		//scroll up to the map
		var destination = $('#gmap2').offset().top;
		$("html:not(:animated),body:not(:animated)").stop().animate({ scrollTop: destination-20}, 500 ,function() {
			
			var directionOpts = {origin:origin_,destination:BreadAndCheese.getPosition(),travelMode:google.maps.DirectionsTravelMode.DRIVING,provideRouteAlternatives:true};
			LargeMap.getDirections(directionOpts);
		});
		
		//set the default bread and cheese marker to false
		BreadAndCheese.setVisible(false);
		
	});
			
}  
