/*************************************************


	Site Name   SFT
	File Name   common.js

	Create Date 2010/10/20
	Update Date 2010/10/26


*************************************************/

/**
 * Common Function
 */

(function($){

	$(function(){
		//$.commonFunc.pngfix('.js_pngfix');
		//$.commonFunc.exfixed('.js_fixed');
		$.commonFunc.animateBG('#mod_lnav .level2 a');
		$.commonFunc.flatheight2line('.js_flatheight2line dd');
		$.commonFunc.flatheight5line('.js_flatheight5line dd');
		$.commonFunc.rollover();
		$.commonFunc.fade();
		$.commonFunc.externallink();
		$.commonFunc.smoothscroll();
	});

	$.commonFunc = {

		/**
		 * Animate BG Image (required jquery.animateimage.js)
		 */
		animateBG : function(elem){
			$(elem).not('.active')
				.css({
					backgroundPosition : '-66px 8px'
				})
				.mouseover(function(){
					$(this).stop().animate({
						backgroundPosition : '(0 8px)'
					},{
						duration : 200,
						easing : 'easeOutQuad'
					});
				})
				.mouseout(function(){
					$(this).stop().animate({
						backgroundPosition : '(-66px 8px)'
					},{
						duration : 200,
						easing : 'easeInQuad',
						complete : function(){
							$(this).css({
								backgroundPosition : '-66px 8px'
							});
						}
					});
				});
		},

		/**
		 * Flat height (required jquery.flatheights.js)
		 */
		flatheight2line : function(elem){
			var sets = [], temp = [];
			$(elem).each(function(i){
				temp.push(this);
				if (i % 2 === 1) {
					sets.push(temp);
					temp = [];
				};
			});
			if(temp.length) sets.push(temp);
			$.each(sets, function() {
				$(this).flatHeights();
			});
		},
		flatheight5line : function(elem){
			var sets = [], temp = [];
			$(elem).each(function(i){
				temp.push(this);
				if (i % 5 === 4) {
					sets.push(temp);
					temp = [];
				};
			});
			if(temp.length) sets.push(temp);
			$.each(sets, function() {
				$(this).flatHeights();
			});
		},

		/**
		 * Check Browser
		 */
		isMSIE6 : ($.browser.msie && $.browser.version < 7) ? true : false,

		/**
		 * Pngfix for IE6
		 */
		pngfix : function(target){
			if(this.isMSIE6) DD_belatedPNG.fix(target);
		},

		/**
		 * Exfixed for IE6
		 */
		exfixed : function(target) {
			if(this.isMSIE6) $(target).exFixed();
		},

		/**
		 * rollover
		 */
		rollover : function(options){
			var setting = $.extend({
				selector : '.js_rollover',
				postfix : '_on'
			}, options);
			var image_cache = new Object();
			$(setting.selector).each(function(){
				var imgsrc = this.src;
				var dot = this.src.lastIndexOf('.');
				var imgsrc_on = this.src.substr(0, dot) + setting.postfix + this.src.substr(dot, 4);
				image_cache[this.src] = new Image();
				image_cache[this.src].src = imgsrc_on;
				$(this).hover(
					function(){this.src = imgsrc_on;},
					function(){this.src = imgsrc;}
				);
			});
		},

		/**
		 * fade
		 */
		fade : function(options){
			var setting = $.extend({
				selector : '.js_fade',
				opacity : '20'
			}, options);
			$(setting.selector).hover(
				function(){
					$(this).dequeue().fadeTo(300, setting.opacity/100);
				},
				function(){
					$(this).dequeue().fadeTo(300, 1);
				}
			);
		},

		/**
		 * open new window for external link
		 */
		externallink : function(options){
			var setting = $.extend({
				selector : '.js_externallink'
			}, options);
			var popupEvent = function(e){
				window.open(this.href);
				e.preventDefault();
				e.stopPropagation();
			}
			$(setting.selector).each(function(){
				$(this).click(popupEvent);
				$(this).keypress(popupEvent);
			});
		},

		/**
		 * smooth scroll
		 */
		smoothscroll : function(options){
			var self = this;
			var setting = $.extend({
				selector : '.js_pagelink',
				sclpos : 0,
				scldurat : 1,
				easing : 'easeOutExpo'
			}, options);
			$(setting.selector).click(function(){
				var target = $(this.hash);
				if(target.length){
					var targetOffset = target.offset().top - setting.sclpos;
					var bodyHeight = $('body').height();
					var windowHeight = $(window).height();
					if(bodyHeight - targetOffset < windowHeight){
						$('html, body').stop().animate(
							{scrollTop : bodyHeight - windowHeight},
							{duration : setting.scldurat * 1000, easing : setting.easing}
						);
					}else{
						$('html, body').stop().animate(
							{scrollTop : targetOffset},
							{duration : setting.scldurat * 1000, easing : setting.easing}
						);
					};
					return false;
				};
			});
		}

	};

})(jQuery);
