function getShopListByPref(url){

	if(!url) return;
	new JAME.Ajax({
		url:url,
		onSuccess:function(resp) {
			JAME.$('shopByPrefContainer').innerHTML=resp;
			setMapHandler(parse('#shopByPrefContainer table'));
			new JAME.FX({duration:400}).Tween(JAME.$('shopByPrefContainer'),{opacity:[1]})
		}
	});
}

function setMapHandler(target) {

	target.onclick=function(e) {

		e = JAME.Events.Normalize(e);
		e.stopPropagation();
		e.preventDefault();

		var target = e.target;
		var tag    = target.tagName.toLowerCase();
		if(!/td|img/.test(tag))
			return;

		if(/img/.test(tag))
			target = target.parentNode;



		if(!target.parentNode.id) return;
		myLightWindow.activateWindow({
			href:'/cgi-bin/shop.cgi?task=show_map&id='+target.parentNode.id,
			height:560,
			width:760,
			title:JAME.DOM.firstNode(target.parentNode).innerHTML
		});		
	}
}

JAME.DOM.Ready(function(){

	var ths     = parse('table#prefTable th');
	ths         = ths && ths.constructor===Array ? ths : ths ? [ths]: [];
	var clicked = ths[0];

	for(var i=0;i<ths.length;i++){
		if(clicked.className=='number_0')
			clicked=ths[i+1];
		else
		 break;
	}


	if(clicked && clicked.tagName) {
		clicked.style.fontWeight="bold";
		getShopListByPref(JAME.DOM.firstNode(clicked).href);
	}

	/* TO DO:use event delegation on the root node */
	ths.each(function(elm,i) {

		elm.onclick=function(e){
			e=JAME.Events.Normalize(e);
			e.preventDefault();
			e.stopPropagation();
			if(!JAME.DOM.firstNode(elm).href) return;
			new JAME.FX({duration:400}).Tween(JAME.$('shopByPrefContainer'),{opacity:[1,0.1]})
			if(clicked)
				clicked.style.fontWeight="normal";
			elm.style.fontWeight="bold";
			clicked=elm;
			getShopListByPref(JAME.DOM.firstNode(elm).href);
		}
	});

	JAME.$('zip').onfocus=function(e){
		e=JAME.Events.Normalize(e);
		if(e.target.value=='郵便番号'){
			e.target.value='';
		}
	}
	JAME.$('zip').onblur=function(e){
		e=JAME.Events.Normalize(e);
		if(e.target.value==''){
			e.target.value='郵便番号';
		}
	}
	JAME.$('shopname').onfocus=function(e){
		e=JAME.Events.Normalize(e);
		if(e.target.value=='店舗名'){
			e.target.value='';
		}
	}
	JAME.$('shopname').onblur=function(e){
		e=JAME.Events.Normalize(e);
		if(e.target.value==''){
			e.target.value='店舗名';
		}
	}
})


