function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
var oBMap;
var goIsBingReady = false;
var init_birdseye = function () {
	//var nBingTimer = window.setInterval(function(){
		if (!document.getElementById('divBing') || !document.getElementById('divBMap')) {
		alert('bing not ready');
			return;
		}
	//	window.clearInterval(nBingTimer);
		
		var mapOptions = {
			 credentials: "At-9Y-Rzkj6wVHyArJ9_nuCTfQmV5nXSP2gYoAKaVRpgK8p1ttCkVaxcoa04LwGM",
			 enableClickableLogo: false,
			 enableSearchLogo: false,
			 center: new Microsoft.Maps.Location(42, -71),
			 mapTypeId: Microsoft.Maps.MapTypeId.birdseye,
			 zoom: 10,
			 showScalebar: true,
			 height:jQuery('#divBing').height(),
			 width:jQuery('#divBing').width()
         };

		oBMap = new Microsoft.Maps.Map(document.getElementById("divBMap"), mapOptions);
		Microsoft.Maps.Events.addHandler(oBMap,'viewchangeend',function () {
			var mapView = oBMap.getBounds();
			oBMap_bounds = {
				ll:	{x:mapView.getWest(),
					y: mapView.getSouth()},
				ur: {x: mapView.getEast(),
					y: mapView.getNorth()}
			};
		});
		//jQuery('#divBingSource').appendTo('#divBing').removeClass('hidden-left');
		goIsBingReady = true;

	//},200);
};

var goCWCEvents;
var showBingMap = function(){
	hideMapTabs();
	activateTab('divBing');
	//removeClass(document.getElementById('divBing'), 'hidden-left');
//	document.getElementById('divBMap').style.display = 'block';
	document.getElementById('divBing').style.width=document.getElementById('divMaps').style.width;
	document.getElementById('divBing').style.height=document.getElementById('divMaps').style.height;
	//document.getElementById('divBing').style.width='760px';
	//document.getElementById('divBing').style.height='400px';
	
	if (CWCIsIE) {
		//ugh.  I feel sick.
		document.getElementById('divBing').style.padding="4px";
	}
	
	goCWCEvents = goCWCJSAPI.CWCEvents;
	//goCWCJSAPI.CWCEvents = null;
	goCWCNAV_CMD = goCWCJSAPI.NAV_CMD;
	goCWCJSAPI.NAV_CMD='';
	goCWCNAV_INPUT_COORDINATES =goCWCJSAPI.NAV_INPUT_COORDINATES;
	goCWCJSAPI.NAV_INPUT_COORDINATES='';
	goCWCNAV_INPUT_TYPE = goCWCJSAPI.NAV_INPUT_TYPE;
	goCWCJSAPI.NAV_INPUT_TYPE='';
	
	if ((goCWCJSAPI.oMap.maxx - goCWCJSAPI.oMap.minx) > 5000) {
		//console.log('bad size');
		//document.getElementById('divSVMessage').innerHTML = 'bad size';
		//return;
	}
	
	var oGeoLL = {
		x: parseInt(goCWCJSAPI.oMap.minx),
		y: parseInt(goCWCJSAPI.oMap.miny)
	};
	var oGeoUR = {
		x: parseInt(goCWCJSAPI.oMap.maxx),
		y: parseInt(goCWCJSAPI.oMap.maxy)
	};
	
	var source = new Proj4js.Proj(gszMapCoordsSRS);
	var dest = new Proj4js.Proj('EPSG:4326');
	
	var nProjReadyTimer = window.setInterval(function(){
		if (!source.readyToUse || !dest.readyToUse) {
			return;
		}
		window.clearInterval(nProjReadyTimer);
		//console.log(oGeoLL,oGeoUR);
		Proj4js.transform(source, dest, oGeoLL);
		Proj4js.transform(source, dest, oGeoUR);
		
		//var ogBounds = new GLatLngBounds(new GLatLng(oGeoLL.y, oGeoLL.x), new GLatLng(oGeoUR.y, oGeoUR.x));
		//new VELatLong(47.6218, -122.3492)
		var yCenter = oGeoLL.y + ((oGeoUR.y-oGeoLL.y)/2);
		var xCenter = oGeoLL.x + ((oGeoUR.x-oGeoLL.x)/2);
		if (!goIsBingReady) {
			init_birdseye();
			}
		//console.log(oGeoLL,oGeoUR);
		if (goIsBingReady) {
			oBMap.setOptions({height:jQuery('#divMaps').height(),
				width:jQuery('#divMaps').width()});		
			var bMapBounds =  Microsoft.Maps.LocationRect.fromEdges (
					oGeoUR.y,
					oGeoLL.x,
					oGeoLL.y,
					oGeoUR.x,
					0,
					Microsoft.Maps.AltitudeReference.ground		
				);
			bMapBounds = Microsoft.Maps.LocationRect.fromLocations([
					new Microsoft.Maps.Location(oGeoLL.y, oGeoLL.x),
					new Microsoft.Maps.Location(oGeoUR.y, oGeoUR.x)
					]);
				
			//bMapBounds = new Microsoft.Maps.LocationRect(new Microsoft.Maps.Location(yCenter,xCenter),oGeoUR.x-oGeoLL.x,oGeoUR.y-oGeoLL.y);
				
			//oBMap.setView({	bounds: bMapBounds	});
			//console.log(oBMap.getZoom());

			//document.getElementById('divBMap').style.display = 'block';
			//oBMap.Resize();
			//window.setTimeout(function () {oBMap.setView({bounds: bMapBounds});},1500);
			oBMap.setView({bounds: bMapBounds});
			goCWCJSAPI.RegisterEvent(MAP_SIZE_CHANGED, "ResizeBingMap");

		} else {
			alert ('Bing map failed to load, please refesh this page and try again');
		}
	}, 200);
	
	return true;
};

