//<![CDATA[

//環境変数
/*******************************************/
//プロットする最大実況中継数(個)
var MAX_PROGRESS_NUM = 50;

//実況中継のアニメーション処理のインターバル(ミリ秒)
var INTERVAL_TIME = 100;

//ドメイン
var domain = "http://ittemia.jp";

//お題　dest_info[目的地ID]
var dest_info = [];
//蔵王エコーライン
dest_info[29758] = [0, "猛吹雪の中、泉水と春の父・正志と、母・梨江子が初めて出会った運命の場所。<br/>お題：雄大な山の景観を撮影して送って下さい。", 'jp01.png'];
//宮城県第三女子高等学校 
dest_info[29759] = [1, "満開の桜のもと、映画のオープニングシーン“春が二階から落ちてきた”の撮影地。<br/>お題：学校周辺の春めいた風景を送って下さい。", 'jp02.png'];
//八木山松波町坂道
dest_info[29760] = [2, "母・梨江子と幼少期の泉水の、お買いもの道。このあと、哀しい事件が…。<br/>お題：坂から見下ろした仙台の家並を撮影して下さい。", 'jp03.png'];
//壱弐参横町
dest_info[29761] = [3, "泉水と春が、連続放火事件の手掛かりを求めて訪れた場所。<br/>お題：昭和の面影を感じる風景を送って下さい。", 'jp04.png'];
//原町商店街
dest_info[29762] = [4, "春が、落書き消しの仕事をするシーンの撮影地。この後、ここで連続放火事件が…。<br/>お題：通りから見上げた空の景色を撮影して下さい。", 'jp05.png'];
//東北大学星陵キャンパス
dest_info[29763] = [5, "遺伝子研究を行う泉水が通う大学。<br/>お題：2008年で100周年を迎えた東北大学の歴史を感じるものを撮影して下さい。", 'jp06-1.png'];
//東北大学青葉山キャンパス
dest_info[29764] = [6, "遺伝子研究を行う泉水が通う大学。<br/>お題：2008年で100周年を迎えた東北大学の歴史を感じるものを撮影して下さい。", 'jp06-2.png'];
//東北大学川内キャンパス
dest_info[29765] = [7, "遺伝子研究を行う泉水が通う大学。<br/>お題：2008年で100周年を迎えた東北大学の歴史を感じるものを撮影して下さい。", 'jp06-3.png'];
//東北大学片平キャンパス
dest_info[29766] = [8, "遺伝子研究を行う泉水が通う大学。<br/>お題：2008年で100周年を迎えた東北大学の歴史を感じるものを撮影して下さい。", 'jp06-4.png'];
//大倉ダム
dest_info[29767] = [9, "泉水が、”ある決意”のもと訪れた巨大ダム。<br/>お題：ダムの高さが分かるような写真を撮影し送って下さい。（ただし、危険な撮影方法は絶対にしないで下さい！）", 'jp07.png'];
//仙台一番町一番街商店街
dest_info[29768] = [10, "泉水と春の父・正志と母・梨江子がお祭り（すずめ踊り）見物をしたアーケード街。<br/>お題：商店街の中で一番面白いと思ったものを撮影し送って下さい。", 'jp08.png'];

//蔵王エコーラインと大倉ダムの目的地ID
var dest_zaou_id = 29758;
var dest_ookura_id = 29767;

/*******************************************/

//マップ変数
var map;
var data = null;

// マーカーを格納する配列
var dest_markers = [];
var pg_markers = []

//アニメーションの変数
var animation_flag = false;
var animationDest_flag = false;
var plotInit_flag = true;
var animationPlot_flag = true;
var anime_id = 0;
var animation_interval = 10;
var tmp_interval = 0;
var highlight_pmarker_id = -1;
var highlight_dmarker_id = -1;
var roop_cnt = 0;

//蔵王エコーラインと大倉ダムのロケ地と実況中継の位置情報データ
//dest_data_sp['目的地ID'] = [目的地Lat,目的地Lon,実況中継Lat,実況中継Lon]
var dest_data_sp = [];
dest_data_sp[dest_zaou_id] = [38.2355,140.8310,38.2345,140.8320,38.2365,140.8340];
dest_data_sp[dest_ookura_id] = [38.2440,140.8320,38.2440,140.8320,38.2420,140.8340];

//蔵王エコーラインと大倉ダムの実況中継カウント
var zaou_pg_cnt = 0;
var ookura_pg_cnt = 0;


