﻿// Reference site
// http://www.panoramio.com/api/
// http://www.neo-tech-lab.com/Panoramio/index.htm
// http://iwata.xrea.jp/sub_panoramio.shtml
// http://chaichan.web.infoseek.co.jp/src/javasc32.htm
// http://qune.cside.com/archives/000559.html
// http://javascriptist.net/
// http://jsajax.com/
// http://d.hatena.ne.jp/onozaty/20060202/p1
// etc.
// ISBN978-4-7741-3234-1, ISBN978-4-7980-1601-6, ISBN4-7741-2720-5
// ----------------------------------------------
// url?order=ORDER&size=SIZE&caption=CAP&set=ID
// ORDER = ( popularity upload_date )
// SIZE = ( medium small thumbnail square )
// CAP = (0 1 2 3 4 5 6 7 8 9 A B C D E F) // 1:serial number, 2:photo id, 4:upload_date, 8:author
// ID =	1597930
//
var npp = 100; // The number of photos on each page.
var defaultID = "1597930";
var defaultORDER = "popularity";
var defaultSIZE = "medium";
var popular = new makeArray(1);
var total_photo=0; // Total of photo.
var maxPage=0;
// ----------------------------------------------
function SetPopular(data1) {
	var photo_str = "";
	var cap = document.menuform1.caption.value;
	var NoStr = "";

//	popular.has_more = data1.has_more;
// count
	if (getHikisuu.data.count == undefined) {

	popular.count = data1.count;
	if (popular.count==undefined) {
		popular.count = 100;
	}
	popular.photos = data1.photos;
}
	else {
	popular.count = getHikisuu.data.count;
	popular.photos = data1.photos;
	}
	document.getElementById("username").innerHTML = '<a href="' + popular.photos[0].owner_url.toString() + '">' + popular.photos[0].owner_name.toString() + '</a>';
	document.getElementById("avatars").innerHTML = '<a href="' + popular.photos[0].owner_url + '/stats" target="stats" >' + '<img src="http://www.panoramio.com/avatars/user/' + popular.photos[0].owner_id + '.jpg" width=54 height=54 title="Jump to user statistics"></a>';
	document.getElementById("count").innerHTML = popular.count;
	if (total_photo==0) {
		total_photo = popular.count;
		set_menu(); // セレクトメニュー初期化
	}
	j = popular.count - get_From();

	for (i=0; i<npp; i++) {
		if (popular.photos[i] != undefined) {
			photo_str = "<a title=\"" + popular.photos[i].photo_title;
			photo_str += "\" href=\"" + popular.photos[i].photo_url.toString();
			photo_str += "\" target=\"mypanoramio\"><img src=\"" + popular.photos[i].photo_file_url;
			photo_str += "\"></img></a>";
			if (cap.search(/[13579BDF]/) != -1) {
				NoStr = "No."+ (i+1+get_From()).toString();
				photo_str += "<br />" + "<a title='Jump to this photo statistics'" + " href=\"" + popular.photos[i].photo_url + "/stats" + "\" target=\"stats\">" + NoStr + "</a>";
				}
			if (cap.search(/[2367ABEF]/) != -1) {
				photo_str += "<br />" + popular.photos[i].photo_id;
				}
			if (cap.search(/[4567CDEF]/) != -1) {
				photo_str += "<br />" + popular.photos[i].upload_date;
				photo_str = photo_str.replace("January", "Jan");
				photo_str = photo_str.replace("February", "Feb");
				photo_str = photo_str.replace("March", "Mar");
				photo_str = photo_str.replace("April", "Apr");
				photo_str = photo_str.replace("May", "May");
				photo_str = photo_str.replace("June", "Jun");
				photo_str = photo_str.replace("July", "Jul");
				photo_str = photo_str.replace("August", "Aug");
				photo_str = photo_str.replace("September", "Sep");
				photo_str = photo_str.replace("October", "Oct");
				photo_str = photo_str.replace("November", "Nov");
				photo_str = photo_str.replace("December", "Dec");
				}
			if (cap.search(/[89ABCDEF]/) != -1) {
				photo_str += "<br />" + "<a title='Jump to user page'" + " href=\"" + popular.photos[i].owner_url;
				photo_str +=  "\">" + popular.photos[i].owner_name + "</a>";
				}
			document.getElementById("photo"+ i.toString() ).innerHTML = photo_str;
		}
		else {
			document.getElementById("photo"+ i.toString() ).innerHTML = "";
		}
	}
}
// ----------------------------------------------
function makeArray(n) {
	this.count = n;
	this.photos = new Array(n);
	for (i = 0; i < n; i++) {
		this.photos[i] = {"photo_title": null, "photo_url": null, "photo_file_url": null, "owner_id": null, "owner_name": null, "owner_url": null};
	}
}
// ----------------------------------------------
function send_cmd() {
    var PTag = document.createElement("script"); PTag.charset = "utf-8"; PTag.type = "text/javascript";
    var Url = "http://www.panoramio.com/map/get_panoramas.php";
	var From = get_From();
	var To = From + npp;
	var Psize = getHikisuu.data.size;
    function sendCMD(obj, stype, sid, pids, pide, minx, miny, maxx, maxy, psize, cbname) {
       obj.src = Url + "?order=" + stype + "&set=" + sid + "&from=" + pids + "&to=" + pide + "&minx=" + minx + "&miny=" + miny + "&maxx=" + maxx + "&maxy=" + maxy + "&size=" + psize + "&callback=" + cbname;
       document.body.appendChild(obj);
       }
	sendCMD( PTag, getHikisuu.data.order, getHikisuu.data.set, From, To, "-180", "-90", "180", "90", Psize, "SetPopular" );
}
// ----------------------------------------------
// http://chaichan.web.infoseek.co.jp/src/javasc32.htm
function getHikisuu(){
     getHikisuu.data = new Array();
     getHikisuu.string = location.search.substring(1);//?をサプレス
     getHikisuu.string = getHikisuu.string.split('&');
     for(var i = 0; i != getHikisuu.string.length; i++) {
         getHikisuu.data[ck_shikibetushi(getHikisuu.string[i].split('=')[0])] =  Escape(unescape(getHikisuu.string[i].split('=')[1]));
     }

     function ck_shikibetushi(shiki){ //識別子（プロパティ）の命名チェック
         shiki_TOP = shiki.substring(0,1);
         if(shiki_TOP.match(/[a-zA-Z_$]/g) == null){
//            alert(shiki+"引数が不正です。（一文字目は、ASCII、_、$、のいずれかです。）");
            return null;
         }
         else if(shiki.match(/[^a-zA-Z0-9_$]/g) != null){
            alert(shiki+"引数が不正です。（命名文字は、ASCII、数字、_、$、のいずれかです。）");
            return null;
         } 
         return shiki;
     }

     function Escape(str){ //文字参照へ変換 
          str = str.replace( /\&/g, '&amp;' );
          str = str.replace( /</g, '&lt;' ); 
          str = str.replace( />/g, '&gt;' ); 
          str = str.replace( /\"/g, '&quot;' );
          str = str.replace( /\'/g, '&#39;' );
          return str;
    }
} new getHikisuu();
// ----------------------------------------------
// ISBN978-4-7741-3234-1
function searchChecked(frmName, elmName) {
    var i;
    // グループ内のすべてのラジオボタンを順に調べる
    for (i = 0; i < document[frmName][elmName].length; i++) {
        // i番目のラジオボタンのチェックがオンになっていたら、iを返す
        if (document[frmName][elmName][i].checked) {
            return i;
        }
    }
    // チェックがついているラジオボタンがなかったら、undefinedを返す
    return undefined;
}
// ----------------------------------------------
function set_menu() {
	var sel = document.menuform2.page;
	var j = Math.floor((total_photo-1)/npp); //切捨て
	maxPage = j;
	var i;
	for (i=0; i < j; i++) {
		sel.length++;
		sel.options[sel.length - 1].value =i+1;
		sel.options[sel.length - 1].text ="P." + (i+2).toString();
	}
	document.getElementById("first_focus").focus();
}
// ----------------------------------------------
function text_enter() {
	if (isNaN(document.menuform1.set.value)) {
		alert("ID is an integer. ");
	}
}
// ----------------------------------------------
// http://d.hatena.ne.jp/onozaty/20060202/p1
function enterCancel(evt){
  if (evt.keyCode == 13) {
    if (evt.preventDefault) {
      evt.preventDefault();
    } else {
      evt.returnValue = false;
    }
  }
}
// ----------------------------------------------
function get_Order() {
	switch (searchChecked('menuform1', 'order')) {
	case 0:
		return "popularity";	break;
	case 1:
		return "upload_date";	break;
	default:
		return "popularity";	break;
	}
}
// ----------------------------------------------
// size =	medium small thumbnail square
function get_Size() {
	switch (searchChecked('menuform1', 'size')) {
	case 0:
		return "medium";	break;
	case 1:
		return "small";	break;
	case 2:
		return "thumbnail";	break;
	case 3:
		return "square";	break;
	default:
		return "medium";	break;
	}
}
// ----------------------------------------------
// caption =	sn pid
function get_caption() {
	switch (searchChecked('menuform1', 'caption')) {
	case 0:
		return "0";	break;
	case 1:
		return "1";	break;
	case 2:
		return "2";	break;
	case 3:
		return "3";	break;
	case 4:
		return "4";	break;
	case 5:
		return "5";	break;
	case 6:
		return "6";	break;
	case 7:
		return "7";	break;
	case 8:
		return "8";	break;
	default:
		return "1";	break;
	}
}
// ----------------------------------------------
function getUrlparam2form() {
	var send = false;
// set
	if (getHikisuu.data.set == undefined) {
		document.menuform1.set.value = defaultID;
		send = true;
	}
	else {
		document.menuform1.set.value = getHikisuu.data.set;
	}
// order
	if (getHikisuu.data.order == undefined) {
		document.menuform1.order[0].checked = true;
		send = true;
	}
	else {
		switch (getHikisuu.data.order) {
			case "upload_date":
				document.menuform1.order[1].checked = true;
				break;
			default:
				document.menuform1.order[0].checked = true;
				break;
		}
	}
// size
	if (getHikisuu.data.size == undefined) {
		document.menuform1.size[0].checked = true;
		send = true;
	}
	else {
		switch (getHikisuu.data.size) {
			case "small":
				document.menuform1.size[1].checked = true;
				break;
			case "thumbnail":
				document.menuform1.size[2].checked = true;
				break;
			case "square":
				document.menuform1.size[3].checked = true;
				break;
			default:
				document.menuform1.size[0].checked = true;
				break;
		}
	}
// caption
	if (getHikisuu.data.caption == undefined) {
		document.menuform1.caption.value = 1;
		send = true;
	}
	else {
		document.menuform1.caption.value = getHikisuu.data.caption;
	}
	if (send) {
	document.menuform1.submit();
	}
}
// ----------------------------------------------
// popularity, upload_date
function set_order() {
	if (getHikisuu.data.order != undefined) {
		switch (getHikisuu.data.order) {
			case "upload_date":
				document.menuform1.order[1].checked = true;
				break;
			default:
				document.menuform1.order[0].checked = true;
				break;
		}
	}
}
// ----------------------------------------------
// size =	medium small thumbnail square
function set_size() {
	if (getHikisuu.data.size != undefined) {
		switch (getHikisuu.data.size) {
			case "small":
				document.menuform1.size[1].checked = true;
				break;
			case "thumbnail":
				document.menuform1.size[2].checked = true;
				break;
			case "square":
				document.menuform1.size[3].checked = true;
				break;
			default:
				document.menuform1.size[0].checked = true;
				break;
		}
	}
}
// ----------------------------------------------
function get_id() {
	var id = document.menuform1.set.value;
	if (id == "") {
		id = defaultID;
	}
	if (isNaN(id)) {
		id = defaultID;
	}
	return id;
}
// ----------------------------------------------
function get_From() {
	return document.menuform2.page.selectedIndex * npp;
}
// ----------------------------------------------
// size =	medium small thumbnail square
function addtbl() {
	var i, j, m, n;
	switch (getHikisuu.data.size) {
		case "small": // 240px
			m=3; n=4; npp=m*n;
			document.getElementById("wrapper").style.width = '1040px';
			tdwidth = "255px";
			tdheight = "260px";
			document.menuform1.size[1].checked = true;
			break;
		case "thumbnail": // 100px
			m=5; n=10; npp=m*n;
			document.getElementById("wrapper").style.width = '1200px';
			tdwidth = "118px";
			tdheight = "130px";
			document.menuform1.size[2].checked = true;
			break;
		case "square": // 60px
			m=10; n=10; npp=m*n;
			document.getElementById("wrapper").style.width = '920px';
			tdwidth = "90px";
			tdheight = "100px";
			document.menuform1.size[3].checked = true;
			break;
		default: // medium 500px
			m=5; n=2; npp=m*n;
			document.getElementById("wrapper").style.width = '1050px';
			tdwidth = "515px";
			tdheight = "550px";
			document.menuform1.size[0].checked = true;
			break;
	}
	var tblID="photo_tbl";
	var body = document.getElementById(tblID);
	for (i=0; i<m; i++) {
		var elemTR = body.insertRow(-1);
		for (j=0; j<n; j++) {
			var elemTD = elemTR.insertCell(-1);
			elemTD.setAttribute("id","photo"+(i*n+j));
			elemTD.setAttribute("width",tdwidth);
			elemTD.setAttribute("height",tdheight);
		}
	}
}
// ----------------------------------------------
function onloadfunction() {
	getUrlparam2form();
	addtbl();
	send_cmd();
}
// ----------------------------------------------
function onclick_enter() {
	if (document.menuform1.set.value == "") {
		document.menuform1.set.value = defaultID;
	}
	if (isNaN(document.menuform1.set.value)) {
		document.menuform1.set.value = defaultID;
	}
	document.menuform1.submit();
}
// ----------------------------------------------
function onclick_enter2() {
	var Psize = getHikisuu.data.size;
	if (Psize == undefined ) {
		Psize = defaultSIZE;
	}
	var Gsize = get_Size(); // radio bottun
	var URL=location.href;
	if(Psize != Gsize) {
		if (URL.indexOf("size",0) < 0 ) {
			if (URL.indexOf("?",0) < 0 ) {
				URL = URL + "?size=" + defaultSIZE;
			}
			else {
				URL = URL + "&size=" + defaultSIZE;
			}
		}
		URL = URL.replace(Psize, Gsize);
	}
	if (getHikisuu.data.set==undefined) {
		if (URL.indexOf("?",0) < 0 ) {
			URL = URL + "?set="+get_id();
		}
		else {
			URL = URL + "&set="+get_id();
		}
	}
	if (getHikisuu.data.set != get_id()) {
		URL = URL.replace("set="+getHikisuu.data.set, "set="+get_id());
	}
	if (URL != location.href) {
		if (getHikisuu.data.order != get_Order() ) {
			URL = URL.replace("order="+getHikisuu.data.order, "order="+get_Order());
		}
		location.href = URL;
	}
	send_cmd();
}
// ----------------------------------------------
function onChange_page() {
	getUrlparam2form();
	send_cmd();
}
// ----------------------------------------------
function prev_click() {
	if (document.menuform2.page.selectedIndex > 0) {
		document.menuform2.page.selectedIndex--;
		getUrlparam2form();
		send_cmd();
	}
}
// ----------------------------------------------
function next_click() {
	if (document.menuform2.page.selectedIndex < maxPage) {
		document.menuform2.page.selectedIndex++;
		getUrlparam2form(); 
		send_cmd();
	}
}
// ----------------------------------------------
function onclick_help() {
	var alert_str = 'My Panoramio Photo (junk0128)\n\nex)\n';
	alert_str += 'URL?order=ORDER&size=SIZE&caption=CAP&set=ID\n\n';
	alert_str += 'ORDER(popularity upload_date)\n';
	alert_str += 'SIZE(medium small thumbnail square)\n';
	alert_str += 'CAP(0 1 2 3 4 5 6 7 8 9 A B C D E F)\n';
	alert_str += 'ID:Panoramio ID';
	alert(alert_str);
}
// ----------------------------------------------

