/*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';
		});
	},

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

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

		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();

		if(e.target.tagName.toLowerCase()!=='td') return;

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

	/* update flash when a category is selected */
	updateMapOnCatSelectionEvent:function(e){
	},

	/* reload the map when a category is checked */
	updateMapOnCatSelection:function(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;
	}
};


/*FLASH MAP INTERACTION */

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

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

function onElementSelected(elm){
	//not implemented
}
function onLoadStart(){
	//not implemented
}
function onLoadComplete(){
	//not implemented
}
function findSWF(movieName) {
    return (window["ie_" + movieName])
		  ? window["ie_" + movieName]
		  : document[movieName];
}

