/* 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 10354 2009-10-29 05:45:52Z yuk $
 */
vw.public = {};

vw.public.postalSearch = function(url, key)
{
	document.write('<input type="button" name="postalSearchBtn" id="postalSearchBtn" value="検索" />');
	$('#' + key + '-row [name=postalSearchBtn]').bind('click', function(e){
		var zip = [$$(key + '-zip1').value, $$(key + '-zip2').value];
		if (isNaN(zip[0]) || isNaN(zip[1]) || zip[0].length < 3 || zip[1].length < 4) {
			return alert('郵便番号は7桁全て正確に入力してください');
		}
		var params = {zipcode: zip.join('-')};
		$.getJSON(url + '/postal/', params, function(json){
			if (!json.count) {
				alert('該当する住所が見つかりませんでした。');
			} else {
				$('#' + key + '-pref').val(json.pref_code);
				$('#' + key + '-city').val(json.city_name);
				$('#' + key + '-addr').val(json.town_name);
				$('#' + key + '-row').effect('highlight', {}, 1000);
				vw.form.cursorMoveToEnd($('#' + key + '-addr')[0]);
			}
		});
		return false;
	});
};

/** 問い合わせ注文 **/
vw.public.order = {
	init: function (type)
	{
		this.calc();
		if (!type) {
			this.switchDisabled();
		}
	},
	/*** 計算 ***/
	calc: function ()
	{
		var qty,up,car;
		var am = 0;
		$('.tr-item').each(function() {
			t   = $(this);
			qty = t.find('.qty').val() - 0;
			up  = t.find('.unitPrice').html().replace(',','') - 0;
			am += (qty * up);
			t.find('.subtotal').html(vw.util.numberFormat(qty * up));
		});
		
		// 合計金額が20,000円以上の場合は送料無料
		car = am >= 20000 ? 0 : 1050;
		$('#totalCarriage').html(vw.util.numberFormat(car));
		$('#totalAmount').html(vw.util.numberFormat(am + car));
	},
	/*** テーブル内の要素をdisabledを切り替える ***/
	switchDisabled: function()
	{
		$('#table-destination').find(':input').each(function (){
			f = $(this).attr('disabled') ? '' : 'disabled';
			$(this).attr({disabled:f}).css({background:(f ? '#ccc' : '')});
		});
	}
}

/*** 商品画像切り換え ***/
$(function() {
	$('.itemPhoto 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);
				$('.itemPhoto p a.thickbox').attr({href:this.href});
			}
		})
	;
});
