// グローバル定数 ==============================================================
//var WEB_ROOT = 'http://localhost/kyoei/';
var WEB_ROOT = 'http://www.kyoei-h.jp/';
var CATE_NUM = 9;
var LINE_COUNT = 15;
var ICON_NUM = 3;
//var DEF_LAT = '31.8569332705401';
//var DEF_LNG = '131.389684975147';
var DEF_LAT = '31.856933';
var DEF_LNG = '131.389684';

// グローバル変数 ==============================================================
var map;
var mode;
var disp;
var objList = new Array();
var category_name = new Array();
var mapIcon = new Array();

// データ初期化 ================================================================
function initData() {
  category_name[0] = 'アパート・マンション';
  category_name[1] = '貸家';
  category_name[2] = '店舗・事務所';
  category_name[3] = '駐車場';
  category_name[4] = '貸土地';
  category_name[5] = '一戸建て';
  category_name[6] = '土地';
  category_name[7] = '分譲マンション';
  category_name[8] = '事業用物件';
}

// データ受け取り ==============================================================
function getQuery(){
  var url = location.href;
  var urlAry = url.split("?");

  if(urlAry[1] == 'mode=0'){
    mode = 0;
  } else if(urlAry[1] == 'mode=1') {
    mode = 1;
  } else if(urlAry[1] == 'mode=2') {
    mode = 2;
  } else if(urlAry[1] == 'mode=3') {
    mode = 3;
  } else if(urlAry[1] == 'mode=4') {
    mode = 4;
  } else if(urlAry[1] == 'mode=5') {
    mode = 5;
  } else if(urlAry[1] == 'mode=6') {
    mode = 6;
  } else if(urlAry[1] == 'mode=7') {
    mode = 7;
  } else if(urlAry[1] == 'mode=8') {
    mode = 8;
  } else {
    mode = 0;
  }
}

// 初期化 ======================================================================
function init() {
  // ==== データの受け取り ====
  getQuery();

	// ==== 地図アイコンの作成 ====
	createIcon();

  // ==== 地図の作成 ====
  createMap(DEF_LAT, DEF_LNG);

  // ==== メニュークリック ====
  clickMenu(mode);
}

// 地図の作成 ==================================================================
function createMap(lat,lng) {
	map = new GMap2($("map"));

	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(lat, lng), 13, G_NORMAL_MAP);

	GEvent.addListener(map, 'zoomend', function(oldLevel, newLevel) {
		if(newLevel > 16){
			map.setZoom(16);
		}
	});
}

// メニュークリック ============================================================
function clickMenu(i) {
  // ==== モードの設定 ====
  mode = i;

  // ==== 何件目を表示しているかを初期化 ====
  disp = '';

  // ==== 条件の設定 ====
  if(mode == 0) {
    document.f1.elements['type'].disabled = false;
    document.f1.elements['rent'].disabled = false;
  } else {
    document.f1.elements['type'].disabled = true;
    document.f1.elements['rent'].disabled = true;
    document.f1.elements['type'].value = 0;
    document.f1.elements['rent'].value = 0;
  }

  // ==== 物件データの取得 ====
  getData();
}

// 物件データの取得 ============================================================
function getData(pars_con) {
  var pars = '&m='+mode;

  if(pars_con == undefined){
    pars_con = disp;
  } else {
    disp = pars_con;
  }
  if(mode == 0) {
    if(document.f1.elements['type'].value > 0) {
      pars_con = pars_con + '&t=' + document.f1.elements['type'].value;
    }
    if(document.f1.elements['rent'].value > 0) {
      pars_con = pars_con + '&r=' + document.f1.elements['rent'].value;
    }
  }
  pars = pars + pars_con;

  var url = './mapview/getdata';
  var myAjax = new Ajax.Request(
    url, {
      method: 'get',
      parameters: pars,
      onComplete: getDataSuccess });
}

// 物件データの取得(完了) ======================================================
function getDataSuccess(data) {
  eval('objList = ' + data.responseText);

  // ==== メニューの変更 ====
  changeMenu();

  // ==== 物件リストの作成 ====
  setList();

  // ==== ページ送りの作成 ====
  setPage();

  // ==== マーカーの設定 ====
  setMarkers();
}

