var map;
var s_img_w = 100;	//TABLE TYPE image width
var c_img_w = 240;	//child window image width
var c_wind_pad = 10;	//chile window padding value 'bigi'
var uniq_img_size = 300;	//UNIQ TYPE image width
var noimg = new Image();
noimg.src = './map/imgs/noimage.gif';
var noloc;
var allMarker = 2 ; //0:only displayed photos, 1:all, 2:category all

var categories = new Array();
var categ;
var gmphotos, gmcat ,gmnavi;
function makecateg( rtn ){
	categ = eval( rtn.responseText );
	if( categ.length > 0 ){
		var sino = 80;	//number of preset marker icons
		var ssno = 0;	//marker icons counter
		for(i=0; i<categ.length; i++){
			categories[ categ[i].cid ] = categ[i];
			categories[ categ[i].cid ].icon = new GIcon();
			categories[ categ[i].cid ].icon.image = './map/imgs/si_'+ ssno +'.png';
			categories[ categ[i].cid ].icon.shadow = './map/imgs/si_shadow.png';
			categories[ categ[i].cid ].icon.iconSize = new GSize(12, 20);
			categories[ categ[i].cid ].icon.shadowSize = new GSize(22, 20);
			categories[ categ[i].cid ].icon.iconAnchor = new GPoint(6, 20);
			categories[ categ[i].cid ].icon.infoWindowAnchor = new GPoint(5, 1);
			(ssno == sino-1) ? ssno=0: ssno++;
		}
		gmphotos = document.getElementById( "gmphotos" );
		gmcat = document.getElementById( "gmcat" );
		gmnavi = document.getElementById( "gmnavi" );

		catSet(0);

		noloc = new GIcon();
		noloc.image = './map/imgs/noloc.png';
		noloc.shadow = './map/imgs/noloc_s.png';
		noloc.iconSize = new GSize( 41, 17 );
		noloc.shadowSize = new GSize( 52, 17 );
		noloc.iconAnchor = new GPoint( 21, 17 );
		noloc.infoWindowAnchor = new GPoint( 21, 1 );
	} else {
		gmphotos.innerHTML = 'There is no category.';
	}
}

var fotos;
var phts = new Array();
var phtcat = new Array();
var imgAry = new Array();
function photos( rtn ){
	fotos = eval( rtn.responseText );
	//alert(rtn.responseText);
	for(i=0; i<fotos.length; i++){
		phts[ fotos[i].lid ]= fotos[i];
		if( phtcat[ fotos[i].cid ]==undefined ) phtcat[ fotos[i].cid ] =[];
		phtcat[ fotos[i].cid ].push( fotos[i] );
		//images preload
		imgAry[ fotos[i].lid ] = new Image();
		imgAry[ fotos[i].lid ].src = photourl + '/' + fotos[i].lid + '.' + fotos[i].ext;
	}
}

