var IE67 = false;

if(navigator.appVersion.indexOf('MSIE 6',0) != -1){IE67 = true;}
if(navigator.appVersion.indexOf('MSIE 7',0) != -1){IE67 = true;}

debug = 1;

function c(val){
	if(navigator.userAgent.indexOf('MSIE',0) == -1 && debug){
		console.log(val);
	}
}

$(document).ready(function($){
	if(IE67){
		if($('#EntryTitle').html() != null){
			$('#contents h2').wrap('<div class="cornerWrap"></div>');
			$('#contents h2').css({'background':'url("/resource/images/bg_border_h2.gif") no-repeat top center','margin':'0','border':'none','padding-bottom':'0'}).parent().css({'background':'url("/resource/images/bg_border_h2.gif") no-repeat bottom center','padding':'0 0 10px','margin':'0 0 1em'});
		}else{
			$('#contents h2').corner('round 5px');
		}
	}
	
	//theme site url
	var theme = new Array(
		'http://jccu.coop/food-safety/',
		'http://jccu.coop/agri/',
		'http://jccu.coop/hidamari/',
		'http://jccu.coop/gender/',
		'http://jccu.coop/kurashi_tasukeai/',
		'http://jccu.coop/eco/',
		'http://jccu.coop/activity/',
		'http://jccu.coop/unicef/',
		'http://jccu.coop/international/',
		'http://jccu.coop/kodomo_land/',
		'http://jccu.coop/kensa/',
		'http://jccu.coop/peace/'
	);

	//theme site url
	var theme2 = new Array(
		'http://tabetai.coop/',
		'http://kidsclub.coop/',
		'http://no-trouble.coop/',
		'http://nenkin.coop/',
		'http://ikuji.coop/pc/',
		'http://goods.jccu.coop/',
		'http://mdinfo.jccu.coop/',
		'http://www.co-op.ne.jp/',
		'http://www.coopclean.co.jp/',
		'http://www.e-kurashi.coop/',
		'http://heart-ribbon.coop/',
		'http://www.coop-book.jp/',
		'http://coop-takuhai.jp/',
		'http://coop-travel.jccu.coop/',
		'https://product-inquiry.jccu.coop/'
	);

	$(".pager").append('<strong id="prev" class="disable">戻る</strong>');
	var count = 0;
	pager_init();
	
	function addExternal(selecter){
		$(selecter).each(function(){
			var url = $(this).attr('href');
			if( (url.search(/^http:\/\//) != -1 || url.search(/^https:\/\//) != -1 || url.search(".pdf") != -1) && url.search(/^http:\/\/test.jccu.coop/) == -1){
				if(url.search(/^http:\/\/jccu.coop/) == -1){
					if($(this).attr('class') != 'external'){
						$(this).addClass('external').append('<img src="/resource/images/icon_outside.gif" />').click(function(){
							window.open($(this).attr('href'));
							return false;
						});
						//console.log('external');
					}
				}
			}
		});
	}

	//外部サイト／PDFファイルの遷移方法を新規ウインドウに（テーマ別サイトは除外）

	if($('body.info').html() != null){
	//　最新情報
		addExternal('#contents .pager_section:visible a');
	
		
		$('#contents small a').each(function(){
			$(this).removeClass('external');
		});

		$('#nav_right .section a, .coop_action .inner a').each(function(){
			for(i=0;i<theme.length;i++){
				if( ( $(this).attr('href').search(/^http:\/\//) != -1 || $(this).attr('href').search(/^https:\/\//) != -1 || $(this).attr('href').search(".pdf") != -1) && $(this).attr('href').search(/^http:\/\/test.jccu.coop/) == -1){
  				if($(this).attr('href').search(/^http:\/\/jccu.coop/) == -1){
 					$(this).addClass('external');
					}
				}
				//テーマ別サイトURLが含まれる
				if( $(this).attr('href').search(theme[i]) != -1){
					$(this).addClass('external');
				}
			}
		});
	// 最新情報 以外
	}else{
		$('#contents a:not([rel=external]), #nav_right .section a, .coop_action .inner a').each(function(){
			//httpもしくは、httpsからはじまり、jccu.coopは含まれない、またはPDF
			if($(this).attr('href').search(/^http:\/\/jccu.coop/) == -1 && $(this).attr('href').search(/^http:\/\/test.jccu.coop/) == -1 && ( $(this).attr('href').search(/^http:\/\//) != -1 || $(this).attr('href').search(/^https:\/\//) != -1 || $(this).attr('href').search(".pdf") != -1)){
				$(this).attr('rel','target_blank');
			}
	
			for(i=0;i<theme.length;i++){
				//テーマ別サイトURLが含まれる
				if( $(this).attr('href').search(theme[i]) != -1){
					$(this).attr('rel','target_blank');				   
				}
			}
		});
	}

	//フォームの背景色
	$('#header input.input, #nav_right form .text').focus(function(){
		$(this).css('background','#fff');
	});

	$('#coda-slider-1').codaSlider({
		dynamicArrows: false,
		dynamicTabs: false,
		autoSlide: true,
		autoSlideInterval: 4000,
		autoSlideStopWhenClicked: true
	});

	/*//////////////////////////////////////////////////////
	//
	//	外部リンクアイコンは、.infoはclass="external"で表示。それ以外はrel="external"
	//	
	//	新規ウインドウ遷移は、.infoはclass="external"で判別、それ以外はrel="target_blank"もしくは、rel="external"で判別
	//
	*///////////////////////////////////////////////////////

	//外部リンク
	$('body:not(.info) a[rel="external"], a.external, a[rel="target_blank"]').click(function(){
		window.open($(this).attr('href'));
		return false;
	});


	//外部サイトアイコン設置
	//$('body.info a.external, body.mt-search-results dt a[rel="target_blank"], body:not(.info) a[rel="external"]').append('<img src="/resource/images/icon_outside.gif" />');
	$('body.mt-search-results dt a[rel="target_blank"]').append('<img src="/resource/images/icon_outside.gif" />');

	if($('body.info').html() != null){
		$('body#info_detail_index #contents #list dl:not(.pager_section) a.external, body#info_tag_index #contents a.external, body#info_index #contents a.external').append('<img src="/resource/images/icon_outside.gif" />');
	}else{
		$('a[rel="external"]').append('<img src="/resource/images/icon_outside.gif" />');
	}


	//ポップアップ
	$('a.popup').click(function(){
		window.open($(this).attr('href'),'mywindow1', $(this).attr('rel')+',menubar=no, toolbar=no, scrollbars=yes');
		return false;
	});

	//折りたたみ
	if($('#archive.section .inner ul li').length >= 4){
		var count = 0;
		$('#archive.section .inner ul li').each(function(){
			if(count < 6){
			}else{
				$(this).hide();
			}
			count++;
		});
	}


	$('#archive.section .more a').toggle(function(){
		$('#archive.section .inner ul li').show();
		$(this).html('折りたたむ');
		return false;
	},function(){
		var count = 0;
		$('#archive.section .inner ul li').each(function(){
			if(count < 6){
			}else{
				$(this).hide();
			}
			count++;
		});
		$(this).html('以前を表示する');
		return false;
	});

	//画像にキャプションを追加
	//srcにaltの語句が含まれていない（alt != src altが自動挿入ではない）
	$('.article img').each(function(){
		for(i=0;i<ary.length;i++){
			str = ary[i].url.replace('.JPG','');
			str = str.replace('.jpg','');
			str = str.replace('.gif','');
			str = str.replace('.GIF','');

			//検索にかかったimg要素のsrcが、配列のurlにマッチしていてdescもセットされていた場合
			if($(this).attr('src').indexOf(str)!= -1 && ary[i].desc != ''){
				$(this).parent().removeAttr('align');//IE対策
				if( $(this).css('text-align') == 'center'){
					if($(this).parent().attr('tagName') == "A"){
					//popupリンクが存在する場合、親のpタグは2階層上
						$(this).parent().parent().attr('align','center');//IE対策
					}else{
						$(this).parent().attr('align','center');//IE対策
					}
					
				}
				$(this).attr('rel',ary[i].desc);
				$(this).wrap('<div class="caption" style="width:'+$(this).attr('width')+'px"></div>');
				$(this).after('<small>'+$(this).attr('rel')+'</small>');	
			}else{
			}
		}
	});

	$('.article p img').each(function(){
		if($(this).css('float') == "left" || $(this).css('float') == "right"){
			$(this).parent().css({
				"min-height":$(this).height(),
				"height":"auto !important",
				"height":$(this).height()
			});
		}
	});


	//font-size
	if($.cookie('font_size')){
		$('#font ul').removeAttr('class');
		switch(Number($.cookie('font_size'))){
			case 0:
				//console.log('大');
				$('#font ul').addClass('large');
				$('link.fontsize').attr('href','/resource/css/font-large.css');
			break;
			case 1:
				//console.log('中');
				$('#font ul').addClass('medium');
				$('link.fontsize').attr('href','/resource/css/font-medium.css');
			break;
			case 2:
				//console.log('少');
				$('#font ul').addClass('small');
				$('link.fontsize').attr('href','/resource/css/font-small.css');
			break;
		}
	}else{
		//console.log('中');
		$('#font ul').addClass('medium');
		$('link.fontsize').attr('href','/resource/css/font-medium.css');
	}

	$('#font ul li').click(function(){
		$(this).parent().removeAttr('class');
		switch($(this).index()){
			case 0:
				//console.log('大');
				$(this).parent().addClass('large');
				$('link.fontsize').attr('href','/resource/css/font-large.css');
				$.cookie('font_size',0,{path:'/'});
			break;
			case 1:
				//console.log('中');
				$(this).parent().addClass('medium');
				$('link.fontsize').attr('href','/resource/css/font-medium.css');
				$.cookie('font_size',1,{path:'/'});
			break;
			case 2:
				//console.log('少');
				$(this).parent().addClass('small');
				$('link.fontsize').attr('href','/resource/css/font-small.css');
				$.cookie('font_size',2,{path:'/'});
			break;
		}
	});


	//pager
	
	function pager_init(){
		$("div#list dl").each(function(){
			if(count == 0){
				$(".pager").append('<span class="current">'+(count+1)+'</span>');
			}else if(count > 8){
				$(".pager").append('<span style="display:none">'+(count+1)+'</span>');
				$(this).hide();
			}else{
				$(".pager").append('<span>'+(count+1)+'</span>');
				$(this).hide();
			}
			count++;
		});
	}
	

	$(".pager").append('<strong id="next">次へ</strong>');
	if(count < 2){
		$(".pager #next").addClass('disable');
	}


	//clickevent
	$('span').click(function(){
		click_num(($(this).index()-1));
	});
	$('strong#prev').click(function(){
		if(1 < $('span.current').index()){
			click_num(Number($('span.current').index()-2));
		}
	});
	$('strong#next').click(function(){
		if($('span.current').index() < count){
			click_num(Number($('span.current').index()));
		}
	});

	//clone
	$('.pager_wrap_head .pager').clone(true).prependTo('.pager_wrap_foot');

	function click_num(num){
		$('span').removeClass('current');
		$('.pager span:eq('+num+')').addClass('current');
		
		//セクションの表示きりかえ
		$('.pager_section').hide();
		$('.pager_section:eq('+num+')').show();
		
		//表示する度に、外部サイトアイコンを表示
		addExternal('#contents .pager_section:visible a');

	
		disable_next(num+1);
		disable_prev(num+1);

		if(num > 8){
			$('.pager span:eq('+(num-9)+')').hide();
		}
		$('.pager span:eq('+(num+9)+')').hide();
		$('.pager span:eq('+(num)+')').show();


		$('.pager_wrap_foot .pager').remove();
		$('.pager_wrap_head .pager').clone(true).prependTo('.pager_wrap_foot');
	}

	function disable_next(num){
		if(num == count){
			$(".pager #next").addClass('disable');
		}else{
			$(".pager #next").removeClass('disable');
		}
	}

	function disable_prev(num){
		if(num != 1){
			$(".pager #prev").removeClass('disable');
		}else{
			$(".pager #prev").addClass('disable');
		}
	}
});

