// JavaScript Document

var jZip =  '';
var jAddrStr = '';
var jPref = 0;
var jCity = 0;
var jLat = 0;
var jLng = 0;


lssPT_AjaxZip2Geo = Class.create();

lssPT_AjaxZip2Geo.prototype = {
	initialize : function(inZip, inAddr){
		this.zipElement = $(inZip);
		if(this.zipElement != "undefined"){
			this.zipElement.onkeyup = this.procKeyup.bind(this);
		}
	},
	
	procKeyup : function(){
		var url = "/cms/cmsUtils/ajZip/";
		var query = 'zip=' + escape(this.zipElement.value);
		var myAjax = new Ajax.Request(url,
								{
									method: 'get'
									,parameters: query
									,onComplete: this.handleZipChanged.bind(this)
								}
							);
	},
	
	handleZipChanged : function(res){
		var xmlDoc  = res.responseXML;
		if(xmlDoc!=null){
		
			var strPref		= this.elementURLDecode(xmlDoc,'pr');
			var strCity		= this.elementURLDecode(xmlDoc,'ct');
			var strStreet	= this.elementURLDecode(xmlDoc,'st');
			
			var cdPref		= this.elementURLDecode(xmlDoc,'pri');
			var cdCity		= this.elementURLDecode(xmlDoc,'cti');
			var cdZip		= this.elementURLDecode(xmlDoc,'cd');

			jZip = cdZip;
			jPref = cdPref;
			jCity = cdCity;
			jAddrStr = strPref+strCity+strStreet;

//alert(jAddrStr);
			var gGeo = new GClientGeocoder();
			gGeo.getLatLng(jAddrStr,
				function(point){
					if(point){
						jLat=point.lat();
						jLng=point.lng();
					//	$("mapMessage").innerHTML = '<span style="color:#FF3333;">[' + jAddrStr + "]付近" + point + "を表示しました</span>";
					}else{
						$("geoFieldAddr").innerHTML = '<span style="color:#FF3333;">[' + jAddrStr + "]に該当する住所が見つかりませんでした" + '</span>';
					}
					geoUpdate(false);
				}
			);
		}
	},
	
	elementURLDecode : function(inXmlDoc, inElementName){
		return decodeURIComponent(inXmlDoc.getElementsByTagName(inElementName).item(0).firstChild.data);
	}
};

function geoUpdate(inInit){
	if(inInit){
		$('geoFieldZip').value = jZip;
	}
	$('geoFieldAddr').value = jAddrStr;
	//$('dispGeoFieldAddr').innerHTML = $('geoFieldAddr').value;
	$('geoFieldPref').value = jPref;
	$('geoFieldCity').value = jCity;
	$('geoFieldGeoLat').value = jLat;
	$('geoFieldGeoLng').value = jLng;
}



