/*@cc_on _d=document;eval('var document=_d')@*/
var map;
var Hv = document;
var icons = new Array();
var icons2 = new Array();
var moveAndMsgList = new Array();
var points = new Array();
var polygon = "";
var imageBaseURL = "";
var resourceURL = "";
var appBaseURL = "";
var isLogined = false;
var maxCount;
var titleSuffix = "";
var searchList;
var centerLatLng;
var commutingType = "";
var isHideReady = false;
var timeoutID;
var bigIcon = null;
var tempData = null;

// 地図ロード
function loadMap(url) {
	var coordinates;	// 座標配列(作業用)
	var property;		// 地図のプロパティ
	var center_icon;	// 中心を示すアイコン
	var toolbar;		// ツールバー

	imageBaseURL = url + "engineer/img";
	resourceURL = url;
	// 地図のロード
	property = {url:appBaseURL + 'ImageRasterProxy', image:imageBaseURL +'/job/item1.3.png', copyrightInfo:{text:' '}};
	map = new NTMap('map', property);
	property=null;
	// 縮尺設定
	map.setZoom(parseInt(jQuery("#clientSearchConditionForm > #mapScale").val()), 
			parseInt(jQuery("#clientSearchConditionForm > #mapZoom").val()));
	// 色設定
	map.setPalette("bright");
	map.addParam("AntiAlias","1");
	// スケールバー表示
	map.loadScalebar();
	// ツールバー表示
//	toolbar = new NTZoomToolbar(0,2,0,5,1,2,1,4,1,8,1,13,1,23,2,3,3,2,3,5,4,3,4,6,4,9);
	toolbar = new NTZoomToolbar(1,8,1,13,1,23,2,3,3,2,3,5);
	map.loadToolbar(toolbar);
	toolbar=null;
	// アイコン表示
	addCenter();
	// 範囲表示追加
	if (polygon != "") {
		polygon = "255,0,0,20,255,0,0,255,1,2" + polygon;
		map.addParam("Polygon", polygon);
	}
	setMapParam(map.getPos(), map.getZoom());

	// タッチイベント設定
	var mapIF = new NT_TOUCHIF.MapIF({'mapElm':document.getElementById("map"),'mapObj':map,'zoomGestureMin':5,'doubleTapZoom':false,'doubleTapMove':true, 'doubleTapTolerance':20});
	jQuery("#map").bind("touchstart", function(){mapIF.touchStartHandle(event);});
	jQuery("#map").bind("touchmove", function(){mapIF.touchMoveHandle(event)});
	jQuery("#map").bind("touchend", function(){mapIF.touchEndHandle(event)});
	jQuery("#map").bind("gesturestart", function(){mapIF.gestureStartHandle(event)});
	jQuery("#map").bind("gesturechange", function(){mapIF.gestureChangeHandle(event)});
	jQuery("#map").bind("gestureend", function(){mapIF.gestureEndHandle(event)});
}


//ロード時の処理
function onInitialLoad() {
	isHideReady = false;
	var latlng = map.getPos();
	var zoom=map.getZoom();
	var d=new Date().getTime();
	if (jQuery("#clientSearchConditionForm > #mapSearch").val() == 1 && jQuery(".error").size() == 0) {
		jQuery.getJSON(
			appBaseURL + "JobAjaxProxy?mode=part&mapCenterIdo=" + latlng.getLatitude()
			+ "&mapCenterKeido=" + latlng.getLongitude()
			+ "&mapScale=" + zoom.scale
			+ "&mapZoom=" + zoom.zoom
			+ "&d=" + d,
			null,
			callbackFunction
		);
	} else {
		// 検索条件でエラーがある場合、AJAXで取得しない
		jQuery.getJSON(appBaseURL+"JobAjaxProxy?mode=error&d="+d,null,callbackFunction);
	}
}

