/**---------------------------------
 * common.js
 * 
 * ...
 * author	: takaaki koyama
 *
 * @use jQuery 1.2.6 later
 ---------------------------------*/
;(function($){
	
	jQuery.extend( jQuery.easing,{
		def: 'easeOutQuad',
		swing: function (x, t, b, c, d) {
			//alert(jQuery.easing.default);
			return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
	})
	
	$(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");
		
		$("a img,:image").mouseover(function(){
			if ($(this).attr("src").match(/_off./)){
				$(this).attr("src",$(this).attr("src").replace("_off.", "_on."));
				return;
			}
		});
	
		$("a img[class!='current'],:image").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 = [];
		$("a img,:image").each(function(index){
				if($(this).attr("src").match(/_off./)){
					 images[index]= new Image();
					 images[index].src = $(this).attr("src").replace("_on.", "_off.");
				}
		});
		
//CSS3 selector 
//		//last-child
//		$("li:last-child, dl > *:last-child, table > *:last-child, tr > *:last-child").addClass("last-child");
//		
//		//nth-child
//		$("ol,ul").each(function(){
//				$(this).children("li").each(function(index){
//					$(this).addClass("n"+(index+1)+"th-child");
//				});
//		});
//		$("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
//		$("li:nth-child(even),dt:nth-child(even),dd:nth-child(even),tr:nth-child(even),tr > *:nth-child(even)").addClass("even");
//		$("li:nth-child(odd),dt:nth-child(odd),dd:nth-child(odd),td:nth-child(odd),td > *:nth-child(odd)").addClass("odd");

// form utils
//		$("input,textarea")
//			.focus(function(){$(this).addClass("focus")})
//			.blur(function(){$(this).removeClass("focus")})
//			.hover(function(){$(this).addClass("hover")},function(){$(this).removeClass("hover")});

		
		// 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 = document.domain + "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,document.domain + "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
		function windowOpen(){
			window.open(this.href,"_blank");
			return false;
		}
							
		//Smooth Scroll
		function smoothScroll() { 
			var target = $(this.hash); 
			if(target.size()) { 
				var top = target.offset().top;
				$($.browser.safari ? 'body' : 'html').animate({scrollTop:top}, 800, 'easeOutQuad'); 
			} 
			return false; 
		}
		
		

		$("a.external").bind("click",windowOpen);
		$('a[href^=#]').bind("click",smoothScroll);
		
	});
	
	/*
	 * RSSから日付を取得
	 */
	  
	$.ajax({
		type     :"GET",
		url      :"/report/?feed=rss2",
		cache    : false,
		dataType :"xml",
		success  : function(xml) {
			var rssDate = new Date( $(xml).find("pubDate:first").text() );
			var $dateBox = $("<div id='day' />");
			var	year   = rssDate.getYear();
			var	month  = rssDate.getMonth() + 1;
			var	day    = rssDate.getDate();
				if (year < 2000) { year  += 1900; }
				if (month  < 10) { month  = '0'+month; }
				if (day    < 10) { day    = '0'+day; }
			
			rssDate = year.toString() + "." + month.toString() + "." +day.toString() + " <span>up</span>";
			$dateBox.appendTo("#global_navi");
			
			$dateBox.append(rssDate)
		}
	});
	
})(jQuery);
		

