var area_data = {
	hokkaido: {
		table: [
			{
				txt: 'カーテンDo!なんごう店',
				link: './shop/nangou.html'
			},
			{
				txt: 'カーテンDo!栄町店',
				link: './shop/sakae.html'
			},
			{
				txt: 'カーテンDo!はっさむ店',
				link: './shop/hassamu.html'
			}
		]
	},
	tohoku: {
		table: [
			{
				txt: 'カーテンDo!With郡山店',
				link: './shop/koriyama.html'
			}
		]
	},
	kanto: {
		table: [
			{
				txt: 'カーテンDo!茅ヶ崎店',
				link: './shop/chigasaki.html'
			},
			{
				txt: 'カーテンDo!伊勢崎店',
				link: './shop/isesaki.html'
			},
			{
				txt: 'カーテンDo!太田店',
				link: './shop/ohta.html'
			}
		]
	},
	cyubu: {
		table: [
			{
				txt: 'カーテンDo!長野店',
				link: './shop/nagano.html'
			},
			{
				txt: 'カーテンDo!松本店',
				link: './shop/matsumoto.html'
			},
			{
				txt: 'カーテンDo!OYA柏崎店',
				link: './shop/kashiwazaki.html'
			},
			{
				txt: 'カーテンDo!志都呂店',
				link: './shop/shitoro.html'
			}
		]
	},
	kansai: {
		table: [
			{
				txt: 'カーテンDo!姫路店',
				link: './shop/himeji.html'
			},
			{
				txt: 'カーテンDo!加古川店',
				link: './shop/kakogawa.html'
			},
			{
				txt: 'カーテンDo!伊川谷店',
				link: './shop/ikawadani.html'
			},
			{
				txt: 'カーテンDo!垂水店',
				link: './shop/tarumi.html'
			},
			{
				txt: 'カーテンDo!神戸阪急店',
				link: './shop/kobehankyu.html'
			},
			{
				txt: 'カーテンDo!西宮店',
				link: './shop/nishinomiya.html'
			},
			{
				txt: 'カーテンDo!伊丹店',
				link: './shop/itami.html'
			},
			{
				txt: 'カーテンDo!大津店',
				link: './shop/otsu.html'
			},
			{
				txt: 'カーテンDo!京都醍醐店',
				link: './shop/daigo.html'
			},
			{
				txt: 'カーテンDo!京都北大宮店',
				link: './shop/kitaohmiya.html'
			},
			{
				txt: 'カーテンDo!茨木店',
				link: './shop/ibaraki.html'
			},
			{
				txt: 'カーテンDo!豊中店',
				link: './shop/toyonaka.html'
			},
			{
				txt: 'カーテンDo!なかもず店',
				link: './shop/nakamozu.html'
			},
			{
				txt: 'カーテンDo!貝塚店',
				link: './shop/kaizuka.html'
			},
			{
				txt: 'カーテンDo!奈良店',
				link: './shop/nara.html'
			},
			{
				txt: 'カーテンDo!大和郡山店',
				link: './shop/yamatokoriyama.html'
			},
			{
				txt: 'カーテンDo!草津店',
				link: './shop/kusatsu.html'
			}

		]
	},
	chugoku: {
		table: [
			{
				txt: 'カーテンDo!広島店',
				link: './shop/hiroshima.html'
			},
			{
				txt: 'カーテンDo!岡山店',
				link: './shop/okayama.html'
			},
			{
				txt: 'カーテンDo!高松店',
				link: './shop/takamatsu.html'
			},
			{
				txt: 'カーテンDo!松山店',
				link: './shop/matsuyama.html'
			}
		]
	},
	kyusyu: {
		table: [
			{
				txt: 'カーテンDo!花立店',
				link: './shop/hanatate.html'
			},
			{
				txt: 'カーテンDo!臼杵店',
				link: './shop/usuki.html'
			},
			{
				txt: 'カーテンDo!中津店',
				link: './shop/nakatsu.html'
			}
		]
	}
};

var area_classnames = [];
for (var i in area_data){
	area_classnames.push(i);
}

