function SVControl(){
	var map;
	var img;
	var mouseover;
	var dragging;
	var hasStreetviewData;
	var marker;
	var dirn;
	var geo = new GClientGeocoder();
}
	
	
SVControl.prototype=new GControl();

SVControl.prototype.initialize=function(map){
	this.map=map;
	this.mouseover=false;
	this.dragging=false;
	this.streetviewclicked=false;
	this.hasStreetviewData=false;
	this.browserflashbug=((navigator.vendor)&&navigator.vendor.indexOf("Apple")!=-1)||((navigator.userAgent)&&navigator.userAgent.indexOf("Chrome")!=-1);
//	this.width=this.map.getContainer().style.width;
//	this.height=this.map.getContainer().style.height;
	this.width=682;
	this.height=410;
	this.panoramacontainer=document.getElementById("stv_div")
	this.streetview=new GStreetviewOverlay();
	this.map.addOverlay(this.streetview);
	this.streetview.hide();
	this.streetview.SVControl=this;
	GEvent.addListener(this.streetview,"changed",this.changedstreetview);
	this.streetviewclient=new GStreetviewClient();
	this.map.addControl(new GLargeMapControl3D(true));
	this.container=document.createElement("div");
	this.container.style.cssText="overflow: hidden; width: 25px; height: 40px; position: absolute;";
	this.svbutton=document.createElement("div");
	this.svbutton.style.cssText="overflow: hidden; width: 25px; height: 40px; ";
	this.svbutton.SVControl=this;
	this.container.appendChild(this.svbutton);
	this.img=document.createElement("img");
	this.img.setAttribute("src","http://maps.gstatic.com/mapfiles/cb/mod_cb_scout/cb_scout_sprite_003.png");
	this.img.style.cssText="border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: -62px; top: -40px; width: 147px; height: 935px;";
	this.svbutton.appendChild(this.img);
	map.getContainer().appendChild(this.container);
	this.dragbutton=new GDraggableObject(this.container,{container:map.getContainer()});
	this.dragbutton.SVControl=this;
	GEvent.addDomListener(this.svbutton,"mouseover",this.funcmouseover);
	GEvent.addDomListener(this.svbutton,"mouseout",this.funcmouseout);
	GEvent.addDomListener(this.dragbutton,"dragstart",this.funcdragstart);
	GEvent.addDomListener(this.dragbutton,"dragend",this.funcdragend);
	this.panorama=new GStreetviewPanorama(this.panoramacontainer);
	this.panorama.SVControl=this;
	GEvent.addDomListener(this.panorama,"error",this.hidestreetview);
	return this.container
};

SVControl.prototype.getDefaultPosition=function(){return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(25,66))};

SVControl.prototype.funcmouseover=function(){
	if(!this.SVControl.dragging){
		if(this.SVControl.hasStreetviewData){
			this.SVControl.mouseover=true;
			this.SVControl.img.style.left="-62px";
			this.SVControl.img.style.top="-320px";
			this.SVControl.streetview.show()
		}
	}
};

SVControl.prototype.funcmouseout=function(){
	if(!this.SVControl.dragging){
		if(this.SVControl.mouseover){
			this.SVControl.mouseover=false;
			this.SVControl.img.style.left="-62px";
			this.SVControl.img.style.top="-40px";
			this.SVControl.streetview.hide()
		}
	}
};

SVControl.prototype.funcdragstart=function(){
	this.SVControl.dragging=true;
	this.SVControl.container.style.width="30px";
	this.SVControl.svbutton.style.width="30px";
	this.SVControl.img.style.left="-52px";
	this.SVControl.img.style.top="-800px"
};

SVControl.prototype.funcdragend=function(){
	function callback(func,opts){
		var cb=function(){
			var args=opts.args?opts.args:[];
			var bind=opts.bind?opts.bind:this;
			var fargs=opts.supressArgs===true?[]:toArray(arguments);
			func.apply(bind,fargs.concat(args))};
			return cb
		}
		function toArray(arrayLike){var arr=[];
		for(var i=0;i<arrayLike.length;i++){
			arr.push(arrayLike[i])
		}
		return arr
	}
	

	if(this.SVControl.dragging)
	{
		this.SVControl.dragging=false;
		this.SVControl.point=this.SVControl.map.fromContainerPixelToLatLng(new GPoint(this.left+(25/2),this.top+(66/2)));
//alert(this.SVControl.point);
//var p1=this.SVControl.point;
		this.SVControl.container.style.width="25px";
		this.SVControl.svbutton.style.width="25px";
		this.SVControl.img.style.left="-62px";
		this.SVControl.img.style.top="-320px";
		this.moveTo(new GPoint(25,66));

//		this.SVControl.geocode(this.SVControl.point)
		
//		while(true){
//			if (svpoint !='') break;
//		}

		this.SVControl.streetviewclient.getNearestPanoramaLatLng(this.SVControl.point,callback(this.SVControl.showstreetview,{bind:this.SVControl,args:[],suppressArgs:true}));
	}
};