function load() {
  if (GBrowserIsCompatible()) {
    //初期化処理
    map = new GMap2(document.getElementById("map"));
    //中心は仙台駅付近
    map.setCenter(new GLatLng(38.253297, 140.864049), 14);
    //地図は動かさない
    map.disableDragging();
    //ダブルクリックでズームアウト・ズームインしない
    map.disableDoubleClickZoom();

    //著作権表示
    var copyrights = new GCopyright(1,new GLatLngBounds(new GLatLng(0,0), new GLatLng(0,0)), 0, "aaa");
    var copyrightCollection = new GCopyrightCollection("bbb");
    copyrightCollection.addCopyright(copyrights);
    
    var tilelayer = new GTileLayer(copyrightCollection, 0, 19);

    tilelayer.getTileUrl = function(tile, zoom){
      var imgurl;
      //alert(tile.x + '　' + tile.y);
    if(tile.x == 14601 && tile.y == 6303 && zoom == 14){
      imgurl = "map/14601_6303.png";
    }else if(tile.x == 14602 && tile.y == 6303 && zoom == 14){
      imgurl = "map/14602_6303.png";
    }else if(tile.x == 14603 && tile.y == 6303 && zoom == 14){
      imgurl = "map/14603_6303.png";
    }else if(tile.x == 14604 && tile.y == 6303 && zoom == 14){
      imgurl = "map/14604_6303.png";

    }else if(tile.x == 14601 && tile.y == 6304 && zoom == 14){
      imgurl = "map/14601_6304.png";
    }else if(tile.x == 14602 && tile.y == 6304 && zoom == 14){
      imgurl = "map/14602_6304.png";
    }else if(tile.x == 14603 && tile.y == 6304 && zoom == 14){
      imgurl = "map/14603_6304.png";
    }else if(tile.x == 14604 && tile.y == 6304 && zoom == 14){
      imgurl = "map/14604_6304.png";

    }else if(tile.x == 14601 && tile.y == 6305 && zoom == 14){
      imgurl = "map/14601_6305.png";
    }else if(tile.x == 14602 && tile.y == 6305 && zoom == 14){
      imgurl = "map/14602_6305.png";
    }else if(tile.x == 14603 && tile.y == 6305 && zoom == 14){
      imgurl = "map/14603_6305.png";
    }else if(tile.x == 14604 && tile.y == 6305 && zoom == 14){
      imgurl = "map/14604_6305.png";

    }else if(tile.x == 14601 && tile.y == 6306 && zoom == 14){
      imgurl = "map/14601_6306.png";
    }else if(tile.x == 14602 && tile.y == 6306 && zoom == 14){
      imgurl = "map/14602_6306.png";
    }else if(tile.x == 14603 && tile.y == 6306 && zoom == 14){
      imgurl = "map/14603_6306.png";
    }else if(tile.x == 14604 && tile.y == 6306 && zoom == 14){
      imgurl = "map/14604_6306.png";

    }else {
      imgurl = "";
    }

      return imgurl;
    };
  
    var tileoverlay = new GTileLayerOverlay(tilelayer);
    map.addOverlay(tileoverlay);
  }
}

//イッテミアデータの取得
function ittemiaCallback(obj) {
	if(obj == null) {
    	if(_isDebug > 0) {
      	alert('引数が不正です。');
    	}
    	return;
  	}
  	data = obj;

  	//目的地のプロットを開始
  	destinationPlot();
		//実況中継のプロットを開始
  	setTimeout('progressPlot()', 5000);

}


/* 目的地のプロット */
var destinationPlot = function() {

  if(!map || !map.isLoaded()) {
    //読み込み中なら後で実行
    setTimeout('destinationPlot()', 100);
    return;
  }

  var thumb_prefix = 'dest';
  var dest;

  //すべての目的地をプロット
  var count = 0;
  for(var i=0; i < data.destination.length; i++) {
		dest = data.destination[i];
		d_plot(dest);
	}
	
	function d_plot(dest){
            
		//静止画アイコン
		var icon = new GIcon();
		icon.image  = "image/" + dest_info[dest.id][2];
		icon.iconSize   = new GSize(100, 40);
		icon.iconAnchor = new GPoint(50, 8);

		//蔵王エコーラインと大倉ダムの目的地は修正
		if(dest.id==dest_zaou_id || dest.id==dest_ookura_id){
			var pos = new GLatLng(dest_data_sp[dest.id][0], dest_data_sp[dest.id][1]);
		}else{
    	var pos = new GLatLng(dest.latitude, dest.longitude);
    }
    
    var opts={icon:icon, clickable:false}
    dest_markers.push(new GMarker(pos,opts));

		var id = dest_markers.length - 1;
		dest_markers[id].dest_id = dest.id;

		map.addOverlay(dest_markers[id]);

		GEvent.addListener(dest_markers[id], "click", function(){
			destinationInfo(dest.id);
		});
  }
  
}


