/**---------------------------------
 * common.js
 * 
 * ...
 * author	: takaaki koyama
 *
 * @use jQuery 1.6.1
 ---------------------------------*/
;(function($){
	
	$.extend($.easing, {
		def: 'easeOutQuart',
		swing: function (x, t, b, c, d) {
			return $.easing[$.easing.def](x, t, b, c, d);
		},
		easeOutQuad: function (x, t, b, c, d) {
			return - c * (t/=d) * (t-2) + b;
		},
		easeOutQuart: function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeInOutQuart: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		},
		easeOutQuint: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t*t*t + 1) + b;
		},
		easeInOutQuint: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeInOutExpo: function (x, t, b, c, d) {
			if (t==0) return b;
			if (t==d) return b+c;
			if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
			return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
		}
	});
	
	$.extend({
		//preloadImage
		preloadImage : function(arr){
			if(typeof(arr) == 'string'){
				var img = new Image();
				img.src = arr;
			}else
			if(typeof(arr) == 'object' && arr.length > 0){
				arr = $.unique(arr);
				var len = arr.length;
				for(var i = 0; i < len; i++){
					$.preloadImage(arr[i]);
				}
			}
		},	 
		
		uif : {
			setup : (function(){
				
				var setup = function(){
					setup.rollover();
					setup.externalLink();
					setup.popup();
					setup.smoothScroll();
					//setup.css3Utils();
					//setup.formUtils();
				}
				
				//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.
				setup.rollover = function(option){
					var config = {
						off : '_off.',
						on : '_on.',
						currentClass : 'current'
					}
					$.extend(config,option);
					
					var offRegExp = new RegExp(config.off);
					var onRegExp = new RegExp(config.on);
					
				
					function rollOverHandler(){
						var $t = $(this);
						if ($t.attr("src").match(offRegExp)){
							$t.attr("src",$t.attr("src").replace(config.off, config.on));
							return;
						}
					};
				
					function rollOutHandler(){
						var $t = $(this);
						if ($t.attr("src").match(onRegExp)){
							$t.attr("src",$t.attr("src").replace(config.on, config.off));
							return;
						}
					}
					
					
					$("a img[src*='_on']").addClass(config.currentClass);
					$("a img[class!='current'],:image")
						.unbind("mouseover",rollOverHandler)
						.bind("mouseover",rollOverHandler);
					
					$("a img[class!='current'],:image")
						.unbind("mouseout",rollOutHandler)
						.bind("mouseout",rollOutHandler);
					
					
					//preload images
					var images = [];
					var img;
					$("a img,:image").each(function(index){
						if($(this).attr("src").match(offRegExp)){
							images[images.length] = $(this).attr("src").replace(config.on, config.off);
						}
					});
					
					$.preloadImage(images);
				}
				
				//externalLink
				//if has class .extenal -> open _blank window
				setup.externalLink = function(option){
					var config = {
						className : 'external',
						ignoreURLs : []
					}
					$.extend(config,option);
					
					// target _blank auto add
					var domains = [document.domain];
					if(config.ignoreURLs.length){
						domains = domains.concat(config.ignoreURLs);
					}
					var selector = ""
					var ls= ':not([href^="http://';
					var lss= ':not([href^="https://';
					var rs = '"])';
					selector  = ls + domains.join(rs + ls) + rs;
					selector += lss + domains.join(rs + lss) +rs;
					if(selector == ls+rs+lss+rs) selector = "";
					$('a[href^="http"]:not([class^="popup"])' + selector).addClass(config.className);
					$('area[href^="http"]:not([class^="popup"])' + selector).addClass(config.className);
					
					function windowOpen(){
						window.open(this.href,"_blank");
						return false;
					}
					
					
					$("a."+config.className)
						.unbind("click",windowOpen)
						.bind("click",windowOpen);
					$("area."+config.className)
						.unbind("click",windowOpen)
						.bind("click",windowOpen);
					
				}
				
				//popup
				// class="popup400x600" 
				//  -> window.open(this.href,"popup","width=400,height=600,...)
				//
				// open in popup parent window.
				// add class="openParentWin" on a-tag in a popup window.
				setup.popup = function(){
					
					function popupOpen(){
						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 = state.concat([
							"location=" + notHasSize,
							"toolbar=" + notHasSize,
							"directories=" + notHasSize,
							"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;
					}
					
					
					function openParentWindow(){
						window.open(this.href,document.domain + "_root");
						
						return false;
					}
					
					$('a[class^="popup"]')
						.unbind('click', popupOpen)
						.bind('click', popupOpen);
					
					$("a.openParentWin")
						.unbind('click', openParentWindow)
						.bind('click', openParentWindow);
					
				}
				
				//smoothScroll
				setup.smoothScroll = function(target){
					var $t = target? $(target) : $($.browser.safari ? 'body' : 'html');
					function smoothScroll() { 
						var target = $(this.hash); 
						if(target.size()) { 
							var top = target.offset().top;
							$t.animate({scrollTop:top}, 800, 'easeOutQuad'); 
						} 
						return false; 
					}
					
					$('a[href^=#]')
						.unbind("click",smoothScroll)
						.bind("click",smoothScroll);
				}
				
				setup.css3Utils = (function(){
					
					var css3Utils = function(){
						css3Utils.firstChild();
						css3Utils.lastChild();
						css3Utils.evan();
						css3Utils.odd();
					}
					
					css3Utils.firstChild = function(){
						css3Utils._set(":first-child","first-chlid");
					}
					
					css3Utils.lastChild = function(){
						css3Utils._set(":last-child","last-chlid");
					}
					
					css3Utils.nthChild = function(nth,className){
						css3Utils._set(":nth-chlid(" + nth + ")", className);
					}
					
					css3Utils.evan = function(){
						css3Utils._set(":even","even");
					}
					
					css3Utils.odd = function(){
						css3Utils._set(":odd","odd");
					}
					
					css3Utils._set = function(selecter,className){
						css3Utils._clear(className);
						
						$("ul > li" + selecter).addClass(className);
						$("ol > li" + selecter).addClass(className);
						$("dl > dt" + selecter).addClass(className);
						$("dl > dd" + selecter).addClass(className);
						$("tr > th" + selecter).addClass(className);
						$("tr > td" + selecter).addClass(className);
						$("table > tr" + selecter).addClass(className);
						$("thead > tr" + selecter).addClass(className);
						$("tbody > tr" + selecter).addClass(className);
						$("tfoot > tr" + selecter).addClass(className);
					},
					
					css3Utils._clear = function(className){
						$("."+className).removeClass(className);
					}
					
					return css3Utils;
				})()
				
				setup.formUtils = function(){
					$("input,textarea,select")
						.focus(function(){$(this).addClass("focus")})
						.blur(function(){$(this).removeClass("focus")})
						.hover(function(){
								$(this).addClass("hover")
							},function(){
								$(this).removeClass("hover")
							});
				}
				
				return setup;
			})()
		},
		
		fontsizeswitcher : {
			setuped : false,
			size : "1em",
			config : {
				target : "#contents",
				cookieName : "fontsizeswitch",
				cookieExpires : 90
			},
			setup : function(options){
				$.extend(this.config, options);
				this.setuped = true;
				var s = this.cookie(this.config.cookieName);
				if(s === null){
					s = this.size;
				}
				this.change(s);
			},
			cookie : function(name, value){
				if(!!value){
					var date = new Date();
					date.setTime(date.getTime() + (1000 * 60 * 60 * 24 * this.config.cookieExpires));
					document.cookie = name + '=' + encodeURIComponent(value) + '; path=/; expires=' + date.toUTCString();
				}else{
						var cookieValue = null;
						if(document.cookie && document.cookie != ""){
							var cookies = document.cookie.split(';');
							var len = cookies.length;
							for (var i = 0; i < len; i++) {
								var cookie = $.trim(cookies[i]);
								// Does this cookie string begin with the name we want?
								if (cookie.substring(0, name.length + 1) == (name + '=')) {
									cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
									break;
								}
							}
						}
						return cookieValue;
				}
			},
			change : function(size){
				if(!this.setuped){
					this.setup();
				}
				$(this.config.target).css("font-size",size);
				this.cookie(this.config.cookieName, size);
				this.size = size;
			}
			
		}
		
	})
	
	$(function(){
		$.uif.setup();
		$("#article table tr:even").addClass("even")
		
		$.fontsizeswitcher.setup();
		
		var font_size = ["1em","1.2em","1.4em"];
		var current = $.inArray($.fontsizeswitcher.size,font_size);
		var $fontsizeswicherButtontns = $("#font_size_swicher dd a");
		var $fontsizeswicherButtontnImages = $("#font_size_swicher dd a img");
		$fontsizeswicherButtontnImages
			.unbind("mouseover")
			.unbind("mouseout")
			.bind("mouseover",function(){
				var $t = $(this);
				if($t.hasClass("current")) return;
				if ($t.attr("src").match(/_off/)){
					$t.attr("src",$t.attr("src").replace("_off", "_on"));
					return;
				}
			})
			.bind("mouseout",function rollOutHandler(){
				var $t = $(this);
				if($t.hasClass("current")) return;
				if ($t.attr("src").match(/_on/)){
					$t.attr("src",$t.attr("src").replace("_on","_off"));
					return;
				}
			})
			.eq(current).mouseover().addClass("current");
		
		$fontsizeswicherButtontns.click(function(){
			var $t = $(this);
			if($t.find(".current").size()) return;
			var index = $fontsizeswicherButtontns.index($t);
			$fontsizeswicherButtontnImages.eq(current).removeClass("current").mouseout();
			current = index;
			$fontsizeswicherButtontnImages.eq(current).mouseover().addClass("current");
			$.fontsizeswitcher.change(font_size[current]);
			return false;
		})
		
		
	});
	
})(jQuery);
		
		

