var _pages = new Array();
_pages["sightForm"] = null;
var _typeId = null;
j( function() {
	DWRPoiManagement.getCities(function(result){
		DWRUtil.removeAllOptions("cityId1");
		DWRUtil.addOptions("cityId1", [{value:"",label:error_please_select}],"value","label");
		DWRUtil.addOptions("cityId1",result,"value","label");
		if(a1){
			DWRUtil.setValue("cityId1",a1);
			getCounties(a1);
		}else{
			getCounties("");
		}
	});
	DWRPoiManagement.getTypes(function(result){
		DWRUtil.removeAllOptions("type1");
		DWRUtil.addOptions("type1", [{value:"",label:error_please_select}],"value","label");
		DWRUtil.addOptions("type1",result,"value","label");
		if(t1){
			DWRUtil.setValue("type1",t1);
			getSubTypes(t1);
		}else{
			getSubTypes("");
		}
	});	
	j("#downloadButton").click(function(){
		downLoadPhotos();
		return false;
	});
	j("#selectAll").click(function(){
		if(j(this).attr("checked")){
			j("[name=sightIdCB]").each(function(){
				j(this).attr("checked",true);
			})
		}else{
			j("[name=sightIdCB]").each(function(){
				j(this).attr("checked",false);
			})
		}
	});
	j("#searchButton").click(function(){
//		_pages["sightForm"] = null;
//		if(keyword){
//			keyword = false;
//		}
//		if(_typeId){
//			_typeId = null;
//		}
//		querySight(true);
//		return false;
		j("#form1").submit();
		return false;
	});
});

function queryByType(typeId){
	if(typeId&&typeId!=""){
		_typeId = typeId
	}
	_pages["sightForm"] = null;
	querySight(false);
	return false;
}
function querySight(getCount){
	j("#sightList").html("loading......");
	var queryCondition=getQueryCondition();
	if(_typeId){
		queryCondition.queryMainPOIDefinition=_typeId;
	}
	DWRPoiManagement.querySight(queryCondition,_pages["sightForm"],getCount,function(result){
		showResult(result);
	});
}
function showResult(result){
	_pages["sightForm"] = result.page;
	sightForm.loadPagination();
	var sight;
	var sightHtml;
	var method;
	var arroundSight;
	j("#sightList").html("");
	for(var i=0;i<result.content.length;i++){
		sight=result.content[i];
		if(sight.SIGHT_SHOP_TYPE=='2'){
			method = "showShop";
		}else{
			method = "showSight";
		}
		if(sight.NAVI_SIGHT_TYPE=='1'){
			arroundSight = "<div class='flr'><span class='f5'><a href=\""+CTX+"/M0200/F0245/show_around.jsp?sightSysId="+sight.SIGHT_SYS_ID+"\" target='_blank'>"+around+"</a></span></div>"
		}else{
			arroundSight = "";
		}
		sightHtml = "<div class='l'><ul>"+"<li><label >"+download+"<br/><input name='sightIdCB' type='checkbox' class='checkbox' value='"+sight.SIGHT_SYS_ID+"'/></label></li>"
			+"<li><img src='"+POI_PHOTO_PATH+sight.PHOTO_ADDRESS.replace(".","_124.")+"' width='124' height='84' /></li>"
			+"<li class='wd'><span class='t1 '><a href=\""+CTX+"/poiAction!"
			+method
			+".action?sightId="+sight.SIGHT_SYS_ID+"\" target='_blank'>"+"<div class='ellipsis'style='width:330px'>"+sight.SIGHT_NAME+"</div></a></span>"
			+"<br/>"
			+"<div class='ct'>"+sight.SIGHT_DESC+"</div>"
			+"<div class='ct_bt'>"+arroundSight
			+"<span style='float:left'><a href=''class='button' onclick=\"return addToMyFavor('"+sight.SIGHT_SYS_ID+"')\"><span>"+add_my_love+"</span></a></span></div></li>"
			+"<li><div class='testh'><span class='t2'><span class='f2'>"+sight.COUNTY_NAME+"</span></span><div></li>"
			+"<li><div class='lb'>"+sight.SUB_DEF+"</div><div class='pf'>"+getAverageScore(sight.AVGSCORE)+"<br/>";
			if(sight.SCORETIMES==0){
				sightHtml = sightHtml+nobody_score;
			}else{
				sightHtml = sightHtml+getAverageScorePer(sight.SCORETIMES)

+"</div></li></ul></div>";
			}
		j("#sightList").append(sightHtml);
	}
	alert(sightHtml);
	if(result.typeList){
		j("#typeList").html("");
		var typeList = result.typeList;
		for(var i=0;i<typeList.length;i++){
			j("<li><a href='' onclick=\"queryByType('"+typeList[i].CONTENT_SYS_ID+"');return false\">"+typeList[i].CONTENT_NAME+"("+typeList[i].COUNT1+")"+"</a></li>").appendTo(j("#typeList"));
		}
	}
}
function getQueryCondition(){
	var queryCondition={};
	queryCondition.queryPOIName=j("#sightName").val();
	queryCondition.queryMainPOIDefinition=j("#type1").val();
	queryCondition.querySubPOIDefinition=j("#type2").val();
	queryCondition.queryPoiCityId=j("#cityId1").val();
	queryCondition.queryPoiCountyId=j("#countyId1").val();
	queryCondition.querySortord=j("[name=orderBy]:checked").val();
	if(keyword){
		queryCondition.keyword = keyword;
	}
	return queryCondition;
}