var selectName = 0;
function catSet( pid ){
	if ( pid != 0 ) {	//pid is 0 or object
		var selectNUM = pid.name;	//choice selector
		pid = pid.value;			//choice category	number, choice, all, none

		if( selectNUM != gmcat.childNodes.length || pid == 'none' ){
			selectName = ( selectNUM==1 || pid != 'none' ) ? selectNUM: selectNUM-1;
			for( i = gmcat.childNodes.length; i > selectName; i-- ){
				gmcat.removeChild( document.getElementById( 'cat' + i ) );
			}
			pid = document.getElementById( 'cat' + selectName ).value;
		}
	}

	var numofcat=0;
	var optionELM2 = [];
	for(i=0; i<categ.length; i++){
		if( categ[i].pid == pid ){	//category extraction
			optionELM2[numofcat] = document.createElement( "option" );
			optionELM2[numofcat].value = categ[i].cid;
			var elem = document.createTextNode( categ[i].title );
			optionELM2[numofcat].appendChild( elem );
			numofcat++;
		}
	}
	if( numofcat > 0 ) {
		var selectELM = document.createElement("select");
		selectELM.onchange = function(){ catSet(this) };
		selectELM.name = ++selectName;	//add Name in select tag
		selectELM.id   = 'cat'+ selectName;	//add ID in select tag

		if( pid == 0 ){	//head part of selector option
			var optionELM = document.createElement( "option" );
			optionELM.value = 'choice';
			var elem = document.createTextNode( choice_text );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
			var optionELM = document.createElement("option");
			optionELM.value = 'all';
			var elem = document.createTextNode( all_text );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
		}else {
			var optionELM = document.createElement("option");
			optionELM.value = 'none';
			var elem = document.createTextNode( '--' );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
		}
		for(i=0; i<numofcat; i++){	//add category part
			selectELM.appendChild( optionELM2[i] );
		}
		gmcat.appendChild( selectELM );
	}

	if( pid == 0 || pid == 'all' || pid == 'choice' /*|| pid == 'none'*/ ){
		var totalnum = { total:photoGobalSum };
		document.getElementById( "gmphtnum" ).innerHTML = sprintf( totalsum , totalnum) ;
	} else {
		var totalnum = { small:categories[pid].smallsum , undercat:(categories[pid].totalsum - categories[pid].smallsum) };
		document.getElementById( "gmphtnum" ).innerHTML = sprintf( categsum , totalnum );
	}
	
	mapSetCenter( pid );
	if( radioVal == 'uniq' ){
		mapMarkerClear();
		uniqLid = undefined;
	}
	if( !(pid == 0 || pid == 'choice' ) ){
		//GLog.write('[catSet before] allMarker2Flag: '+allMarker2Flag + ' / '+tempCid+' / '+pid);
		if( allMarker==2 && tempCid != pid ) allMarker2Flag = false;
		//GLog.write('[catSet after] allMarker2Flag: '+allMarker2Flag);
		dispDistri( pid );
	} else {	//choice
		removeChilds( gmphotos );
		removeChilds( gmnavi );
		mapMarkerClear();
		navi = undefined;
		naviU = undefined;
	}
}


function mapMarkerClear(){
	if(  allMarker==0 || 
		(allMarker==1 && ! allMarkerFlag) ||
		(allMarker==2 && ! allMarker2Flag) 
	){
		map.clearOverlays();
	}
}

function mapSetCenter( cid ){
	var lng = d_lng;
	var lat = d_lat;
	var zm  = d_zm;
	if( categories[ cid ] != undefined && categories[ cid ].longi!=0 && categories[ cid ].lati!=0 ){
		lng = ( categories[ cid ].longi != undefined ) ? categories[ cid ].longi: d_lng;
		lat = ( categories[ cid ].lati != undefined ) ? categories[ cid ].lati: d_lat;
		zm  = ( categories[ cid ].zoom != undefined ) ? categories[ cid ].zoom: d_zm;
	}
	map.setCenter( new GLatLng( parseFloat(lat), parseFloat(lng) ), Math.floor(zm) );
}

