/**
 * this script jQuery 1.4 or higher required.
 *
 * created: 2010-04-20
 * modified: 2010-08-27
 * (c) 2010 u8
**/

(function($) {
	// initializing
	$(function() {
		//$.global.addFirstChild();
		//$.global.addLastChild();
		//$.global.addHoverClass();
		$.global.externalLink();
		$.global.fadeRollover();
		$.global.currentLink();
		$.global.easingScroll();
		$.heightAlign();
		
		// stripe table
		$('th, td', '#maincol table tr:first-child').addClass('firstLine');
		$('th:first-child, td:first-child', '#maincol table tr').addClass('first-child');
		$('#maincol table tr:nth-child(odd)').addClass('odd');
		
		// trackback url select
		$('#tbUrl').click(function() {
			$(this).select();
		});
	});
	
	// add easing
	$.extend( $.easing, {
		easeOutQuint: function( x, t, b, c, d ) {
			return c*((t=t/d-1)*t*t*t*t+1)+ b;
		}
	});
	
	// add height align
	$.extend({
		heightAlign: function() {
			$('#globalFooter > div.section').heightAlign();
		}
	});
	
	// script start
	$.global = {
		addFirstChild: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'firstChild'
			}, options);
			
			$(c.selector).each(function() {
				$(':first-child', this).addClass(c.addClass);
			});
		},
		addLastChild: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'lastChild'
			}, options);
			
			$(c.selector).each(function() {
				$(':last-child', this).addClass(c.addClass);
			});
		},
		addHoverClass: function(options) {
			var c = $.extend({
				selector: '',
				addClass: 'hover'
			}, options);
			
			$(c.selector).each(function() {
				$(this).hover(function() {
					$(this).addClass(c.addClass);
				}, function(){
					$(this).removeClass(c.addClass);
				});
			});
		},
		externalLink: function(options) {
			var c = $.extend({
				addClass: 'externalLink',
				notWindow: '',
				myDomain: 'a[href^="' + location.protocol + '//' + location.hostname + '/"]'
			}, options);
			
			$('a[href*="http"], a[href$=".pdf"]').not(c.notWindow + ',' + c.myDomain).each(function() {
				$(this).addClass(c.addClass).click(function() {
					window.open(this.href, '_blank');
					return false;
				});
			});
		},
		fadeRollover: function(options) {
			var c = $.extend({
				before: '_off.',
				after: '_on.',
				easing: 'swing',
				duration: 600
			}, options);
			
			$('img').filter(function() {
				return $(this).attr('src').match(c.before);
			}).each(function() {
				var self = $(this);
				
				$('<img>', {
					src: self.attr('src').replace(c.before, c.after),
					css: {
						position: 'relative',
						zIndex: '100',
						opacity: '0'
					}
				}).insertBefore( self );
				
				self.css({
					position: 'relative',
					marginLeft: '-' + this.width + 'px'
				});
				
				self.prev().bind('mouseover focus', function() {
					$(this).stop(true, false).animate({
						opacity: 1
					}, c.duration, c.easing, false);
				}).bind('mouseout blur', function() {
					$(this).stop(true, false).animate({
						opacity: 0
					}, c.duration, c.easing, false);
				});
			});
		},
		currentLink: function(options) {
			var c = $.extend({
				selector: '#globalNav li',
				addClass: 'current',
				before: '_off.',
				after: '_on.'
			}, options);
			
			$('a', c.selector).each(function() {
				var myURL = $(this).attr('href');
				var findImg = $(this).find('img');
				
				if ( location.pathname != "/" ) {
					var current = location.pathname.split('/');
					
					if ( myURL.indexOf('/' + current[1] + '/') != -1 ) {
						$(this).addClass(c.addClass);
						if ( findImg ) {
							findImg.attr('src', findImg.attr('src').replace(c.before, c.after)).unbind();
						}
					}
				}
			});
		},
		easingScroll: function(options) {
			var c = $.extend({
				easing: ($.easing.easeOutQuint) ? 'easeOutQuint' : 'swing',
				duration: 800
			}, options);
			
			var scrollTarget = ($.browser.opera) ? $('html') : $('html, body');
			
			$('a[href^="#"]').filter(function() {
				return this.hash.length > 0;
			}).each(function() {
				$(this).click(function() {
					var offset = $(this.hash).offset();
					
					scrollTarget.animate({
						scrollTop: offset.top,
						scrollLeft: offset.left
					}, c.duration, c.easing, false);
					
					return false;
				});
			});
			
			$(document).click(function() {
				scrollTarget.stop();
			});
		}
	};
	
	$.fn.heightAlign = function(options) {
		var c = $.extend({
			column: 0
		}, options);
		
		var n = 0, heightList = new Array(), columnHeight = new Array();
		columnHeight[n] = 0;
		
		$(this).each(function( i ) {
			$(this).removeAttr('style');
			
			var height = $(this).height();
			heightList[i] = height;
			
			if ( c.column > 1 ) {
				if ( height > columnHeight[n] ) {
					columnHeight[n] = height;
				}
				if ( (i > 0) && (((i+1) % c.column) == 0) ) {
					n++;
					columnHeight[n] = 0;
				};
			}
		});
		
		heightList = heightList.sort(function descend( a, b ) { return b - a; });
		var maxHeight = heightList[0];
		
		var supportMinHeight = (typeof document.documentElement.style.minHeight != 'undefined') ? true : false ;
		
		if ( c.column > 1 ) {
			for ( var j = 0; j < columnHeight.length; j++ ) {
				for ( var k = 0; k < c.column; k++ ) {
					if ( supportMinHeight ) {
						$(this).eq(j * c.column + k).css('minHeight', columnHeight[j]);
					}
					else {
						$(this).eq(j * c.column + k).css('height', columnHeight[j]);
					}
				}
			}
		}
		else {
			if ( supportMinHeight ) {
				$(this).css('minHeight', maxHeight);
			}
			else {
				$(this).css('height', maxHeight);
			}
		}
		
		if ( $('#checkSize').css('display') != 'none' ) {
			$('<div>', {
				text: 'checking text size',
				id: 'checkSize',
				css: {
					display: 'none'
				}
			}).appendTo( $('body') );
			
			var defHeight = $('#checkSize').height();
			
			var checkHeight = function() {
				if( defHeight != $('#checkSize').height() ) {
					defHeight = $('#checkSize').height();
					$.heightAlign();
				}
			}
			
			setInterval(checkHeight, 1000);
		}
	}
})(jQuery);
