var map;
function showMap(data){

	map=new GMap2(document.getElementById("map"));
	
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());
  	
	map.enableScrollWheelZoom();  
	map.enableContinuousZoom();

	GEvent.addDomListener(document.getElementById("map"),"DOMMouseScroll",CancelEvent);
	GEvent.addDomListener(document.getElementById("map"),"mousewheel",CancelEvent);
	
	var point=new GLatLng(data[2][0],data[2][1]);
	
	map.setCenter(point,18);

	var icon=new GIcon();
	icon.image="http://labs.google.com/ridefinder/images/mm_20_red.png";
	icon.shadow="http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize=new GSize(12, 20);
	icon.shadowSize=new GSize(22, 20);
	icon.iconAnchor=new GPoint(6, 20);
	icon.infoWindowAnchor=new GPoint(5,1);
	flags=new GMarker(point,icon);
	map.addOverlay(flags);

	flags.openInfoWindowHtml("<div class=\"mapheadings\"><span>"+data[1]+"</span></div><div class=\"mapaddress\"><span>"+data[3]+"</span></div><div id=\"panorama\"></div>");
	var ev=GEvent.addListener(map,'infowindowopen',function(){
		var panorama=new GStreetviewPanorama(document.getElementById("panorama"),{latlng:point});
		GEvent.removeListener(ev);
	});

/*
	for(var i=0;i<gD.length;i++){
		var q=new GIcon();
		q.iconSize=new GSize(15,16);
		q.iconAnchor=new GPoint(16*(gD[i][1]),0);
		q.infoWindowAnchor=new GPoint(6,0);
		q.image="/shared/img/map/"+(gD[i][0]+1)+".png";
		var c=new GLatLng(gD[i][2],gD[i][3]);
		flags[i]=new GMarker(c,q);
		flags[i].myno=i;
		map.addOverlay(flags[i]);
		GEvent.addListener(flags[i],"click",function(overlay,point){moveMaps(this.myno);});
	}
*/
}
function makeBalloon(title,address,info,img){
	var htmlText="<div class=\"infoBox clearfix\">\n";
	htmlText+="<dl>\n<dt>";
	htmlText+=title;
	htmlText+="</dt>\n<dd><p>";
	htmlText+=address;
	htmlText+="</p><p class=\"s\">";
	htmlText+=info;
	htmlText+="</p></dd>\n</dl><div class=\"timgs\"><img src=\"/prg_img/thumbnail2/";
	htmlText+=img;
	htmlText+="\" width=\"120\" height=\"120\" /></div>\n</div>";
	return htmlText;
}
function CancelEvent(event){
	e=event;
	if(typeof e.preventDefault=='function')e.preventDefault();
	if(typeof e.stopPropagation=='function')e.stopPropagation();
	if(window.event){
		window.event.cancelBubble=true;
		window.event.returnValue=false;
	}
}
$(document).ready(function(){

	var i=0;
	var data=[];
	//var t=$(".mainimg").children().attr("href");
	//var img=t.match(/img[0-9]+.jpg$/);

	$(".map").each(function(){
		var s=$(this).children().attr("href");
		$(this).children().attr("target","");
		$(this).children().attr("href","javascript://");
		var a=s.match(/q=([^&]+)&sll=([^&]+)&g=([^&]+)/);
		data[i]=['',decodeURI(a[1].replace(/\+/g," ")),a[2].split(","),decodeURI(a[3].replace(/\+/g," "))];
		i++;
	});

	$(".map").click(function(){

		var n=$(".map").index(this);

		var arrayPageSize=getPageSize();
		var arrayPageScroll=getPageScroll();

		$("#ovly").width(arrayPageSize[0]);
		$("#ovly").height(arrayPageSize[1]);
		$("#ovly").css("top",0);
		$("#ovly").css("left",0);
		
		var bs=[640,480];
		
		$("#bse").width(bs[0]);
		$("#bse").height(bs[1]);
		$("#bse").css("top",(arrayPageSize[3]-bs[1])/2+arrayPageScroll[1]);
		$("#bse").css("left",(arrayPageSize[2]-bs[0])/2);
		
		$("#map").width(bs[0]-10);
		$("#map").height(bs[1]-10);
		$("#map").css("top",(arrayPageSize[3]-bs[1])/2+5+arrayPageScroll[1]);
		$("#map").css("left",(arrayPageSize[2]-bs[0])/2+5);
		
		$("select").hide();
		
		$("#ovly").hide().css({opacity:0.6}).fadeIn("fast",function(){
			$("#bse").fadeIn("fast",function(){
				$("#map").fadeIn("fast",function(){
					showMap(data[n]);
				});
			});
		});
		$("#ovly").click(function(){
			$("#map").hide();
			$("#bse").fadeOut("slow",function(){
				$("#ovly").fadeOut("slow",function(){
					GUnload();
					$("select").show();
				});
			});
		});
	});
});

function getPageSize(){
var xScroll,yScroll;
if(window.innerHeight&&window.scrollMaxY) {
xScroll=window.innerWidth+window.scrollMaxX;
yScroll=window.innerHeight+window.scrollMaxY;
}else if(document.body.scrollHeight > document.body.offsetHeight){
xScroll=document.body.scrollWidth;
yScroll=document.body.scrollHeight;
}else{
xScroll=document.body.offsetWidth;
yScroll=document.body.offsetHeight;
}
var windowWidth,windowHeight;
if(self.innerHeight) {
if(document.documentElement.clientWidth){
windowWidth=document.documentElement.clientWidth;
}else{
windowWidth=self.innerWidth;
}
windowHeight=self.innerHeight;
}else if(document.documentElement&&document.documentElement.clientHeight) {
windowWidth=document.documentElement.clientWidth;
windowHeight=document.documentElement.clientHeight;
}else if(document.body) {
windowWidth=document.body.clientWidth;
windowHeight=document.body.clientHeight;
}
if(yScroll<windowHeight){
pageHeight=windowHeight;
}else{
pageHeight=yScroll;
}
if(xScroll<windowWidth){
pageWidth=xScroll;
}else{
pageWidth=windowWidth;
}
var arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);
return arrayPageSize;
}
function getPageScroll(){
var xScroll,yScroll;
if(self.pageYOffset) {
yScroll=self.pageYOffset;
xScroll=self.pageXOffset;
}else if(document.documentElement&&document.documentElement.scrollTop){
yScroll=document.documentElement.scrollTop;
xScroll=document.documentElement.scrollLeft;
}else if(document.body) {
yScroll=document.body.scrollTop;
xScroll=document.body.scrollLeft;
}
var arrayPageScroll=new Array(xScroll,yScroll);
return arrayPageScroll;
}