/* 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 11545 2010-02-26 20:31:11Z shogo $
 */
/** コンテナ **/
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="small-button2"></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('商品名・キーワードを入力してください');
	}
};
/** 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 a').mouseover(function(){
		$('#itemMainPhotoLink').attr({href: this.href});
		$(this).find('img').each(function(){ $('#itemMainPhoto').attr({src: this.src}); });
	});
	
	/*** 注文関連 ***/
	var dst = $('input[name="_destination"]');
	if (dst.length) {
		dst._lastChecked = null;
		vw.public.onChangeDestination = function(dst){
			var x = dst._lastChecked;
			if (x == dst.filter(':checked').val()) {
				return false;
			}
			x = dst._lastChecked = $('input[name="_destination"]:checked').val();
			if (x == 2) {
				$("#select_addressbook").removeAttr('disabled');
			} else {
				$("#select_addressbook").attr('disabled', 'disabled');
			}
			$('#newAddressTable,#newAddressRequired')[x == 3 ? 'slideDown' : 'slideUp']();
		};
		dst.click(function(){ vw.public.onChangeDestination(dst); });
		vw.public.onChangeDestination(dst);
	}
	var udw = $('input[name="use_delivery_wish"]');
	if (udw.length) {
		vw.public.onChangeUseDeliveryWish = function(){
			$('#delivery_wish_form')[(udw.filter(':checked').val() == 0) ? 'slideUp' : 'slideDown']();
		};
		udw.click(function(){ vw.public.onChangeUseDeliveryWish(); });
		vw.public.onChangeUseDeliveryWish();
	}
});
