(function(){
	var rm = window['Rimoh_salon'] = {
		mapmovie: {},
		areas: {
			hokkaido_tohoku: 1,
			kantoh: 2,
			joetsu_hokuriku: 3,
			chubu_tokai: 4,
			kansai_kinki: 5,
			chugoku_shikoku: 6,
			kyushu_okinawa: 7
		}
	};
	var debug = location.search.match(/debug/);
	jQuery.extend(rm, {
		areaSelect: function(area_id) {
			if ($('#inbox-movie').length) removeMovie();

			var areaname = function(areacode){
				var areaname;
				$.each(this, function(name, value){
					if (areacode == value) {
						areaname = name;
					}
				});
				return areaname;
			}.call(rm.areas, area_id);
			if (rm.areainfo && rm.areainfo[areaname].saloncount > rm.salonsCountLimit) {
				$.getJSON('/gateway/getPrefecturesList/'+area_id, function(json){
					$('#area-selected-salon-list').fadeTo(0, 0.1);
					if (rm.contentHeight) $('#contents-bg').height(rm.contentHeight);
					$('#area-selected-salon-list').html(json.html);
					if ($('#contents-bg').height() < (parseInt($('#area-selected-salon-list').position().top) + $('#area-selected-salon-list').height())) $('#contents-bg').height($('#area-selected-salon-list').height() + parseInt($('#area-selected-salon-list').position().top));
					$('#area-selected-salon-list').fadeTo(200, 1);
					$('#bgscreen').fadeTo(200, 0.8, function(){
						$('#area-selected-salon-list #pref-list li a').click(function(){
							var prefname = $(this).attr('rel');
							rm.prefSelected(prefname);
							return false;
						});
					});
				});
			}
			else {
				$.getJSON('/gateway/getSalonsAtSelectedArea/'+area_id, function(json){
					$('#area-selected-salon-list').fadeOut(200, function(){
						$('#area-selected-salon-list').fadeTo(0, 0.1);
						if (rm.contentHeight) $('#contents-bg').height(rm.contentHeight);
						$('#area-selected-salon-list').html(json.html);
						if ($('#contents-bg').height() < (parseInt($('#area-selected-salon-list').position().top) + $('#area-selected-salon-list').height())) $('#contents-bg').height($('#area-selected-salon-list').height() + parseInt($('#area-selected-salon-list').position().top));
						$('#area-selected-salon-list').fadeTo(200, 1);
						$('#bgscreen').fadeTo(200, 0.8);
					});
				});
			}
		},
		areaSelected: function(areaname) {
			rm.areaSelect(rm.areas[areaname]);
		},
		prefSelect: function(pref_id){
			$.getJSON('/gateway/getSalonsAtSelectedPrefecture/'+pref_id, function(json){
				$('#area-selected-salon-list').fadeOut(200, function(){
					$('#area-selected-salon-list').fadeTo(0, 0.1);
					if (rm.contentHeight) $('#contents-bg').height(rm.contentHeight);
					$('#area-selected-salon-list').html(json.html);
					if ($('#contents-bg').height() < (parseInt($('#area-selected-salon-list').position().top) + $('#area-selected-salon-list').height())) $('#contents-bg').height($('#area-selected-salon-list').height() + parseInt($('#area-selected-salon-list').position().top));
					$('#area-selected-salon-list').fadeTo(200, 1);
					$('#bgscreen').fadeTo(200, 0.8);
				});
			});
		},
		prefSelected: function(prefname) {
			rm.prefSelect(rm.prefectures[prefname]);
		}
	});
	jQuery(function($){
		$.getJSON('/gateway/getMeta', function(json){
			$.extend(rm, json);
		});
		$('#header-main').insertBefore('#footer .main-navigations');
		$.getJSON('/gateway/is_campaign', function(json){
			if (!$('body').is('.campaign-page') && (json.campaign || location.search.match(/check/))) {
				$('#header .main-navigations #salon-link').replaceWith('<li id="campaign-link" ><a href="/campaign" title="普及キャンペーン指定サロン">普及キャンペーン指定サロン</a></li>');
				$('#footer .main-navigations #footer-salon-link').replaceWith('<li id="footer-campaign-link" ><a href="/campaign" title="普及キャンペーン指定サロン">普及キャンペーン指定サロン</a></li>');
				$('#assignment').replaceWith('<p id="campaign-assignment">指定サロン</p>');
			}
		});
		if (!location.href.match(/select/)) {
			//window['removeMovie'] = function(){ swfobject.removeSWF('inbox-movie'); };
			//$('#area-selected-salon-list').html('<div id="inbox-movie"/>');
			//swfobject.embedSWF('/images/inbox.swf', 'inbox-movie', '545', '354', '10.0.0', '/images/expressInstall.swf');
			$('#informations-list').jCarouselLite({
				btnNext:'#next-info',
				btnPrev:'#prev-info',
				selector:$('#info-list')[0],
				visible:1
			});
			$('#next-info,#prev-info').fadeTo(0, 0.2).hover(function(){$(this).fadeTo(0,0.8)},function(){$(this).fadeTo(0,0.2)});
		}
		$('<div id="map-japan"/>').appendTo('#contents');
		var mapmoviefile = $('body').is('.whiten')?'/images/lumielinaSalonSelector_whiten.swf':'/images/lumielinaSalonSelector2.swf';
		swfobject.embedSWF(mapmoviefile, 'map-japan', '431', '453', '10.0.0', '/images/expressInstall.swf', null, {wmode:'transparent', allowScriptAccess:'always'}, {id:'nippon'});
		$('#area-selector li a').click(function(){
			var h = $(this).attr('href'), areacode = h.substr(h.lastIndexOf('/')+1);
			rm.areaSelect(areacode);
			var areaname = function(areacode){
				var areaname;
				$.each(this, function(name, value){
					if (areacode == value) {
						areaname = name;
					}
				});
				return areaname;
			}.call(rm.areas, areacode);
			rm.mapmovie.areaSelect(areaname);
			return false;
		});
	});
	jQuery(window).load(function(){
		var content = $('#contents'),
		    contentHeight = content.innerHeight(),
			contentPosition = content.offset(),
			contentPaddingTop = $.curCSS(content[0], 'paddingTop'),
		    contentsBg = $('<div id="contents-bg"/>').height(contentHeight);

		rm['contentHeight'] = contentHeight;
		content.css({position:'absolute'}).after(contentsBg).css({
			left:contentPosition.left,
			top:contentPosition.top,
			zIndex:10
		});
		if ($('#contents-bg').height() < (parseInt($('#area-selected-salon-list').position().top) + $('#area-selected-salon-list').height())) $('#contents-bg').height($('#area-selected-salon-list').height() + parseInt($('#area-selected-salon-list').position().top));
		$('<div id="bgscreen"/>').width(545).height(contentHeight).css({
			position:'absolute',
			left:parseInt(contentPosition.left)+433+'px',
			top:parseInt(contentPosition.top)+parseInt(contentPaddingTop)+'px',
			background:$('body').is('.whiten')?'#ffffff':'#000000',
			zIndex:5
		}).appendTo('#contents-bg').fadeTo(0, 0);
		if (location.pathname.match(/select/)) {
			$('#bgscreen').fadeTo(0, 0.8);
		}
		$('#nippon').css({position:'absolute',zIndex:6});
		rm.mapmovie = $('#nippon object')[0]||$('#nippon')[0];
		if (location.href.match(/areaselect/)) {
			var h = location.href, areacode = h.substr(h.lastIndexOf('/')+1);
			var areaname = function(areacode) {
				var areaname;
				$.each(this, function(name, value){
					if (areacode == value) {
						areaname = name;
					}
				});
				return areaname;
			}.call(rm.areas, areacode);
			var selector = setInterval(function(){
				if (rm.mapmovie && rm.mapmovie.areaSelect) {
					rm.mapmovie.areaSelect(areaname);
					clearInterval(selector);
				}
			}, 100);
		}
	});
})()