var radioVal;
var navi, naviU;
var bigimgAry = new Array();
var uniqLid;
function dispDistri( cid , page , lid ){
	var radio = document.cat.menu;
	for ( i = 0 ; i<radio.length ; i++ ){
		if( radio[i].checked ) radioVal = radio[i].value;
	}
	closeChildWin();
	if( radioVal == 'uniq' ){	//Single type
		if( lid != undefined ) uniqLid = lid;
		removeChilds( gmphotos );
		if( naviU == undefined || cid != naviU.cid ) {
			removeChilds( gmnavi );
			navi = undefined;
			naviU = new naviUniq( cid );
			naviU.create();
		}
		if( uniqLid != undefined ) {
			mapMarkerClear();
			if( uniqLid != 0 ){
				if( phtUniq[ uniqLid ] == undefined ){
					if( makethumb && bigimgAry[ uniqLid ] == undefined ){	//big images preload
						bigimgAry[ uniqLid ] = new Image();
						bigimgAry[ uniqLid ].src = bigPhotoUrl + '/' + uniqLid + '.' + phts[ uniqLid ].ext;
					}
					var postdata = 'lid=' + uniqLid;
					//Get uniq data
					httpXmlRequest( './map/uniq.php' , 'POST' , postdata , backDispUniq );
				} else {
					dispUniq();
				}
			} else {
				mapSetCenter();
			}
		}
		if( allMarker==2 && allMarker2Flag==false ) mapMarkerClear();
	} else if( phtcat[ cid ] != undefined || cid == 'all' ) {	//table type and list type
		uniqLid = undefined;
		removeChilds( gmphotos );
		mapMarkerClear();

		if( navi != undefined ) {
			if( cid != navi.cid ){
				removeChilds( gmnavi );
				navi = undefined;
			}else if( ! navi.pointCHECK() ){
				mapSetCenter( cid );
			}
		} 
		if( navi == undefined ) {
			naviU = undefined;
			navi = new navigation( cid );
			navi.create();
		}
		navi.pageOffset = ( page == undefined ) ? navi.pageOffset: ( page-1 ) * navi.phtno;

		switch ( radioVal ) {
			case 'list':
				dispList();
				break;
			case 'tbl':
				dispTable();
				break;
			default:
		}
	} else {
		removeChilds( gmphotos );
		removeChilds( gmnavi );
		if( allMarker==2 && allMarker2Flag==false ) mapMarkerClear();
	}
}

function changeRadio(){
		var nodesLEN = gmcat.childNodes.length;
		var cid = ( gmcat.childNodes[ nodesLEN-1 ].value == 'none') ? gmcat.childNodes[ nodesLEN-2 ].value: gmcat.childNodes[ nodesLEN-1 ].value;
		removeChilds( gmnavi );
		if( ! ( naviU == undefined && uniqLid != undefined ) ) {
			mapMarkerClear();
			mapSetCenter( cid );
		}
		navi = undefined;
		naviU = undefined;
		dispDistri( cid );
}

var allMarkerFlag = allMarker2Flag = false;
var tempCid;
function dispTable(){
	var tate = Math.ceil( navi.tempPHOTOs.length / tbl_yoko );
	tate = ( tate > tbl_tate ) ? tbl_tate: tate;
	
	for( i=1; i <= tate; i++ ){
		var div = document.createElement( "div" );
		div.name = 'prow'+ i;
		div.id = 'prow'+ i;
		for( j=1; j <= tbl_yoko; j++ ){
			if( navi.tempPHOTOs.length < (i-1)*tbl_yoko+j + navi.pageOffset ) continue;
			var span = document.createElement( "span" );
			span.name = 'pcol'+ i +'_'+ j;
			span.id = 'pcol'+ i +'_'+ j;
			var img = document.createElement( "img" );
			var imgfile = imgAry[ navi.tempPHOTOs[ (i-1)*tbl_yoko+j-1 + navi.pageOffset ].lid ];
			if( imgfile.width==1 && imgfile.height==1 ){
				img.setAttribute( "src", noimg.src );
			} else {
				img.setAttribute( "src", imgfile.src );
				var haba = ( imgfile.width > s_img_w ) ? s_img_w: imgfile.width;
				var takasa = Math.floor( haba * imgfile.height / imgfile.width );
				img.setAttribute( "width", haba );
				img.setAttribute( "height", takasa );
			}
			img.name = navi.tempPHOTOs[ (i-1)*tbl_yoko+j-1 + navi.pageOffset ].lid;
			img.onclick = function(){ imgClick( this.name ); };
			img.onmouseover = function(){ this.style.cursor = 'pointer';};
			span.appendChild( img );
			var text = document.createTextNode( navi.tempPHOTOs[ (i-1)*tbl_yoko+j-1 + navi.pageOffset ].title );
			var br = document.createElement( "br" );
			span.appendChild( br );
			span.appendChild( text );
			div.appendChild( span );
			gmphotos.appendChild( div );
			if( allMarker == 0 ){
				createMarker( navi.tempPHOTOs[ (i-1)*tbl_yoko+j-1 + navi.pageOffset ].lid );
			}
		}
		var br = document.createElement( "br" );
		br.style.clear = 'both';
		gmphotos.appendChild( br );
	}
	if( allMarker == 1 && allMarkerFlag == false ){
		for( i=1 ; i<fotos.length ; i++ ){
			createMarker( fotos[i].lid );
		}
		allMarkerFlag = true;
	}else if( allMarker == 2 ){
		if( tempCid != navi.cid ){
			for( i=0 ; i<navi.tempPHOTOs.length ; i++ ){
				createMarker( navi.tempPHOTOs[i].lid );
			}
			tempCid = navi.cid;
		}
		allMarker2Flag = true;
	}
	//GLog.write('[dispTable] allMarker2Flag: '+allMarker2Flag + ' /tempCid: '+ tempCid +' /navi.cid: '+navi.cid);
	setHeight( gmphotos )
}