// 物件リストの作成 ============================================================
function setList() {
  var row_no;
  var html = '';
  var d_list = $('list');

  switch(mode) {
    case 0:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="60px">家賃<br>共益費</th>'
           + '<th width="36px">タイプ</th>'
           + '</tr>';
      break;
    case 1:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="60px">家賃<br>共益費</th>'
           + '<th width="36px">タイプ</th>'
           + '</tr>';
      break;
    case 2:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="60px">家賃<br>共益費</th>'
           + '<th width="36px">構造</th>'
           + '</tr>';
      break;
    case 3:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="150px">名称</th>'
           + '<th width="80px">賃料<br>消費税</th>'
           + '</tr>';
      break;
    case 4:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="96px">坪単価<br>広さ</th>'
           + '</tr>';
      break;
    case 5:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="60px">価格</th>'
           + '<th width="36px">タイプ</th>'
           + '</tr>';
      break;
    case 6:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="110px">名称</th>'
           + '<th width="120px">価格<br>面積</th>'
           + '</tr>';
      break;
    case 7:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="110px">名称</th>'
           + '<th width="120px">価格<br>面積</th>'
           + '</tr>';
      break;
    case 8:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="80px">名称</th>'
           + '<th width="50px">価格</th>'
           + '<th width="100px">建物<br>土地</th>'
           + '</tr>';
      break;
    default:
      html = '<table>'
           + '<tr class="list_tr_00">'
           + '<th width="134px">名称</th>'
           + '<th width="60px">家賃<br>共益費</th>'
           + '<th width="36px">タイプ</th>'
           + '</tr>';
  }

  if(objList == null){
  } else {
    objList.list.each( function(s,i) {
      row_no = (i % 2) + 1;
      html = html
           + '<tr class="list_tr_0' + row_no + '">';
      if(s.point_lat > 0 && s.point_lng > 0) {
        html = html
             + '<td style="text-align:left;"><a href="javascript:clickList(' + s.id + ')">' + s.name + '</a></td>';       } else {
        html = html
             + '<td style="text-align:left;">' + s.name + '</td>';
      }
      switch(mode) {
        case 0:
          html = html
               + '<td>' + s.yatin + '<br>' + s.kyoeki + '</td>'
               + '<td>' + s.type_data_num + s.type_data_type + '</td>'
               + '</tr>';
          break;
        case 1:
          html = html
               + '<td>' + s.yatin + '<br>' + s.kyoeki + '</td>'
               + '<td>' + s.type_data_num + s.type_data_type + '</td>'
               + '</tr>';
          break;
        case 2:
          html = html
               + '<td>' + s.yatin + '<br>' + s.kyoeki + '</td>'
               + '<td>' + s.structure + '</td>'
               + '</tr>';
          break;
        case 3:
          html = html
               + '<td>' + s.yatin + '<br>' + s.yatin_tax + '</td>'
               + '</tr>';
          break;
        case 4:
          html = html
               + '<td>' + s.tubotanka + '<br>' + s.hirosa + ' / ' + s.tubo + '</td>'
               + '</tr>';
          break;
        case 5:
          html = html
               + '<td>' + s.kakaku + '</td>'
               + '<td>' + s.type_data_num + s.type_data_type + '</td>'
               + '</tr>';
          break;
        case 6:
          html = html
               + '<td>' + s.kakaku + '<br>' + s.hirosa + '㎡ / ' + s.tubo + '</td>'
               + '</tr>';
          break;
        case 7:
          html = html
               + '<td>' + s.kakaku + '<br>' + s.hirosa + '㎡ / ' + s.tubo + '</td>'
               + '</tr>';
          break;
        case 8:
          html = html
               + '<td>' + s.kakaku + '</td>'
               + '<td>' + s.tatenobe + '㎡ / ' + s.tatetubo + '<br>' + s.hirosa + '㎡ / ' + s.tubo + '</td>'
               + '</tr>';
          break;
        default:
          html = html
               + '<td>' + s.yatin + '<br>' + s.kyoeki + '</td>'
               + '<td>' + s.type_data_num + s.type_data_type + '</td>'
               + '</tr>';
          break;
      }
    });
  }
  html = html
       + '</table>'
       + '</div>';
  d_list.innerHTML = html;
}

// ページ送りの設定 ============================================================
function setPage() {
  var html = '';
  var d_page = $('list_page');

  if(objList == null) {
  } else {
    if(objList.prev != null) {
      html = '<a href="javascript:getData(\'' + objList.prev + '\');">前のページへ</a>';
    }
    if(objList.next != null) {
      if(html == ''){
        html = '<a href="javascript:getData(\'' + objList.next + '\');">次のページへ</a>';
      } else {
        html = html
             + '　<a href="javascript:getData(\'' + objList.next + '\');">次のページへ</a>';
      }
    }
  }
  d_page.innerHTML = html;
}

