var map = null;
var cursor = null;
var curno = 0;
var Gownerid;
var Glist;

function initOnLoad(defloc, scale){
	var pos = defloc.split(",");
	if(typeof pos[0] == 'undefined'){ pos[0] = 31; }
	if(typeof pos[1] == 'undefined'){ pos[1] = 131; }
	map = new GMap2(document.getElementById("map"));
	scale = Number(scale);
	map.addControl(new GOverviewMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT));
	map.setCenter(new GLatLng(pos[0], pos[1]), scale);
	cursor = new GIcon();
	cursor.image = 'red.gif';
	cursor.iconSize = new GSize(35, 35);
	cursor.iconAnchor = new GPoint(17, 17);
	cursor.infoWindowAnchor = new GPoint(17, 17);
}


function gmap(id, lon, lat, sname, status, photo, tel, address, note, icon) {
	var images = '';

	// DEFAULT
	if(typeof twitpic_size == 'undefined'){ twitpic_size = 'thumb'; }
	if(typeof plog_size == 'undefined'){ plog_size = 'thumb1'; }
	if(typeof twitvid_width == 'undefined'){ twitvid_width = '100'; }
	if(typeof plixi_size == 'undefined'){ plixi_size = 'thumbnail'; }
	if(typeof img_prefix == 'undefined'){ img_prefix = ''; }
	if(typeof img_suffix == 'undefined'){ img_suffix = ''; }

	status = status.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
		// TWITPIC
		if(url.match(/\/twitpic.com\/([0-9a-zA-Z])/)){
			var img_url = url.replace(/http\:\/\/twitpic\.com\//, function(img_url){
				return 'http://twitpic.com/show/' + twitpic_size + '/';
			})
			images += img_prefix + '<a href="'+url+'" target=_blank alt="twitpic"><img border=0 class="mappic" src="'+img_url+'"></a>' + img_suffix;
			return '';

		// PHOTO ROBOX
		}else if(url.match(/\/photo.robox.org\/([_a-z0-9])/i)){
			var img_url = url.replace(/home\/([_a-z0-9]+)/ig, function(img_url){
				return 'photo/' + plog_size;
			});
			img_url = img_url + '.jpg';
			images += img_prefix + '<a href="'+url+'" target=_blank alt="PHOTO ROBOX"><img border=0 class="mappic" src="'+img_url+'"></a>' + img_suffix;
			return '';

		// TWITVID
		}else if(url.match(/\/twitvid.com\/([_a-z0-9])/i)){
			var img_url = url.replace(/twitvid.com/ig, 'images.twitvid.com');
			img_url = img_url + '.jpg';

			images += img_prefix + '<a href="'+url+'" target=_blank><img border=0 class="mappic" src="'+img_url+'" width=' + twitvid_width + '></a>' + img_suffix;
			return '';

		// Ow.ly
		}else if(url.match(/\/ow.ly\/i\/([a-z])/i)){
			var img_url = url.replace(/http\:\/\/ow\.ly\/i\//, 'http://static.ow.ly/photos/thumb/');
			img_url = img_url + '.jpg';

			images += img_prefix + '<a href="'+url+'" target=_blank alt="Ow.ly"><img border=0 class="mappic" src="'+img_url+'"></a>' + img_suffix;
			return '';

		// Yfrog
		}else if(url.match(/\/yfrog.com\/([0-9a-zA-Z])/)){
			var img_url = url + '.th.jpg';

			images += img_prefix + '<a href="'+url+'" target=_blank alt="Yfrog.com"><img border=0 width=75 class="mappic" src="'+img_url+'"></a>' + img_suffix;
			return '';

		// Plixi
		}else if(url.match(/\/plixi.com\/p\/([0-9a-zA-Z])/)){
			var img_url = url.replace(/plixi.com\/p\//ig, 'api.plixi.com/api/TPAPI.svc/imagefromurl?size=');
			img_url += plixi_size + '&url=' + url;

			images += img_prefix + '<a href="'+url+'" target=_blank alt="Plixi"><img border=0 class="mappic" src="'+img_url+'"></a>' + img_suffix;
			return '';

		}else{
			return '<a href="'+url+'" target=_blank>'+url+'</a>';
		}
	}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
		return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	}).replace(/\B#([_a-z0-9]+)/ig, function(reply) {
		return  reply.charAt(0)+'<a href="http://twitter.com/#search?q=%23'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
	}).replace(/\[.+?\]/g, function(reply) {
		return '<b>' + reply.slice(1, -1) + '</b>';
	});

	// お店詳細のLINK
	var shop_url = '/bin/shop?ID=' + id;

	// お店写真の定義
	if(typeof photo == 'undefined'){
		photo = '';
	}else{
		photo = '<a href="'+shop_url+'"><img src="'+photo+'"></a>';
	}