function setHeight( gmphotos ){
	var h = ( mapHeight < gmphotos.clientHeight ) ? gmphotos.clientHeight: mapHeight + 20;
	document.getElementById( "pmtable" ).style.height = h + 'px';
}

function dispList(){
	for( i=0; i < list_no; i++ ){
		var div = document.createElement( "div" );
		var img = document.createElement( "img" );
		var offno = i + navi.pageOffset ;
		if( navi.tempPHOTOs[ offno ] != undefined ){
			var cccid = navi.tempPHOTOs[ offno ].cid;
			img.setAttribute( "src", categories[ cccid ].icon.image );
			img.alt = categories[ cccid ].title;
			img.title = categories[ cccid ].title;
			var text = document.createTextNode( '  '+ navi.tempPHOTOs[ offno ].title );
			div.appendChild( img );
			div.appendChild( text );
			div.alt = navi.tempPHOTOs[ offno ].title;
			div.title = navi.tempPHOTOs[ offno ].title;
			div.name = navi.tempPHOTOs[ offno ].lid;
			div.onclick = function(){ listClick( this.name ); };
			div.onmouseover = function(){ this.style.cursor = 'pointer';};
			gmphotos.appendChild( div );
			var lid = navi.tempPHOTOs[ offno ].lid;
			if( allMarker == 0 ){
				createMarker( lid );
			}
			if( makethumb && bigimgAry[ lid ] == undefined ){	//image preload for childwin
				bigimgAry[ lid ] = new Image();
				bigimgAry[ lid ].src = bigPhotoUrl + '/' + lid + '.' + phts[ lid ].ext;
			}
		}
	}
	if( allMarker == 1 && allMarkerFlag == false ){
		for( i=1 ; i<fotos.length ; i++ ){
			createMarker( fotos[i].lid );
		}
		allMarkerFlag = true;
	}else if( allMarker == 2 ){
		if( tempCid != navi.cid ){
			for( i=0 ; i<navi.tempPHOTOs.length ; i++ ){
				createMarker( navi.tempPHOTOs[i].lid );
			}
			tempCid = navi.cid;
		}
		allMarker2Flag = true;
	}
	//GLog.writeHtml('<font color="#ff0000">[dispList]</font> allMarker2Flag: '+allMarker2Flag + ' /tempCid: '+ tempCid +' /navi.cid: '+navi.cid);
	setHeight( gmphotos );
}

var phtUniq = new Array();
function backDispUniq( rtn ){
	//alert( rtn.responseText );
	var temp = eval( rtn.responseText );
	phtUniq[ uniqLid ] = temp[0];
	dispUniq();
}

