
config.type = 'men';
stash.skip_logging = true;


BEYES.showRecommend = function() {
	$('#sbi_reco1')
		.css({
			display: 'block',
			height : 0
		})
		.animate({
			height: $.browser.msie ? 250 : 235
		}, 1000);
};


BEYES.setup = function() {
	var menu = BEYES.PickupMenu();
	menu.setup();
	menu.show();

	BEYES.Feature.setup();

	BEYES.QuickCart.append();
};


BEYES.Feature = {
	container: '#topFeatureMen',
	elements : [
		'#bnrFeatureReco',
		'#bnrFeatureTPU01',
		'#bnrFeatureTPU02',
		'#bnrFeatureTPU03',
		'#bnrFeatureTPU04',
		'#bnrFeatureHills',
		'#bnrFeatureSale'
	],
	template : '<a href="#{link}"><img src="#{img}" alt="" width="625" height="400"/></a>',

	setup: function() {
		var self = this;
		var image = feature_data.recommend.box[this.choose(feature_data.recommend_per)];

		var callback = function() {
			if (!timer) return;
			clearTimeout(timer);
			timer = null;
			self.show(image);
		};
		$('<img src="'+image.img+'"/>').load(callback);
		var timer = setTimeout(callback, 1000 * 5);
	},

	show: function(image) {
		var self = BEYES.Feature;
		$('#bnrFeatureReco')
			.css('background-color', '#FFF')
			.html(applyTemplate(self.template, image));

		$('a', self.elements.join()).mouseenter(function() {
			$('img', this)
				.stop()
				.animate({ opacity: 0.6 }, 120, function() {
					$(this).animate({ opacity: 1 }, 240);
				});
		});
	},

	choose: function(weight, ratio) {
		ratio = ratio || 0.01;
		var point = Math.random();
		for (var i = 0, m = 0, l = weight.length; i < l; i++) {
			var length = parseFloat(weight[i], 10) * ratio;
			if (point <= m + length)
				return i;
			m += length;
		}
	}
};


