/*globals are evil*/
JAME.Package('Mavic.mapSearch');

Mavic.mapSearch={

	/* contain the flash map*/
	swf : undefined,

	/* server side file to query DB */
	cgi : "/cgi-bin/shop_listup_by_cat.cgi",

	/* handle the ajax return response to the shop search */
	handleQuerySuccess: function(resp){

		JAME.$('searchResultContainer').innerHTML=resp;

		JAME.$('searchResultMask').style.display='block';

		var resultTable = parse('#shopTableContainer table');
		var height      = parseInt(JAME.CSS.getStyle(resultTable,'height'));

		JAME.$('returnToMap').onclick = Mavic.mapSearch.hideShopList;

		JAME.$('shopTableContainer').style.overflowY=(height>380)?'scroll':'hidden';

		new JAME.FX({duration:400,easing:JAME.FX.Transition.Sine.In}).Tween(JAME.$('searchResultContainer'),{top:[0]}).queue(function(){
			JAME.$('searchResultContainer').style.top='0px';
			resultTable.onclick=Mavic.mapSearch.handleMapDisplay;
		});
	},

	/* hide the result from the query */
	hideShopList: function(){
		new JAME.FX({duration:400,easing:JAME.FX.Transition.Sine.In}).Tween(JAME.$('searchResultContainer'),{top:[-500]}).queue(function(){
			JAME.$('searchResultContainer').style.top = '-500px';
			JAME.$('searchResultMask').style.display  = 'none';
		});
	},


	/* display/hide the explanation regarding the MPS cat */
	toggleMPSExplanation: function(){

		var mask = JAME.$('MPSExplanationMask');
		var val  = mask.style.display=='none'?0:500;

		if(val==0)
			mask.style.display='block';

		JAME.$('MPSExplanation').innerHTML=(val==0)?'CLOSE':'MORE';

		new JAME.FX({duration:400}).Tween(JAME.$('MPSExplanationContainer'),{left:[val]}).queue(function(){
			JAME.$('MPSExplanationContainer').style.left=val+'px';
			if(val!=0)
				mask.style.display='none';
		});
	},

	/* handle submition of the search form */
	handleShopSearchFormSubmit: function(e){

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

		if(JAME.$('MPSExplanationMask').style.display=='block')
			Mavic.mapSearch.toggleMPSExplanation();

		var prefs = Mavic.mapSearch.swf.getSelectedPrefectures();
		if(prefs=='none')
			return alert("都道府県を選択してから、\n「SEARCH」ボタンを押してください。")

		var query = JAME.Form.serialize(JAME.$('shop_search'))+'&pref='+prefs;

		new JAME.Ajax({
			url:Mavic.mapSearch.cgi+"?task=getHTMLShopList&"+query,
			onSuccess:Mavic.mapSearch.handleQuerySuccess
		});
	},

	/* display the shop information on GMap */
	handleMapDisplay: function(e){
		e=JAME.Events.Normalize(e);
		e.preventDefault();
		e.stopPropagation();
		var target = e.target;
		if(target.tagName.toLowerCase()=='img') target = target.parentNode;
		if(target.tagName.toLowerCase()!=='td') 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
		});
	},

	/* update flash when a category is selected */
	updateMapOnCatSelectionEvent:function(e){
		e = JAME.Events.Normalize(e);
		if(e.target.name=='cat')
			Mavic.mapSearch.updateMapOnCatSelection(JAME.Form.serialize(JAME.$('shop_search')));
	},

	/* reload the map when a category is checked */
	updateMapOnCatSelection:function(query){
  		Mavic.mapSearch.swf.reload(Mavic.mapSearch.cgi+"?task=getXMLList&"+query);
	},

	/* hide/display the categories checkbox according the map state */
	toggleCategoriesCheckBoxes: function(display){
		var cats=document.getElementsByName('cat');
		for(var i=0,ln=cats.length;i<ln;i++)
			cats[i].style.visibility=display;
	}
};

JAME.DOM.Ready(function(){

	Mavic.mapSearch.swf     = findSWF("shopmap");

	JAME.$('MPSExplanationMask').style.display='none';

	Mavic.mapSearch.toggleCategoriesCheckBoxes('hidden');

	JAME.$('MPSExplanation').onclick = Mavic.mapSearch.toggleMPSExplanation;
	JAME.$('shop_search').onclick    = Mavic.mapSearch.updateMapOnCatSelectionEvent;
	JAME.$('shop_search').onsubmit   = Mavic.mapSearch.handleShopSearchFormSubmit;

});


/*FLASH MAP INTERACTION */

function onZoom(level){
	if(level==0) {
		Mavic.mapSearch.toggleCategoriesCheckBoxes('hidden');
		JAME.$('normalShop').style.visibility='hidden';
	}
}

function toggleSearchCategories(val){
	Mavic.mapSearch.toggleCategoriesCheckBoxes(val);
	JAME.$('normalShop').style.visibility=val;
}

function onElementSelected(elm){

	if(Mavic.mapSearch.swf.getSelectedPrefectures()=='none')
		return toggleSearchCategories('hidden');

	toggleSearchCategories('visible');

}
function onLoadStart(){
	//not implemented
}
function onLoadComplete(){
	//not implemented
}
function findSWF(movieName) {
		var movobject = (document[movieName])? document[movieName] 
			: (window[movieName])? window[movieName]
			: (document["ie" + movieName])? (document["ie" + movieName])
			: (window["ie_" + movieName])? window["ie_" + movieName]
			: document[movieName];
		return movobject;		
//   return (window["ie_" + movieName])
//		  ? window["ie_" + movieName]
//		  : document[movieName];
}