function dispUniq(){
	//GLog.write('uniqLid   :  '+uniqLid);
	if( uniqLid != 0 ){
		//** title **
		var html = '<div class="title">'+ phts[ uniqLid ].title +'</div>';
		//** image **
			var imgfile = ( makethumb ) ? bigimgAry[ uniqLid ]: imgAry[ uniqLid ];
			var haba = ( imgfile.width > uniq_img_size ) ? uniq_img_size: imgfile.width;
			var takasa = Math.floor( haba * imgfile.height / imgfile.width );
		html += '<div class="image">';
			if( imgfile.width==1 && imgfile.height==1 ){
				html += '<img src="'+ noimg.src + '" />';
			} else {
				if( imgfile.width > uniq_img_size ){
					html += '<a href="'+ imgfile.src +'" alt="click big!" title="click big!" target="_blank">';
					html += '<img src="'+ imgfile.src + '" width="'+ haba +'" height="'+ takasa +'" /></a>';
				}else{
					html += '<img src="'+ imgfile.src + '" width="'+ haba +'" height="'+ takasa +'" />';
				}
			}
		html += '</div>';
		////(( 1ST LINE ))** edit
		if( phtUniq[ uniqLid ].canedit ) html += '<a href="'+ mod_url + '/editphoto.php?lid=' + uniqLid +'">'+ editicon +'</a>';
		//** lid
		html += '&nbsp;['+ uniqLid +']&nbsp;';
		//** title
		html += '<a href="'+ mod_url +'/photo.php?lid='+ uniqLid +'&amp;cid='+ phts[ uniqLid ].cid +'">'+ phts[ uniqLid ].title +'</a>&nbsp;';
		//** category
		html += '<a href="'+ mod_url + '/viewcat.php?cid=' + phts[ uniqLid ].cid +'">'+ caticon +'&nbsp;'+ categories[ phts[ uniqLid ].cid ].title +'</a>&nbsp;';
		//** new, update, popular ICON
		if( phtUniq[ uniqLid ].newicon ) html += newicon + '&nbsp;';
		if( phtUniq[ uniqLid ].updateicon ) html += updateicon + '&nbsp;';
		if( phtUniq[ uniqLid ].popicon ) html += popicon;
		html += '<br />';
		////(( 2ND LINE ))** submitter
		if( phtUniq[ uniqLid ].submitter > 0){
			html += '<a href="'+ xoops_url + '/userinfo.php?uid=' + phtUniq[ uniqLid ].submitter +'">'+ submicon +'</a>';
			html += phtUniq[ uniqLid ].submitter_name + '&nbsp;';
			html += '<a href="'+ mod_url + '/viewcat.php?uid=' + phtUniq[ uniqLid ].submitter +'">';
			html += '<img src="'+ mod_url +'/images/myphotos.gif" border="0" alt="'+ phtUniq[ uniqLid ].morephoto +'" title="'+ phtUniq[ uniqLid ].morephoto +'" /></a>&nbsp; &nbsp;';
		} else {
			html += submicon;
			html += phtUniq[ uniqLid ].submitter_name + '&nbsp; &nbsp;';
		}
		//** date
		html += clockicon + '&nbsp;' + phtUniq[ uniqLid ].date + '&nbsp; &nbsp; ';
		//** TELL A FRIEND
		if( cantellafriend ){
			html += '<a href="'+ xoops_url +'/modules/tellafriend/index.php?target_uri='+ e_mod_url +'/photo.php%3Flid%3D'+ uniqLid +'&amp;subject='+ subject_taf +'"><img src="'+ mod_url +'/images/tellafriend.gif" width="24" height="16" alt="'+ l_cantellafriend +'" title="'+ l_cantellafriend +'" />'+ l_cantellafriend +'</a>'
		}
		html += '<br />';
		////(( 3RD LINE ))** hits
		html += hitsicon + '&nbsp;' + phtUniq[ uniqLid ].hits + '&nbsp; &nbsp;';
		//** comments
		html += comicon + '&nbsp;' + phtUniq[ uniqLid ].comments + '&nbsp; &nbsp;';
		//** rank, rating
		if( canrateview ){
			if( phtUniq[ uniqLid ].rating > 0 ){
				html += '<img src="'+ mod_url +'/images/rank'+ phtUniq[ uniqLid ].rankicon +'.gif" border="0" alt="'+ phtUniq[ uniqLid ].rating +'" title="'+ phtUniq[ uniqLid ].rating +'" /></a>&nbsp;' + phtUniq[ uniqLid ].infovotes;
			}else{
				html += '<img src="'+ mod_url +'/images/rank_none.gif" border="0" alt="'+ phtUniq[ uniqLid ].infovotes +'" title="'+ phtUniq[ uniqLid ].infovotes +'" /></a>&nbsp;' + phtUniq[ uniqLid ].infovotes;
			}
		}
		//** vote
		if( canratevote ) html+='&nbsp; <a href="'+ mod_url + '/ratephoto.php?lid=' + uniqLid +'">'+ voteicon +'</a>';
		//** DESCRIPTION
		if( phtUniq[ uniqLid ].desc ) html+= '<div class="desc">'+ phtUniq[ uniqLid ].desc +'</div>';
		//** lng, lat, zoom
		if( phts[ uniqLid ].lat != 0 && phts[ uniqLid ].lng != 0 ){
			html += '<div class="lnglat">';
			html += l_lat +'&nbsp;:&nbsp;'+ phts[ uniqLid ].lat +'&nbsp;/&nbsp;';
			html += l_lng +'&nbsp;:&nbsp;'+ phts[ uniqLid ].lng +'&nbsp;/&nbsp;';
			html += l_zm +'&nbsp;:&nbsp;'+ phts[ uniqLid ].zoom;
			html += '</div>';
			map.setCenter( new GLatLng( parseFloat( phts[ uniqLid ].lat ), parseFloat( phts[ uniqLid ].lng ) ), Math.floor( phts[ uniqLid ].zoom ) );
		} else {
			mapSetCenter( phts[ uniqLid ].cid );
		}
		gmphotos.innerHTML = html;
		if( allMarker == 0 ){
			createMarker( uniqLid , 1 , 1 );
		}
		if( allMarker == 1 && allMarkerFlag == false ){
			for( i=1 ; i<fotos.length ; i++ ){
				createMarker( fotos[i].lid );
			}
			allMarkerFlag = true;
		}else if( allMarker == 2 ){
			if( tempCid != naviU.cid ){
				for( i=0 ; i< phtcat[ phts[ uniqLid ].cid ].length ; i++ ){
					//GLog.write( phtcat[ phts[ uniqLid ].cid ][i].lid  );
					createMarker( phtcat[ phts[ uniqLid ].cid ][i].lid );
				}
				tempCid = naviU.cid;
			}
			allMarker2Flag = true;
		}
		//GLog.writeHtml('<font color="#0000ff">[dispUniq]</font> allMarker2Flag: '+allMarker2Flag + ' /tempCid: '+ tempCid +' /navi.cid: '+naviU.cid);


	}
}