function ResizeBingMap () {
	document.getElementById('divBing').style.width=document.getElementById('divMaps').style.width;
	document.getElementById('divBing').style.height=document.getElementById('divMaps').style.height;
	oBMap.setOptions({height:jQuery('#divMaps').height(),
					width:jQuery('#divMaps').width()});		
}

//over-ride the showMapTabs() call:
(function() {
	var oOrigShowMapTabs = showMapTabs;

	showMapTabs = function() {
		// see streetview.js for how this is used.  Basically, it disables the chameleon events chain until
		// showMapTabs is called again.
		// it also changes zooms the map view to the current google maps view in the street view tab.
		if (window.goCWCEvents && window.goCWCEvents != null) {
			//goCWCJSAPI.CWCEvents = goCWCEvents;
			goCWCEvents = null;
			goCWCJSAPI.NAV_CMD = goCWCNAV_CMD;
			goCWCJSAPI.NAV_INPUT_COORDINATES = goCWCNAV_INPUT_COORDINATES ;
			goCWCJSAPI.NAV_INPUT_TYPE = goCWCNAV_INPUT_TYPE ;

		
			//need to reproject back into map coords
			var source = new Proj4js.Proj('EPSG:4326');
			var dest = new Proj4js.Proj(gszMapCoordsSRS);
		
			var nProjReadyTimer = window.setInterval(function(){
				if (!source.readyToUse || !dest.readyToUse) {
					return;
				}
				window.clearInterval(nProjReadyTimer);
			
				var oBoundsLL = oBMap_bounds.ll;
				var oBoundsUR = oBMap_bounds.ur;
				//console.log(oBoundsLL,oBoundsUR);
				Proj4js.transform(source, dest, oBoundsLL);
				Proj4js.transform(source, dest, oBoundsUR);
			
				//shrink the box a little bit on zoom-back
				var nXshrink = .34;
				var nYshrink = .25;
				var oShrunkBoundsLL = {
					x: oBoundsLL.x + (nXshrink * (oBoundsUR.x - oBoundsLL.x)),
					y: oBoundsLL.y + (nYshrink * (oBoundsUR.y - oBoundsLL.y))
				}
				var oShrunkBoundsUR = {
					x: oBoundsLL.x + ((1-nXshrink) * (oBoundsUR.x - oBoundsLL.x)),
					y: oBoundsLL.y + ((1-nYshrink) * (oBoundsUR.y - oBoundsLL.y))
				}
			//console.log(oBoundsLL,oBoundsUR,oShrunkBoundsLL,oShrunkBoundsUR );
				goCWCJSAPI.oMap.SetExtents(oShrunkBoundsLL.x,oShrunkBoundsLL.y,oShrunkBoundsUR.x,oShrunkBoundsUR.y);
			}, 200);
		
		}

		oOrigShowMapTabs();
	}
})();