var lssPT_AjaxAddrDropdown2Geo = new Class.create({
	
	option : {
		"showPref" : [
			{
			 	"value":"21"
				,"label":"岐阜県"
			},
			{
			 	"value":"23"
				,"label":"愛知県"
			}
		]
		, "prefDropDownId" : "geoPrefDoropDown"
		, "cityDropDownId" : "geoCityDoropDown"
	},
	
	initialize : function(inFieldPref, inFieldCity, inFieldReset){
		this.prefField = $(inFieldPref);
		this.cityField = $(inFieldCity);
		this.resetField = $(inFieldReset);
		this.option.initialPref = $('geoFieldPref').value;
		this.option.initialCity = $('geoFieldCity').value;
		
		//this.createPrefTag();
		Event.observe($(this.option.prefDropDownId), "change", this.changePref.bind(this));
		Event.observe($(this.option.cityDropDownId), "change", this.changeCity.bind(this));
		this.checkValue();
		/*
		prefElements = $(this.option.prefDropDownId).options;
		for(index=0; index<prefElements.length; index++){
			if($(this.option.prefDropDownId).options[index].value == this.option.initialPref){
				$(this.option.prefDropDownId).options[index].selected = true;
				this.changePref(function(){
					cityElements = $(this.option.cityDropDownId).options;
					for(index2=0; index2<cityElements.length; index2++){
						if($(this.option.cityDropDownId).options[index2].value == this.option.initialCity){
							$(this.option.cityDropDownId).options[index2].selected = true;
							this.checkValue();
							break;
						}
					}
				});
				break;
			}
		}
		*/
	},
	
	resetValue : function(){
		if($F(this.option.prefDropDownId) != this.option.initialPref){
			this.prefInitialize();
		}else if($F(this.option.cityDropDownId) != this.option.initialCity){
			this.cityInitialize();
		}
	},
	
	clearValue : function(){
		var clearHTML = [];
		clearHTML.push('<input type="hidden" name="' + this.option.prefDropDownId + '" id="' + this.option.prefDropDownId + '" value="0" />');
		clearHTML.push('<input type="hidden" name="' + this.option.cityDropDownId + '" id="' + this.option.cityDropDownId + '" value="0" />');
		$("formCostomize").innerHTML = clearHTML.join("\n");
		$("formCostomize").submit();
	},
	
	prefInitialize : function(){
		prefElements = $(this.option.prefDropDownId).options;
		for(index=0; index<prefElements.length; index++){
			if($(this.option.prefDropDownId).options[index].value == this.option.initialPref){
				$(this.option.prefDropDownId).options[index].selected = true;
				this.changePref(this.cityInitialize.bind(this));
				break;
			}
		}
	},
	
	cityInitialize : function(){
		cityElements = $(this.option.cityDropDownId).options;
		for(index2=0; index2<cityElements.length; index2++){
			if($(this.option.cityDropDownId).options[index2].value == this.option.initialCity){
				$(this.option.cityDropDownId).options[index2].selected = true;
				this.checkValue();
				break;
			}
		}
	},
	
	createPrefTag : function(){
		// 未使用
		Event.stopObserving($(this.option.prefDropDownId), "change", this.changePref.bind(this));
		//this.prefField.innerHTML = this.createDropdownTag(this.option.prefDropDownId, this.option.showPref, false);
		this.prefField.removeChild(this.prefField.firstChild);
		this.prefField.appendChild(this.createDropdownTag(this.option.prefDropDownId, this.option.showPref, false));
		Event.observe($(this.option.prefDropDownId), "change", this.changePref.bind(this));
	},
	
	changePref : function(inCallback){
		if(!Object.isFunction(inCallback)){
			inCallback = Prototype.emptyFunction;
		}
		if($F(this.option.prefDropDownId) > 0){
			var url = "/cms/cmsUtils/ajAddr/";
			var query = 'v=' + $F(this.option.prefDropDownId) + '&t=c';
			new Ajax.Request(url,
				{
					"method": 'get'
					,"parameters": query
					,"onSuccess": this.drawCityTag.bind(this)
					,"onComplete": inCallback.bind(this)
					, "asyncflag" : true
				}
			);
		}
	},
	
	drawCityTag : function(objRes){
		Event.stopObserving($(this.option.cityDropDownId), "change", this.changeCity.bind(this));
	//	this.cityField.innerHTML = "";
		var response = eval("(" + objRes.responseText + ")");
		//this.cityField.innerHTML = this.createDropdownTag(this.option.cityDropDownId, response, true);
		//this.cityField.removeChild(this.cityField.firstChild);
		while(this.cityField.firstChild){
			this.cityField.removeChild(this.cityField.firstChild);
		}
		this.cityField.appendChild(this.createDropdownTag(this.option.cityDropDownId, response, true));
		Event.observe($(this.option.cityDropDownId), "change", this.changeCity.bind(this));
		this.checkValue();
	},
	
	changeCity : function(){
		if($F(this.option.cityDropDownId) > 0){
			jAddrStr = $(this.option.prefDropDownId).options[$(this.option.prefDropDownId).selectedIndex].innerHTML
						+ $(this.option.cityDropDownId).options[$(this.option.cityDropDownId).selectedIndex].innerHTML;
			jPref = $F(this.option.prefDropDownId);
			jCity = $F(this.option.cityDropDownId);
			
			var gGeo = new GClientGeocoder();
			gGeo.getLatLng(jAddrStr,
				function(point){
					if(point){
						jLat=point.lat();
						jLng=point.lng();
					//	$("mapMessage").innerHTML = '<span style="color:#FF3333;">[' + jAddrStr + "]付近" + point + "を表示しました</span>";
					}else{
						$("geoFieldAddr").innerHTML = '<span style="color:#FF3333;">[' + jAddrStr + "]に該当する住所が見つかりませんでした" + '</span>';
					}
					geoUpdate(false);
				}
			);
		}
		this.checkValue();
	},
	
	checkValue : function(){
		this.resetField.innerHTML = "";
		
		if(($F(this.option.prefDropDownId) != this.option.initialPref || $F(this.option.cityDropDownId) != this.option.initialCity)
		    && ($F(this.option.prefDropDownId) > 0 && $F(this.option.cityDropDownId) > 0)){
			var objSubmit = document.createElement("img");
			objSubmit.src = "/realestate/images/customOn.jpg";
			objSubmit.onmouseover = function(){ this.src = "/realestate/images/customOn_f2.jpg"; }
			objSubmit.onmouseout = function(){ this.src = "/realestate/images/customOn.jpg"; }
			objSubmit.alt = "設定・変更";
			objSubmit.width = "92";
			objSubmit.height = "21";
			objSubmit.border = "0";
			Event.observe(objSubmit, "click", function(){  $("formCostomize").submit(); });
			
			this.resetField.appendChild(objSubmit);
		}
		
		//if($F(this.option.prefDropDownId) != this.option.initialPref || $F(this.option.cityDropDownId) != this.option.initialCity){
		if($F(this.option.cityDropDownId)>0 && this.option.initialCity>0){
			var objReset = document.createElement("img");
			objReset.src = "/realestate/images/customReset.jpg";
			objReset.onmouseover = function(){ this.src = "/realestate/images/customReset_f2.jpg"; }
			objReset.onmouseout = function(){ this.src = "/realestate/images/customReset.jpg"; }
			objReset.alt = "設定解除";
			objReset.width = "92";
			objReset.height = "21";
			objReset.border = "0";
			//Event.observe(objReset, "click", this.resetValue.bind(this));
			Event.observe(objReset, "click", this.clearValue.bind(this));
			
			this.resetField.appendChild(objReset);
		}
		//}
		//alert($F(this.option.prefDropDownId) + " / " + this.option.initialPref + "  --  " + $F(this.option.cityDropDownId) + " / " + this.option.initialCity);
	},
	
	createDropdownTag : function(inDoropdownId, inValuesArray, inSetSelect){
		var tagSelect = document.createElement("select");
		tagSelect.id = inDoropdownId;
		if(inSetSelect == true){
			tagSelect.options[tagSelect.length] = new Option("▼選択してください", 0);
		}
		if(Object.isArray(inValuesArray)){
			inValuesArray.each(function(obj){
				tagSelect.options[tagSelect.length] = new Option(obj.label, obj.value);
			});
		}
		return tagSelect;
		
		/*
		var tag = '<select id="' + inDoropdownId + '">' + "\n";
		if(inSetSelect == true){
			tag += '<option value="0">▼選択してください</option>';
		}
		if(Object.isArray(inValuesArray)){
			inValuesArray.each(function(obj){
				tag += '<option value="' + obj.value + '">' + obj.label + '</option>' + "\n";
			});
		}
		tag += '</select>'
		
		return tag;
		*/
	}
	
});


