var mjStreetView = {
	map: null,
	isExecutedOnLoad: false,
	isThere: false,
	isStreetView: false,
	toggling:false,
	isAvatarMoving:false,
	autoPan:false,
	isTileDownloaded:false,
	pois:[],
	largeMapControl: null,
	smallMapControl: null,
	options: {
		mapId: "map",
		streetId: "mapStreet",
		frenchFlag: "0", // 0: English, 1: French
		serverName: "www.canpages.ca",
		svHeight: 250,
		normalMapHeight: 500,
		toggledMapHeight: 250,
		pageType: 'M', // M: main page, D: detail page, R: report page, MS: Map Search
		initLat: 0,
		initLng: 0,
		isInternal: '1',
		viewPoint: ""
	},

	init: function(opts) {
		this.options = jQuery.extend({}, this.options, opts);
		this.map = this.options.mapInstance;
	},

	getNearByJack: function(jackLat, jackLng, _radius) {
		var nearbyPois = [];
		var radius = _radius||50;
		var jackLL = new GLatLng(jackLat, jackLng); 

		for(var i = 0; i < this.pois.length; i++) {
			var dist = jackLL.distanceFrom(new GLatLng(this.pois[i].lat, this.pois[i].lon));

			if(dist <= radius) {
				nearbyPois.push(this.pois[i]);
			}
		}

		return nearbyPois;
	},
	
	getPano: function() {
		var lat, lng;
		if(this.options.pageType == 'M') {
			lat = this.map.GetCenter().Latitude;//map.getCenter().lat();
			lng = this.map.GetCenter().Longitude;//map.getCenter().lng();
		}
		else {
			lat = this.options.initLat;
			lng = this.options.initLng;
		}

		if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
			alert("This page requires AC_RunActiveContent.js....");
		} 
		else {
			var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
			hasRightVersion = true; // Bug in the new auto-download version DetectFlashVer doesnt work anymore

			if (hasRightVersion) {
				var obj = this.getPanoParams(this.options.serverName, 'pano', lat, lng);
				return obj;
			} else {
				var alternateContent = ''
					+ '<div><br>&nbsp;&nbsp;You need <a target=_top href=http://www.macromedia.com/go/getflash/ >'
					+ 'Flash Player version 9</a> to use this page.<br/>&nbsp;</div>'
					+ '<div align=center><a target=_top href=http://www.macromedia.com/go/getflash/ ><img src=Images/FlashGUI.jpg border=0></a></div>';
				return alternateContent;
			}
		}
	},
	
	getPanoParams: function(playerServer, dataFolder, lat, lng, isStandAlone) {
		var panoObjName = isStandAlone == true ? "panoGUI2" : "panoGUI"; 
			
		return AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
				'width', '100%',
				'height', '100%',
				'src', 'http://'+playerServer+'/pano/pano_5',  // Filename
				'quality', 'autohigh',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'align', 'middle',
				'play', 'true',
				'loop', 'false',
				'scale', 'showall',
				'wmode', 'transparent', // not window to show other layers like movie, save as, more info etc.
				'devicefont', 'false',
				'id', panoObjName, //'panoGUI',
				'bgcolor', '#000000',
				'name', panoObjName, //'panoGUI',
				'menu', 'false',
				'allowScriptAccess','sameDomain',
				'allowFullScreen','false',
				'movie', 'http://'+playerServer+'/pano/pano_5',  // Filename
				'salign', '',
				'allowFullScreen','true',
				'FlashVars','linkHereUrl=http://www.canpages.ca/?&lang='+this.options.frenchFlag+'&lat='+lat+'&lon='+lng+'&enableSettings=1&showStatus=0&showNextPrev=1&pictureUrl=http://cpgimg.com/'+dataFolder+'/pictures/&dataUrl=http://cpgimg.com/'+dataFolder+'/data/'
			);		
	},

	getTilePath : function(tileContext) {
	   if(tileContext == null || tileContext == "undefined")
		   return ;

	   var path = "/pano/dots/" + tileContext.ZoomLevel + "/" + tileContext.XPos + "/" + 
       			  tileContext.ZoomLevel + "_" + tileContext.XPos + "_" + tileContext.YPos + ".gif";

	   return "http://cpgimg.com" + path;
	},

	showTile : function(opacity_) {
		try {
			this.map.DeleteTileLayer("canpages_mj");
		} catch(e) { }

		var opacity = opacity_||0;
	    var tileSourceSpec = new VETileSourceSpecification("canpages_mj", "");

	    tileSourceSpec.GetTilePath = this.getTilePath;
	    tileSourceSpec.MinZoomLevel = 8;
	    tileSourceSpec.MaxZoomLevel = 19;
	    tileSourceSpec.ZIndex = 100;
	    tileSourceSpec.Opacity = opacity;

	    this.map.AddTileLayer(tileSourceSpec, true);
	    
	    // only for IE6
	    if(jQuery.browser.version=="6.0" && jQuery.browser.msie) {
	        MapLIB.FixTiles();
	    }	    
	},

	toggleView : function () {
		if(!mjStreetView.isStreetView) {
			mjStreetView.isStreetView = true;
			mjStreetView.toggling = true;
			initAvatarIcons();
			jQuery('#'+this.options.streetId).addClass('mjShow');
			jQuery('#'+this.options.mapId).height(this.options.toggledMapHeight);//.css('border','1px');

			if(this.options.pageType == 'M')
				setSize();
			else if(this.options.pageType == 'D')
				map.Resize(jQuery('#'+this.options.mapId).width(), this.options.toggledMapHeight);

			document.getElementById(this.options.streetId).innerHTML = this.getPano();
			panoramaLoad();

			WebcamObj.clearAll();
			MapLIB.removeInfoBox();
			
			MapLIB.hideCustomZoomBar();
		} 
		else {
			mjStreetView.isStreetView = false;
			removePanOverlays();

			jQuery('#'+this.options.streetId).empty().removeClass('mjShow');
			jQuery('#'+this.options.mapId).height(this.options.normalMapHeight);

			if(this.options.pageType == 'M') {
				setSize();
				iMap.doIMapQuery(1);
			}
			else if(this.options.pageType == 'D') {
				map.Resize(jQuery('#'+this.options.mapId).width(), this.options.normalMapHeight);
				Webcam.getWebcamMarkers();
			}
			
			MapLIB.showCustomZoomBar();
		}
	}
}

var streetViewControl = function() {}

function initSVButton() {
//	var svText = mjStreetView.options.frenchFlag == '0' ? "Street Scene" : "Vue des rues";
	var svText = mjStreetView.options.frenchFlag == '1' ? "Scène de rues" : "Street Scene";
	var ignoreClick = false;
	
	var switchStreetView = function() {
	var svNode = document.getElementById("sv_toggle");
	
		if(!ignoreClick) {
			if(svNode.checked) {
				svNode.checked = false;
			} else {
				svNode.checked = true;
			}
			mjStreetView.toggleView();
		} 
		else {
			ignoreClick = false;
		}
	}
	
	var switchStreetView2 = function() {
		ignoreClick = true;
		mjStreetView.toggleView();
	}
}