// ロケーション(都道府県)毎の画像とエリアコードの設定
var LOC = new Array();
LOC.push({name: '日本全国',   area_id: 0,     zoom:3,  center:'38.200000,138.700000'});
LOC.push({name: '北海道',     area_id: 11,    zoom:5,  center:'43.516680,142.602539'});
LOC.push({name: '青森県',     area_id: 12,    zoom:7,  center:'40.824527,140.739992'});
LOC.push({name: '岩手県',     area_id: 13,    zoom:7,  center:'39.483777,141.130714'});
LOC.push({name: '宮城県',     area_id: 14,    zoom:7,  center:'38.269050,140.872107'});
LOC.push({name: '秋田県',     area_id: 15,    zoom:7,  center:'39.718824,140.102355'});
LOC.push({name: '山形県',     area_id: 16,    zoom:8,  center:'38.240658,140.363624'});
LOC.push({name: '福島県',     area_id: 17,    zoom:7,  center:'37.332392,140.291762'});
LOC.push({name: '東京都',     area_id: 18,    zoom:8,  center:'35.689718,139.691699'});
LOC.push({name: '神奈川県',   area_id: 19,    zoom:8,  center:'35.447736,139.642346'});
LOC.push({name: '埼玉県',     area_id: 20,    zoom:8,  center:'36.000000,139.048848'});
LOC.push({name: '千葉県',     area_id: 21,    zoom:8,  center:'35.605289,140.123298'});
LOC.push({name: '栃木県',     area_id: 22,    zoom:8,  center:'36.563065,139.883423'});
LOC.push({name: '群馬県',     area_id: 23,    zoom:8,  center:'36.390888,139.060414'});
LOC.push({name: '茨城県',     area_id: 24,    zoom:8,  center:'36.342041,140.446794'});
LOC.push({name: '山梨県',     area_id: 25,    zoom:8,  center:'35.664392,138.568454'});
LOC.push({name: '新潟県',     area_id: 26,    zoom:7,  center:'37.485500,138.298001'});
LOC.push({name: '長野県',     area_id: 27,    zoom:8,  center:'36.651516,138.180950'});
LOC.push({name: '富山県',     area_id: 28,    zoom:8,  center:'36.695522,137.211342'});
LOC.push({name: '石川県',     area_id: 29,    zoom:8,  center:'36.594909,136.625569'});
LOC.push({name: '福井県',     area_id: 30,    zoom:8,  center:'36.065405,136.221521'});
LOC.push({name: '静岡県',     area_id: 31,    zoom:8,  center:'34.977355,138.383081'});
LOC.push({name: '愛知県',     area_id: 32,    zoom:8,  center:'35.180421,136.906557'});
LOC.push({name: '岐阜県',     area_id: 33,    zoom:8,  center:'35.391446,136.722300'});
LOC.push({name: '三重県',     area_id: 34,    zoom:8,  center:'34.730521,136.508582'});
LOC.push({name: '滋賀県',     area_id: 35,    zoom:8,  center:'35.113168,135.940704'});
LOC.push({name: '京都府',     area_id: 36,    zoom:8,  center:'35.021474,135.755589'});
LOC.push({name: '和歌山県',   area_id: 37,    zoom:8,  center:'34.226742,135.167327'});
LOC.push({name: '奈良県',     area_id: 38,    zoom:8,  center:'34.685558,135.832751'});
LOC.push({name: '大阪府',     area_id: 39,    zoom:8,  center:'34.686528,135.519662'});
LOC.push({name: '兵庫県',     area_id: 40,    zoom:8,  center:'34.691504,135.183077'});
LOC.push({name: '鳥取県',     area_id: 41,    zoom:7,  center:'35.504125,134.237738'});
LOC.push({name: '島根県',     area_id: 42,    zoom:7,  center:'35.023916,132.083709'});
LOC.push({name: '岡山県',     area_id: 43,    zoom:8,  center:'34.661981,133.934412'});
LOC.push({name: '広島県',     area_id: 44,    zoom:8,  center:'34.396783,132.459626'});
LOC.push({name: '山口県',     area_id: 45,    zoom:7,  center:'34.186193,131.470642'});
LOC.push({name: '香川県',     area_id: 46,    zoom:8,  center:'34.340371,134.043438'});
LOC.push({name: '徳島県',     area_id: 47,    zoom:8,  center:'34.065956,134.559367'});
LOC.push({name: '愛媛県',     area_id: 48,    zoom:8,  center:'33.841851,132.765677'});
LOC.push({name: '高知県',     area_id: 49,    zoom:8,  center:'33.559939,133.531072'});
LOC.push({name: '福岡県',     area_id: 50,    zoom:8,  center:'33.606810,130.418293'});
LOC.push({name: '佐賀県',     area_id: 51,    zoom:8,  center:'33.249670,130.299804'});
LOC.push({name: '長崎県',     area_id: 52,    zoom:8,  center:'32.925600,129.873762'});
LOC.push({name: '大分県',     area_id: 53,    zoom:8,  center:'33.238400,131.612628'});
LOC.push({name: '熊本県',     area_id: 54,    zoom:8,  center:'32.790053,130.741661'});
LOC.push({name: '宮崎県',     area_id: 55,    zoom:7,  center:'32.091803,131.379940'});
LOC.push({name: '鹿児島県',   area_id: 56,    zoom:7,  center:'31.560400,130.557983'});
LOC.push({name: '沖縄県',     area_id: 57,    zoom:7,  center:'26.212647,127.680938'});

