/* 
 *
 * library.js var0.1
 * 
 * Copyright(c) INVOGUE.CO,. Ltd. ALL Rights Reserved.
 * http://www.invogue.co.jp/
 * 
 */

(function($) {
	$.library = {
		analysis: function(path){
			var self = this;
			this.originalPath = path;
			this.absolutePath = (function(){
				var e = document.createElement('span');
				e.innerHTML = '<a href="' + path + '" />';
				return e.firstChild.href;
			})();
			
			var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
			var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
			for (var field in fields) {
				this[field] = r[fields[field]];
			}
			this.querys = {};
			if(this.query){
				$.each(self.query.split('&'), function(){
					var a = this.split('=');
					if (a.length == 2) self.querys[a[0]] = a[1];
				});
			}
		},
		rollover: function(options) {
			var c = $.extend({
						hoverSelector : 'img.rover, input.rover, .all-rover img',
						groupSelector : '.group-rover',
						activeclass   : 'active',
						postfix       : '_on'
					}, options);
			
			var rolloverImgs = $(c.hoverSelector).filter(isNotCurrent);
			
			rolloverImgs.each(function(){
				this.originalSrc = $(this).attr('src');
				this.rolloverSrc = this.originalSrc.replace(new RegExp('('+c.postfix+')?(\.gif|\.jpg|\.png)$'), c.postfix+"$2");
				this.rolloverImg = new Image;
				this.rolloverImg.src = this.rolloverSrc;
			});
			
			var groupingImgs = $(c.groupSelector).find('img').filter(isRolloverImg);
			
			rolloverImgs.not(groupingImgs).hover(function(){
				if(!$(this).hasClass(c.activeclass)){$(this).attr('src',this.rolloverSrc);}
			},function(){
				if(!$(this).hasClass(c.activeclass)){$(this).attr('src',this.originalSrc);}
			});
			
			$(c.groupSelector).hover(function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					if(!$(this).hasClass(c.activeclass)){$(this).attr('src',this.rolloverSrc);}
				});
			},function(){
				$(this).find('img').filter(isRolloverImg).each(function(){
					if(!$(this).hasClass(c.activeclass)){$(this).attr('src',this.originalSrc);}
				});
			});
			
			function isNotCurrent(i){return Boolean(!this.currentSrc);}
			function isRolloverImg(i){return Boolean(this.rolloverSrc);}
		},
		active: function(options) {
			var c = $.extend({
						id       : '',
						type     : 'img',  //(img text Either becomes active.)
						addclass : 'active',
						postfix  : '_on'
					}, options);
			if(c.id != '' && c.type == 'img'){
				$("img#"+c.id).each(function(){
					var dot = $(this).attr('src').lastIndexOf('.');
					var imgsrc_ro = $(this).attr('src').substr(0, dot) + c.postfix + $(this).attr('src').substr(dot, 4);
					$(this).attr('src',imgsrc_ro).addClass(c.addclass);
				});
			}else if(c.id != '' && c.type == 'text'){
				$("#"+c.id).each(function(){
					$(this).addClass(c.addclass);
				});
			}
		},
		scroll: function(options) {
			var scroller = (function() {
				var c = $.extend({
					easing:100,
					step:30,
					fps:60,
					fragment:''
				}, options);
				c.ms = Math.floor(1000/c.fps);
				var timerId;
				var param = {
					stepCount:0,
					startY:0,
					endY:0,
					lastY:0
				};
				function move() {
					if (param.stepCount == c.step) {
						//setFragment(param.hrefdata.absolutePath);
						window.scrollTo(getCurrentX(), param.endY);
					} else if (param.lastY == getCurrentY()) {
						param.stepCount++;
						window.scrollTo(getCurrentX(), getEasingY());
						param.lastY = getEasingY();
						timerId = setTimeout(move, c.ms); 
					} else {
						if(getCurrentY()+getViewportHeight() == getDocumentHeight()) {
							setFragment(param.hrefdata.absolutePath);
						}
					}
				}
				function setFragment(path){
					location.href = path
				}
				function getCurrentY() {
					return document.body.scrollTop  || document.documentElement.scrollTop;
				}
				function getCurrentX() {
					return document.body.scrollLeft  || document.documentElement.scrollLeft;
				}
				function getDocumentHeight(){
					return document.documentElement.scrollHeight || document.body.scrollHeight;
				}
				function getViewportHeight(){
					return (!$.browser.safari && !$.browser.opera) ? document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight : window.innerHeight;
				}
				function getEasingY() {
					return Math.floor(getEasing(param.startY, param.endY, param.stepCount, c.step, c.easing));
				}
				function getEasing(start, end, stepCount, step, easing) {
					var s = stepCount / step;
					return (end - start) * (s + easing / (100 * Math.PI) * Math.sin(Math.PI * s)) + start;
				}
				return {
					set: function(options) {
						this.stop();
						if (options.startY == undefined) options.startY = getCurrentY();
						param = $.extend(param, options);
						param.lastY = param.startY;
						timerId = setTimeout(move, c.ms); 
					},
					stop: function(){
						clearTimeout(timerId);
						param.stepCount = 0;
					}
				};
			})();
			$('a[href^=#], area[href^=#]').not('a[href=#], area[href=#]').each(function(){
				this.hrefdata = new $.library.analysis(this.getAttribute('href'));
			}).click(function(){
				var target = $('#'+this.hrefdata.fragment);
				if (target.length == 0) target = $('a[name='+this.hrefdata.fragment+']');
				if (target.length) {
					scroller.set({
						endY: target.offset().top,
						hrefdata: this.hrefdata
					});
					return false;
				}
			});
		},
		blank: function(options) {
			var c = $.extend({
				externalClass : 'wind_blank',
				addIconSrc    : ''
			}, options);
			var uri = new $.library.analysis(location.href);
			var e = $('a[href^="http://"]').not('a[href^="' + uri.schema + '://' + uri.host + '/' + '"]');
			if(c.addIconSrc){
				e.not(':has(img)').after($('<img src="'+c.addIconSrc+'" class="externalIcon" />'));
			}
			e.attr('target','_blank');
			e.addClass(c.externalClass);
		},
		inputfocus: function(options) {
			var c = $.extend({
				usuallyClass : 'input-usually',
				focusClass   : 'input-focus'
			}, options);
			$('input[type=text],input[type=password],textarea').addClass(c.usuallyClass);
			$('.input-usually').focus(function(){
				$(this).addClass('input-focus');
			});
			$('.input-usually').blur(function(){
				if($(this).find(c.focusClass)){
					$(this).removeClass(c.focusClass);
				}
			});
		}
	};
	
	
	$(function() {
		$.library.rollover();
		$.library.scroll();
		$.library.blank();
		$.library.inputfocus();
	});
})(jQuery);