function onMapLoad(latlng, map) {
	jQuery("#map").css("font-size", "");
	hideDialog();
	return true;
}
// 地図パラメータ設定
function setMapParam(latlng, zoom) {
	jQuery.get(appBaseURL + "JobAjaxProxy?mode=set_param&mapCenterIdo=" + latlng.getLatitude()
			+ "&mapCenterKeido=" + latlng.getLongitude()
			+ "&mapScale=" + zoom.scale
			+ "&mapZoom=" + zoom.zoom);
}
// コールバック
function callbackFunction(data) {
	tempData = data;
	icons = new Array();
	icons2 = new Array();
	var imgFileName;
	var counter = 1;
	var counter2 = 0;
	var data2 = data['jigyosho'];
	moveAndMsgList = new Array();
	addCenter();
	if (data2.list.length <= maxCount) {
		var bottomCnt = new Array();
		var loopCnt;
		for (loopCnt = 1; loopCnt < 9; loopCnt++) {
			bottomCnt[loopCnt] = 0;
		}
		for (cnt in data2['list']) {
			if (isNaN(cnt)) {
				break;
			}
			var jigyosho = data2['list'][cnt];
			var bunyaImageTag = "";
			var ankenImageTag = "";
			for (i in jigyosho.bunyaImages) {
				if (isNaN(i) || i > 2) {
					break;
				}
				bunyaImageTag = bunyaImageTag + createImgTag(jigyosho.bunyaImages[i]);
			}
			var bunyaCd = "no_image";
			for (i in jigyosho.bunyaList) {
				if (isNaN(i) || i > 0) {
					break;
				}
				bunyaCd = jigyosho.bunyaList[i];
			}
			imgFileName = "";
			var loopFlg = true;
			for (var bunyaCnt = 0; bunyaCnt < jigyosho.bunyaList.length && loopFlg; bunyaCnt++) {
				var bunya = jigyosho.bunyaList[bunyaCnt];
				//alert('kigyoCd:' + jigyosho.id.kigyoCd + ' / jigyoshoCd:' + jigyosho.id.jigyoshoCd + ' / bunyaCd:' + bunya.bunyaCd);
				for (var cdCnt = 1; cdCnt < 9; cdCnt++) {
					if ($("input:checkbox[name='productAreasList'][value=10"+cdCnt+"]").attr('checked') == 'checked'
							&& bunya.bunyaCd == "10"+cdCnt) {
						imgFileName = "/map/icon/bill_icon_r0"+cdCnt+".gif";
						bottomCnt[cdCnt]++;
						loopFlg = false;
					}
				}

			}
			if (imgFileName == "") {
				continue;
			}
			if ($("input:radio[name='commutingType'][value=3]").attr('checked') == 'checked' && !jigyosho.isInner) {
				continue;
			}
			var koutei = "";
			for (i in jigyosho.shokushuList) {
				koutei = koutei + nvl2empty(jigyosho.shokushuList[i].shokushuName);
				koutei = koutei + "<br />";
				//break;
			}
	        moveAndMsgList[jigyosho.id.kigyoCd+jigyosho.id.jigyoshoCd] = {
	        		ido:jigyosho.ido, 
	                keido:jigyosho.keido,
	                name:nvl2empty(jigyosho.maskedCorporationName),
	    			gyoshu:nvl2empty(jigyosho.gyoshuName),
	    			bunyaImg:bunyaImageTag,
	    			jigyoNaiyo:nvl2empty(jigyosho.maskedName),
	                baseURL:appBaseURL + 'job/',
	                kigyoCd:jigyosho.id.kigyoCd,
	                jigyoshoCd:jigyosho.id.jigyoshoCd, 
					koutei:koutei,
					isInner:nvl2false(jigyosho.isInner),
					bunyaCd:bunyaCd
					//isTrainAvailable:nvl2false(jigyosho.isTrainAvailable),
					//isWalkable:nvl2false(jigyosho.isWalkable)
	        	};
	        var temp_icon = createIcon(imageBaseURL + imgFileName, 
					jigyosho.ido, jigyosho.keido, 
					appBaseURL + 'job/', 
					jigyosho.id.kigyoCd, jigyosho.id.jigyoshoCd);
	        if (jigyosho.isInner) {
				icons2[counter2] = temp_icon;
				counter2++;
	        	
	        } else {
				icons[counter] = temp_icon;
				counter++;
	        }
	        temp_icon = null;
		}
		icons = icons.concat(icons2);
		var allCnt = 0;
		for (loopCnt = 1; loopCnt < 9; loopCnt++) {
			$("#bunya_cnt_"+loopCnt).text(bottomCnt[loopCnt]);
			allCnt += bottomCnt[loopCnt];
		}
		$("#all_cnt").text(allCnt);
		for(var tmp = 0; tmp < 3 - $("#all_cnt").text().length; ){
			$("#all_cnt").text("0" + $("#all_cnt").text());
		}
	}
	var baseURL = appBaseURL + "job/"
		+ "lat/" + $("#mapCenterIdo").val() + "/"
		+ "lon/" + $("#mapCenterKeido").val() + "/"
		+ "scale/" + $("#mapScale").val() + "/"
		+ "zoom/" + $("#mapZoom").val() + "/"
		+ "category/";
	var categoryFlg = "";
	for (var cdCnt = 1; cdCnt < 9; cdCnt++) {
		if ($("input:checkbox[name='productAreasList'][value=10"+cdCnt+"]").attr('checked') == 'checked') {
			categoryFlg = categoryFlg + "1";
		} else {
			categoryFlg = categoryFlg + "0";
		}
	}
	$("a#list_000").attr("href", baseURL + "000/" + categoryFlg);
	for (var cdCnt = 1; cdCnt < 9; cdCnt++) {
		$("a#list_10" + cdCnt).attr("href", baseURL + "10" + cdCnt + "/" + categoryFlg);
	}
	addIcons(1);
	data2=null;
	return true;
}