function toUniq( lid ){
	uniqLid = lid;
	document.cat.menu[2].checked = true;
	var cid = ( navi != undefined ) ? navi.cid : naviU.cid ;
	//var cid = navi.cid;
	navi = undefined;
	//GLog.write("cid: "+cid+ ' / phts.cid: ' +phts[ lid ].cid);
	//if( allMarker == 1 ){//&& cid != phts[ lid ].cid
		//removeChilds( document.getElementById( "gmphtnum" ) );
		//removeChilds( gmcat );
	//	document.getElementById( "gmphtnum" ).innerHTML = '';
	//	gmcat.innerHTML = '' ;
	//}
	dispDistri( cid , undefined , lid );
}

function naviUniq( cid ){
	this.cid = cid;
	this.tempPHT = ( this.cid == 'all' ) ? fotos : phtcat[ this.cid ];
}
naviUniq.prototype.create = function(){
	if( this.cid != 'choice' ){
		var selectELM = document.createElement( "select" );
		selectELM.onchange = function(){ dispDistri( naviU.cid , undefined , this.value ) };
			var optionELM = document.createElement("option");
			optionELM.value = 0;
			var elem = document.createTextNode( '--' );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
		for(i=0; i < this.tempPHT.length; i++){
			var optionELM = document.createElement("option");
			optionELM.value = this.tempPHT[ i ].lid;
			if( this.tempPHT[ i ].lid == uniqLid ) optionELM.selected = true;
			var outputText = this.tempPHT[ i ].title;
			var elem = document.createTextNode( outputText );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
		}
		gmnavi.appendChild( selectELM );
	}
}

