/*エファジャパン*/

//デバッグ時のエラー回避（IE6～8）
if (!window.console) {
	window.console = {
		log : function () {},
		info : function () {},
		warn : function () {},
		error : function () {},
		time : function () {},
		timeEnd : function () {}
	};
};



jQuery(function() {
		
	//■レイアウト微調整
	jQuery('#header1_navi li:first').addClass('first');
	jQuery('#header1_navi li:last').addClass('last');
	
	jQuery('#sidebar_navi1 img:last').addClass('last');
	jQuery('#sidebar_navi1 > li > ul:last').addClass('last');
	jQuery('#sidebar_navi1 > li > ul').each(function() {
		jQuery('> li:last', this).addClass('last');
	});
	
	jQuery('#footer_navi li:first').addClass('first');
	
	if (jQuery('#content .entry_content :first').is('h2')) {
		jQuery('#content .entry_content :first').css('margin-top', 0);
	}
	
	
	
	//■フッターの西暦「xxxx-」を「xxxx-現在の西暦」に書き換える
	(function() {
		var elem = '#footer_copy';
		var year = new Date().getFullYear();
		var str = jQuery(elem).text();
		jQuery(elem).text(str.replace(/([0-9]{4}\-)/, '$1' + year));
	})();
	
	
	
	//■サイドナビ現在地表示
	//imgタグにunbindを使用しても良いかも
	(function() {
		var id = '#sidebar_navi1';
		var current = jQuery(id).attr('class');
		if (current != '') {
			jQuery('.' + current + ' img', id).removeClass('swapimg').attr('src', function() {
				return this.src.replace(/^(.+)(\.[a-z]+)$/, '$1_ov$2');
			});
			jQuery('.' + current + ' ul', id).css('display', 'block');
		}
	})();
	
	
	
	//■ノーマルロールオーバー
	jQuery('.swapimg').each(function() {
		this.oSrc = jQuery(this).attr('src');
		this.rSrc = this.oSrc.replace(/^(.+)(\.[a-z]+)$/, '$1_ov$2');
		jQuery('<img>').attr('src', this.rSrc); //プリロード
	}).hover(function() {
			this.src = this.rSrc;
	},function() {
			this.src = this.oSrc;
	});
	
		
	
	//■ページ内スクロール
	jQuery('.scrollTo').click(function() {
		jQuery.scrollTo(jQuery(this).attr('href'), 400, {margin:true});
		return false;
	});
	
	
	//■<table>のヘッダの幅を揃える
	(function() {
		var max = 0;
		jQuery('.align_col').each(function() {
			var size = jQuery('th:first-child', this).css('width').split('px')[0];
			if (parseInt(size) > parseInt(max)) {
				max = size;
			}
		});
		jQuery('.align_col th').css('width', max + 'px');
	})();
	
	
	
	//■フォーム表示切替
	function checkFormType(cookieName)
	{
		var cookieVal = jQuery.cookie(cookieName);
		if (cookieVal && cookieVal != '0') {
			jQuery('#form .section:eq(' + (cookieVal - 1) + ')').show();
			jQuery('#form .stage:eq(' + (cookieVal - 1) + ')').show();
			jQuery('#form .payment_method:eq(' + (cookieVal - 1) + ')').show();
			jQuery('#form #formtype option:eq(' + cookieVal + ')').attr('selected', 'selected');
		}
	};
	
	function changeFormType(cookieName, optionVal) {
		var cookieVal = jQuery.cookie(cookieName);
		if (optionVal != '0') {
			if (optionVal != cookieVal) {	
				
				if (jQuery('#form .section:eq(' + (cookieVal - 1) + ')')[0]) {
					jQuery('#form .section:eq(' + (cookieVal - 1) + ')').fadeOut(600, function() {
						jQuery('#form .section:eq(' + (optionVal - 1) + ')').fadeIn(600);
					});
				} else {
					jQuery('#form .section:eq(' + (optionVal - 1) + ')').fadeIn(600);
				}
				
				if (jQuery('#form .stage:eq(' + (cookieVal - 1) + ')')[0]) {
					jQuery('#form .stage:eq(' + (cookieVal - 1) + ')').fadeOut(600, function() {
						jQuery('#form .stage:eq(' + (optionVal - 1) + ')').fadeIn(600);
					});
				} else {
					jQuery('#form .stage:eq(' + (optionVal - 1) + ')').fadeIn(600);
				}
				
				if (jQuery('#form .payment_method:eq(' + (cookieVal - 1) + ')')[0]) {
					jQuery('#form .payment_method:eq(' + (cookieVal - 1) + ')').fadeOut(600, function() {
						jQuery('#form .payment_method:eq(' + (optionVal - 1) + ')').fadeIn(600);
					});
				} else {
					jQuery('#form .payment_method:eq(' + (optionVal - 1) + ')').fadeIn(600);
				}
			
			}	
		} else {
			jQuery('#form .section, #form .stage, #form .payment_method').fadeOut(600);
		}
		jQuery.cookie(cookieName, optionVal, {expires:30, path:'/'});
	};
	
	if (jQuery('#form.joining')[0]) {
		checkFormType('joining_type');
	}
	if (jQuery('#form.donation')[0]) {
		checkFormType('donation_type');		
	}
	if (jQuery('#form.fundraising')[0]) {
		checkFormType('fundraising_type');		
	}
	jQuery('#form.joining #formtype').change(function() {
		changeFormType('joining_type', jQuery(this).val());
	});	
	jQuery('#form.donation #formtype').change(function() {
		changeFormType('donation_type', jQuery(this).val());
	});	
	jQuery('#form.fundraising #formtype').change(function() {
		changeFormType('fundraising_type', jQuery(this).val());
	});		
	
	
	
	
	//■エファグッズ
	if (jQuery('#goods #aaa input').is(':checked')) {
		jQuery('#bbb').show();
	}
	jQuery('#goods #aaa input').change(function() {
		if (jQuery(this).is(':checked')) {
			jQuery('#bbb').fadeIn(600);
		} else {
			jQuery('#bbb').fadeOut(600);
		}
	});	

	
	
	
	
	
	
	//■最近の活動レイアウト調整（トップ）
	jQuery('body.home .entry_excerpt').hide(0);
	jQuery('body.home .entry_excerpt img').removeAttr('height').attr('width', '160');
	jQuery('body.home .entry_excerpt .wp-caption').width('160px');
	jQuery('body.home .entry_excerpt .wp-caption-text').css('padding-right', 0);
	jQuery('body.home .entry_excerpt').show(0);
	jQuery('body.home .entry_excerpt .more-link').after('）');
	jQuery('body.home .entry_excerpt .more-link').before('・・・（');
	
	
	
	//■印刷ボタン
	jQuery('button.print').click(function() {
		print();
	});
	
	
	
	//エファ通信バックナンバー
	jQuery('.category-3 #info_backnumber_past select').change(function() {
		var val = jQuery(':selected:first', this).val();
		if (val != '') {
			window.open('wp-content/uploads/other/tushin' + val + '.pdf');
		}
	});
	
	
	/*
	(function() {
		if (jQuery('body.page-id-3222')[0]) {
			jQuery('#content .section1, #content .section2,').css('display', 'none');
			var set_date = new Date(2011, 1 - 1, 1, 0, 0, 0);
			var now_date = new Date();
			if (set_date <= now_date) {
				jQuery('#content .section2').css('display', 'block');
			} else {
				jQuery('#content .section1').css('display', 'block');
			}
		}
	})();
	*/
	
});



