var unit_no;
if ( unit_no == null ) {
	unit_no = 0;
}

var loop_flg;
var myPano = Array();
var myPOV = Array();
var panoList = new Object();
var iframePano;

var _ggmap_apikey = "";

if ( unit_no == 0 ) {
	try {
		_ggmap_apikey = ggmap_apikey;
	} catch(e){  }
}

var myView_target;
var src_user;

if ( _ggmap_apikey != "" ) {
	src_user = "http://maps.google.com/maps?file=api&v=2&sensor=false&key="+_ggmap_apikey;
	document.write(
		'<' +
		'script charset="utf-8" src="' + 
		src_user + 
		'"' + 
		' type="text/javascript"><' + 
		'/script>'
	);
	_ggmap_apikey = "";
}

function changePano(obj,no) {
	var wPov = myPano[no].getPOV();
	wPov.zoom = 0;
	if (window.navigator.userAgent.toLowerCase().indexOf("opera") > -1) {
		myPano[no].panTo(wPov);
	}
	else {
		myPano[no].setPOV(wPov);
	}

	var newPlace =
		new GLatLng(
			panoList[obj.value][0],
			panoList[obj.value][1]
		);
	myPOV[no].yaw	= panoList[obj.value][2];
	myPOV[no].pitch	= panoList[obj.value][3];
	myPOV[no].zoom	= 0;
	myPano[no].setLocationAndPOV(newPlace, myPOV[no]);
}

function handleNoFlash(errorCode) {
	if (errorCode == 603) {
//		alert("Error: Flash doesn't appear to be supported by your browser");
		return;
	}
}

function handleUnload_createPano() {

	GUnload();
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		try {
			CollectGarbage();
		}
		catch(e) {
		}
	}
}

if (window.attachEvent){
	window.attachEvent('onunload', handleUnload_createPano);
}
else {
	window.addEventListener('unload', handleUnload_createPano, false);
}

function createPano(w,h,s) {

	var key;
	var save="";
	unit_no++;

str="";
str+="<div name=\"pano"+unit_no+"\" id=\"pano"+unit_no+"\"";
str+=" style=\"width: "+w+"px; height: "+h+"px\"></div> \n";
str+="<select id=\"combo"+unit_no+"\" onChange='changePano(this,"+(unit_no-1)+")'> \n";

	loop_flg = false;
	for (key in panoList)   {
		if ( s == key ) {
			loop_flg = true;
		}
		if ( loop_flg ) { 
			if ( save == '' ) {
				save = key;
			}
			str+="<option value=\""+key+"\">"+panoList[key][4]+"</option> \n";
		}
	}

str+="</select> \n";
str+=" ";

	document.write(str);

	myPano[(unit_no-1)] = new GStreetviewPanorama(document.getElementById("pano"+unit_no));
	var fenwayPark = new GLatLng(panoList[save][0],panoList[save][1]);
	myPOV[(unit_no-1)] = {yaw:panoList[save][2],pitch:panoList[save][3],zoom:0};
	myPano[(unit_no-1)].setLocationAndPOV(fenwayPark, myPOV[(unit_no-1)]);

	GEvent.addListener(myPano[(unit_no-1)], "error", handleNoFlash);

	document.getElementById("combo"+unit_no).selectedIndex = 0;
}

