/* bda common js */
$(document).ready(ready);
jQuery.event.add(window, "load",loaded);

	/*
	* DOM読込み時に実行
	*/
	function ready(){
	

		//pagetop設定
		$('a.pagetop').pagetopScroll();
		
		//rollover
		$('.ro').setRollover();
		
		//rollover
		$('.ro2').setRollover2();
		
		//target _blank
		$('a.blank').setTargetBlank();
		
		$(this).pageLink();
				
	}

	/*
	* 画像など全て読込み時に実行
	*/
	function loaded(){
	
	
		
	}

/* -----------------------------original plugins for jquery --------------------------------- */
(function($){


	/**
	*  target_blank
	*/
	$.fn.setTargetBlank = function(){
		this.attr("target","_blank");
/*
		this.click(function(){
//			open($(this).attr("href"));
			return false;
		});
*/
		return this;
	};

	/*
	* switchDisplay
	*/
	$.extend({
		switchDisplays:[]
	});
	
	$.fn.switchDisplay = function(selectTitle,selectBody){
		this.each(function(){
			var obj = {
				id:$.switchDisplays.length
			};
			var $titles = $(selectTitle,$(this));
			var $bodys = $(selectBody,$(this));
			var err = false;
			if($titles.size()!=$bodys.size())	err =true;
			if(err){
				alert('要素の数がいっちしません');
				return;			
			}
			
			$bodys.hide();		

			$titles
			.css({'cursor':'pointer'})
			.each(function(i){
				$($bodys[i]).addClass("switchDisplay"+obj.id+"-"+i);
				var $title = $(this);
				$title
				.attr("rel",obj.id+"-"+i)
				.unbind('click')
				.click(function(){
					var id = $(this).attr("rel");
					var $target = $(".switchDisplay"+id);
					if($target.css('display')=='none'){
						$target.fadeIn(500);
					}else{
						$target.fadeOut(100);
					}
					return false;
				});

			});
			$.switchDisplays.push(obj);
		});
		return this;
	};
	
	/*
	* popup
	*/
	$.fn.popup = function(w,h,t,l){
		$.extend({
			popup:{
				width:w,
				height:h,
				top:t,
				left:l
			},
			popupWin:0
		});
		this.each(function(){
			$(this).click(function(){
				var $a = $(this);
				var url = $a.attr("href");
				if($.popupWin){	if(!$.popUpWin.closed) $.popUpWin.close();}
				$.popUpWin = open(url,'popUpWin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+w+',height='+h+',left='+l+', top='+t+',screenX='+l+',screenY='+t+'');	
				return false;
			});
		});
	};
	


	/*
	* pageLink
	*/
	$.fn.pageLink = function(){
		
		var $links = $('a[href^=#], area[href^=#]',$(this),this).not('a[href=#], area[href=#]');
		$links.click(function(e){
			var $target = $($(this).attr("href"));
			var targetY = $target.position().top;
			$('html,body').animate({ scrollTop: targetY },500);
			return false;
		});	
	};

	/*
	* pagetopScroll
	*/
	$.fn.pagetopScroll = function(){
		this
		.attr("href","")
		.click(function(){ $('html,body').animate({ scrollTop: 0 },500); return false;});
	};


	/*
	* tab
	*/
	$.fn.tab = function(){
		this.each(function(){
			$(".tab_area_contant div",this).hide();
			var $navis = $(".tab_area_navi a",this);
			$navis.each(function(){
				$(this)
					.unbind("click")
					.click(function(){
						var $target = $($(this).attr("href"));
						$target.fadeIn(300);
						$target.prevAll().hide();
						$target.nextAll().hide();
						return false;				
					});
			});
			$($navis[0]).trigger('click');
		});
	};
	
	/*
	* フォームのロック　作りかけ
	*/
	$.fn.overlay = function(){
		$.extend({overlay:false});
		$.extend({overlayClear:function(){
			if($.overlay){
				$.overlay.fadeOut(500,function(){ $(this).remove(); });
			}
		}});
				
		this.each(function(){
			$(this).click(function(){
				if(!$.overlay){
					var $overlay = $('<div id="overlay"></div>');
						$overlay
							.css({
								"position":"absolute",
								"opacity":"0.2",
								"background-color":"#000",
								"width":($(window).width())+"px",
								"height":($(window).height())+"px",
								"text-align":"center",
								"font-size":"30px",
								"padding":"100px",
								"display":"none",
								"cursor":"pointer",
								"top":($(window).scrollTop())+"px"	
							})
							.fadeIn(200)
							.click($.overlayClear);
						$("body").before($overlay);
						$(window).scroll(function(){
								 $("#submitLock").css({"top":($(window).scrollTop())+"px"});
						});
					$.overlay = $overlay;
				}
			});
		});
		return this;
	};

	/*
	* HTMLコードを表示する。( &lt; , &gt;への置換)
	*/
	$.fn.setHtmlCode = function(){
		this.each(function(){
			var code = $(this).html();
			code = code.replace(/</g,"&lt;");
			code = code.replace(/>/g,"&gt;");
			code = code.replace(/$\n/g,"");
			code = code.replace(/$\n/g,"");
			code = "<pre>"+code+"</pre>";
			$(this).html(code);
		});
		return this;
	
	};
	
	/*
	* フォントサイズの変更
	*/	
	$.fn.fontSizeChanger = function(){
	
		$.extend({
			fontSize : new function(){
				this.size = {
					's':'0.8em',
					'm':'1em',
					'l':'1.3em'
				};
				this.setFontSize = function(size){
					$.cookie("fontSize",size,{expires:14,path:'/'});
					$("#container").css("fontSize",size);
				};
			}
		});
		$('li',this).css("cursor","pointer");
		$('li.l',this).click(function(){	$.fontSize.setFontSize($.fontSize.size.l);	});
		$('li.m',this).click(function(){	$.fontSize.setFontSize($.fontSize.size.m);	});
		$('li.s',this).click(function(){	$.fontSize.setFontSize($.fontSize.size.s);	});
		if(!$.cookie('fontSize')){
			$.fontSize.setFontSize($.fontSize.size.m);
		}
		$.fontSize.setFontSize($.cookie('fontSize'));
		return this;
	};
		
	/*
	* シンプルロールオーバー
	*/
	$.fn.setRollover = function(){
		this.css("cursor","pointer")
			.hover(
				function(){	$(this).css("opacity",0.5); },
				function(){	$(this).css("opacity",1); }
			)
		return this;
	};
	
	/*
	* シンプルロールオーバー の　2
	*/
	$.fn.setRollover2 = function(){
		this.children()
			.css("cursor","pointer")
			.hover(
				function(){
					$(this).parent().children().css("opacity",0.5);
					$(this).css("opacity",1);
				},
				function(){
					$(this).parent().children().css("opacity",1);
				}
			);
		return this;
	};
	
	/*
	* cookieのチェック
	*/	
	$.extend({
		isCookie:function(){
			$.cookie("DUMMY",0);
			$.cookie("DUMMY",1);
			return $.cookie('DUMMY');  
		}
	});
	
	/*
	* 連続文字改行
	*/
	$.fn.wordbreak = function(){
		this.sep = String.fromCharCode(8203);
		var self = this;
		if($.browser.msie){	//ieはcssで対応
			$(this).css('word-break', 'break-all');
		}else{				//etc は&#8203;を１文字ごとに挿入
			$(this).each(function(){
				$(this).text(this.textContent.split('').join(self.sep).replace(/^\s+|\s+$/g,""));
			});
		}
		return this;
	};
}(jQuery));