/*
	// お店詳細の文字数制限
	if(note.length > 100){
		note = note.substr(0, 100);
		note += '...';
	}

	var info = '<div style="text-align:center"><a href="'+shop_url+'"><img width=50% height=50% src="'+photo+'"></a></div><div style="text-align:center;font-weight: bold">'+sname+'</div><div style="margin:5px">'+note+'</div><div style="margin:5px">'+address+'</div><div style="margin:5px">'+tel+'</div>';
*/

	var info = '<div style="text-align:center">'+photo+'</div><div style="text-align:center;font-weight: bold">'+sname+'</div><div style="text-align:center; margin:5px;">'+address+'</div><div style="text-align:center; margin:5px;">'+tel+'</div>';
	document.getElementById('map_info').innerHTML = info;

	var html = '<div style="width:400px">';
	html += '<div style="font-weight: bold"><a href="'+shop_url+'">' + sname + '</a></div>';
	html += '<span style="font-size:small">' + status + '</span>';
	if(images){
		html += '<div style="height:90px">' + images + '</div>';
	}
	html += '</div>';

	// マーカーの定義

	cursor.image = icon;
	cursor.shadow = '/images/box-shadow.png';
	cursor.iconSize = new GSize(35, 35);
	cursor.shadowSize = new GSize(45, 45);
	cursor.iconAnchor = new GPoint(17, 17);
	cursor.infoWindowAnchor = new GPoint(17, 17);
/*
	if(curno % 3 == 1){
		cursor.image = 'blue.gif';
		cursor.iconSize = new GSize(35, 35);
		cursor.iconAnchor = new GPoint(17, 17);
		cursor.infoWindowAnchor = new GPoint(17, 17);
	}else if(curno % 3 == 2){
		cursor.image = 'green.gif';
		cursor.iconSize = new GSize(35, 35);
		cursor.iconAnchor = new GPoint(17, 17);
		cursor.infoWindowAnchor = new GPoint(17, 17);
	}else{
		cursor.image = 'red.gif';
		cursor.iconSize = new GSize(35, 35);
		cursor.iconAnchor = new GPoint(17, 17);
		cursor.infoWindowAnchor = new GPoint(17, 17);
	}
	curno++;
*/


	map.clearOverlays();

	var point = new GLatLng(lon, lat)

	map.panTo(point);

	var marker = new GMarker(point, cursor);
	map.addOverlay(marker);
	map.openInfoWindowHtml(point, html);

}


function tw2map(twitdata) {
	var time = 0;
	var statusHTML = [];
	var Scripts = [];

	var refresh = twitdata[0].refresh;
	var defloc = twitdata[0].defloc;
	var scale = twitdata[0].scale;

	initOnLoad(defloc, scale);

	refresh *= 1000;

	for (var i=0; i<twitdata.length; i++){
		var id = twitdata[i].id;
		var photo = twitdata[i].photo;
		var twitid = twitdata[i].twitid;
		var loc = twitdata[i].loc;
		var sname = twitdata[i].sname;
		var tel = twitdata[i].tel;
		var address = twitdata[i].address;
		var note = twitdata[i].note;
		var status = twitdata[i].status;
		var icon = twitdata[i].icon;

		var twitpage = 'http://twitter.com/' + twitid;

		thumb = '<img align="left" class="photo" src="' + photo + '">';
		thumb = '<a href="' + twitpage + '" target=_blank>' + icon + '</a>';

    	var line = '<p>' + thumb + '<span>' + sname + status + '</span>';


		time = i * refresh;

		line += '<br clear=all /></p>';
		line += '<div>'+loc+'('+time+')</div>';
    	statusHTML.push(line);


		var info = '<div style="text-align:center"><img src="'+photo+'"></div><div style="font-weight: bold">'+sname+'</div><div>'+note+'</div><div>'+address+'</div><div>'+tel+'</div>';

		var pg_line = 'window.setTimeout(function() {gmap('+id+','+loc+',"'+sname+'","'+status+'","'+photo+'","'+tel+'","'+address+'","'+note+'","'+icon+'")},'+time+');';

		Scripts.push(pg_line);

	}

	time += refresh;
	Scripts.push('window.setTimeout(function() {load_map_twit("'+Gownerid+'","'+Glist+'")},'+time+');');
//	Scripts.push('window.setTimeout(function() {window.location.reload()},' + time + ');');

	var ele = document.createElement("script");
	ele.type = "text/javascript";
//	ele.innerHTML = Scripts.join('');
	ele.text = Scripts.join('');
	document.body.appendChild(ele);

//	document.getElementById('info_list').innerHTML = statusHTML.join('');

}


function load_map_twit(ownerid, list) {
	Gownerid = ownerid;
	Glist = list;

	var ele = document.createElement("script");
	ele.type = "text/javascript";
	ele.src = '/bin/town.json?ownerid='+ownerid+'&list='+list+'&cb=tw2map';
	document.body.appendChild(ele);
}