function createPanoArea(w,h,a,b,c,d) {

	unit_no++;

	var appVersion = window.navigator.appVersion.toLowerCase();

	if (appVersion.indexOf("msie 5.0") > -1) {

str="";
str+="<iframe \n";
str+="	id=\"pano"+unit_no+"\" \n";
str+="	frameborder=\"no\" \n";
str+="	scrolling=\"no\" \n";
str+="	width=\""+ (w+50) +"\" \n";
str+="	height=\""+ (h+50) +"\" \n";
str+="	src=\"http://winofsql.jp/ggv_pano_basic.php?w="+w+"&h="+h+"&a="+a+"&b="+b+"&c="+c+"&d="+d+"\" \n";
str+="></iframe> \n";
		document.write(str);

		document.write(
			'<' +
			'script ' + 
			' type="text/javascript">' + 
			'try {document.getElementById("'+'pano'+unit_no+'").contentWindow.location.reload();}catch(e){}' + 
			'<' + 
			'/script>'
		);

	}
	else {


str="";
str+="<div name=\"pano"+unit_no+"\" id=\"pano"+unit_no+"\"";
str+=" style=\"width: "+w+"px; height: "+h+"px\"></div> \n";

		document.write(str);

		if ( arguments.length == 8 ) {
			var panoOpts = { 
				features: { streetView: false, userPhotos: true },
				userPhotoOptions: { photoRepositories: [ 'panoramio'] } 
			}; 
			myView_target = new GStreetviewPanorama(document.getElementById("pano"+unit_no), panoOpts); 
			var fenway = { repository: "panoramio", id: arguments[7] }; 
			GEvent.addListener(myView_target, "error", handleNoFlash);
			myView_target.setUserPhoto(fenway);
		}
		else {
			myView_target = new GStreetviewPanorama(document.getElementById("pano"+unit_no));
			var fenwayPark = new GLatLng(a,b);
			var myPanoView = {yaw:c,pitch:d,zoom:0};
			myView_target.setLocationAndPOV(fenwayPark, myPanoView);

			GEvent.addListener(myView_target, "error", handleNoFlash);
		}

	}

	if ( arguments.length >= 7 ) {

		if ( arguments[6] == 1 ) {

str="";
			if (appVersion.indexOf("msie 5.0") > -1) {
				str+="<br>";
			}
str+="<a style='font-size:12px;text-decoration:none;color:navy;' class=\"createpano\" href=\"http://maps.google.co.jp/maps?q="+a+",+"+b+"&iwloc=A&hl=ja\" target=\"_blank\">&#71;&#111;&#111;&#103;&#108;&#101;&#32;&#77;&#97;&#112;&#32;&#12391;&#34920;&#31034;&#12377;&#12427;</a> ";
			document.write(str);
		}

		if ( arguments[6] == 2 ) {
str="";
str+="<a style='font-size:12px;text-decoration:none;color:navy;' class=\"createpano\" href=\"http://maps.google.co.jp/maps?q="+a+",+"+b+"&iwloc=A&hl=ja\" target=\"_blank\">&#71;&#111;&#111;&#103;&#108;&#101;&#32;&#77;&#97;&#112;&#32;&#12391;&#34920;&#31034;&#12377;&#12427;</a> ";
str+="<"+"script type=\"text/javascript\"> \n";
str+="var mylat,mylng,mv"+unit_no+"; \n";
str+="mv"+unit_no+"=myView_target; \n";
str+="try { \n";
str+="	GEvent.addListener(mv"+unit_no+", \"initialized\", getTargetLatLng); \n";
str+="}catch(e){} \n";
str+="try { \n";
str+="	GEvent.addListener(mv"+unit_no+", \"pitchchanged\", function(pitch){ \n";
str+="		document.getElementById(\"pitch"+unit_no+"\").value=pitch; \n";
str+="	}); \n";
str+="}catch(e){} \n";
str+="try { \n";
str+="	GEvent.addListener(mv"+unit_no+", \"yawchanged\", function(yaw){ \n";
str+="		document.getElementById(\"yaw"+unit_no+"\").value=yaw; \n";
str+="	}); \n";
str+="}catch(e){} \n";
str+=" \n";
str+="function getTargetLatLng(myLocation) { \n";
str+="	document.getElementById(\"lat"+unit_no+"\").value = myLocation.latlng.lat(); \n";
str+="	document.getElementById(\"lng"+unit_no+"\").value = myLocation.latlng.lng(); \n";
str+="} \n";
str+=" \n";
str+="</"+"script> \n";
str+="<br> \n";
str+="yaw : <input type=\"text\" id=yaw"+unit_no+" size=10> pitch : <input type=\"text\" id=pitch"+unit_no+" size=10> \n";
str+="&#32239;&#24230;(lat) : <input type=\"text\" id=lat"+unit_no+" size=10> &#32076;&#24230;(lng) : <input type=\"text\" id=lng"+unit_no+" size=10> \n";
str+="<input type=\"button\" value=\"&#26032;&#12375;&#12356;&#20301;&#32622;\" onClick='setNewData(\"newdata"+unit_no+"\",\"lat"+unit_no+"\",\"lng"+unit_no+"\",\"yaw"+unit_no+"\",\"pitch"+unit_no+"\")'> \n";
str+="<br> \n";
str+="<span style='font-size:12px;'>yaw : &#30495;&#21271;&#48;&#12289;&#26481;&#57;&#48;&#12289;&#21335;&#12289;&#49;&#56;&#48;&#12289;&#35199;&#50;&#55;&#48;&#32;&#47;&#32;&#112;&#105;&#116;&#99;&#104;&#32;&#58;&#32;&#57;&#48;&#40;&#30495;&#19978;&#41;&#12316;&#45;&#57;&#48;&#40;&#30495;&#19979;&#41;</span> \n";
str+="<br> \n";
str+="<TEXTAREA id=newdata"+unit_no+" onclick='this.select()' style='width:600px;height:60px;'></TEXTAREA> \n";
str+=" ";

			document.write(str);
		}

	}
}

function setNewData(s,a,b,c,d) {
	str="";
	str+="<"+"script type=\"text/javascript\"> \n";
	str+="createPanoArea(600,400,"+document.getElementById(a).value
	str+=","+document.getElementById(b).value
	str+=","+document.getElementById(c).value
	str+=","+document.getElementById(d).value+",1); \n";
	str+="</"+"script> \n";
	document.getElementById(s).value = str
}

function createPanoSet(w,h,url) {

	var opt;

	if ( arguments.length == 4 ) {
		opt = "&" + arguments[3];
	}
	else {
		opt = ""
	}

	url = encodeURIComponent(url);

str="";
str+="<iframe \n";
str+="	frameborder=\"no\" \n";
str+="	scrolling=\"no\" \n";
str+="	width=\""+(w+50)+"\" \n";
str+="	height=\""+(h+50)+"\" \n";
str+="	src=\"http://winofsql.jp/ggv_pano_view.php?w="+w+"&h="+h+"&id="+url+opt+"\" \n";
str+="></iframe> ";

	document.write(str);

}

