var App = {
	default_img_main: '',
	$img_main: '',
	init: function() {
		if($('#lookbook').length) {
			LB.init();
		}
		App.$img_main = $('#img-main');
		App.default_img_main = App.$img_main.attr('src');
		App.bind();
		App.layout();
		App.news();
	},

	bind: function() {
		// 共通
		$('#contact .inner-contact h5').click(function() {
			$(this).parent().next().find('.inner-contact').toggle();
		});
		$('a[rel=external]').live('click',function() {
			window.open(this.href,'');
			return false;
		});

		// トップ
		$('#brand-image div img').hover(function() {
			var h = parseInt($(this).height());
			$('#brand-image').animate({
					'height': h + 'px'
			},{
				duration: 300
			});
		}, function() {
			$('#brand-image').animate({
					'height': '300px'
			},{
				duration: 300,
				complete: function() {
					$('#brand-image span').height('300px');
				}
			});
		});

		// 商品一覧
		$('ul.itemlist li a').mousemove(function(e) {
			t = (e.pageY - 30) + 'px';
			l = (e.pageX + 20) + 'px';
			$(this).find('span').css({'top':t,'left':l});
		});
		
		// 商品詳細
		$('#item-imagebox .item-thumb img').hover(function() {
			App.swap_image(this);
		}, function() {
			App.swap_image();
		});
		
		// 画像右クリック禁止
		$('img').bind('contextmenu', function() {
			return false;
		});
	},

	layout: function() {
		$('#bio #inner-bio .section').autoHeight();
	},

	swap_image: function(el) {
		if(el) {
			App.$img_main.attr('src', $(el).attr('title'));
		} else {
			App.$img_main.attr('src', App.default_img_main);
		}
	},

	news: function() {
		var feed_url = $('#feed').html();
		if(feed_url) {
			initialize(feed_url);
			$('#feed').show();
		}
	},

}

LB = {
	current: 0,
	pitch: 311,
	$lookbook: '',
	$imgs: '',
	brand_code: '',
	init: function() {
		LB.$lookbook = $('#lookbook');
		LB.$imgs = $('#imgs');
		LB.bind();
		LB.brand_code = $('#imgs').html();
		var script_name = '/files/lookbook/' + LB.brand_code + '/data.js';
		$.getScript(script_name, function() {
			LB.render();
		});
	},
	bind: function() {
		$('#lookbook ul#nav-pn li').click(function() {
			LB.move($(this).attr('class'));
		});
	},
	render: function() {
		LB.$imgs.html('');
		var w = 0;
		for(i=0; i<items.length; i++) {
			$img = $('<img/>')
				.attr('src','/files/lookbook/' + LB.brand_code + '/img/' + items[i]['image']);
			$('#imgs').append($img);
			w += $img.width() + 20;
		}
		LB.$imgs.css('width',w);
	},
	get_position: function() {
		var left = $('#imgs').css('left');
		return parseInt(left.replace('px',''));
	},
	move: function(dir) {
		var l = LB.get_position();
		if(dir == 'nav-p') {
			l += LB.pitch;
		} else if(dir == 'nav-n') {
			l -= LB.pitch;
		}
		if(l < LB.$lookbook.width() - LB.$imgs.width()) {
			l = LB.$lookbook.width() - LB.$imgs.width();
		} else if(l > 0) {
			l = 0;
		}
		LB.$imgs.animate({
				'left': l,
			}, {
				duration: 150,
			});
		if(l == 0) {
			$('#nav-pn .nav-p a').attr('class','dead');
		} else {
			$('#nav-pn .nav-p a').attr('class','');
		}
		if(l == LB.$lookbook.width() - LB.$imgs.width()) {
			$('#nav-pn .nav-n a').attr('class','dead');
		} else {
			$('#nav-pn .nav-n a').attr('class','');
		}
	}
}

$().ready(function() {
	App.init();
});

