/************************************************
 * 
 * エクスプローラバーJS(※スライドの場合、左は無効)
 * 
 * DEPENDENCIES
 *  - /common_style/js/library/jquery.js
 *  - /common_style/js/layerutil.js
 * 
 * Author        : $Author: akasaka_y $
 * Last Modified : $Date: 2008/08/12 09:22:53 $
 * Version       : $Revision: 1.3 $
 * 
 * (c) CYBER AGENT.LTD
 * 
 ************************************************/
var ExplorerBar = {
	DefaultUrl:'#',
	areaId:'sub_a',
	searchContentsId:'explorerbarContent',
	closeId:'explorerbarCloseBtn',
	mainContentsId:'main',
	sliderFlag:false,		// スライドするかしないか(displayPosition=rightの時はスライド不可)
	displayPosition:'left',	// 表示位置：left or right

	zIndex:2777,
	moveSpeed:3,
	movePosition:-200,//エクスプローラバー幅分(クローズ部分を差し引く)
	moveJudge:-1,
	moveTarget:0,
	onTop:0,offTop:0,
	onPosition:0,offPosition:-200,
	onAreaWidth:211,offAreaWidth:11,
	closeTop:100,
	onTitle:'閉じる',offTitle:'開く',
	timerId:null,

	/**
	 * 初期処理(window.onloadで呼ぶこと)
	 * 
	 * @param boolean sliderFlag スライドするかしないか(任意)
	 * @param String displayPosition 表示位置：left or right(任意)
	 * @param boolean shortcutKeyFlag ショートカットキーを設定するかどうか(任意)
	 */
	initialize:function (sliderFlag, displayPosition, shortcutKeyFlag){
		var self = this;
		this.offPosition['left']=-200;
		this.offPosition['right']=0;
		var contents = '';
		if (sliderFlag != null){this.sliderFlag = sliderFlag;}
		if (displayPosition != null){this.displayPosition = displayPosition;}
		if (this.displayPosition == 'right'){
			// スライドは無効
			this.sliderFlag = false;
			this.offPosition = 0;
		}
		if (document.getElementById(this.areaId) == null) {
			// 直接htmlに書いたほうがよい
			contents += '<div id="'+this.areaId+'" class="'+this.displayPosition+' explorerbarArea">';
			contents += '<p class="closeArea"><a class="off" id="'+this.closeId+'" onmousemove="ExplorerBar.closePosition(event, this);" href="javascript:void(0);" title="+this.offTitle+"><span>×</span></a></p>';
			contents += '<div id="'+this.searchContentsId+'">';
			contents += '</div>';
			contents += '</div>';
			$("body").append(contents);
		}
		var offheight = (this.setBodyHeight()) || 0;
//		alert(offheight);
		var target = document.getElementById(this.areaId) || null;
		if (target) {
			target.style.zIndex = this.zIndex;
			target.style.height = offheight + 'px';
			target.style.display = 'block';
			target.style.position = 'absolute';
			var nowClassName = target.className || '';
			target.className = nowClassName+' '+this.displayPosition;
			if (this.displayPosition == 'left') {
				document.getElementById(this.searchContentsId).style.display = 'block';
			}
			if (this.sliderFlag == true) {
				target.onmouseover = function(){self.slider(true);};
			}
			$(window).resize(function (){document.getElementById(ExplorerBar.areaId).style.height=self.setBodyHeight()+'px';});
		}
		if (this.sliderFlag == true) {
			document.getElementById(this.mainContentsId).onmouseover = function(){self.slider(false);};
		}
		this.hide();
		if (shortcutKeyFlag == true) {
			this.shortcutKey();
		}
	},
	/**
	 * ショートカット設定
	 */
	shortcutKey:function (){
		var self = this;
		if (self.sliderFlag == false) {
			$(document).keyup(function(e){
				//検索エクスプローラ表示(スライドの場合は処理しない)：ctrl + alt + e
				if(e.ctrlKey && e.altKey && (e.keyCode == 69)){
					self.switchView();
				} else if (e.ctrlKey && e.altKey && (e.keyCode == 9)) {	// 左右切替(スライドの場合は処理しない) ：ctrl + alt + tab
					var displayFlag = false;
					var className = document.getElementById(self.closeId).className || '';
					if (className == 'on') {displayFlag = true;}
					self.switchPositionType();
					if (displayFlag) {self.hide();} 
					else {self.show();}
				}
			});
		}
	},
	/**
	 * 表示
	 */
	show:function (){
		this.changePosition(this.onPosition, this.onTop);
		this.changeBar(false);
	},
	/**
	 * 非表示
	 */
	hide:function (){
		var position = this.offPosition || 0;
		if (this.sliderFlag == false) {position = 0;}
		this.changePosition(position, this.offTop);
		this.changeBar(true);
	},
	/**
	 * 表示切替：開閉
	 */
	switchView:function (){
		var target = document.getElementById(this.closeId) || null;
		if (target != null) {
			if (target.className != 'off') {
				this.show();
			} else {
				this.hide();
			}
		}
	},
	/**
	 * スクロールin
	 */
	moveIn:function (){
		var self = this;
		this.movePosition = Math.round(this.movePosition + (this.moveTarget - this.movePosition) / this.moveSpeed);
		this.timerId = setTimeout(function(){self.moveIn();},10);
		if (this.movePosition > this.moveJudge) {
			this.changePosition(this.moveTarget);
			clearTimeout(this.timerId);
		} else {
			this.changePosition(this.movePosition);
		}
		this.checkPosition();
	},
	/**
	 * スクロールout
	 */
	moveOut:function (){
		var self = this;
		this.movePosition = Math.round(this.movePosition + (this.moveTarget - this.movePosition) / this.moveSpeed);
		this.timerId = setTimeout(function(){self.moveOut();},10);
		if (this.movePosition < this.moveJudge) {
			this.changePosition(this.moveTarget);
			clearTimeout(this.timerId);
		} else {
			this.changePosition(this.movePosition);
		}
		this.checkPosition();
	},
	/**
	 * スクローリング
	 */
	slider:function (inoutFlag){
		if (inoutFlag == true) {	//表示
			this.moveTarget = 0;
			this.moveJudge = -1;
			this.moveIn();
		}
		if (inoutFlag == false) {	//非表示
			this.moveTarget = this.offPosition;
			this.moveJudge = this.offPosition-1;
			this.moveOut();
		} else {
			return false;
		}
	},
	/**
	 * クローズ背景画像の位置をカーソル位置に移動
	 */
	closePosition:function (e, target){
		if(document.all)e = event;
		var top = e.clientY || this.closeTop;
		var scrollTop = Math.max(window.self.document.body.scrollTop,window.self.document.documentElement.scrollTop);
		var offTop = 10;
		if ((top+scrollTop-20) > 10) {offTop = (top+scrollTop-20)}
		target.style.backgroundPosition = '0px '+offTop+'px';
	},
	/**
	 * エクスプローラ表示位置を確認し、クローズボタン処理変動
	 */
	checkPosition:function (){
		var target = document.getElementById(this.areaId) || null;
		if (target != null) {
			if (this.displayPosition == 'left' && (target.style.left == '0px' || target.style.left == '-1px')) {
				this.changeBar(false);
			} else if (this.displayPosition == 'right' && (target.style.right == '0px' || target.style.right == '-1px')) {
				this.changeBar(false);
			} else {
				this.changeBar(true);
			}
		}
	},
	/**
	 * 位置変動
	 */
	changePosition:function (position, top){
		var target = document.getElementById(this.areaId) || null;
		var position = position || 0;
		var top = top || 0;
		if (target != null) {
			if (this.displayPosition == 'right') {
				target.style.right = position + 'px';
				target.style.left = 'auto';
			} else {
				target.style.right = 'auto';
				target.style.left = position + 'px';
			}
			target.style.top = (top + 'px');
//alert(sign + position + 'px'+"\nleft="+target.style.left+"\nright="+target.style.right+"\ntop="+target.style.top);
		}
	},
	/**
	 * 左右切替
	 */
	switchPositionType:function (){
		var target = document.getElementById(this.areaId) || null;
		var oldPosition = this.displayPosition || '';
		if (this.displayPosition == 'right') {
			this.displayPosition='left';
		} else {
			this.displayPosition='right';
		}
		var getClassName = target.className || '';
		if (getClassName.indexOf(oldPosition) > -1) {
			setClassName = getClassName.replace(oldPosition, this.displayPosition);
			target.className = setClassName;
//			alert(target.className);
		}
	},
	/**
	 * クローズボタン処理変動
	 */
	changeBar:function (flag){
		var self = this;
		var target = document.getElementById(this.closeId) || null;
		if (target != null) {
			if (flag) {
				if (this.sliderFlag == false) {
					document.getElementById(this.searchContentsId).style.display = 'none';
					document.getElementById(this.areaId).style.width = this.offAreaWidth + 'px';
					target.oncontextmenu = function(){self.switchPositionType();self.hide();return false;};
				}
				if (window.createPopup && !window.opera) {LayerUtil.closeLayerBackground(document.getElementById(this.areaId));}
				if (target.className != 'on') {
					target.onclick = function(){self.show();};
					target.className = 'on';
					target.title = this.offTitle;
				}
			} else {
				if (this.sliderFlag == false) {
					document.getElementById(this.searchContentsId).style.display = 'block';
					document.getElementById(this.areaId).style.width = this.onAreaWidth + 'px';
					target.oncontextmenu = function(){self.switchPositionType();self.show();return false;};
				}
				// IE5.5以上のみ、セレクトボックスが優先表示されるため、targetの下にiframeを敷く
				if (window.createPopup && !window.opera) {LayerUtil.openLayerBackground(document.getElementById(this.areaId), this.displayPosition);}
				if (target.className != 'off') {
					target.onclick = function(){self.hide();};
					target.className = 'off';
					target.title = this.onTitle;
				}
			}
			if (this.sliderFlag == true) {
				//スライドの時はクローズボタンの処理は消す
				target.onclick = function(){};
				target.title = '';
			}
		}
	},
	/**
	 * body内高さ取得
	 */
	setBodyHeight:function (){
		var yScroll = 0;
		if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) { // firefox
			yScroll = window.innerHeight + window.scrollMaxY;
			var deff = document.documentElement;
			var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
			yScroll -= (window.innerHeight - hff);
		} else if (window.innerHeight && document.body.offsetHeight){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
			if (window.innerHeight > document.body.offsetHeight) {
				if (document.body.scrollHeight < window.innerHeight) {
					yScroll = window.innerHeight;
				}
			} else if (document.body.scrollHeight < document.body.offsetHeight) {
				yScroll = document.body.offsetHeight;
			}
		} else if (document.body.scrollHeight){ // IE
			yScroll = (document.body.scrollHeight > document.documentElement.scrollHeight)?document.body.scrollHeight:document.documentElement.scrollHeight;
			if (document.body.offsetHeight > yScroll) {
				yScroll = document.body.offsetHeight;
			}
//			alert("document.body.offsetHeight="+document.body.offsetHeight+"\ndocument.body.scrollHeight="+document.body.scrollHeight+"\ndocument.documentElement.scrollHeight="+document.documentElement.scrollHeight);
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = document.body.offsetHeight;
		}
		return yScroll;
	}
};