// メニューの変更 ==============================================================
function changeMenu() {
  var html = '';
  var d_category = $('category');

  // ==== メニューの表示 ====
  html = '<ul>';
  for(i = 0; i < CATE_NUM; i++){
    if(i == mode) {
      html = html
           + '<li id="category' + i + '_on">'
           + '<a href="javascript:clickMenu(' + i + ');">' + category_name[i] + '</a></li>';
    } else {
      html = html
           + '<li id="category' + i + '">'
           + '<a href="javascript:clickMenu(' + i + ');">' + category_name[i] + '</a></li>';
    }
  }
  html = html + '</ul>';
  d_category.innerHTML = html;
}

// マーカーの設定 ==============================================================
function setMarkers() {
  var html = '';

  // すべてのオーバーレイオブジェクトの削除
  map.clearOverlays();

  // 物件マーカーの設定
  objList.list.each( function(s,i) {
    if(s.point_lng > 0 || s.point_lat > 0) {
      s.createMarker = _createMarker;
      var marker = s.createMarker();
    } else {
      s.createMarker = null;
    }
  });
  // 大学マーカーの設定
  // 宮崎大学（木花）
  if(document.ck.elements[0].checked == true) {
    var marker01 = new GMarker(new GLatLng(31.830343, 131.414755), MapIcon[1]);
    GEvent.addListener(marker01, 'click', function() {
      map.setCenter(marker01.getPoint());
      html = '<div class="info">宮崎大学（木花）</div>';
      marker01.openInfoWindowHtml(html);
    });
    map.addOverlay(marker01);
  }
  // 宮崎大学（清武）
  if(document.ck.elements[1].checked == true) {
    var marker02 = new GMarker(new GLatLng(31.839722, 131.399831), MapIcon[1]);
    GEvent.addListener(marker02, 'click', function() {
      map.setCenter(marker02.getPoint());
      html = '<div class="info">宮崎大学（清武）</div>';
      marker02.openInfoWindowHtml(html);
    });
    map.addOverlay(marker02);
  }
  // 宮崎看護大学
  if(document.ck.elements[2].checked == true) {
    var marker03 = new GMarker(new GLatLng(31.859042, 131.415002), MapIcon[1]);
    GEvent.addListener(marker03, 'click', function() {
      map.setCenter(marker03.getPoint());
      html = '<div class="info">宮崎看護大学</div>';
      marker03.openInfoWindowHtml(html);
    });
    map.addOverlay(marker03);
  }
  // 宮崎国際大学・宮崎女子短期大学
  if(document.ck.elements[3].checked == true) {
    var marker04 = new GMarker(new GLatLng(31.860209, 131.394853), MapIcon[1]);
    GEvent.addListener(marker04, 'click', function() {
      map.setCenter(marker04.getPoint());
      html = '<div class="info">宮崎国際大学・宮崎女子短期大学</div>';
      marker04.openInfoWindowHtml(html);
    });
    map.addOverlay(marker04);
  }

  // 共栄ハウス
  var marker00 = new GMarker(new GLatLng(DEF_LAT, DEF_LNG), MapIcon[0]);
  map.addOverlay(marker00);
  GEvent.addListener(marker00, 'click', function() {
    map.setCenter(marker00.getPoint());
    html = '<div class="info">共栄ハウス</div>';
    marker00.openInfoWindowHtml(html);
  });
}