BEYES.PickupMenu = function() {
	var list = new BEYES.GenreList({
		criteria: {
			sortColumns: [
				{ column: 'Toppage', direction: 'Ascending' },
				{ column: 'SellingDate', direction: 'Descending' }
			],
			limit: 60
		}
	});

	return {
		setup: function() {
			BEYES.UnitList.update = function(p) {
				var genre = p && p.genre ? p.genre : null;

				var params = list.rebase({
					span    : null,
					criteria: {
						condition: {
							GenreId: genre,
							BrandId: null
						},
						offset   : 0
					}
				});
				$('#swfGenreSelector01').fl().setPageParam(params);
				$('#ctrlRefine01 .current a').text('すべて');
				$('#ctrlRefine02 > ul:eq(1) .current a').text(config.genre[genre] || 'すべて');
				$('#ctrlRefine03 .current a').text('すべて');
			};			
		},

		show: function() {
			var self = this;

			var s = list.settings;
			var c = s.queries.criteria;
			var condition = c.condition;
			$([
				'<div class="utility">',
				'<div class="labelRefine" id="labelRefine01">',
				'<p><img width="52" height="30" class="btn" alt="新着順" src="/img/men/top/txt_sort_newer_01.png"/></p>',
				'</div>',
				'<div class="labelRefine" id="labelRefine02">',
				'<p><img width="78" height="30" class="btn" alt="商品カテゴリ" src="/img/men/top/txt_prd_cate_01.png"/></p>',
				'</div>',
				'<div class="labelRefine" id="labelRefine03">',
				'<p><img width="78" height="30" class="btn" alt="人気ブランド" src="/img/men/top/txt_popular_brand_01.png"/></p>',
				'</div>',

				'<div class="ctrlRefine" id="ctrlRefine01">',
				'<p class="label"><img width="32" height="10" alt="新着順" src="/img/men/top/txt_sort_newer_01_cr.png"/></p>',
				'</div>',
				'<div class="ctrlRefine" id="ctrlRefine02">',
				'<p class="label"><img width="58" height="10" alt="商品カテゴリ" src="/img/men/top/txt_prd_cate_01_cr.png"/></p>',
				'</div>',
				'<div class="ctrlRefine" id="ctrlRefine03">',
				'<p class="label"><img width="58" height="10" alt="人気ブランド" src="/img/men/top/txt_popular_brand_01_cr.png"/></p>',
				'</div>',

				'<div class="ctrlNumber01">',
				'<p class="label"><img width="30" height="10" alt="表示数" src="/img/men/top/txt_disp_number_01.png"/></p>',
				'</div>',
				'</div>',

				'<div id="swfGenreSelector01"/>'
			].join(''))
				.appendTo('#refineUnit01')
				.find('.btn').roll().end()
				.find('#labelRefine01 img').click(function() {
					$('.ctrlRefine').hide();
					$('#ctrlRefine01') .css('left', 499).fadeIn();
					$('#labelRefine01').css('left', 499);
					$('#labelRefine02').css('left', 665);
					$('#labelRefine03').css('left', 743);
				}).click().end()
				.find('#labelRefine02 img').click(function() {
					$('.ctrlRefine').hide();
					$('#ctrlRefine02') .css('left', 415).fadeIn();
					$('#labelRefine01').css('left', 362);
					$('#labelRefine02').css('left', 415);
					$('#labelRefine03').css('left', 743);
				}).end()
				.find('#labelRefine03 img').click(function() {
					$('.ctrlRefine').hide();
					$('#ctrlRefine03') .css('left', 628).fadeIn();
					$('#labelRefine01').css('left', 497);
					$('#labelRefine02').css('left', 550);
					$('#labelRefine03').css('left', 628);
				}).end()
				.find('#ctrlRefine01').pulldown(
					['すべて', '1週間前', '2週間前', '3週間前', '4週間前', '2ヶ月前', '3ヶ月前'],
					null,
					s.queries.span || null,
					function(n) {
						list.rebase({
							span    : n || null,
							criteria: {
								condition: { BrandId: null },
								offset   : 0
							}
						});
						$('#ctrlRefine03 .current a').text('すべて');
					}
				).end()
				.find('#ctrlRefine02')
					.pulldown(
						['すべて', 'TOPS', 'INNER', 'BOTTOM', 'BAG', 'SHOES', 'ACCESSORIES', 'INTERIOR', 'HOBBY'],
						null,
						condition.MenCategory || null,
						function(n) {
							list.rebase({
								span    : null,
								criteria: {
									condition: {
										MenCategory: n || null,
										BrandId    : null
									},
									offset   : 0
								}
							});
							$('#ctrlRefine01 .current a').text('すべて');
							$('#ctrlRefine03 .current a').text('すべて');
						}
					)
					.find('.select').css('margin-right', 27).end()
					.pulldown(
						['すべて', 'ドレス', 'カジュアル', 'バッグ＆シューズ＆アザーズ'],
						null,
						[0, 0, 0, 1, 0, 2, 3][condition.GenreId] || null,
						function(n) {
							var genre_id = [0, 3, 5, 6][n];
							var params = list.rebase({
								span    : null,
								criteria: {
									condition: {
										GenreId: genre_id || null,
										BrandId: null
									},
									offset   : 0
								}
							});
							$('#swfGenreSelector01').fl().setPageParam(params);
							$('#ctrlRefine01 .current a').text('すべて');
							$('#ctrlRefine03 .current a').text('すべて');
						}
					)
					.end()
				.find('#ctrlRefine03').pulldown(
					getKey('すべて', config.menPopularBrand),
					null,
					$.inArray(condition.BrandId, getValue(config.menPopularBrand)) + 1 || null,
					function(n) {
						list.rebase({
							span    : null,
							criteria: {
								condition: {
									BrandId: getValue(config.menPopularBrand)[n - 1] || null
								},
								offset   : 0
							}
						});
						$('#ctrlRefine01 .current a').text('すべて');
					}
				).end()
				.find('.ctrlNumber01').pulldown(
					config.limits,
					null,
					String(c.limit || config.limits[0]),
					function() {
						list.rebase({
							criteria: {
								offset: 0,
								limit : Number($(this).text())
							}
						});
					}
				).end();

			$('#swfGenreSelector01').fl({
				src             : '/swf/src/pickupItemsBar.swf',
				width           : 945,
				height          : 37,
				flashvars       : parseQuery(),
				expressInstaller: null
			});

			list.show();
		}
	};
};
