var mjStreetView = {
	map: null,
	isExecutedOnLoad: false,
	isThere: false,
	isStreetView: 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
		initLat: 0,
		initLng: 0,
		isInternal: '1',
		viewPoint: "",
		impId: "10"
	},

	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 = map.getCenter().lat();
			lng = 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 = 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://'+this.options.serverName+'/pano/pano_i2',  // 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', 'panoGUI',
			'bgcolor', '#000000',
			'name', 'panoGUI',
			'menu', 'false',
			'allowScriptAccess','sameDomain',
			'allowFullScreen','false',
			'movie', 'http://'+this.options.serverName+'/pano/pano_i2',  // Filename
			'salign', '',
			'allowFullScreen','true',
			'FlashVars','linkHereUrl=http://www.canpages.ca/?&lat='+lat+'&lon='+lng+'&enableSettings=1&showStatus=0&showNextPrev=1&pictureUrl=http://imfimg.com/pano/pictures/&dataUrl=http://imfimg.com/pano/data/&imp='+this.options.impId+'&reportaConcernURL='+this.options.serverName
					);
				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>';
				//document.write(alternateContent);
				return alternateContent;
			}
		}
	},

	toggleView : function () {
		var $ = jQuery;

		if(!mjStreetView.isStreetView) {
			mjStreetView.isStreetView = true;
			jQuery('#'+this.options.streetId).addClass('showDiv');
			jQuery('#'+this.options.mapId).height(this.options.toggledMapHeight).css('border','1px');

			document.getElementById(this.options.streetId).innerHTML = this.getPano();
			panoramaLoad();

			// for detail page it is defined in detail_google.js
			resizeMap();
		} 
		else {
			mjStreetView.isStreetView = false;
			jQuery('#'+this.options.streetId).empty().removeClass('showDiv');
			jQuery('#'+this.options.mapId).height(this.options.normalMapHeight);
			
			map.checkResize();
			removePanOverlays();
			
			if(this.options.pageType == 'M')
				iMap.doIMapQuery(1);

			// for detail page it is defined in detail_google.js			
			resizeMap();

			updateDotOverlay();
		}
	}
}

var mjStreetViewControl = function() {}

function initSVButton() {
	var svText = mjStreetView.options.frenchFlag == '1' ? "infocalle" : "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();
	}

	mjStreetViewControl.prototype = new GControl();

	mjStreetViewControl.prototype.initialize = function() {
		var svContainer = document.createElement("div");

		// for checkbox
		var svCheck = document.createElement("input");
		svCheck.setAttribute("id", "sv_toggle");
		svCheck.setAttribute("type", "checkbox");
		svCheck.style.height="19"; // only IE
		svCheck.style.cssFloat="left";
		svCheck.style.styleFloat="left"; // for IE
		svCheck.style.position="relative";
		svCheck.style.top="-2px";
		svCheck.onclick=switchStreetView2;
	
		var svDiv = document.createElement("div");
	
		this.setButtonStyle_(svDiv);
		svContainer.appendChild(svDiv);
		svDiv.appendChild(svCheck);
		svDiv.appendChild(document.createTextNode(svText));

		GEvent.addDomListener(svDiv, "click", switchStreetView);
		map.getContainer().appendChild(svContainer);

		if(mjStreetView.isStreetView)
			document.getElementById("sv_toggle").checked = true;

		return svContainer;
	}

	mjStreetViewControl.prototype.getDefaultPosition = function() {
		var offsetX;

		// for detail pages
		if(mjStreetView.options.pageType == 'D')
			offsetX = "202";
		// in street scene mode, 'More Webcams' button is removed, so that it needs to be adjusted to move beside Move&Refresh
		else if(mjStreetView.options.frenchFlag == '1') {
			if(mjStreetView.isStreetView)
				offsetX = "208";
			else
				offsetX = "343";
		}
		else { 
			if(mjStreetView.isStreetView)			
				offsetX = "218";
			else
				offsetX = "333";
		}

		return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(offsetX, 7));
	}

	//Sets the proper CSS for the given button element.
	mjStreetViewControl.prototype.setButtonStyle_ = function(button) {
		button.style.color = "#000000";
		button.style.backgroundColor = "white";
	    button.style.font = "small Arial";
		button.style.border = '1px solid #000000';	    
	    button.style.padding = "1px";
	    button.style.height="15px";
	    if(mjStreetView.options.frenchFlag == '1')
	    	button.style.width = "75px";
	    else
	    	button.style.width = "95px";
		button.style.cursor = "pointer";
	    button.style.fontSize = "12px";
	}	
}