/* vim: set tabstop=2 shiftwidth=2 foldmethod=marker: */
/**
 * @author      Shogo Kawase <shogo@arcstyle.jp>
 * @copyright   2007-2008 Arc Style Inc.
 * @version     CVS: $Id: public.js 12115 2010-04-12 02:43:08Z 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); }
		);
	});
	
	// 2重ポスト対策
	$('form').find(':submit,:image').attr('disabled', false);
	$('form')
		.submit(function(){ $(this).find(':submit,:image').attr('disabled', true); })
		.find(':submit,:image')
		.dblclick(function(){ return $(this).click() && false; })
	;
	
	// フォームの先頭要素に自動フォーカス
	$('form:not(.nofocus):first :text:first').focus();
};

/** 画像先読み **/
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)
{
	$('<a href="#">[住所検索]</a>')
		.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;
		})
		.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('Enter Title, Author, Publisher, or ISBN (Hyphen not required)');
	}
};
vw.public.checkKeyword = function(e)
{
	if ($('#text_keyword').val().length <= 0) {
		vw.public.setCaptionToKeyword();
		alert('検索条件を入力してください.\nPlease provide at least one search term.');
		e.preventDefault();
		e.stopImmediatePropagation();
	}
};
/** $.ready **/
$(function(){
	/*** 共通部分 検索フォーム ***/
	$('#form_search').submit(function(e) {
		vw.public.rmCaptionFromKeyword();
		vw.public.checkKeyword(e);
	});
	$('#text_keyword')
		.focus(function(){ vw.public.rmCaptionFromKeyword(); })
		.blur(function(){ vw.public.setCaptionToKeyword(); })
	;
	vw.public.setCaptionToKeyword();
	
	/*** 注文関連 ***/
	var dst = $('input[name="_destination"]');
	if (dst.length) {
		var v = null;
		var changeAddress = function(){
			if (v != dst.filter(':checked').val() - 0) {
				v = dst.filter(':checked').val() - 0;
				switch (v) {
					case 2:
						$("#form_orderInfo #select_addressbook").removeAttr('disabled');
						$('#form_orderInfo #newAddress').hide();
						break;
					case 3:
						$("#form_orderInfo #select_addressbook").attr('disabled','disabled');
						if (vw.userAgent.msie) {
							$('#form_orderInfo #newAddress').show();
						} else {
							$('#form_orderInfo #newAddress').slideDown();
						}
						break;
					case 1:
					default:
						$("#form_orderInfo #select_addressbook").attr('disabled','disabled');
						$('#form_orderInfo #newAddress').hide();
				}
			}
		}
		dst.click(function(){ changeAddress(); });
		changeAddress();
	}
	var pym = $('input[name="pay_method"]');
	if (pym.length) {
		vw.public.onChangePayMethod = function(){
			if (pym.filter(':checked').val() == 536879104) {
				if (vw.userAgent.msie) {
				$('#documentTable').show();
				} else {
					$('#documentTable').slideDown();
				}
			} else {
				$('#documentTable').hide();
			}
		};
		pym.click(function(){ vw.public.onChangePayMethod(); });
		vw.public.onChangePayMethod();
	}
	
	/*** 会員登録・更新フォーム ***/
	var ezn = $('input[name="option10"]');
	if (ezn.length) {
		vw.public.onChangeEzine = function(){
			if (ezn.filter(':checked').val() == 1) {
				$('#div-customer_option').slideDown();
			} else {
				$('#div-customer_option').hide();
			}
		};
		ezn.click(function(){ vw.public.onChangeEzine(); });
		vw.public.onChangeEzine();
	}
	
	
	/*** カテゴリープルダウン ***/
	if (vw.userAgent.msie) {
		var scState   = false;
		var scElement = $('#select_category1');
		if (vw.userAgent.version == 7) {
			scElement.mousedown(function(){
				if (scState = !scState) {
					scElement.width('auto');
				} else {
					setTimeout(function(){ scElement.width(120); }, 150);
				}
			}).blur(function(){
				setTimeout(function(){ scElement.width(120); scState = false; }, 50);
			});
		} else if (vw.userAgent.version == 6) {
			scElement.mouseover(function(){
				scElement.width('auto');
			}).click(function(){
				scState = !scState;
				if (!scState) $(this).blur();
			}).blur(function(){
				setTimeout(function(){ scElement.width(120); scState = false; }, 50);
			});
		}
	}
	$('a.remove').click(function(){
		return window.confirm('Are you sure remove this item from wish list?');
	});
});