/* 実況中継のプロット */
var progressPlot = function() {

  if(!map || !map.isLoaded()) {
    //読み込み中なら後で実行
    setTimeout('progressPlot()', 100);
    return;
  }

  var thumb_prefix = 'pg';
  var pg;
  
  var count = 0;
  for(var i=0; i < data.progress.length; i++) {
		plot(i);
 		count++;
	}
	
	//プロット処理
  function plot(j) {
		var pg = data.progress[j];

		//写真があるものだけをプロットする
		if(pg.image != '') {

			//実況中継画像アイコン
			var icon = new GIcon();
			icon.iconSize   = new GSize(50, 50);
			icon.iconAnchor = new GPoint(24, 24);
			icon.image  = domain+ "/image_display.php?history_id=" +pg.id+ "&type=history&mode=icon";
			
			//蔵王エコーラインと大倉ダムの実況中継は修正
			if(pg.destination_id==dest_zaou_id){
				if(zaou_pg_cnt%2==0){
					var pos = new GLatLng(dest_data_sp[pg.destination_id][2], dest_data_sp[pg.destination_id][3]);
				}else{
					var pos = new GLatLng(dest_data_sp[pg.destination_id][4], dest_data_sp[pg.destination_id][5]);
				}
				zaou_pg_cnt++;
			}else if(pg.destination_id==dest_ookura_id){
				if(ookura_pg_cnt%2==0){
					var pos = new GLatLng(dest_data_sp[pg.destination_id][2], dest_data_sp[pg.destination_id][3]);
				}else{
					var pos = new GLatLng(dest_data_sp[pg.destination_id][4], dest_data_sp[pg.destination_id][5]);
				}
				ookura_pg_cnt++;
			}else{
				var pos = new GLatLng(pg.latitude, pg.longitude);
			}
			pg_markers.push(new GMarker(pos,icon));

			var id = pg_markers.length - 1;
			pg_markers[id].dest_id = pg.destination_id;

			GEvent.addListener(pg_markers[id], "click", function(){
				//対応する画像をlightboxにて表示
				myLightbox.start($(thumb_prefix+pg.id));
			});

			var odai = dest_info[pg.destination_id][1];
			var user_name = pg.author + 'さん' + '　(' + pg.date + ')';
			var destination_name = '<br />' + pg.destination_name;
			var detail_link = '<img src="image/icon_link_square.gif">&nbsp<a href="' +domain+ '/progress_detail.php?history_id=' +pg.id+ '"target="_blank">もっと見る・コメントする</a>';
			var comment = ['<span style="font-size:12pt;line-height:1.2;">'+user_name,nl2br(pg.comment)+'</span>',detail_link,destination_name,odai].join('<br />');
                     
			//lightbox用のエレメント
			var div_obj = document.createElement('div');
			var link_obj = document.createElement('a');
			link_obj.id = thumb_prefix + pg.id;
			link_obj.setAttribute('href', pg.image);
			link_obj.setAttribute('rel', 'lightbox[link_area]');
			link_obj.setAttribute('title', comment);
			link_obj.setAttribute('onclick', 'myLightbox.start(this); return false;');
			link_obj.setAttribute('alt', 'クリックすると大きいサイズで見られます');
			div_obj.appendChild(link_obj);
			
			$('link_area').innerHTML = div_obj.innerHTML + $('link_area').innerHTML;
		}
	}
	
	if(data.progress.length > 0){
		//アニメーション開始
		animation_flag = true;
		animateProgress();
  }
}

/* アニメーション */
var animateProgress = function() {
	//1秒間隔でフラグを確認
	if((tmp_interval++) % animation_interval != 0) {
		if(animation_flag) {
			setTimeout('animateProgress()', 300);
		}
		return;
	}

	if(animation_flag) {
		if(anime_id < data.progress.length) {
			map.addOverlay(pg_markers[anime_id]);
			//map.removeOverlay(dest_markers[dest_info[pg_markers[anime_id].dest_id][0]]);
			//map.addOverlay(dest_markers[dest_info[pg_markers[anime_id].dest_id][0]]);
			anime_id++;
			animateProgress();
		}else {
			anime_id = 0;
			animation_flag = false;
		}
	}
}


/* 実況中継アニメーションの初期化 */
var animateProgressInit = function(dest_id) {
	anime_id = 0;
	animation_interval = 10;
	tmp_interval = 0;
	select_dest_id = dest_id;
	highlight_pmarker_id = -1;
	animation_flag = true;
}


//改行コードをbrタグに変換する関数
function nl2br(text) {
	text = text.replace(/\r\n/g, "<br />");
	text = text.replace(/(\r\n|\n|\r)/g,"<br />");
	return text;
}
//]]>