// 遷移先のURL
var SHOP_SEARCH_URL = '/shop/';
var SHOPS_AJAX_URL  = '/ajaxs/ajax_area_shops/';

// 都道府県保存用のCookieオプション
var REGION_COOKIE_NAME = 'region';
var REGION_COOKIE_OPTIONS = {expires: 3600*24*365, path: '/'};

// 強制的に都道府県を指定する場合
var REGION = '';

// クライアント側のロケーション情報の確定
function getLocation(){
	var region = REGION;
	if(REGION == ''){
		if(client_location_cookie(REGION_COOKIE_NAME)){
			region = client_location_cookie(REGION_COOKIE_NAME);
		}else{
			if(google.loader.ClientLocation && google.loader.ClientLocation.address && google.loader.ClientLocation.address.region){
				var region = google.loader.ClientLocation.address.region;
			}else{
				return LOC[0];
			}
		}
	}
	
	for (var i = 0; i < LOC.length; i++) {
		if(LOC[i].name == region){
			return LOC[i];
		}
	}
	return LOC[0];
}

// 指定されたエレメントにロケーション情報を描画
var CLIENT_LOCATION_ON_LOAD_EVENT = {};
var MAP_DEFAULT_HTML = {};
function renderLocation(elmID, linkID, size, shoplistID, regionID){
	if(!size) size = '180x90';
	
	var mydate = new Date();
	var mytime = mydate.getTime();
	var params = '?t='+mytime;
	
	var loc = getLocation();
	
	var link_href = SHOP_SEARCH_URL;
	if(loc.area_id > 0){
		link_href += '?area_id=' + loc.area_id;
	}
	if(elmID){
		var elm = jQuery('#' + elmID);
		if(elm) {
			if(!CLIENT_LOCATION_ON_LOAD_EVENT[elmID]){
				MAP_DEFAULT_HTML[elmID] = elm.html();
			}
			if(loc.area_id > 0){
				var html = '<a href="'+ link_href +'"><img src="'+ getStaticMapURL(loc.area_id, size) +'" border="0" /></a>';
			}else{
				var html = MAP_DEFAULT_HTML[elmID];
			}
			elm.html(html);
		}
	}
	
	if(linkID){
		jQuery('#' + linkID).attr({href: link_href});
	}
	
	// 主な店舗表示
	if(shoplistID && jQuery('#' + shoplistID).size() > 0){
		var area_id = loc.area_id;
		jQuery('#' + shoplistID).load(SHOPS_AJAX_URL + area_id + params);
	}
	
	// 都道府県選択イベント
	if(regionID && jQuery('#' + regionID).size() > 0 && !CLIENT_LOCATION_ON_LOAD_EVENT[elmID]){
		jQuery('#' + regionID).change(function(event){
			REGION = this.value;
			renderLocation(elmID, linkID, size, shoplistID, regionID);
			
			// set Cookie
			client_location_cookie(REGION_COOKIE_NAME, REGION, REGION_COOKIE_OPTIONS);
		});
		jQuery('#' + regionID).val(loc.name);
	}
	CLIENT_LOCATION_ON_LOAD_EVENT[elmID] = 1;
}

// 指定されたリンクエレメントにロケーション付き店舗検索URLを設定
function renderLocationGnavi(linkID){
	jQuery(document).ready( function() {
		var loc = getLocation();
		if(loc == false) return;
		
		var link_href = SHOP_SEARCH_URL;
		if(loc.area_id > 0){
			link_href += '?area_id=' + loc.area_id;
			jQuery('#' + linkID).attr({href: link_href});
		}
	});
}

//GoogleStaticMapのURL＋パラメータの生成
function getStaticMapURL(area_id, size){
	if(!size) size = '180x90';
	var url = '/resources_shared/scripts/apps/mapion/';
	url += size + '/';
	url += area_id + '.gif';
	return url;
}


function client_location_cookie(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = (cookies[i] || "").replace( /^\s+|\s+$/g, "" );
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
}


