var spotProp =
{
	"spotPageURL": "/spot",
	"serviceURL": "/spot/panel",
	"iconDIR": "/images/spot_icon/",
	"defaultScale": 15
};

var spotList = new Array();
var markerList = new Array();

window.onload = function(){
	createPlayList();
}

window.onunload = function(){
	GUnload();
}


/**
 * HTMLのタグに記述されたスポットのIDとスケールのリストからデータを取得し
 * WebServiceを呼び出す
 *
 * (スポットリストFLASHから呼ばれるメソッド)
 */
function loadIcon()
{
	var spotIdList = document.getElementById("spotIdList").getElementsByTagName("input");

	if( !!document.getElementById("defaultScale") )
	{
		this.spotProp.defaultScale = this.trim(document.getElementById("defaultScale").value);
	}

	// webServiceに渡すスポットIDの羅列
	var spotIds = "";

	// 記述されたスポットIDをデータにつめる
	var len = spotIdList.length;
	pushId: for(var i=0; i<len; i++)
	{
		// データをフォーマット
		var value = spotIdList[i].value.split("@");
		/*
		if(MachilogAreaCookie.getAreaScale())
		{
			value[1] = MachilogAreaCookie.getAreaScale();
		}
		else */ if(value.length == 1)
		{
			value.push(spotProp.defaultScale);
		}
		else if(value[1]=="")
		{
			value[1] = this.trim(spotProp.defaultScale);
		}
		value[0] = this.trim(value[0]);

		// おなしスポットID重複は無効
		var length = this.spotList.length;
		for(var j=0; j<length; j++)
		{
			if( this.spotList[j].id == value[0] ){
				continue pushId;
			}
		}

		// データをつめる
		var obj = new Object();
		obj.id = value[0];
		obj.scale = value[1];
		this.spotList.push(obj);


		if(i != 0) spotIds += ",";
		spotIds += value[0];
	}


	// webサービス呼び出し
	if(spotIds){
		this.loadSpotInfo(spotIds);
	}
}

/**
 * Webサービス問い合わせ
 * jsonp使用
 */
function loadSpotInfo(spotIds)
{
	new Ajax.Request(
		spotProp.serviceURL + '?id=' + spotIds +'&review=' + 39 +'&mode=1',
		{
			method: 'post',
			onComplete:this.loadSpotInfoHandler
		}
	);
}

function loadSpotInfoError(request)
{

}

/**
 * Webサービス問い合わせ結果（スポット情報）受け取り
 */
function loadSpotInfoHandler(ajax, json)
{

	var data = eval('('+ajax.responseText+')');

	switch(data.result)
	{
		case 0:
			break;
		case -1:
			break;
		case -2:
			break;
	}

	/*
	 * 順番にプレイリストフラッシュにつめていく
	 * スポットのデータをWebサービスから帰ってきたデータのみに入れ替える
	 */
	var list = this.spotList;
	this.spotList = new Array();
	var len = list.length;
	for(var i=0; i<len; i++)
	{
		var id = list[i].id;
		var scale = list[i].scale;

		var length = data.spot.length;
		for(var j=0; j<length; j++)
		{
			if(id == data.spot[j].id)
			{
				data.spot[j].scale = scale;
				data.spot[j].url = decodeURIComponent(data.spot[j].url);
				// スポットデータをセット
				this.setSpotData(data.spot[j]);
				this.spotList.push(data.spot[j]);
			}
		}
	}


	// 一番目のスポットをアクティブにする
	if(this.markerList.length > 0){
		swfName("playListFlash").chActSpot(this.markerList[0].getSpotId());
	}
}

/**
 * スポットデータをプレイリストとgooglemapにセットする
 * @param data
 * @return
 */
function setSpotData( data )
{
	// スポット情報をプレイリストに渡す
	this.swfName("playListFlash").setPlaylistItem(
		data.id,
		data.type,
		this.truncate_text(this.deleteLineFeed(decodeURIComponent(data.title)), 16),
		this.truncate_text(this.deleteLineFeed(decodeURIComponent(data.review)), 38),
		decodeURIComponent(data.thumbnail),
		this.spotProp.spotPageURL + "/" + data.id,
		data.view_count
	);


	// googlemap起動
	if(AreaMap.map == null){
		AreaMap.init(data.latitude*1, data.longitude*1, data.scale*1);
	}


	// アイコン情報未取得時表示用
	var spotIcon = new Object();
	spotIcon.normal = new Object();
	spotIcon.normal.imageURL =  this.spotProp.iconDIR + data.icon;
	spotIcon.normal.width =  data.icon_width;
	spotIcon.normal.height = data.icon_height;
	spotIcon.normal.placeX = data.icon_anchor_x;
	spotIcon.normal.placeY = data.icon_anchor_y;
	spotIcon.active = new Object();
	spotIcon.active.imageURL =  this.spotProp.iconDIR + data.active_icon;
	spotIcon.active.width =  data.active_icon_width;
	spotIcon.active.height = data.active_icon_height;
	spotIcon.active.placeX = data.active_icon_anchor_x;
	spotIcon.active.placeY = data.active_icon_anchor_y;

	spotIcon.spotId = data.id;
	spotIcon.lat = data.latitude;
	spotIcon.lng = data.longitude;
	spotIcon.scale = data.scale;

	//マーカー作成、プロット、通常状態にセット
	var marker = new AreaSpotMarker();
	marker.createMarker(spotIcon);
	AreaMap.addOverlay(marker.getNormal());
	AreaMap.addOverlay(marker.getActive());
	marker.setNormal();
	this.markerList.push(marker);
}

/**
 * 指定されたスポットIDのスポットデータを返す
 * @param id
 * @return
 */
function getSpotDataById( id ){
	var len = this.spotList.length;
	for(var i=0; i<len; i++){
		if(this.spotList[i].id == id){
			return this.spotList[i];
		}
	}

	return null;
}

/**
 *
 */
function setCookie()
{

}

/**
 * トリム
 */
function trim(s){
	s = s.replace(/^\s+|\s+$/g, "");
	if (s.length == 0)
	{
		return "";
	}
	else
	{
		return s;
	}
}

/**
 * 文字列切り取り
 */
function truncate_text(str, num){
	if(str.length > num){
		return str.substring(0, num - 2) + "…";
	}else{
		return str;
	}
}

/**
 * 改行コード削除
 */
function deleteLineFeed(myLen) {
    var newLen = '';
    for(var i=0; i<myLen.length; i++){
        text = escape(myLen.substring(i, i+1));
        if(text != "%0D" && text != "%0A"){
            newLen += myLen.substring(i, i+1);
        }
    }
    return(newLen);
}
