/* ============================================================
cau1Lib.js
 common javascript library for http://books.livedoor.com/
 gather global properties,methods,classes under a namespace.
  created: 2008-09-01
  modified:
  $Date:: 2008-11-13 18:50:43#$
  $Author: nakamurak $
  $Revision: 55152 $
============================================================ */

window.cau1Lib = {};
cau1Lib.Class = {};

(function($) {
	//Classes
	// create classes under cau1Lib.Class.

	// create new stylesheet object
	cau1Lib.Class.newStyleSheet = function() {//singleton
		var self = arguments.callee;
		if (self.instance == null) {
			this._initialize.apply(this);
			self.instance = this;
		}
		return self.instance;
	};
	$.extend(cau1Lib.Class.newStyleSheet.prototype,{
		_initialize: function() {
			//create new stylesheet object
			if ($.browser.msie) {
				this.sheet =  document.createStyleSheet();
			}
			else {
				var element = document.createElement('style');
				document.getElementsByTagName('head')[0].appendChild(element);
				this.sheet =  element.sheet;
			}
		},
		add: function(target,css) {
			//IE has its own property
			var propCssRules = this.sheet.cssRules || this.sheet.rules;
			var ruleIdx = propCssRules.length;
			//IE has its own method
			if (this.sheet.insertRule) this.sheet.insertRule(target + "{" + css + "}",ruleIdx);
			else if (this.sheet.addRule) this.sheet.addRule(target,css,ruleIdx);
			return ruleIdx;
		},
		remove: function(idx) {
			//IE has its own method
			if (this.sheet.deleteRule) this.sheet.deleteRule(idx);
			else if (this.sheet.removeRule) this.sheet.removeRule(idx);
		}
	});

	//checks if the size of the window has changed.
	cau1Lib.Class.windowSizeChecker = function() {//singleton
		var self = arguments.callee;
		if (self.instance == null) {
			this._initialize.apply(this,arguments);
			self.instance = this;
		}
		return self.instance;
	};
	$.extend(cau1Lib.Class.windowSizeChecker.prototype,{
		_initialize: function() {
			var self = this;
			if (cau1Lib.pageWidthLiquid) {
				this.checkObj = $("div#wrp");
				this.currentWidth = this.checkObj.width();
				this.prevWidth = this.currentWidth;
			}
		},
		check: function() {
			//only check when the page width is liquid
			if (cau1Lib.pageWidthLiquid) {
				this.currentWidth = this.checkObj.width();
				if (this.currentWidth != this.prevWidth) {
					this.prevWidth = this.currentWidth;
					return true;
				}
			}
			return false;
		}
	});

	//checks if the size of the font has changed.
	cau1Lib.Class.fontSizeChecker = function() {//singleton
		var self = arguments.callee;
		if (self.instance == null) {
			this._initialize.apply(this,arguments);
			self.instance = this;
		}
		return self.instance;
	};
	$.extend(cau1Lib.Class.fontSizeChecker.prototype,{
		_initialize: function() {
			this.checkObj = $("<i/>").html("&nbsp;").prependTo("body")
			.css({
				display:"block",
				position:"absolute",
				left:"-1em",
				zIndex:"-2000",
				width:"1em",
				visibility:"hidden"
			});
			this.currentWidth = this.checkObj.width();
			this.prevWidth = this.currentWidth;
		},
		check: function() {
			this.currentWidth = this.checkObj.width();
			if (this.currentWidth != this.prevWidth) {
				this.prevWidth = this.currentWidth;
				return true;
			}
			return false;
		}
	});

	//equalize the height.
	cau1Lib.Class.heightEqualizer = function(target) {//singleton
		var self = arguments.callee;
		if (self.instance == null) {
			this._initialize.apply(this,arguments);
			self.instance = this;
		}
		self.instance._addItem(target);
		return self.instance;
	};
	$.extend(cau1Lib.Class.heightEqualizer.prototype,{
		_initialize: function() {
			var self = this;
			this.targets = [];
			this.checkHandler = function() {self._check()};
			this.equalizeAllHandler = function() {self._equalizeAll()};
			this.bindToWindow = $(window).bind("needReCalc",this.equalizeAllHandler);
			this.setTimer = setInterval(this.checkHandler,1000);
		},
		_equalizeAll: function() {
			var self = this;
			$.each(this.targets,function() {
				self._execute(this);
			});
		},
		_check: function() {
			if (cau1Lib.fontSizeHasChanged() || cau1Lib.pageWidthHasChanged())
				$(window).trigger("needReCalc");
		},
		_addItem: function(target) {
			this._execute(target);
			this.targets.push(target);
		},
		//equalize the height of target
		_execute: function(targets) {
			var tallest = 0;
			targets.css({"height":"","min-height":""})
			.each(function() {
				var height = $(this).height();
				if (height > tallest) tallest = height;
			})
			.css(cau1Lib.browserIeBelow6 ? "height" : "min-height",tallest);
		}
	});

	//show item summary mini window.
	cau1Lib.Class.showItemSummary = function(target,options) {
		var self = this;
		//optional settings
		options = $.extend({
			appendArea:"#contentsWrp"
		},options);
		this.targetObj = $(target);
		this.appendArea = options.appendArea;
		this.summaryAreaShown = false;
		this.toggleHandler = function(evt) {self._toggleShowHide(evt);};
		this.hideHandler = function() {self._hide();};
		this.targetObj.unbind('click.showItemSummary').bind('click.showItemSummary',this.toggleHandler);
		$(window).bind("closeAllOverlays",this.hideHandler);
	};
	$.extend(cau1Lib.Class.showItemSummary.prototype,{
		_show: function(x,y) {
			var self = this;
			//create DOM if there isn't.
			if (!this.summaryArea)
				this._createSummaryArea();
			//hide already opend summary, and bind closing this one for next open.
			$(window)
			.trigger("showItemSummaryHide")
			.unbind("showItemSummaryHide.prev")
			.bind("showItemSummaryHide.prev",this.hideHandler);
			$(window).trigger("hideSelectIfIE6");
			//show
			var windowWidth = $(window).width();
			if (x > 300)
				this.summaryArea.css({left:x - this.summaryArea.width(),top:y-10}).addClass("itemSummaryWrpRight").show();
			else
				this.summaryArea.css({left:x,top:y-10}).removeClass("itemSummaryWrpRight").show();
			this.summaryAreaShown = true;
		},
		_hide: function() {
			this.summaryArea.hide();
			this.summaryAreaShown = false;
			$(window).trigger("resetSelectIfIE6");
		},
		_toggleShowHide: function(evt) {
			evt.preventDefault();
			if (this.summaryAreaShown) {
				this._hide();
			} else {
				this._show(evt.pageX,evt.pageY);
			}
		},
		_createSummaryArea: function() {
			var self = this;
			if (this.targetObj.find("a").attr("href").replace(/^.*\/item\//,"")) {
				var apiUrl = "/api/item?id=" + this.targetObj.find("a").attr("href").replace(/^.*\/item\//,"").replace(/\?/,"&");
				this.summaryArea = $("<div />")
				.addClass("itemSummaryWrp")
				.css({opacity:"0.9"})
				.hide()
				.appendTo(this.appendArea)
				.load(apiUrl, function(){
					$('<p class="closeBtn"><a href="javascript:void(0);" title="閉じる">&nbsp;</a></p>')
					.appendTo(self.summaryArea.children().eq(0))
					.click(self.hideHandler);
				})
			} else {
				this.summaryArea = $("<div />");
			}
		}
	});

	//hide long text.
	//font-size/window.sizeの変更未対応
	cau1Lib.Class.hideLongText = function(target,options) {
		var self = this;
		//optional settings
		options = $.extend({
			minHeight:"2.8em"
		},options);
		this.target = target;
		this.targetObj = $(target);
		this.minHeight = options.minHeight;
		this.openHandler = function() {self._open();};
		this.closeHandler = function() {self._close();};
		this._init();
	};
	$.extend(cau1Lib.Class.hideLongText.prototype,{
		_init: function() {
			this.fullHeight = this.targetObj.height();
			this.fixedHeight = this.targetObj.height(this.minHeight).height();
			if (this.fullHeight > this.fixedHeight) {
				this._appendBtn();
			} else {
				//this.fixedHeight = this.fullHeight;
			}
			//this.targetObj.css({visibility:"visible"});
		},
		_appendBtn: function() {
			this.hideTextBtn = $("<div />")
			.addClass("hideTextBtn")
			.insertAfter(this.targetObj);
			this.hideTextBtnLink = $("<a />")
			.attr("href","javascript:void(0);")
			.toggle(
				this.openHandler,
				this.closeHandler
			)
			.appendTo(this.hideTextBtn);
			this._close();
		},
		_open: function() {
			this.hideTextBtnLink
			.toggleClass("closed")
			.text("隠す");
			this.targetObj.animate({height:this.fullHeight});
		},
		_close: function() {
			this.hideTextBtnLink
			.toggleClass("closed")
			.text("続きを見る");
			this.targetObj.animate({height:this.fixedHeight});
		}
	});

	//open / close buttons to areas.
	//font-size/window.sizeの変更未対応
	cau1Lib.Class.columnOpenClose = function(target,options) {
		var self = this;
		//optional settings
		this.options = $.extend({
			showHideArea:"div.openCloseWrapper",
			actionOpen:function() {this.showHideArea.height("auto").slideDown();},
			actionClose:function() {this.showHideArea.slideUp();},
			linkTextHalfOpen:false,
			linkTextOpen:false,
			linkTextClose:"隠す",
//			showHideArea:"ol.commentsList, ul.bookList, ul.categoryList, form.editArea, p.button",
			useHalfOpen: false,
			halfOpenDetect:function() {
				this.openHeight = this.showHideArea.height();
				if (this.openHeight > this.options.halfOpenHeight)
					return true;
				else
					return false;
			},
			actionHalfOpen:function() {this.showHideArea.animate({height:this.options.halfOpenHeight});},
			halfOpenHeight: 100,
			halfOpenCount:5
		},options);
		this.targetObj = $(target);
		this.showHideArea = this.targetObj.find(this.options.showHideArea);
		this.openHandler = function() {self._open();};
		this.closeHandler = function() {self._close();};
		this.halfOpenHandler = function() {self._halfOpen();};
		if (this.options.useHalfOpen && !this.options.halfOpenDetect.call(this)) {
				//don't need halfOpen if thers's not enough contents.
				this.options.useHalfOpen = false;
		};
		this._initialize();
	};
	$.extend(cau1Lib.Class.columnOpenClose.prototype,{
		_initialize: function() {
			this._appendBtn();
			this._appendText();
			if (this.options.useHalfOpen) {
				this.halfOpenHandler();
			} else {
				this.openHandler();
			}
		},
		_appendBtn: function() {
			this.targetObj.css({position:"relative"});
			this.closeBtn = $("<a />")
			.addClass("closeBtn")
			.attr({href:"javascript:void(0);",title:"閉じる"})
			.text("閉じる")
			.appendTo(this.targetObj);
			this.openBtn = $("<a />")
			.addClass("openBtn")
			.attr({href:"javascript:void(0);",title:"開く"})
			.text("開く")
			.appendTo(this.targetObj);
		},
		_appendText: function() {
			this.linkTextOpen = this.options.linkTextOpen || this.targetObj.find("h2 span").eq(0).text().replace(/ \（.*\）/,"")  + "一覧を見る";
			this.linkTextHalfOpen = this.options.linkTextHalfOpen || this.linkTextOpen;
			this.linkTextClose = this.options.linkTextClose;
			this.openCloseText = $("<div />")
			.addClass("openCloseText")
			.appendTo(this.targetObj);
			this.openCloseTextLink = $("<a />")
			.attr("href","javascript:void(0);")
			.appendTo(this.openCloseText)
		},
		_open: function() {
			this.targetObj.removeClass("halfOpened closed").addClass("opened");
			this.openCloseTextLink.text(this.linkTextClose);
			this.options.actionOpen.call(this);
			//this.showHideArea.animate({height:this.openHeight});
			if (this.options.useHalfOpen) {
				this.closeBtn.add(this.openCloseTextLink).unbind("click").click(this.halfOpenHandler);
			} else {
				this.closeBtn.add(this.openCloseTextLink).unbind("click").click(this.closeHandler);
			}
		},
		_close: function() {
			this.targetObj.removeClass("opened halfOpened").addClass("closed");
			this.openCloseTextLink.text(this.linkTextOpen);
			//this.showHideArea.slideUp();
			this.options.actionClose.call(this);
			if (this.options.useHalfOpen) {
				this.openBtn.add(this.openCloseTextLink).unbind("click").click(this.halfOpenHandler);
			} else {
				this.openBtn.add(this.openCloseTextLink).unbind("click").click(this.openHandler);
			}
		},
		_halfOpen: function() {
			this.targetObj.removeClass("opened closed").addClass("halfOpened");
			this.openCloseTextLink.text(this.linkTextHalfOpen);
			this.options.actionHalfOpen.call(this);
			this.openBtn.add(this.openCloseTextLink).unbind("click").click(this.openHandler);
			this.closeBtn.unbind("click").click(this.closeHandler);
		}
	});

	//move aigent iframe content to parent window and show.
	//移動要素のクラス名のみ移行。
	//use window.load to prevent iframeLoaded? probrems.
	cau1Lib.Class.showRecommendsFromAigent = function(options) {
		var self = this;
		//optional settings
		this.options = $.extend({
			dispArea:"#aigentRecommendArea",
			targetIframe:"#aigentFrame",
			moveObj:"ul.showItemSummary"
		},options);
		this.dispArea = $(this.options.dispArea);
		this.iframe = $(this.options.targetIframe);
		this.childDoc = this.iframe.contents();
		this.moveObj = this.childDoc.find(this.options.moveObj);
		this._moveContent();
	};
	$.extend(cau1Lib.Class.showRecommendsFromAigent.prototype,{
		_moveContent:function() {
			this.appendObj = $("<" + this.moveObj.attr("tagName") + " />")
			.addClass(this.moveObj.attr("className"))
			.html(this.moveObj.html())
			.insertAfter(this.iframe);
			//reattach events
			if (this.appendObj.hasClass("showItemSummary")) {
				var opt = {};
				if (location.href.match(/\/mypage|\/profile/) && (location.href.match(/\/mypage\/recommend/) == null))
					opt.appendArea = "div.profileArea";
				this.appendObj.find(".itemImage").each(function() {
					new cau1Lib.Class.showItemSummary(this,opt);
				});
			}
			this.dispArea.show();
		}
	});

	//mypage accordion menu in side_nav
	cau1Lib.Class.myPageMenu = function(options) {
		var self = this;
		//optional settings
		options = $.extend({
			target:"#sideNav .userContentsBox dl.userMenu dd.mypage",
			opened:false
		},options);
		this.targetObj = $(options.target);
		this.parentObj = this.targetObj.prev();
		this.openAtStart = options.opened;
		this.toggleHandler = function() {self._toggleShowHide();};
		this._createBtn();
		if (this.openAtStart)
			this.toggleHandler();
	};
	$.extend(cau1Lib.Class.myPageMenu.prototype,{
		_createBtn: function() {
			this.parentObj.css({background:"none", paddingLeft:0});
			this.clickArea = $("<div />")
			.text("マイページメニュー")
			.addClass("clickBtn")
			.prependTo(this.parentObj)
			.click(this.toggleHandler);
			//this.parentObj.click(this.toggleHandler);
		},
		_toggleShowHide: function() {
			this.targetObj.slideToggle();
			this.clickArea.toggleClass("opened");
		}
	});

	//ヘッダー検索フォームの切り替え
	cau1Lib.Class.headerSearchChangeType = function(options) {
		var self = this;
		//optional settings
		this.options = $.extend({
			targetForm:"#itemSearch"
		},options);
		this.form = $(this.options.targetForm);
		this.select = this.form.find("select[name='c']");
		this.additionalInput = $("<input type='hidden' />").attr({disabled:"disabled"}).appendTo(this.form);
		this.submitHandler = function() {self._submit();};
		this.form.submit(this.submitHandler);
	};
	$.extend(cau1Lib.Class.headerSearchChangeType.prototype,{
		_submit: function() {
			if (this.select.val() == "typeAuthor") {
				this.additionalInput
				.removeAttr("disabled")
				.attr({name:"type",value:"author"});
				this.select.attr({disabled:"disabled"});
			} else if (this.select.val() == "typeISBN") {
				this.additionalInput
				.removeAttr("disabled");
				this.additionalInput.attr({name:"type", value:"isbn"});
				this.select.attr({disabled:"disabled"});
			} else if (this.select.val() == "") {
				this.select.attr({disabled:"disabled"});
			}
		}
	});

//profile.livedoor.comからnickname取得。
	//今の所、複数回リクエストが飛んでる
	cau1Lib.Class.nickNames = function() {//singleton
		var self = arguments.callee;
		if (self.instance == null) {
			this._initialize.apply(this,arguments);
			self.instance = this;
		}
		return self.instance;
	};
	$.extend(cau1Lib.Class.nickNames.prototype,{
		_initialize: function(options) {
			var self = this;
			//optional settings
			this.options = $.extend({
				requestURL:"http://portal.profile." + (window.location.hostname.match(/\.dev/)? "dev.":"") + "livedoor.com/api/user/nick?callback=?"
			},options);
			this.nickDB = {};
		},
		_fetch: function(obj,ldId) {
			var self = this;
			//handle timeout (or any other error)
			var timeOutChecker = window.setTimeout(function(){self._replaceTextAndShow(obj,ldId);},1000);
			//$.ajaxSetup({cache:false});
			$.getJSON(this.options.requestURL,{livedoor_id:ldId},function(data) {
				window.clearTimeout(timeOutChecker);
				if (data.status == "success") {
					self.nickDB[ldId] = data.nick;
				} else {
					self.nickDB[ldId] = ldId;
				}
				self._replaceTextAndShow(obj,self.nickDB[ldId]);
			});
		},
		_replaceTextAndShow: function(obj,text) {
			obj.text(text).show();
		},
		rewrite: function(jqTarget) {
			var ldId = jqTarget.text();
			if (this.nickDB[ldId])
				this._replaceTextAndShow(jqTarget,this.nickDB[ldId]);
			else
				this._fetch(jqTarget,ldId);
		}
	});

	// preview in mini window
	cau1Lib.Class.previewWindow = function(reloadAction, options) {
		var self = this;
		//optional settings
		this.options = $.extend({
			targetForm:"#reviewForm, #topicForm, #commentForm",
			container:"#reviewPreview",
			openBtn:"#previewReview",
			closeBtn:".closeBtn a",
			reloadBtn:".reloadBtn a"
		},options);
		this.options.dialogOption = $.extend({
			title:"プレビュー",
			autoOpen:false,
			position:[10,10],
			width:500,
			height:"auto",
			dialogClass:"previewWindow",
			resizable:!$.browser.msie
		},this.options.dialogOption);
		this.reloadAction = reloadAction;
		this.targetFormObj = $(this.options.targetForm),
		this.dialogObj = $(this.options.container);
		this.openBtnObj = $(this.options.openBtn);
		this.closeBtnObj = this.dialogObj.find(this.options.closeBtn);
		this.reloadBtnObj = this.dialogObj.find(this.options.reloadBtn);
		this.openHandler = function() {self._open();};
		this.closeHandler = function() {self._close();};
		this.reloadHandler = function() {self.reloadAction();};
		this._initialize();
		$(window).bind("closeAllOverlays",this.closeHandler);
	};
	$.extend(cau1Lib.Class.previewWindow.prototype,{
		_initialize: function() {
			//create dialog
			this.dialogObj.dialog(this.options.dialogOption);
			//set action to buttons
			this.openBtnObj.click(this.openHandler);
			this.closeBtnObj.click(this.closeHandler);
			this.reloadBtnObj.click(this.reloadHandler);
		},
		_open:function() {
			this.reloadAction();
			this.dialogObj.dialog("open");
			//reset window size
			$(".ui-dialog").height("auto")
			$(".ui-dialog-content").height("auto");
			$(window).trigger("hideSelectIfIE6");
		},
		_close:function() {
			this.dialogObj.dialog("close");
			$(window).trigger("resetSelectIfIE6");
		}
	});

	//set itemName to title of input, to send it to SiteCatalyst.
	cau1Lib.Class.setTitleToInput = function(area,options) {
		//optional settings
		this.options = $.extend({
			nameArea:"p.title a:first",
			targetInput:"input[name^='itemid']:first",
			usedFlg:false
		},options);
		var self = this;
		this.area = $(area);
		this.targetInput = this.area.find(this.options.targetInput);
		this.name = this.area.find(this.options.nameArea).text();
		if (this.options.usedFlg)
			this.name += '|中古';
		if (this.targetInput.attr("title") == "")
			this.targetInput.attr("title",this.name);
	}

	// cartin message
	cau1Lib.Class.cartInMessage = function(target, options) {
		var self = this;
		//optional settings
		this.options = $.extend({
			disappearAfter:10000,
			closeBtn:".closeBtn a",
			titleArea:"> dt:first"
		},options);
		this.options.dialogOption = $.extend({
			dialogClass:"cartInWindow",
			width:195,
			height:"auto",
			position:["center",120],
			resizable:false
		},this.options.dialogOption);
		this.target = $(target);
		this.closeBtnObj = this.target.find(this.options.closeBtn);
		this.closeHandler = function() {self._close();};
		this.openHandler = function() {self._open();};
		this.titleArea = this.target.find(this.options.titleArea);
		this.options.dialogOption.title = this.titleArea.html();
		this.options.dialogOption.open = this.openHandler;
		this._initialize();
	};
	$.extend(cau1Lib.Class.cartInMessage.prototype,{
		_initialize: function() {
			//hide title img
			this.titleArea.hide();
			//create dialog
			this.target.dialog(this.options.dialogOption);
			//set action to buttons
			this.closeBtnObj.click(this.closeHandler);
			//self close after certain time
			this.timeOutChecker = window.setTimeout(this.closeHandler,this.options.disappearAfter);
			$(window).bind("closeAllOverlays",this.closeHandler);
		},
		_open:function() {
			this.target.height("auto").show();
			this.target.parents(".ui-dialog:first").css({opacity:"0.95"});
			$(window).trigger("hideSelectIfIE6");
		},
		_close:function() {
			this.target.fadeOut("fast",function() {$(this).dialog("close");});
			$(window).trigger("resetSelectIfIE6");
		}
	});

	// hide selectbox in IE6 (if overlay happens)
	// see http://blog.fkoji.com/2006/09161340.html
	cau1Lib.Class.showHideSelect = function() {
		var self = this;
		this.hideHandler = function() {self._hide();};
		this.showHandler = function() {self._show();};
		if (cau1Lib.browserIeBelow6) {
			this.selects = $("select");
			this.selects.wrap("<b />");
			this.selects.parent("b").css({fontWeight:"normal"});
			$(window)
			.bind("hideSelectIfIE6",this.hideHandler)
			.bind("resetSelectIfIE6",this.showHandler);
		}
	};
	$.extend(cau1Lib.Class.showHideSelect.prototype,{
		_hide: function() {
			var self = this;
			this.selects.css({visibility:"hidden"})
			.parent("b").css({border:"1px solid gray"}).bind("click.showHideSelect",function() {self._show("fromBTag");});
		},
		_show: function(opt) {
			this.selects.css({visibility:"visible"})
			.parent("b").css({border:"none"}).unbind("click.showHideSelect");
			if (opt == "fromBTag")
				$(window).trigger("closeAllOverlays");
		}
	});

	//properties
	$.extend(cau1Lib,{
		//browserIeBelow6 (boolean)
		browserIeBelow6: ($.browser.msie && $.browser.version < 7),
		//pageWidthLiquid (boolean)
		pageWidthLiquid: new function(){
			$(function() {
				cau1Lib.pageWidthLiquid = $("div#wrp").is("div");
			})
		},
		//add css rule (rather than set style to element)
		cssRules: new cau1Lib.Class.newStyleSheet()
	});


	//methods
	$.extend(cau1Lib,{
		//show recommend accesser from child window
		showRecommend: function(options) {
			new cau1Lib.Class.showRecommendsFromAigent(options);
		},
		//pageWidth has changed? (since last time called)
		pageWidthHasChanged: function() {
			var checker = new cau1Lib.Class.windowSizeChecker();
			return checker.check();
		},
		//font size has changed? (since last time called)
		fontSizeHasChanged: function() {
			var checker = new cau1Lib.Class.fontSizeChecker();
			return checker.check();
		},
		//高さ揃え
		equalizeHeight: function(obj) {
			var equalizerObj = new cau1Lib.Class.heightEqualizer(obj);
			return equalizerObj;
		},
		//shuffle an array(or array like object) itself
		_shuffle: function(ary) {
			var i = ary.length;
			while(i){
				var j = Math.floor(Math.random()*i);
				var t = ary[--i];
				ary[i] = ary[j];
				ary[j] = t;
			}
			return ary;
		},
		//rollover
		setRollOver: function(hoverObj,img) {
			var imgName = img.src;
			var dot = imgName.lastIndexOf('.');
			var imgNameHover = imgName.substr(0, dot) + '_on' + imgName.substr(dot);
			//preload
			var imgCache = new Image();
			imgCache.src = imgNameHover;
			$(hoverObj).hover(
				function() { img.src = imgNameHover;},
				function() { img.src = imgName;}
			);
		},
		// http://gyauza.egoism.jp/clip/archives/2007/04/ie6minwidthminheight/
		SetFakeMinWidth: function(target,MinWidthSetter){
			//  子コンテナをtableで囲む
			$(target)
			.wrap("<table width='100%' cellspacing='0' style='margin:0; padding:0; border:0;'><tr><td><\/td><\/tr><\/table>")
			.parent("td").prepend("<div style='height:1px;overflow:hidden;text-align:left;vertical-align:top'>&nbsp;<\/div>")
			.find("div:first").css("width",$(MinWidthSetter).css("min-width"));
		},
		//input default text function
		defaultTextToInput: function(_target, _text) {
			_target.val(_text)
			.css({color:"#999999"})
			.focus (function() {
				$(this).css({color:""});
				if ($(this).val() == _text) {$(this).val("");}
			})
			.blur (function() {
				if ($(this).val() == "") {
					$(this).val(_text).css({color:"#999999"});
				}
			})
			.each (function() {
				var _self = $(this);
				_self.parents("form:last").submit (function() {
					if (_self.val() == _text) {_self.val("");}
					return true;
				});
			});
		},
		//input check
		inputCheck: function(formObj) {
			if ($(formObj).find("input:checkbox:checked").length < 1) {
				alert ("商品を選択してください");
				return false;
			} else {
				return true;
			}
		},
		//set default to selectBox
		setDefaultToSelect: function(selectObj,_value) {
			$(selectObj).select(function() {
				if ($(this).val() == "") {
					$(this).val(_value);
				}
			});
		},
		//shuffles the content inside the target
		shuffleContent: function(target) {
			//accept both array or string
			if (target.constructor == Array) {
				var targets = $.map(target,function(n) {return n;});
			} else {
				var targets = [target];
			}
			var contents = $.map(targets,function(n) {
				return n.map(function() {
					return $(this).children();
				})
			});
			var idx = targets[0].map(function(i){return i});
			cau1Lib._shuffle(idx);
			$.each(idx,function(i,val) {
				$.each(targets,function(j) {
					this.eq(i).append(contents[j].get(val));
				});
			});
		}
	});

})(jQuery);