function removeChilds( elem ){	//child node ALL DELETE
	for( i = elem.childNodes.length; i > 0; i-- ){
		elem.removeChild( elem.lastChild );
	}
}

function navigation( id ){	//navi for Tabel and List
	this.cid = id;
	this.point = map.getCenter();
	this.zoom = map.getZoom();
	this.type = radioVal;
	this.phtLEN = ( this.cid == 'all' ) ? fotos.length: phtcat[ this.cid ].length;
	this.phtno = ( this.type == 'tbl' ) ? tbl_yoko * tbl_tate: list_no;
	this.optionNO = Math.ceil( this.phtLEN / this.phtno );
	this.tempPHOTOs = ( this.cid == 'all' ) ? fotos : phtcat[ this.cid ];
	this.pageOffset = 0;
}
navigation.prototype.create = function(){
	if( this.optionNO > 1 ){
		var selectELM = document.createElement( "select" );
		selectELM.onchange = function(){ dispDistri( navi.cid , this.value ) };
		for( i = 1; i <= this.optionNO; i++ ){
			var optionELM = document.createElement("option");
			optionELM.value = i;
			var text1 = ( (i-1) * this.phtno ) + 1 ;
			var text2 = ( i == this.optionNO ) ? this.phtLEN: this.phtno * i;
			var outputText = ( text1 == text2 ) ? text1: text1 + '-' + text2
			var elem = document.createTextNode( outputText );
			optionELM.appendChild( elem );
			selectELM.appendChild( optionELM );
		}
		gmnavi.appendChild( selectELM );
	}
}
navigation.prototype.pointCHECK = function(){
	return this.point.equals( map.getCenter() ) && this.zoom == map.getZoom();
}

var noLocMarker;
function createMarker( lid , uniq , noL ){
	if( !( phts[ lid ].lat == 0 && phts[ lid ].lng == 0 ) ){
		var marker = new GMarker( new GLatLng( phts[ lid ].lat , phts[ lid ].lng ) , categories[ phts[ lid ].cid ].icon );
		GEvent.addListener( marker, 'click', function(){
			if( uniq == 1 ){
				//var zl = ( phts[ lid ].zoom-3 < 0 ) ? 0: phts[ lid ].zoom-3;
				//marker.showMapBlowup( { zoomLevel:zl } );
				marker.showMapBlowup(  );	//{ mapType:G_Normal_Map } / G_SATELLITE_MAP
			} else {
				var infoTEXT = '<b>'+ categories[ phts[ lid ].cid ].title + '</b> / ' + phts[ lid ].title;
				infoTEXT += '<br /><a onclick="toUniq('+ lid +')" style="cursor:pointer;">' + uniqText + '</a>';
				marker.openInfoWindowHtml( infoTEXT );
			}
		} );
		map.addOverlay( marker );
	} else if( noL == 1 ) {
		if( noLocMarker ) map.removeOverlay( noLocMarker );
		noLocMarker = new GMarker( new GLatLng( categories[ phts[ lid ].cid ].lati, categories[ phts[ lid ].cid ].longi ), noloc )
		GEvent.addListener( noLocMarker, 'click', function(){
			if( uniq == 1 ){
				noLocMarker.showMapBlowup(  );
			} else {
				var infoTEXT = '<b>'+ categories[ phts[ lid ].cid ].title + '</b> / ' + phts[ lid ].title;
				infoTEXT += '<br /><a onclick="toUniq('+ lid +')" style="cursor:pointer;">' + uniqText + '</a>';
				noLocMarker.openInfoWindowHtml( infoTEXT );
			}
		} );
		map.addOverlay( noLocMarker );
	}
}

function imgClick( lid , uniq ){
	uniqLid = lid;
	if( !( phts[ lid ].lat == 0 && phts[ lid ].lng == 0 ) ) {
		map.setCenter( new GLatLng( parseFloat( phts[ lid ].lat ), parseFloat( phts[ lid ].lng ) ), Math.floor( phts[ lid ].zoom ) );
	} else {
		mapSetCenter( phts[ lid ].cid );
		createMarker( lid , uniq , 1 )
	}
}

