/**---------------------------------
 * common.js
 * 
 * ...
 * author	: takaaki koyama
 *
 * @use jQuery later
 ---------------------------------*/
;(function($,undefined){
		   
	$.extend($.easing, {
		def: 'easeInOutQuint',
		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($.isArray(arr)){
				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.css3Utils.firstChild();
						setup.css3Utils.lastChild();
						setup.css3Utils.evan();
						//setup.css3Utils.odd();
					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,: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.off, config.on);
						}
					});
					
					$.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!=='' ? [document.domain] : [];
					if(config.ignoreURLs.length){
						domains = domains.concat(config.ignoreURLs);
					}
					
					var selector = ""
					if(domains.length){
						var ls= ":not([href^='http://",lss= ":not([href^='https://", rs = "'])";
						selector  = ls + domains.join(rs + ls) + rs;
						selector += lss + domains.join(rs + lss) +rs;
					}
					
					$("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}, setup.smoothScroll.speed); 
						} 
						return false; 
					}
					
					$('a[href^=#]')
						.unbind("click",smoothScroll)
						.bind("click",smoothScroll);
				}
				setup.smoothScroll.speed = 1000;
				
				setup.css3Utils = (function(){
					
					var css3Utils = function(){
						css3Utils.firstChild();
						css3Utils.lastChild();
						css3Utils.evan();
						css3Utils.odd();
					}
					
					css3Utils.firstChild = function(){
						css3Utils._set(":first-child","first-child");
					}
					
					css3Utils.lastChild = function(){
						css3Utils._set(":last-child","last-child");
					}
					
					css3Utils.nthChild = function(nth,className){
						css3Utils._set(":nth-child(" + nth + ")", className);
					}
					
					css3Utils.evan = function(){
						css3Utils._set(":even","even");
					}
					
					css3Utils.odd = function(){
						css3Utils._set(":odd","odd");
					}
					
					css3Utils._set = function(selecter,className){
						css3Utils._clear(className);
						$(css3Utils.selectors.join(selecter +',')).addClass(className);
					},
					
					css3Utils._clear = function(className){
						$(css3Utils.selectors.join('.'+className+',')).removeClass(className);
					}
					
					css3Utils.selectors = [
						//"div",
						"ul > li",
						"ol > li",
						"dl > dt",
						"dl > dd",
						"tr > th",
						"tr > td",
						"table > tr",
						"thead > tr",
						"tbody > tr",
						"tfoot > tr"
					];
					
					
					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;
			})()
		}
	})
	
	function set_point_icon($targets, $titles){
		var src = $('#navi_skip img').attr('src').replace('skip.gif','p.gif');
		var $point_icon = $('<span class="point_icon"><img src="'+src+'" width="5" height="5" alt="+" /></span>');
		var $title_point_icon = $('<span class="title_point_icon"><img src="'+src+'" width="5" height="5" alt="+" /></span>');
		
		$targets.each(function(index,elem){
			var $t = $(elem);
			var $c = $point_icon.clone();
			$c.css('top',$t.outerHeight()-2);
			$t.addClass('point_icon_c').append($c);
		});
		
		$titles.addClass('point_icon_c').append($title_point_icon);
	}
	
	function set_link_block_hover($targets){
		
		function rollOverHandler(e){
			var $t = $(this).find('img:eq(0)');
			if ($t.attr("src").match(/_off/)){
				$t.attr("src",$t.attr("src").replace('_off', '_on'));
				return;
			}
		}
		function rollOutHandler(e){
			var $t = $(this).find('img:eq(0)');
			if ($t.attr("src").match(/_on/)){
				$t.attr("src",$t.attr("src").replace('_on', '_off'));
				return;
			}
		}
		
		$targets.find('img').unbind('mouseover mouseout');

		$targets
			.unbind("mouseover",rollOverHandler).bind("mouseover",rollOverHandler)
			.unbind("mouseout",rollOutHandler).bind("mouseout",rollOutHandler);
	}
	
	
	$(function(){
		//console.time('$.uif.setup()');
		$.uif.setup();
		set_link_block_hover($('.link_block'));
		set_point_icon(
			$([
			   	'#global_navi li',
				'.add_point_icons > *',
				'.add_point_icon',
			   	'#local_navi h3'
				].join(',')),
			$([
			   '#article .section_title',
			   '#article .content_title',
			   '#aside .module_title'
			   ].join(','))
		);
		//console.timeEnd('$.uif.setup()');
	});
	
})(jQuery);