SVControl.prototype.geocode=function (query){
  this.SVControl.geo.getLocations(query, this.SVControl.addressesget());
}

SVControl.prototype.addressesget = function(addresses){
	var delay = 0;
	if (addresses.Status.code == 620) 
	{
			// Too fast, try again, with a small pause
			delay = 500;
	} 
	else 
	{
		if (addresses.Status.code == 200) 
		{
			var result = addresses.Placemark[0];
			var lat = result.Point.coordinates[1];
			var lng = result.Point.coordinates[0];
			this.SVControl.point = new GLatLng(lat, lng);
		}
	}
}

SVControl.prototype.funcdrag=function(){
	if(this.SVControl.dragging){}
};

SVControl.prototype.showstreetview=function(point){
	if(point){
		window.streetviewOn=true;
		if (!this.map.getInfoWindow().isHidden()) { this.map.closeInfoWindow(); }
		this.point=point;
		this.panorama.remove();
		this.panorama.setLocationAndPOV(this.point,null);
//		this.width=this.map.getContainer().style.width;
		this.clientWidth=this.map.getContainer().clientWidth;
//		this.width=682;
//		this.height=410;
//		this.height=this.map.getContainer().style.height;
//		this.map.getContainer().style.width="0px";
//		this.map.getContainer().style.height="0px";
//		document.getElementById("cls_div").style.zIndex=20000;
		document.getElementById("map_div").style.display='none';
		document.getElementById("stv_div").style.display='';
		this.map.getContainer().style.overflow="hidden";
		this.map.checkResize();
//		this.panoramacontainer.style.zIndex=2000;
		this.panorama.checkResize();
		this.closecontainer=document.getElementById("cls_div")
//		this.closecontainer.style.zIndex=20200;
		this.closecontainer.style.display='';
		
		this.closecontainer.SVControl=this;
		this.closebuttonevent=GEvent.addDomListener(this.closecontainer,"click",this.hidestreetview)
	}
};

SVControl.prototype.hidestreetview=function(){
//		if (document.getElementById("all_search_result_column").style.width != '200px'){
//			pasek_on();
//		}

		window.streetviewOn=false;
//		this.SVControl.panoramacontainer.style.zIndex=1;
		this.SVControl.panorama.checkResize();
		this.SVControl.panorama.hide();
//		this.SVControl.map.getContainer().style.width=this.SVControl.width;
//		this.SVControl.map.getContainer().style.height=this.SVControl.height;
//		this.SVControl.map.getContainer().style.width=682;
//		this.SVControl.map.getContainer().style.height=395;
		this.SVControl.map.getContainer().style.display='';
		
		this.SVControl.map.checkResize();
		this.SVControl.map.addOverlay(this.SVControl.streetview);
//		document.getElementById("cls_div").style.zIndex=100;
		document.getElementById("stv_div").style.display='none';
		document.getElementById("cls_div").style.display='none';
		GEvent.removeListener(this.SVControl.closebuttonevent)
};

SVControl.prototype.changedstreetview=function(hasStreetviewData){
	this.SVControl.hasStreetviewData=hasStreetviewData;
	if(hasStreetviewData){
		if(this.SVControl.mouseover){
			this.SVControl.img.style.left="-62px";
			this.SVControl.img.style.top="-320px"
		}
		else
		{
			this.SVControl.img.style.left="-62px";
			this.SVControl.img.style.top="-40px"
		}
	}
	else
	{
		this.SVControl.img.style.left="-102px";
		this.SVControl.img.style.top="-845px"
	}
};

var client;
var panorama;


function openPanoramaBubble(lat,lng,pov) {
  var contentNode = document.getElementById("svstyles_window");
  contentNode.style.textAlign = 'center';
  contentNode.style.width = '500px';
  contentNode.style.height = '300px';
  contentNode.innerHTML = 'Loading panorama';

  var smallNode = document.createElement('div');
  smallNode.style.width = '200px';
  smallNode.style.height = '200px';
  smallNode.id = 'pano';
  document.body.appendChild(smallNode);

  panorama = panorama || new GStreetviewPanorama(smallNode);
  panorama.setContainer(smallNode);
  
  panorama.setLocationAndPOV(new GLatLng(lat,lng), pov);
  
  // Update handlers for info window and marker event listeners.
  maximizeclickHandler = function() {
    panorama.remove();
  };
  maximizeendHandler = function() {
    panorama.setContainer(contentNode);
    window.setTimeout("panorama.checkResize()", 5);
  };
  infowindowbeforecloseHandler = function() {
    panorama.remove();
  };
}