//■ページ読み込み完了後に実行
jQuery(window).load(function() {
	
	//■このファイル置いてあるディレクトリを調べる
	var this_url;
	var parent_this_url;
	this_url = jQuery('script[src$="/basic.js"]')[0].src;
	this_url = this_url.slice(0, this_url.lastIndexOf('/') + 1);
	parent_this_url = this_url.slice(0, this_url.lastIndexOf('/'));
	parent_this_url = parent_this_url.slice(0, parent_this_url.lastIndexOf('/') + 1);
	parent_this_url = parent_this_url + 'json';
	
	//■住所自動入力読み込み
	if (location.href.indexOf('/form/') != -1 || location.href.indexOf('mode=buy') != -1) {
		jQuery.ajax({
			type: 'get',
			url: this_url + 'ajaxzip2.js',
			dataType: 'script',
			scriptCharset: 'utf-8',
			success: function() {
				AjaxZip2.JSONDATA = parent_this_url;
			}
		});
	}
		
	
	
	//■スタッフブログのfeedの読み込みと表示（トップページ）
	if (jQuery('body.home #sub .staffblog')[0]) {
		var gFeed = new google.feeds.Feed('http://efa-japan-staffblog.seesaa.net/index20.rdf');
		gFeed.setNumEntries(10);
		gFeed.load(function(result) {
			if (!result.error) {
				var entry = result.feed.entries[0];
				var html = '\n<div class="entry">\n';
				html += '<p class="entry_title"><a href="' + entry.link + '">' + entry.title + '</a></p>\n';
				html += '<p class="entry_body">' + entry.contentSnippet.slice(0, 120) + '・・・（<a href="' + entry.link + '">続きを読む</a>）</p>\n';
				var pDate = new Date(entry.publishedDate);
				html += '<p class="entry_date">' + pDate.getFullYear() + '年' + (pDate.getMonth() + 1) + '月' + pDate.getDate() +'日</p>\n';
				html += '</div>\n';
				jQuery('body.home #sub .staffblog').append(html);
			}
		});
	}
});