function addToMyFavor(sightId){
	DWRCollection.addMyFavor(sightId,function(result){
		if(result==2){
			alert(login_please);
		}else if(result==3){
			alert(have_collected);
		}else if(result){
			alert(success);
		}
	});
	return false;
}

function downLoadPhotos(){
	var sightIdArr = new Array();
	var photoStr = "";
	var i = 0;
	j("[name=sightIdCB]:checked").each(function(){
		sightIdArr[i++] = j(this).val();
	})
	if(sightIdArr==null||sightIdArr.length==0){
		alert(info_please_selectdown);
		return false;
	}
	DWRPoiManagement.getDefaultPics(sightIdArr,function(result){
		if(!result){
			alert(login_please);
			return false;
		}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+"&region="+region_url;
		}
	})
}

function getCounties(cityId){
	if(cityId==""){
		DWRUtil.removeAllOptions("countyId1");
		DWRUtil.addOptions("countyId1", [{value:"",label:error_please_select}],"value","label");
	}else{
		DWRPoiManagement.getCounties(cityId,function(result){
			DWRUtil.removeAllOptions("countyId1");
			DWRUtil.addOptions("countyId1", [{value:"",label:error_please_select}],"value","label");
			DWRUtil.addOptions("countyId1",result,"value","label");
			if(a1&&a2){
				DWRUtil.setValue("countyId1",a2);
			}
		});
	}
}

function getCounties1(cityId){
//	if(cityId==""){
//		DWRUtil.removeAllOptions("countyId1");
//		DWRUtil.addOptions("countyId1", [{value:"",label:error_please_select}],"value","label");
//		querySight(true);
//	}else{
//		DWRPoiManagement.getCounties(cityId,function(result){
//			DWRUtil.removeAllOptions("countyId1");
//			DWRUtil.addOptions("countyId1", [{value:"",label:error_please_select}],"value","label");
//			DWRUtil.addOptions("countyId1",result,"value","label");
//			if(countyId!=null&&countyId!="null"&&countyId!=""){
//				DWRUtil.setValue("countyId1",countyId);
//				querySight(true);
//			}
//		});
//	}
}

function getSubTypes(typeId){
	if(typeId==""){
		DWRUtil.removeAllOptions("type2");
		DWRUtil.addOptions("type2", [{value:"",label:error_please_select}],"value","label");
	}else{
		DWRPoiManagement.getSubTypes(typeId,function(result){
			DWRUtil.removeAllOptions("type2");
			DWRUtil.addOptions("type2", [{value:"",label:error_please_select}],"value","label");
			DWRUtil.addOptions("type2",result,"value","label");
			if(t2){
				DWRUtil.setValue("type2",t2);
			}
		});
	}
}
