/* 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 10473 2009-11-25 13:04:52Z yuk $
 */
/** コンテナ **/
vw.public = {};

/** 初期化 **/
vw.public.init = function(j)
{
	// jQuery UI
	vw.include(j + '/lib/jquery/jquery-ui-1.8.14.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); }
		);
	});
	
	// ツールチップ表示
	vw.toolTips.start();
};

/** 画像先読み **/
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 class="button zip" href="#"><span>検索</span></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')
	;
};

/** $.ready **/
$(function() {
	/*** 商品画像切り換え ***/
	$('.images ul.swapImageList a.thickbox')
		.each(function(){
			var img = new Image();
			img.src = this.rev;
		})
		.mouseover(function(){
			var img = $('#itemMainPhoto');
			if (img.attr('src') != this.rev) {
				img.hide().attr({src:this.rev}).fadeIn(500);
				$('#itemMainPhotoLink').attr({href:this.href});
			}
		})
	;
	
	/*** マイページ関連 ***/
	$('ul#mypageMenu').hover(function(){
		$('ul#mypageMenu li ul').show();
	}, function(){
		$('ul#mypageMenu li ul').hide();
	});
	
	/*** faq表示 ***/
	$('.faqContainer dt').each(function(){
		var t = $(this);
		t.click(function(){
			if (t.attr('rel') - 0) {
				t.next().slideUp();
				t.attr('rel', 0);
			} else {
				t.next().slideDown();
				t.attr('rel', 1);
			}
		});
	});
	
	/*** 注文関連 ***/
	var v = new Number();
	var changeAddress = function(){
		v = $('input[name="_destination"]:checked').val() - 0;
		switch (v) {
			case 1:
				$("#select-profile_id").attr('disabled','disabled');
				$('#table-destination').hide();
				break;
			case 2:
				$("#select-profile_id").removeAttr('disabled');
				$('#table-destination').hide();
				break;
			case 3:
				$("#select-profile_id").attr('disabled','disabled');
				$('#table-destination').slideDown();
				break;
			default:
				break;
		}
	}
	$('input[name="_destination"]').change(function(){
		changeAddress();
	});
	changeAddress();
	
	$('#form_input #text-total_expend_point').change(function(){
		var v = $('#cart-total_amount').val() - 0;
		var r = $('#cart-point_rate').val() - 0;
		$('#order-pay_method-container')[this.value * r == v ? 'hide' : 'show']();
		if (this.value * r == v) {
			$('#pay_method-268435472').attr('checked', 'checked');
		}
	});
	/**/
});


