jQuery.noConflict();

(function($){//BEGIN jQuery
$(function($){//BEGIN jQuery.ready()
	
	/*main, side*/
	var $main = $('#mainContents');
	var $side = $('#sidebar');
	
	
	
	/* 共通
	 ***********************************/
	
	
	/*gNav*/
	$('#gNavList')
		.pageInit({
			  '.site_index':	'> li:eq(0)'
			, '.seminar':		'> li:eq(1)'
			, '.qa':			'> li:eq(2)'
			, '.column':		'> li:eq(3)'
			, '.government':	'> li:eq(4)'
			, '.voice':			'> li:eq(5)'
		}, function(){
			$(this).addClass('active');
		})
		.chimg({autoActive:true});
	
	
	
	/*toPageTop*/
	$('div.toPageTop a', $main)
		.click(function(){
			$('html, body').animate({
				scrollTop: 0
			}, 500);
			return false;
		});
	
	
	/*IEonly: hr*/
	if($.browser.msie){
		$('hr', $main)
			.replaceWith('<div class="hr"></div>');
	}
	
	
	/*btn_bookmark_here*/
	$('#btn_bookmark_here')
		.click(function(){
			var url = location.href;
			var title = document.title;
			
			if(window.sidebar){ // firefox
				window.sidebar.addPanel(title, url, "");
			}else if(window.opera && window.print){ // opera
				var elem = document.createElement('a');
				elem.setAttribute('href', url);
				elem.setAttribute('title', title);
				elem.setAttribute('rel', 'sidebar');
				elem.click();
			}else if(document.all){// ie
				window.external.AddFavorite(url, title);
			}
			
			return false;
		});
	
	
	/*side_find_by_month_select*/
	$('#side_find_by_month_select')
		.change(function(){
			var $selected = $(this).find('option:selected');
			location.href = $selected.val();
		});
	
	
	/*entries_box*/
	(function($entries_box){
		var $entry_boxes = $entries_box.find('> div.entry_box');
		
		
		/*一覧ページ newアイコン*/
		(function($body){
			if(!$body.is('.index')) return;
			
			var $entry_box = $entry_boxes.eq(0);
			var $entry_title = $entry_box.find('h3.entry_title');
			var $new_icon = $('<img src="/_common/img/icon_new.gif" width="31" height="9" alt="NEW!" class="icon icon_new" />');
			
			$entry_title
				.append($new_icon);
			
			if($body.is('.site_index')){
				$('div.index_entry_list_box')
					.each(function(){
						var $entry_row = $(this).find('tr').eq(0);
						var $entry_title = $entry_row.find('> td.title');
						$entry_title
							.append($new_icon.clone());
					});
				
			}
		})($('body'));
		
		
		/*ページ送り*/
		(function(count){
			var limit = 8;
			var page_num_limit = 5;
			var current_class = 'current';
			if(count <= limit) return;
			
			var $prev_link = $('<li class="prev"><a href="#">&lt;</a></li>');
			var $next_link = $('<li class="next"><a href="#">&gt;</a></li>');
			
			var $page_nav = $('<div class="section page_nav_box"></div>');
			var $page_nav_list = $('<ol class="inline"></ol>').appendTo($page_nav);
			
			var page = {
				total: Math.ceil(count / 8)
				, current: 1
				, prev: 0
				, next: 2
				, block: 1
			}
			page.total_block = Math.ceil(page.total / page_num_limit);
			
			
			//update_page_info
			function update_page_info(current){
				page.current = current;
				page.prev = current - 1;
				page.next = current + 1;
				page.block =  Math.ceil(current / page_num_limit);
			}
			
			
			//cut
			function cut_entry_boxes(){
				var end = page.current * limit - 1;
				var start = end - limit + 1;
				
				$entry_boxes.show();
				$entry_boxes.filter([':lt(', start,')'].join('')).hide();
				$entry_boxes.filter([':gt(', end, ')'].join('')).hide();
			}
			
			
			//display
			var page_links = [];
			for(var i = 1; i <= page.total; i++){
				page_links.push(['<li num="', i, '"><a href="#">', i, '</a></li>'].join(''));
			}
			var $page_nums = $(page_links.join(''));
			$page_nav_list.append($page_nums);
			$entries_box.after($page_nav);
			var $current_num = $page_nums.filter([':eq(', page.current - 1,')'].join(''));
			$current_num.addClass(current_class);
			
			
			//ch_page_nums
			function ch_page_nums(block){
				var end = page.block * page_num_limit - 1;
				var start = end - page_num_limit + 1;
				
				$page_nums.show();
				$page_nums.filter([':lt(', start,')'].join('')).hide();
				$page_nums.filter([':gt(', end, ')'].join('')).hide();
				
				var $visible_nums = $page_nums.filter(':visible');
				if(page.block - 1){
					$visible_nums.filter(':first').before($prev_link.css('display', 'inline'));
				}else{
					$prev_link.hide();
				}
				if(page.block + 1 <= page.total_block){
					$visible_nums.filter(':last').after($next_link.css('display', 'inline'));
				}else{
					$next_link.hide();
				}
			}
			
			
			
			//attach
			$page_nums
				.click(function(ev){
					$current_num.removeClass(current_class)
					$current_num = $(ev.currentTarget);
					$current_num.addClass(current_class);
					
					var num = $current_num.attr('num');
					
					update_page_info(num);
					cut_entry_boxes();
					ch_page_nums(page.block);
					$('html, body').scrollTop(0);
					
					return false;
				});
			$prev_link
				.click(function(ev){
					ch_page_nums(--page.block);
					return false;
				})
			$next_link
				.click(function(ev){
					ch_page_nums(++page.block);
					return false;
				})
			
			
			//init
			cut_entry_boxes();
			ch_page_nums(page.block);
			
			
		})($entry_boxes.length);
		
		
	})($('#entries_box'));
	
	
	/* ページ単位
	 ***********************************/
	
	
	/* A-Form bugfix */
	(function($aform){
		if(!$aform.length) return;
		
		if($.browser.msie && $.browser.version < 7){
			var $form = $aform.find('form');
			var $dd = $form.find('> dd');
			var $title_dl = $form.find('> dl');
			$title_dl.after($dd.children());
		}
		
	})($('body.form div.aform'));
	
	
	/* フォーム(cgi)でのインクルード */
	(function($body){
		if(!$body.length) return;
		
		var inc_dir = [
			'/'
			, '_libs/view/_common/'
		].join('');
		var $inc_divs = $('div.include_php');
		
		$inc_divs.each(function(){
			var $inc_div = $(this);
			var inc_path = [inc_dir, $inc_div.attr('src'), '?type=js&basename=contact&template_name=other'].join('');
			
			$inc_div.load(inc_path, function(){
				$('#sidebar2 > div.side_twitter_box').hide();
			});
		});
		
		
	})($('#form-confirm'));
	
	
	/* related_boxの中身が無ければ非表示 */
	(function($body){
		if(!$body.length) return;
		
		var $related_box = $('div.related_box', $main);
		
		if(!$.trim($related_box.html())) $related_box.remove();
	})($('body.entry'));
	
	
	
});//END jQuery.ready()
})(jQuery);//END jQuery
