/* vim: set tabstop=2 shiftwidth=2 foldmethod=marker: */
/**
 * @author      Shogo Kawase <shogo@arcstyle.jp>
 * @author      Yusuke Naito <yuk@arcstyle.jp>
 * @copyright   Arc Style Inc.
 * @version     SVN: $Id: public.js 19658 2011-10-22 10:03:33Z yuk $
 */
/** コンテナ **/
vw.public = {};

/** 初期化 **/
vw.public.init = function(j)
{
	// jQuery UI
	vw.include(j + '/lib/jquery/ui.min.js', 'body');
	
	// onhover 画像切り換え処理
	$('img,:image').filter('.enable_on_hover').each(function(){
		var normal = this.src;
		var img    = new Image();
		img.src    = normal.replace('normal', 'hover')
		$(this).hover(
			function(){ $(this).attr('src', img.src); },
			function(){ $(this).attr('src', normal); }
		);
	});
	
	// フォームの先頭要素に自動フォーカス
	$('form:not(.nofocus):first :text:first').focus();
	
	// スムーススクロール
	$('a[href^=#]').click(function() { 
		var href= this.hash; 
		var $target = $(href == '#_top' ? 'body' : href); 

		if ($target.size()) { 
			 var top = $target.offset().top; 
			 $($.browser.safari ? 'body' : 'html').animate({scrollTop:top}, 800, 'swing'); 
		} 
		return false; 
	}); 
};

/** 画像先読み **/
vw.public.imgLoad = function()
{
	var i, j = 0, a = arguments, x = [];
	for (i = a.length - 1; i >= 0; --i) {
		(x[j++] = new Image).src = a[i];
	}
};

/** 郵便番号検索 **/
vw.public.postal = function(key, url)
{
	var btn = $('<button type="button">検索</button>')
		.click(function(e){
			var zip = [
				$(key + '-zip-0').val(),
				$(key + '-zip-1').val()
			];
			if (isNaN(zip[0]) || isNaN(zip[1]) || zip[0].length < 3 || zip[1].length < 4) {
				alert('郵便番号は7桁全て正確に入力してください');
			} else {
				$.getJSON(url + '/postal/zipcode=' + zip.join('-') + '/_=' + (new Date).getTime(), function(json){
					if (!json.count) {
						alert('該当する住所が見つかりませんでした。');
					} else {
						var x = [key + '-pref-container', key + '-city-container', key + '-addr-container'].join(',');
						$(key + '-pref').val(json.pref_code);
						$(key + '-city').val(json.city_name);
						$(key + '-addr').val(json.town_name);
						$(x).effect('highlight', {color:'#FFD'}, 1500);
						vw.form.cursorMoveToEnd($(key + '-addr')[0]);
					}
				});
			}
			return false;
		})
	;
	$('<p class="postalBtn"></p>').append(btn).appendTo(key + '-zip-container');
};
/** キーワード検索のデフォルト値処理 **/
vw.public.rmCaptionFromKeyword = function()
{
	$('#text_keyword:not(.removed)').addClass('removed').val('');
};
vw.public.setCaptionToKeyword = function()
{
	var kw = $('#text_keyword');
	if (kw.val() == '') {
		$('#text_keyword').removeClass('removed').val('商品名・キーワードを入力してください');
	}
};
/** 会員登録 **/
vw.public.signup = {
		url:null,
		/*** init() ***/
		init:function(url){
			this.url = url;
			$('select[name=_corp-initial]').change(function(){
				vw.public.signup.changeShop();
			});
			this.changeShop();
		},
		/*** changeShop() ***/
		changeShop:function(){
			var ini  = $('select[name=_corp-initial]').val()
			var shop = $('select[name=_corp]').val();
			$.getJSON(this.url + '/signup/json?id=' + ini, function(json){
				var i, o ,s;
				s = $('#select-corp').get(0);
				$(s).empty().append('<option value="">▼選択して下さい</option>');
				i = 1;
				$.each(json, function (k, v){
					o = new Option(v, k);
					o.selected = (k == shop);
					s.options[i++] = o;
				});
			});
		}

};
/** jquery.ready **/
$(function(){
	/*** キーワード検索 ***/
	$('#form-search').submit(function() { vw.public.rmCaptionFromKeyword(); });
	$('#text_keyword')
		.click(function(){ vw.public.rmCaptionFromKeyword(); })
		.blur(function(){ vw.public.setCaptionToKeyword(); })
	;
	vw.public.setCaptionToKeyword();
	
	/*** サイドバーカテゴリ ***/
	$("#global-nav li").hover(function(){
		var t = $(this).addClass('active');
		clearTimeout(this._tim2);
		this._tim1 = setTimeout(function(){ t.find('ul:first').fadeIn(); }, 150);
	},function(){
		var t = $(this).removeClass('active');
		clearTimeout(this._tim1);
		this._tim2 = setTimeout(function(){ t.find('ul').hide(); }, 300);
	});
	
	/*** 商品画像の切替処理 ***/
	$('#item-pic-area .thumbs-box a').mouseover(function(){
		$('#itemMainPhotoLink').attr({href: this.href});
		$(this).find('img').each(function(){ $('#itemMainPhoto').attr({src: this.src}); });
	});
	
	/*** 注文関連 ***/
	vw.public._lastChecked = null;
	vw.public.onChangeDestination = function(dst){
		var x = vw.public._lastChecked;
		if (x == null) {
			//未選択
			$('#tbody-shop').hide();
			$('#tbody-name').hide();
			$('#tbody-detail').hide();
			$('#delivery_wish_form select').removeAttr('disabled');
			$('#payment-normal').hide().find(':radio').removeAttr('checked');
			$('#payment-shop').hide().find(':radio').removeAttr('checked');
		}
		x = $('input[name="delivery_method"]:checked').val();
		if (x == vw.public._lastChecked) {
			return false;
		}
		vw.public._lastChecked = x;
		if (x == 0) {
			// 自宅配送
			$('#tbody-shop').hide();
			$('#tbody-name').show();
			$('#tbody-detail').show();
			$('#delivery_wish_form').show();
			$('#delivery_wish_fixed').hide();
			$('#delivery_wish_form select').removeAttr('disabled');
			$('#payment-shop').hide().find(':radio').removeAttr('checked');
			$('#payment-normal').show();
			$('#pay_method-268443648').removeAttr('checked');
		} else {
			// 店舗配送
			$('#tbody-shop').show();
			$('#tbody-name').show();
			$('#tbody-detail').hide();
			$('#delivery_wish_form').hide();
			$('#delivery_wish_fixed').show();
			$('#delivery_wish_form select').attr('disabled', 'disabled');
			$('#payment-normal').hide().find(':radio').removeAttr('checked');
			$('#payment-shop').show();
			$('#pay_method-268443648').attr('checked', 'checked');
		}
		$('#tbody-shop')[x == 0 ? 'hide' : 'show']();
		$('#tbody-detail')[x == 0 ? 'show' : 'hide']();
	};
	var dst = $('input[name="delivery_method"]');
	if (dst.length && dst.is(':radio')) {
		vw.public._lastChecked = null;
		dst.click(function(){ vw.public.onChangeDestination(dst); });
		vw.public.onChangeDestination(dst);
	}
});