// マーカーの作成 ==============================================================
function _createMarker() {
  var html = '';
  var image = '';
  var marker = new GMarker(new GLatLng(this.point_lat, this.point_lng), MapIcon[2]);
  map.addOverlay(marker);

  if(this.image_look) {
    image = this.image_look;
  } else {
    image = 'now_printing.jpg';
  }

  // ==== 情報ウインドウの設定 ====
  html = '<div class="info">';
  switch(mode) {
    case 0:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/apaman/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>家賃:</b>　' + this.yatin + '</p>'
           + '<p>　<b>共益費:</b>　' + this.kyoeki + '</p>'
           + '<p>　<b>タイプ:</b>　' + this.type_data_num + this.type_data_type + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/apaman/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 1:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/kasiya/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>家賃:</b>　' + this.yatin + '</p>'
           + '<p>　<b>共益費:</b>　' + this.kyoeki + '</p>'
           + '<p>　<b>タイプ:</b>　' + this.type_data_num + this.type_data_type + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/kasiya/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 2:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/tenpojimu/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>家賃:</b>　' + this.yatin + '</p>'
           + '<p>　<b>共益費:</b>　' + this.kyoeki + '</p>'
           + '<p>　<b>構造:</b>　' + this.structure + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/tenpojimu/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 3:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/tyusyajo/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>賃料:</b>　' + this.yatin + '</p>'
           + '<p>　<b>消費税:</b>　' + this.yatin_tax + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/tyusyajo/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 4:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/kasitoti/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>坪単価:</b>　' + this.tubotanka + '</p>'
           + '<p>　<b>広さ:</b>　' + this.hirosa + '㎡</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/kasitoti/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 5:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/ikkodate/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>価格:</b>　' + this.kakaku + '</p>'
           + '<p>　<b>タイプ:</b>　' + this.type_data_num + this.type_data_type + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/ikkodate/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 6:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/toti/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>価格:</b>　' + this.kakaku + '</p>'
           + '<p>　<b>面積:</b>　' + this.hirosa + '㎡ / ' + this.tubo + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/toti/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 7:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT +  'photo/mansyon/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>価格:</b>　' + this.kakaku + '</p>'
           + '<p>　<b>面積:</b>　' + this.hirosa + '㎡ / ' + this.tubo + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/mansyon/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    case 8:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + 'photo/jigyou/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>価格:</b>　' + this.kakaku + '</p>'
           + '<p>　<b>建物:</b>　' + this.tatenobe + '㎡ / ' + this.tatetubo + '</p>'
           + '<p>　<b>土地:</b>　' + this.hirosa + '㎡ / ' + this.tubo + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/jigyou/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
    default:
      html = html
           + '<div class="info_left">'
           + '<img src="' + WEB_ROOT + '/photo/apaman/gaikan/' + image + '" alt="" width="120px" height="100px" />'
           + '</div>'
           + '<div class="info_right">'
           + '<p>　<b>名称:</b>　' + this.name + '</p>'
           + '<p>　<b>家賃:</b>　' + this.yatin + '</p>'
           + '<p>　<b>共益費:</b>　' + this.kyoeki + '</p>'
           + '<p>　<b>タイプ:</b>　' + this.type_data_num + this.type_data_type + '</p>'
           + '<p>　<a href="' + WEB_ROOT + 'search/apaman/' + this.id + '" target="_brank">[詳細]</a></p>'
           + '</div>';
      break;
  }
  html = html
       + '</div>';

  GEvent.addListener(marker, 'click', function() {
    map.setCenter(marker.getPoint());
    marker.openInfoWindowHtml(html);
  });
  this.marker = marker;
  return marker;
}

// 物件選択 ====================================================================
function clickList(id) {
  objList.list.each( function(s,i) {
    if((s.id == id) && (s.marker != null)) {
      GEvent.trigger(s.marker,'click');
//      break;
    }
  });
}

// 地図アイコンの作成 ==========================================================
var MapIcon = new Array();
function createIcon() {
	for(i = 0; i < ICON_NUM; i++){
		if(i == 0) {
			// アイコンを設定
			var icon = new GIcon();

			icon.image						= WEB_ROOT + 'img/kyoei.png';
			icon.iconSize					= new GSize (21, 21);
			icon.iconAnchor				= new GPoint(21, 21);
			icon.shadow						= WEB_ROOT + 'img/kyoeis.png';
			icon.shadowSize				= new GSize (42, 21);
			icon.printImage				= WEB_ROOT + 'img/kyoei.png';
			icon.printShadow			= WEB_ROOT + 'img/kyoeis.gif';
			icon.infoWindowAnchor = new GPoint(10, 0);
			icon.imageMap					= new Array(0,0,10,0,10,34,0,34);
			MapIcon[i] = icon;
		} else {
			// アイコンを設定
			var icon = new GIcon();
			icon.image						= WEB_ROOT + 'img/icon' + (i) + '.png';
			icon.iconSize					= new GSize (12, 20);
			icon.iconAnchor				= new GPoint(12, 20);
			icon.shadow						= WEB_ROOT + 'img/icons.png';
			icon.shadowSize				= new GSize (22, 20);
			icon.infoWindowAnchor = new GPoint(10, 0);
			icon.imageMap					= new Array(0,0,10,0,10,20,0,20);
			MapIcon[i] = icon;
		}
	}
}

