_pages["sightForm"] = null;//分页对象初始化
var middleSize="_124.";
j(document).ready(function(){	
	j("#distant").empty();//清空下拉框
	j("#mainDef").empty();
	j("#subDef").empty();
	DWRShowAroundPoi.getDistances(loadDistance);
	DWRShowAroundPoi.getAlllMainDefinitionOptions(j("#regionId").val(),loadMainDef);
	j("#mainDef").bind("change",function(){
		DWRShowPOIQueryCondition.getSubPoiDefinitionsByMain(j(this).val(),j("#regionId").val(),loadMainSub);
	});	
	DWRShowAroundPoi.getCurPOIInfo(j("#sightId").val(),function(sight){
		if(sight!=null){
			j("#curSightName").text(sight.name);
			j("#curSightCountry").text(sight.countryName);
			j("#curSightCity").text(sight.cityName);
			j("#curSightCounty").text(sight.countyName);
			j("#curSightDesc").text(sight.intro);
		}
	});
	
	
});

function loadDistance(distants){		
	j.each(distants, function(i){   
		j("<option value='"+distants[i]+"'>"+distants[i]+"</option>").appendTo("#distant");
	});	
	DWRShowAroundPoi.getDefaultAroundPOIs({poiBaseInfo:{poiId:j("#sightId").val()}},"5",null,loadSightMap);
}
function loadMainDef(mainDs){
	j("<option value=''>"+error_please_select+"</option>").appendTo("#mainDef");
	j.each(mainDs, function(i){ 
		j("<option value='"+mainDs[i].value+"'>"+mainDs[i].label+"</option>").appendTo("#mainDef");
	});	
}
function loadMainSub(mainSs){
	j("#subDef").empty();
	j("<option value=''>"+error_please_select+"</option>").appendTo("#subDef");
	j.each(mainSs, function(i){ 
		j("<option value='"+mainSs[i].value+"'>"+mainSs[i].label+"</option>").appendTo("#subDef");
	});	
}

function queryButton(){
	_pages["sightForm"] = null;
	query();
}

function query(){
	j("#downloadError").text("");	
	j("#downloadError").parent().prev().show();
	DWRShowAroundPoi.getAroundPOIs({poiBaseInfo:{poiId:j("#sightId").val()}},j("#distant").val(),j("#mainDef").val(),_pages["sightForm"],j("#subDef").val(),{callback:loadSightMap});
}
function loadSightMap(data){
	_pages["sightForm"] = data.page;
	var sights = data.roundSight;
	var curMapInfo = data.sightMap;
	_miotourmap.setEdgeValues(data.edgeValues);
	sightForm.loadPagination();
	j("#jdnamelist").empty();
if(sights !=undefined && sights.length>0){
	j.each(sights, function(i,s){ 
		j("#jdnamelist").append(addRow(i+1,s));
	});
	}else{
		j("#downloadError").text(error_no_record);	
		j("#downloadError").parent().prev().hide();
		
	}	
	_miotourmap.openMap(j("#distant").val(),"aroundMap",data);
	var pathStr = curMapInfo.poiBaseInfo.defaultPicPath;
	var pathStrs = pathStr.split(".");
	j("#curSightImg").attr({ src: POI_PHOTO_PATH+pathStrs[0]+middleSize+pathStrs[1]}); 
}



function addRow(number,sight){
	return '<div class="lsfy"><ul><li>'
		+'<div class="ls"><a href="javascript:;" onclick="showMapLabel(\''+sight.position.latitude+'\',\''+sight.position.longitude+'\',\''+sight.poiBaseInfo.poiId+'\',\''+sight.poiBaseInfo.name+'\',\''+sight.poiBaseInfo.address+'\',\''+sight.poiBaseInfo.defaultPicPath+'\');">'+ number+ '</a></div></li>'
		+ '<li><div class="ts"><span class="f7 fl lh_22"><a href="'+CTX+'/'+region_url+'/poi/'+sight.poiBaseInfo.poiId+'" target="_blank">'+sight.poiBaseInfo.name+ '</a> </span></div></li>'
		
		
		+ '<div class="left-1">'
		
		+'<span class="fl ml10 lh_22">'+download+ '</span>'
		+ '<input type="checkbox" name="download" id="download'+sight.poiBaseInfo.poiId+'" class="fl mr10 lh_18" />'
		
		+ '<div class="ml10">'//sight.poiBaseInfo.fax 放的是最爱
		+	((sight.poiBaseInfo.fax=="1")?'':'<a href="#" onclick="addMyLove(\''+sight.poiBaseInfo.poiId+'\');" id="img'+sight.poiBaseInfo.poiId+'"><img src="../../images/The-heart.gif" width="20" height="20" /></a>')
		+ '<div id="msgInfo'+sight.poiBaseInfo.poiId+'"></div>'
		+'</div>'
		
		+'</div>'
		
		
		+'<div class="left-2 indent-2 fl">'
		+j.trim(sight.poiBaseInfo.intro)
		//+ ((j.trim(sight.poiBaseInfo.intro).length>22)?j.trim(sight.poiBaseInfo.intro).substring(0,22)+"<span class=\'tt\'>...</span>":j.trim(sight.poiBaseInfo.intro));
		+ '</div>'
		+'</li></ul></div>';
	}

function addMyLove(id){
	j("#msgInfo"+id).text("");	
	/*if(j("#userId").val()==null || j("#userId").val()==""){
		j("#msgInfo"+id).text(login_please);
	}else{*/
		DWRCollection.addMyFavor(id,function(flag){
			if(flag == 2){
				j("#msgInfo"+id).text(login_please);				
			}else if(flag ==3){
				j("#img"+id).remove();
				j("#msgInfo"+id).text(have_collected);
			}else if(flag){
				j("#img"+id).remove();
			}else{
				j("#msgInfo"+id).text(save+fail);
			}
		});	
	//}
}

function donwlo() {
	j("#downloadError").text("");
	if (j(":checkbox").length > 0) {
		/*if (j("#userId").val() == null || j("#userId").val() == "") {
			j("#downloadError").text(login_please);
		} else {*/
			var downLs = new Array();
			j.each(j(":checkbox"), function(i, che) {
				if (che.checked) {
					downLs.push(che.id.replace("download", ""));
				}
			});
			if (downLs.length > 0) {
				var photoStr = "";
				DWRPoiManagement.getDefaultPics(downLs, function(result) {
					if(!result){
						j("#downloadError").text(login_please);
					}else{
					for ( var i = 0; i < result.length; i++) {
						if (i > 0) {
							photoStr = photoStr + ",";
						}
						photoStr = photoStr + result[i].sightName + ":"
								+ result[i].filePath;
					}
					photoStr = encodeURI(photoStr);
					location.href = DOWNLOAD_SERVLET + "?fileStr=" + photoStr;
					}
				});
			} else {
				j("#downloadError").text(error_please_select);
			}
		//}
	}
}


function showMapLabel(latitude,longitude,poiId,name,addr,defaultPicPath){
	var miniSize="_72.";
	var viewSightPath="/"+region_url+"/poi/";
	miomap.openInfoWindowHtml(new google.maps.LatLng(Number(latitude), Number(longitude)),poi_name+":  <a href='"+contextPath+viewSightPath+poiId+"' target='_blank'><b>" + name + "</b></a><br/>"+poi+address+": <b>" + addr + "</b><br/><a href='"+contextPath+viewSightPath+poiId+"' target='_blank'><img src='"+POI_PHOTO_PATH+defaultPicPath.replace(".",miniSize)+"' title='"+info_click_show_detail+"' width='72' height='54'/></a>"); 
}
