﻿/**---------------------------------
 * common.js
 * 
 * ...
 * author	: takaaki koyama
 *
 * @use jQuery 1.2.6 later
 ---------------------------------*/
$(document).ready(function(){
//rollover	
// switching image xxx_off.xxx -> xxx_on.xxx
// if image name is bnr_xxxx or btn_xxx which don't has neme _off , _on
// fade effect on mouse over.
	$("a img[src*='_on']").addClass("current");
	
	$("img").live("mouseover",function(){
		if($(this).hasClass('current')) return
		if ($(this).attr("src").match(/_off./)){
			$(this).attr("src",$(this).attr("src").replace("_off.", "_on."));
			return;
		}
	});

	$("img:not('.current')").live("mouseout",function(){
		if($(this).hasClass('current')) return
		if ($(this).attr("src").match(/_on./)){
			$(this).attr("src",$(this).attr("src").replace("_on.", "_off."));
			return;
		}
	});
	
	//preload images
	var images = [];
	$("img").each(function(index){
			if($(this).attr("src").match(/_off./)){
				 images[index]= new Image();
				 images[index].src = $(this).attr("src").replace("_on.", "_off.");
			}
	});
	
// pingfix
	//$("*").ifixpng();

//news scoller
	var no_cache = (new Date()).getTime().toString();
	$("#head_line_news dd").load("/common/data/news_data.html?_="+no_cache+" ul",{},function(){
		$("#head_line_news ul").liScroll({travelocity: 0.02});
	}).css({"overflow":"hidden","position":"relative"});


//CSS3 selector 
	//last-child
	$("#article li:last-child,"+
	  "#article dt:last-child,"+
	  "#article dd:last-child,"+
	  "#article tr:last-child,"+
	  "#article .main .section:last-child").addClass("last-child");
	
	//nth-child
	$("#article ol,#article ul").each(function(){
			$(this).children("li").each(function(index){
				$(this).addClass("n"+(index+1)+"th-child");
			});
	});
	$("#article dl").each(function(){
			$(this).children("dt").each(function(index){
				$(this).addClass("n"+(index+1)+"th-child");
			});
			$(this).children("dd").each(function(index){
				$(this).addClass("n"+(index+1)+"th-child");
			});
	});
	//:even, :odd
	$("#article ol li:even,#article ul li:even,#article dl dt:even,#article dl dd:even").addClass("even");
	$("#article ol li:odd,#article ul li:odd,#article dl dt:odd,#article dl dd:odd").addClass("odd");
	
// popup
// class="popup400x600" -> window.open(this.href,"popup","width=400,height=600,...)
	$("a[class^='popup']").click(function(){
		if($.browser.safari ){
			window.open(this.href,"_blank");
			return false;
		}
		var className = $(this).attr("class").match(/^popup([0-9]{1,})x([0-9]{1,})/) ;	
		var width = RegExp.$1;
		var height = RegExp.$2;
		var state = "";
		var notHasSize = "yes"
		if(width!=null && height !=null){
			state += "width="+width+",height="+height+",";
			notHasSize = "no"
		}
		state += "location="+notHasSize+",toolbar="+notHasSize+",directories="+notHasSize+",";
		state += "status=yes,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes";
		window.name = "root";
		window.open(this.href,"popup"+(new Date()).getTime().toString(),state);
		return false;
	});
	// open in popup parent window.
	// add class="openParentWin" on a-tag in a popup window.
	$("a.openParentWin").click(function(){
		window.open(this.href,"root");
		return false;
	});
	
// target _blank auto add
	var domains = [document.domain,"groupsitedomains"];
	var domain_selector = ""
	var left_str= ":not([href^='http://";
	var left_str_https= ":not([href^='https://";
	var right_str = "'])";
	domain_selector = left_str+domains.join(right_str+left_str)+right_str;
	domain_selector+= left_str_https+domains.join(right_str+left_str_https)+right_str;
	$("a[href^='http']:not([class^='popup'])"+domain_selector+", area[href^='http']:not([class^='popup'])"+domain_selector)
	.addClass("external");
	
	//if has class .extenal -> open _blank window
	$("a.external").click(function(){
		window.open(this.href,"_blank");
		return false;
	});
	
//Smooth Scroll
	$('a[href^=#]').click(function() { 
		var target = $(this.hash);
		if(target.size()) { 
			if(this.hash != "#page"){
				var top = target.offset().top  + $("#page").scrollTop() -125;
				$("#page").animate({scrollTop:top}, 500, 'swing'); 
			}else{
				$("#page").animate({scrollTop:0}, 500, 'swing'); 
			}
		} 
		return false; 
	}); 
	
	if(this.location.hash != ""){
		$("#page").scrollTop(0); 
	}
	if(this.location.search != ""){
		var c = this.location.search.substr(1);
		setTimeout(function(){
			var top = $("#"+c).offset().top-125;
			$("#page").animate({scrollTop:top}, 500, 'swing'); 
		},0)
	}
	
});