/*	jquery.lh.js
	http://www.lhsalon.jp/sample/jquery/
-------------------------------------------------------------------------- */

jQuery.fn.extend({
	/*	RollOver
	-------------------------------------------------------------------------- */
	rollOver : function(findObj) {
		var _fObj = findObj;
		$(this).hover( rOver, rOut ).each( preLoad );
		function rOver() {
			var tgt = changeTgt($(this));
			tgt.attr("src", tgt.attr("src").replace("_off.", "_on."));
		}
		function rOut() {
			var tgt = changeTgt($(this));
			tgt.attr("src", tgt.attr("src").replace("_on.", "_off."));
		}
		function changeTgt(tgt) {
			return (_fObj != null) ? tgt.find(_fObj) : tgt;
		}
		function preLoad() {
			var tgt = changeTgt($(this));
			$("<img>").attr("src", tgt.attr("src").replace("_off.", "_on."));
		}
		return $(this);
	},
	/*	Alpha RollOver
	-------------------------------------------------------------------------- */
	alphaRo : function(opt) {
		if( !opt ) opt = new Object();
		var _from = opt.from!=null ? opt.from : 1;
		var _to = opt.to!=null ? opt.to : 0.8;
		var _f = opt.fade!=null ? opt.fade : false;
		var _s = opt.speed!=null ? opt.speed : "normal";
		var _g = opt.group!=null ? opt.group : true;
		$(this).css({"opacity":_from, display:"inline-block"}).bind("mouseenter", {toNum:_to}, toOpacity).bind("mouseleave", {toNum:_from}, toOpacity);
		function toOpacity(event) {
			var tgt = $(this);
			var rel = tgt.attr('rel');
			if( rel && _g ) {
				tgt = $("*[rel='"+rel+"']");
			}
			( _f ) ? tgt.queue([]).fadeTo(_s, event.data.toNum) : tgt.css("opacity", event.data.toNum);
		}
		return $(this);
	},
	/*	Smooth Scroll
	-------------------------------------------------------------------------- */
	smScroll : function (opt) {
		var _hash, _h, _inH, _flag=true;
		var strUA = navigator.userAgent.toLowerCase();
		var _scrTgt = $('html');
		if( strUA.indexOf("safari") != -1 ) _scrTgt = $('body');
		if( !opt ) opt = new Object();
		var s = opt.speed!=null ? opt.speed : 700;
		var ease = opt.easing!=null ? opt.easing : '';
		$(this).click(function() {
			var tgt = $(this).attr('href').substr(1);
			_h = Math.max( document.body.clientHeight , document.body.scrollHeight );  
			_h = Math.max( _h , document.documentElement.scrollHeight );  
			_h = Math.max( _h , document.documentElement.clientHeight );
			_inH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
			toPos(tgt);
			return false;
		});
		setInterval( function() {
			var sHash = getHash();
			toPosBrowse(sHash);
		}, 200 );
		function toPos(gHash) {
			if( _flag ) {
				_hash = gHash;
				_flag = false;
				_scrTgt.animate({scrollTop:toGetPoint()}, s, ease, setHash);
			}
		}
		function toPosBrowse(gHash) {
			if( _hash !== gHash && _flag ) {
				_hash = gHash;
				if( _hash != "" ) {
					_scrTgt.queue([]).scrollTop(toGetPoint());
				}
				_flag = true;
			}
		}
		function toGetPoint() {
			var result = 0;
			if(_hash != '') {
				result = $("*[name='"+_hash+"']").offset().top;
				if(result+_inH > _h) result = _h-_inH;
			}
			return result;
		}
		function getHash() {
			if ( typeof window.location.hash !== 'undefined' ) {
				return window.location.hash.substr(1);
			} else {
				return location.hash.substr(1);
			}
		}
		function setHash() {
			if ( typeof window.location.hash !== 'undefined' ) {
				if ( window.location.hash !== _hash ) {
					window.location.hash = _hash;
				}
			} else if( location.hash !== _hash ) {
				location.hash = _hash;
			}
			_flag = true;
		}
		return $(this);
	},
	/*	Popup Window
	-------------------------------------------------------------------------- */
	anotherWin : function(opt) {
		if( !opt ) opt = new Object();
		var _w = opt.width!=null ? opt.width : 500;
		var _h = opt.height!=null ? opt.height : false;
		var _n = opt.name!=null ? opt.name : 'subWindow';
		var _s = opt.scrollbars!=null ? opt.scrollbars : 'yes';
		var _r = opt.resizable!=null ? opt.resizable : 'yes';
		var _m = opt.menubar!=null ? opt.menubar : 'yes';
		$(this).click( function() {
			var url = $(this).attr('href');
			var rule = 'width='+_w+',scrollbars='+_s+',resizable='+_r+',menubar='+_m;
			if( _h ) rule += ',height='+_h;
			window.open(url, _n, rule);
			return false;
		});
		return $(this);
	},
	/*	Set Same Height
	-------------------------------------------------------------------------- */
	setHeight : function(child, opt) {
		if( !opt ) opt = new Object();
		var _o = opt.outer!=null ? opt.outer : false;
		$(this).each( function() {
			var sHeight = 0;
			$(this).find( child ).each( function() {
				$(this).css('height', 'auto');
				var gH = _o ? $(this).outerHeight() : $(this).height();
				if( sHeight<gH )
					sHeight = gH;
			} );
			$(this).find( child ).each( function() {
				var lHeight = _o ? sHeight-$(this).outerHeight()+$(this).height() : sHeight;
				$(this).height(lHeight);
			} );
		} );
		return $(this);
	},
	/*	SubMenuStoker
	-------------------------------------------------------------------------- */
	subMenuStoker : function(child) {
		var toPos = 20;
		var cBox = $(this);
		var lBox = $(this).find(child);
		var dTop = lBox.offset().top;
		var nowP = dTop;
		var scrInterval, chkInterval;
		if( !$("body.index").length ) {
			cBox.css("position", "relative");
			lBox.css( {"position":"absolute", "top":0} );
			goMoving();
		}
		function chkMoving() {
			chkInterval = setInterval( function() {
				var sabun = getTargetPos()-nowP;
				if( Math.abs(sabun) > 5 ) {
					clearInterval(chkInterval);
					goMoving();
				}
			}, 700 );
		}
		function goMoving() {
			scrInterval = setInterval( function() {
				var sabun = ( getTargetPos()-nowP )/15;
				nowP = nowP + sabun;
				
				if( Math.abs(sabun) < 0.5 ) {
					clearInterval(scrInterval);
					nowP = getTargetPos();
					lBox.css('top', nowP );
					chkMoving();
				} else {
					lBox.css('top', nowP);
				}
			}, 10);
		}
		function getTargetPos() {
			var nHeight = lBox.outerHeight({margin:true});
			var cHeight = cBox.outerHeight({margin:true});
			var scr = $(window).scrollTop()+toPos;
			var tgt = scr-dTop;
			tgt = tgt<0 ? 0 : tgt;
			if( tgt+nHeight > cHeight )
				tgt = cHeight-nHeight;
			return tgt;
		}
	},
	/*	Default Inputarea
	-------------------------------------------------------------------------- */
	exClick : function( tgt, opt ) {
		if( !opt ) opt = new Object();
		var _tC = opt.toColor!=null ? opt.toColor : "#999999";
		var _fC = opt.fromColor!=null ? opt.fromColor : "#333333";
		var exList = $(this);
		$(this).each( function() {
			if( $(this).val() == "" ) {
				$(this).val( $(this).attr("rel") ).css("color", _tC).focus( inpFocus ).blur( inpBlur );
			}
		} );
		$(tgt).submit( function() {
			exList.each( function() {
				if( $(this).val() == $(this).attr("rel") ) {
					$(this).val("");
				}
			} );
		} );
		function inpFocus() {
			$(this).css("color", _fC).val("");
		}
		function inpBlur() {
			if( $(this).val() == "" ) {
				$(this).val( $(this).attr("rel") ).css("color", _tC);
				$(this).bind("focus", inpFocus);
			} else {
				$(this).unbind("focus");
			}
		}
	}

});