function narrowing(){
	if (bigIcon != null) {
		var smallIcon = new NTIcon(bigIcon.icon.getPos(),
				new NTImage(bigIcon.path, 35, 51),bigIcon.icon.getId());
		smallIcon.addEvent("click", createMouseOverFunction(bigIcon.kigyoCd, bigIcon.jigyoshoCd, bigIcon.path));
		smallIcon.addEvent("touchstart", createMouseOverFunction(bigIcon.kigyoCd, bigIcon.jigyoshoCd, bigIcon.path));
		smallIcon.setGroup(bigIcon.group);
		smallIcon.setIconPosition(4);
		map.clearIcon(bigIcon.group);
		map.addIcon(smallIcon);
		smallIcon = null;
		bigIcon = null;
	}
	callbackFunction(tempData);
	return false;
}

function nvl2false(val) {
	if(val == null || val == undefined || val == ""){
		return false;
	}
	return val;
}

function nvl2empty(val) {
	if(val == null || val == undefined){
		return "";
	}
	return val;
}

// 画像タグ生成
function createImgTag(img) {
	var result = "";
	if (img != null && img != undefined) {
		result = result + "<img src='" + resourceURL + img.fileName + "' ";
		result = result + " alt='" + img.alt + "' />";
	}
	return result;
}

// ポップアップ表示
function showMsg1(obj) {
	$('li.kigyou_name > a').text(obj.name + "　" + obj.jigyoNaiyo);
	$('li.kigyou_name > a').attr('href', obj.baseURL + obj.kigyoCd + "/" + obj.jigyoshoCd);
	$('p.kigyou_content_bottom > a#btn_detail').attr('href', obj.baseURL + obj.kigyoCd + "/" + obj.jigyoshoCd);
	$('p.kigyou_content_bottom > a#btn_entry').attr('href', appBaseURL + "entry/entry_input.html?kigyoCd=" + obj.kigyoCd + "&jigyoshoCd=" + obj.jigyoshoCd);
	var kigyou_content_detail = "";
	kigyou_content_detail = kigyou_content_detail + "<span>製品分野：</span>";
	kigyou_content_detail = kigyou_content_detail + "<br />";
	kigyou_content_detail = kigyou_content_detail + obj.bunyaImg;
	kigyou_content_detail = kigyou_content_detail + "<br />";
	kigyou_content_detail = kigyou_content_detail + "<span>工程：</span>";
	kigyou_content_detail = kigyou_content_detail + "<br />";
	kigyou_content_detail = kigyou_content_detail + obj.koutei;
	$('p.kigyou_content_detail').html(kigyou_content_detail);
	$('p.kigyou_image > img').attr('src', appBaseURL + "DynamicImageProxy/jigyosho/" + obj.kigyoCd + "/" + obj.jigyoshoCd);
	$('p.kigyou_image > img').attr('alt', obj.name + "_イメージ");
	$('div#map_area > div#kigyou_detail').css('visibility', 'visible');
	$('div#map_area > div#kigyou_detail').css('display', 'block');
	return;
}
function addCenter(){
	map.clearIcon();
	// 中心
	if (centerLatLng != null) {
		var centerIcon = new NTIcon(centerLatLng, new NTImage(imageBaseURL + "/map/icon/human_icon.gif",41,61));
		centerIcon.setIconPosition(4);
		map.addIcon(centerIcon);
		centerIcon = null;
	}
}
// アイコン追加処理
function addIcons(cnt) {
	var startCnt = parseInt(cnt);
	timeoutID = setTimeout("addIcons('" + (startCnt + 20) +"')", 10);
	if (startCnt < icons.length) {
		outIcon(startCnt, icons.length, 20);
	} else {
		hideDialog();
		clearTimeout(timeoutID);
	}
}