function listClick( lid ){
	imgClick( lid );
	openChildWin( lid );
}

function openChildWin( lid ){
	closeChildWin();
	if( makethumb && bigimgAry[ lid ] == undefined ){
		bigimgAry[ lid ] = new Image();
		bigimgAry[ lid ].src = bigPhotoUrl + '/' + lid + '.' + phts[ lid ].ext;
	}
	var imgfile = ( makethumb ) ? bigimgAry[ lid ]: imgAry[ lid ];
	if( imgfile.width != 1 && imgfile.height != 1 ){
		var childwin = document.getElementById( 'childwindow' );
		var bar = document.getElementById( 'bar' );
		var bigi = document.getElementById( 'bigi' );
		var barTitle = document.getElementById( 'cwtitle' )
		var mapw = document.getElementById( 'map' )
		var imgWidth = ( imgfile.width > c_img_w ) ? c_img_w: imgfile.width;
		var topLoc = mapw.offsetTop;
		var leftLoc = mapw.offsetLeft - imgWidth - c_wind_pad * 2 -2;
		childwin.style.left = leftLoc + 'px';
		childwin.style.top = topLoc  + 'px';
		childwin.style.width = ( imgWidth + c_wind_pad * 2) + 'px';
		childwin.style.visibility = 'visible';
		bar.style.width = ( imgWidth + c_wind_pad * 2) + 'px';
		barTitle.innerHTML = phts[ lid ].title;
		bigi.style.padding = c_wind_pad + 'px';
		if( imgfile.width > c_img_w ){
			bigi.innerHTML = '<a href="'+ imgfile.src +'" target="_blank"><img src="'+ imgfile.src +'" width="'+ imgWidth +'" alt="Click! BIG" title="Click! BIG" /></a>'
		} else {
			bigi.innerHTML = '<img src="'+ imgfile.src +'" width="'+ imgWidth +'" />'
		}
	}
}

function closeChildWin(){
	document.getElementById( 'childwindow' ).style.visibility = 'hidden';
}

//*---- httpXmlRequest ----*//
function httpXmlRequest( target_url, method, data, callback_func ) {
	try {
		if( window.XMLHttpRequest ) {
			var httpObj = new XMLHttpRequest();
		} else if( window.ActiveXObject ) {
			var httpObj = new ActiveXObject( "Microsoft.XMLHTTP" );
		} else {
			var httpObj = false;
		}
	} catch(e) {
		httpObj = false;
	}
	if( ! httpObj ) requestErr( 'Error : XMLHttpRequest is not supported' );
	var timeout_sec = 20;
	var timerId = setInterval( function (){
		timeout_sec--;
		if( timeout_sec <= 0 ){
			clearInterval( timerId );
			httpObj.abort();
			requestErr( 'TIME OUT' );
		}
	}, 1000 );
	httpObj.open( method, target_url, true );
	httpObj.setRequestHeader( 'Content-Type' , 'application/x-www-form-urlencoded' );//for POST to PHP
	httpObj.onreadystatechange = function() {
		if ( httpObj.readyState == 4 ) {
			clearInterval( timerId );
			if ( httpObj.status == 200 ) {
				callback_func( httpObj );
			} else {
				requestErr( httpObj.status + ' : ' + httpObj.statusText );
				return false;
			}
		}
	}
	httpObj.send( data );
}

function requestErr( msg ){
	removeChilds( gmphotos );
	gmphotos.innerHTML = msg +'<br />This menu cannot be used now.<br />';
}

//#xxx# of the character string is replaced by data
//by Gijutsuhyouron-sha [Ajax] chap.5
function sprintf( template, obj ){
	var rslt = template;
	for( var Name in obj ){
		value = obj[Name];
		reg = new RegExp( "#" + Name + "#", "g" );
		rslt = rslt.replace( reg, value );
	}
	return rslt;
}