$(function(){
	// iframe fontsize_changer & resize height
	$('#Content-bottom iframe')
		.load(function(){
			var $this = $(this);
			
			fontsize_changer($this.contents().find('#Container'));
			
			var resize = function(){
				var $if_con = $this.contents();
				var len = $if_con.find('#Content ul li').length;
				
				if ($this.get(0).id == 'Iframe-pickup'){
					$this
						.css('height', $if_con.find('#Content ul:first').height() + 'px');
				} else if (len <= x) {
					var height = $if_con.find('#Content ul').height();
					$this
						.css('height', height + 'px')
						.attr('scrolling','no');
					
					$if_con
						.find('html')
							.css('overflow-y','hidden');
				} else {
					var height = 0;
					$if_con
						.find('#Content ul li:lt(x)').each(function(){
						 	height += $(this).outerHeight()
						});
					
					$this
						.css('height', height + 'px')
						.attr('scrolling','auto');
					
					$if_con
						.find('html')
							.css('overflow-y','scroll');
				}
			}
			
			$this.contents().find('ul li:last').css('background-image','none');
			
			$this.contents().find('#Container').bind('fontsize_change', resize);
			resize();
		});

		
	//show map area
	//var $map_sel = $('#Content-bottom-right .section2 .section-common-main');
	show_map_area('#Content-bottom-right .section2 .section-common-main');
	
	// pickup
	var $section1 = $('#Content-bottom-left .section1');
	var show_items = 5;
	var total_items;
	var fade_time = 'slow';
	var $fontsize_change = $('#Header .header-others .fontsize-change li');
	
	$section1
		.find('.section-common-main iframe')
			.load(function(e){
				var $this = $(this);
				var $content = $this.contents().find('#Content');
				var $li = $content.find('.list li');
				total_items = $li.length;
				
				var j = 0;
				$li.each(function(i){
					if (i >= show_items) {
						if (j == 0) {
							$content.append('<ul class="list">');							
						}
						
						$content.find('.list:last').append($(this));
						
						j++;
						if (j == show_items) {
							j = 0;
						}
					}
				});
				
				// iframe resize height
				$this.contents().find('#Container').trigger('fontsize_change');
				
				$content
					.find('.list')
						.fadeOut(1,function(){
							/*$content
								.find('.list')*/
								$(this)
									.css({
										position:'absolute',
										top:0,
										left:0
									});
						})
						.end()
					.find('.list:first').fadeIn(1);
				
				var total_num = Math.ceil(total_items / show_items);
				var current_num = 1;
				
				var $current = $section1.find('.pagenum .current')
				$current.text(current_num);
				
				$section1.find('.pagenum .total').text(total_num);
				
				if (total_num > 1) {
					$section1
						.find('.paginate')
							.find('.prev')
								.click(function(){
									if (current_num != 1 && total_num != 1) {
										$content
											.find('.list:eq('+ (current_num-1) +')')
												.fadeOut(fade_time)
												.end()
											.find('.list:eq('+ (current_num-2) +')')
												.fadeIn(fade_time);
												
										current_num--;
										$current.text(current_num);
									}
								})
								.end()
							.find('.next')
								.click(function(){
									if (current_num != total_num) {
										$content
											.find('.list:eq('+ (current_num-1) +')')
												.fadeOut(fade_time)
												.end()
											.find('.list:eq('+ current_num +')')
												.fadeIn(fade_time);
										
										current_num++;
										$current.text(current_num);
									}
								});

				}
				
			});
	
	
	// shop infomation
	var $area_info = $('#Content-bottom-right .section2 .section-common-main .area-info');
	
	$('#Content-bottom-right .section2 .section-common-main .area-btn')
		.click(function(e){
			var classes = this.className.split(' ');
			for (var i in classes){
				if (classes[i].match(/^btn\-(.*)/)){
					var area_name = RegExp.$1;
					var data = area_data[area_name];
					var area_classname = 'show-area-' + area_name;
					
					$area_info
						.addClass(area_classname)
						.find('table tr td')
							.each(function(i){
								$(this).empty();
								
								if (data.table.length > i){
									$(this)
										.append(
											$('<a>')
												.attr('href', data.table[i].link)
												.text(data.table[i].txt)
										);
								}
							})
							.end()
						.find('.close-btn')
							.click(function(e){
								$area_info
									//.hide()
									.fadeOut(fade_time)
									.removeClass(area_classname);
							})
							.end()
						//.show();
						.fadeIn(fade_time);
					break;
				}
			}
		});
});