function outIcon(from, max, cnt) {
	if (max - from >= 20) {
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14],
				icons[from + 15], icons[from + 16], icons[from + 17],
				icons[from + 18], icons[from + 19]);
	} else if(max - from == 19){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14],
				icons[from + 15], icons[from + 16], icons[from + 17],
				icons[from + 18]);
	}else if(max - from == 18){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14],
				icons[from + 15], icons[from + 16], icons[from + 17]);
	}else if(max - from == 17){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14],
				icons[from + 15], icons[from + 16]);
	}else if(max - from == 16){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14],
				icons[from + 15]);
	}else if(max - from == 15){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13], icons[from + 14]);
	}else if(max - from == 14){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12], icons[from + 13]);
	}else if(max - from == 13){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11],
				icons[from + 12]);
	}else if(max - from == 12){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10], icons[from + 11]);
	}else if(max - from == 11){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9], icons[from + 10]);
	}else if(max - from == 10){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8], 
				icons[from + 9]);
	}else if(max - from == 9){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7], icons[from + 8]);
	}else if(max - from == 8){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6], icons[from + 7]);
	}else if(max - from == 7){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5], 
				icons[from + 6]);
	}else if(max - from == 6){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4], icons[from + 5]);
	}else if(max - from == 5){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3], icons[from + 4]);
	}else if(max - from == 4){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2], 
				icons[from + 3]);
	}else if(max - from == 3){
		map.addIcon(icons[from], icons[from + 1], icons[from + 2]);
	}else if(max - from == 2){
		map.addIcon(icons[from], icons[from + 1]);
	}else if(max - from == 1){
		map.addIcon(icons[from]);
	}
}

// アイコン作成処理
function createIcon(imagePath, ido, keido, baseURL, kigyoCd, jigyoshoCd){
	var icon = new NTIcon(new NTLatLng(parseInt(ido), parseInt(keido)),
			new NTImage(imagePath, 35, 51),"icon" + kigyoCd + "_" + jigyoshoCd);
	icon.addEvent("click", createMouseOverFunction(kigyoCd, jigyoshoCd, imagePath));
	icon.addEvent("touchstart", createMouseOverFunction(kigyoCd, jigyoshoCd, imagePath));
	icon.setGroup("icon" + kigyoCd + "_" + jigyoshoCd);
	icon.setIconPosition(4);
	try {
		return icon;
	} finally {
		icon = null;
	}
}

// 座標作成処理
function createZahyo(ido, keido) {
	return new NTLatLng(parseInt(ido), parseInt(keido));
}

// マウスクリック時の処理を返す処理
function createMouseOverFunction(kigyoCd, jigyoshoCd, imagePath){
	return function(){
		obj = moveAndMsgList[kigyoCd + jigyoshoCd];

		var oldIcon = map.getIcon("icon" + kigyoCd + "_" + jigyoshoCd)[0];

		if (bigIcon != null) {
			var smallIcon = new NTIcon(bigIcon.icon.getPos(),
					new NTImage(bigIcon.path, 35, 51),bigIcon.icon.getId());
			smallIcon.addEvent("click", createMouseOverFunction(bigIcon.kigyoCd, bigIcon.jigyoshoCd, bigIcon.path));
			smallIcon.addEvent("touchstart", createMouseOverFunction(bigIcon.kigyoCd, bigIcon.jigyoshoCd, bigIcon.path));
			smallIcon.setGroup(bigIcon.group);
			smallIcon.setIconPosition(4);
			map.clearIcon(bigIcon.group);
			map.addIcon(smallIcon);
			smallIcon = null;
			bigIcon = null;
		}
		var bigNewIcon = new NTIcon(oldIcon.getPos(),
				new NTImage(imagePath.replace("bill_icon_r", "bill_icon_l"), 49, 74),"icon" + kigyoCd + "_" + jigyoshoCd);
		bigNewIcon.setGroup("icon" + kigyoCd + "_" + jigyoshoCd);
		bigNewIcon.setIconPosition(4);
		bigNewIcon.addEvent("click", createMouseOverFunction(kigyoCd, jigyoshoCd, imagePath));
		bigNewIcon.addEvent("touchstart", createMouseOverFunction(kigyoCd, jigyoshoCd, imagePath));
		map.clearIcon("icon" + kigyoCd + "_" + jigyoshoCd);
		map.addIcon(bigNewIcon);
		bigIcon = {kigyoCd : kigyoCd, jigyoshoCd : jigyoshoCd, 
				path : imagePath, icon : bigNewIcon, group : "icon" + kigyoCd + "_" + jigyoshoCd};
		showMsg1(obj);
	};
}

function popupDialog(){
    var maskHeight = jQuery('#map_section_in').height();
    var maskWidth = jQuery('#map_section_in').width();
    var maskLeft = jQuery('#map_section_in').offset().left - jQuery('#dialog-overlay').parent().parent().offset().left;
    var maskTop = jQuery('#map_section_in').offset().top;
    jQuery('#dialog-overlay').css("left", maskLeft);
    jQuery('#dialog-overlay').css("top", maskTop);
    var dialogTop = maskTop + (maskHeight/2) - (jQuery('#dialog-box').height()/2);  
    var dialogLeft = maskLeft + (maskWidth/2) - (jQuery('#dialog-box').width()/2); 
    jQuery('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();
    jQuery('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();
}

function hideDialog() {
	if (isHideReady) {
		jQuery('#dialog-overlay, #dialog-box').hide();
	} else {
		isHideReady = true;
	}
}

