/**
 * @author masa
 */


if (navigator.userAgent.match(/ipad|iphone/i)) {
  $(function(){
    $('#locator-resultwrap .list-overflow').css({ height: 'auto' });
  });
}

function getStorePref(){
	$.get('xml/store.xml', function(response){
		var pref = $(response).find('pref');
		pref.each(function(){
			$('#prefectural').append('<option value="' + $(this).attr('id') + '">' + $(this).attr('name') + '</option>');
		})
	})
	
	$(".submitbtn").click(function(){
		var i = $("input[type='checkbox']:checked")
		var p = $("option:selected");
		var v = [], f = $('#dummy');
		// initialize
		$('input',f).val('');
		i.each(function(){			
			if ($(this).hasClass('category')) {
				v.push($(this).val())
			} else if($(this).attr('name') == 'type') {
				$('input[name="type"]', f).val($(this).val());
			} else if($(this).attr('name') == 'type2') {
				$('input[name="type2"]', f).val($(this).val());
			}
		})
		if (v.length) $('input[name="category"]', f).val(v);
		$('input[name="prefectural"]',f).val(p.val());
		f.submit()
		return false;
	})
}

function getStore(){
	var s = $("div.list-overflow");
	var q = location.query();
	var cat = (q.category)? decodeURIComponent(q.category).split(','):[];
	$.get('xml/store.xml',function(response){
		var p = $(response).find('pref');
		prefs = (!q.prefectural)? $(p):$(p.get(q.prefectural-1));
		prefs.each(function(n,i){
			var idn = 'pref'+$(this).attr('id');
			$(s).append('<div class="resultbody" id="'+ idn +'"><h4>'+$(this).attr('name')+'</h4><ul></ul></div>');
			stores = $.grep($('map',this),function(n,i){
				if(!q.type2 && q.type){
					return ($(n).attr('type')=='1')
				} else if(!q.type && q.type2){
					return ($(n).attr('type')=='2') 
				} else {
					return $(n)
				}
			});
						
			var id = $('#'+idn).hide();
			$(stores).each(function(){
				var j = [];
				var c = $('category',this).text().split(',');
				if(cat.length){
					$(cat).each(function(i,n){ j.push($.inArray(n,c))})
					if ($.inArray(-1, j) == -1) $('ul', id).append(drowStore(this))
				} else {
					$('ul',id).append(drowStore(this))
				}
			})
			if($('li',id).size()){
				id.show();
			} else {
				id.remove();
			} 
		})
		if(!$('div',s).length){
			s.append('<div class="no-result"><p><strong>お探しの店舗が見つかりませんでした。</strong></p><p><a href="locator.html">店舗検索に戻る</a></p></div>');
		}
	})
}

function drowStore(store){
	var st = '<li><div class="address">'+ $('temp',store).text() +'&nbsp;'+ $('item',store).text() +'<br />'+$('address',store).text()+'&nbsp;';
	st += ($('temp',store).attr('flag') == "0")? $('temp',store).text()+'<br/>':'<br/>'
	if($('exception',store).length){
	    st += '<span class="exception">' + $('exception',store).text()+'</span><br/>'
	}
	st += '<span class="maplink">（<a href="locator_map.html?lat='+$('lat',store).text()+'&lon='+$('lon',store).text()+'">地図はこちら</a>）</span></div>';
	st += '<div class="tel">'+$('tel',store).text()+'</div>';
	st += '<div class="time">'+$('time',store).text()+'</div></li>';
	return st;
}

