// JavaScript Document
var SearchConfig = {
	ID_FLASH_AREA		: "jsid-searchFlashArea",
	ID_E_FLASH_AREA		: "jsid-searchExpandFlashArea",
	ID_ASSOC_LEFT		: "jsid-searchAssocLeft",
	ID_ASSOC_BTN		: "jsid-searchAssocBtn",
	ID_ASSOC_CLEAR_BTN	:	"jsid-assocClearBtn",
	ID_ASSOC_TEXT_BTN	: "jsid-assocTextBtn",
	ID_ASSOC_TEXT_AREA	: "jsid-assocTextArea",
	ID_ASSOC_TODAY_BTN	: "jsid-assocTodayBtn",
	ID_ASSOC_TODAY_LINK	: "jsid-assocTodayLink",
	ID_MATCH_LEFT		: "jsid-searchMatchLeft",
	ID_MATCH_BTN		: "jsid-searchMatchBtn",
	ID_MATCH_COND		: "jsid-searchMatchCond",
	ID_MATCH_CLEAR_BTN	:	"jsid-matchClearBtn",
	ID_MATCH_SEARCH_BTN	: "jsid-matchSearchBtn",
	ID_MATCH_TEXT_AREA	: "jsid-matchTextArea",
	ID_MATCH_BOOK_TAB	: "jsid-matchBookTab",
	ID_MATCH_WORKS_TAB	: "jsid-matchWorksTab",
	ID_MATCH_PERSON_TAB	: "jsid-matchPersonTab",
	ID_MATCH_BOOK_LIST	: "jsid-matchBookList",
	ID_MATCH_WORKS_LIST	: "jsid-matchWorksList",
	ID_MATCH_PERSON_LIST: "jsid-matchPersonList",
	ID_LOCK_BTN			: "jsid-searchLockBtn",
	ID_SHELF_BTN		: "jsid-searchShelfsBtn",
	
	ID_HEADER			: "gl-documentHeader",
	ID_HEADER_LAYER		: "layer-documentHeader",
	ID_DOC_BODY			: "layer-documentBody",
	
	CLS_MATCH_CONDLIST	: "set-ConditionList",
	CLS_SET_ERROR		: "set-Error",
	CLS_RELEASE_LOCK	: "set-Fluxion",
	CLS_SET_CLOSED		: "set-Closed",
	
	IMG_MATCH_COND_BTN_ON	: "/externals/images/btn-condition-01-ovr.gif",
	IMG_MATCH_COND_BTN_OFF	: "/externals/images/btn-condition-02-ovr.gif",
	IMG_LOCK_BTN_LOCK		: "/externals/images/icn-lock-02.png",
	IMG_LOCK_BTN_RELEASE	: "/externals/images/icn-lock-01.png",
	IMG_LOCK_BTN2_LOCK		: "/externals/images/icn-lock-04.png",
	IMG_LOCK_BTN2_RELEASE	: "/externals/images/icn-lock-03.png",
	
	IMG_ASSOC_BTN_PRE		: "/externals/images/tab-gh-01",
	IMG_MATCH_BTN_PRE		: "/externals/images/tab-gh-02",
	IMG_SHELF_BTN_PRE		: "/externals/images/tab-gh-03",
	
	ASSOC_WIDTH:376,
	MATCH_WIDTH:561,
	EXPAND_HEIGHT:2915,
	DEFAULT_HEIGHT:224,
	DIFF_FLASH_HEIGHT:55
}




/**
* a class which operates whole search area
**/
var SearchCondition = function(){
	this.assocBtn	= document.getElementById(SearchConfig.ID_ASSOC_BTN);
	this.assocClearBtn = document.getElementById(SearchConfig.ID_ASSOC_CLEAR_BTN);
	this.matchBtn	= document.getElementById(SearchConfig.ID_MATCH_BTN);
	this.matchClearBtn = document.getElementById(SearchConfig.ID_MATCH_CLEAR_BTN);
	this.shelfBtn	= document.getElementById(SearchConfig.ID_SHELF_BTN);
	this.assocLeft	= document.getElementById(SearchConfig.ID_ASSOC_LEFT);
	this.matchLeft	= document.getElementById(SearchConfig.ID_MATCH_LEFT);
	this.flashArea	= document.getElementById(SearchConfig.ID_FLASH_AREA);
	this.eFlashArea = document.getElementById(SearchConfig.ID_E_FLASH_AREA);
	this.lockBtn	= document.getElementById(SearchConfig.ID_LOCK_BTN);
	
	this.header		= document.getElementById(SearchConfig.ID_HEADER);
	this.headerLayer= document.getElementById(SearchConfig.ID_HEADER_LAYER);
	this.docBody	= document.getElementById(SearchConfig.ID_DOC_BODY);
	this.isShelfChanging = false;
	this.intervalId;
	
	this.tabArr		= new Array();
	this.tabArr[SearchMode.ASSOC] = {btn:this.assocBtn, img:SearchConfig.IMG_ASSOC_BTN_PRE};
	this.tabArr[SearchMode.MATCH] = {btn:this.matchBtn, img:SearchConfig.IMG_MATCH_BTN_PRE};
	this.tabArr[SearchMode.SHELF] = {btn:this.shelfBtn, img:SearchConfig.IMG_SHELF_BTN_PRE};
	
	this.matchCondition = new MatchCondition();
	this.assocCondition = new AssocCondition();
	
	//trace(this.matchLeft)
	this.matchLeft.style.display = "none";
	//trace(this.eFrashArea)
	this.eFlashArea.style.display = "none";
	
	this._setListener();
	this._reset();
}

SearchCondition.prototype = {
	_setListener:function(){
		var self = this;
		
		var len = this.tabArr.length;
		for(mode in this.tabArr){
			(function(){
				var tab = self.tabArr[mode];
				var tabMode = mode;
				var img = tab.btn.getElementsByTagName("IMG")[0];
				tab.btn.onclick = function(){
					if(self.isShelfChanging) return;
					self.changeSearchMode(tabMode);
				}
				tab.btn.onmouseover = function(){
					if(GlobalValues.searchMode == tabMode) return;
					img.src = tab.img + "-ovr.png";
				}
				tab.btn.onmouseout = function(){
					if(GlobalValues.searchMode == tabMode) return;
					img.src = tab.img + ".png";
				}
			})();
		}
		
		this.lockBtn.onclick = function(){
			GlobalValues.headerLockFlg = !GlobalValues.headerLockFlg;
			var img = this.getElementsByTagName("IMG")[0];
			if(GlobalValues.headerLockFlg){
				$(document.body).addClass(SearchConfig.CLS_RELEASE_LOCK);
				img.src = SearchConfig.IMG_LOCK_BTN_LOCK;
			}else{
				$(document.body).removeClass(SearchConfig.CLS_RELEASE_LOCK);
				img.src = SearchConfig.IMG_LOCK_BTN_RELEASE;
			}
		}
		
		this.assocClearBtn.onclick = function(){
			self.assocCondition._clearText();
		}
		
		this.matchClearBtn.onclick = function() {
			self.matchCondition._clearCurrentCondition();
		}
	},
	
	
	setIE6:function(){
		GlobalValues.headerLockFlg = true;
		$(document.body).addClass(SearchConfig.CLS_RELEASE_LOCK);
		var img = this.lockBtn.getElementsByTagName("IMG")[0];
		img.style.display = "none";
		this.lockBtn.onclick = null;
	},
	
	
	changeSearchMode:function(mode){
		if(GlobalValues.searchMode == mode) return;
		
		clearInterval(this.intervalId);
		var self = this;
		var current = GlobalValues.searchMode;
		var currentAssocMatch = (current == SearchMode.ASSOC || current == SearchMode.MATCH);
		var nextAssocMatch = (mode == SearchMode.ASSOC || mode == SearchMode.MATCH);
		
		//Switch Assoc and Match
		if(currentAssocMatch && nextAssocMatch){
			this._toggleAssocMatch(mode);
			return;
		}
		
		if(mode == SearchMode.SHELF)
			HistoryManager.setURL(self, self._expandShelf);
		else this._shrinkShelf(mode);
	},
	
	_expandShelf:function(){
		scrollToTop();
		
		this.lockBtn.style.display = "none";
		this.isShelfChanging = true;
		
		var self = this;
		var currentIsAssoc = (GlobalValues.searchMode == SearchMode.ASSOC);
		var target = (currentIsAssoc) ? this.assocLeft : this.matchLeft;
		
		var childLen = target.childNodes.length;
		for(var i = 0; i < childLen; i++){
			try {
				target.childNodes[i].style.display = "none";
			}catch(e){}
		}
		
		var prevWidth = this.flashArea.offsetWidth;
		this.header.style.position = "relative";
		this.headerLayer.style.height = Number(SearchConfig.EXPAND_HEIGHT + SearchConfig.DIFF_FLASH_HEIGHT) + "px";
		this.docBody.style.paddingTop = "0px";
		this.docBody.style.display = "none";
		this.flashArea.style.display = "none";
		this.eFlashArea.style.display = "block";
		target.style.width = "10px";
		
		var so = new SWFObject("/externals/flash/swf/baseShelf.swf", TopConfig.EXTERNAL_E_SHELF, "100%", SearchConfig.EXPAND_HEIGHT, "10");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("width", prevWidth);
		so.addVariable("stageWidth", $(this.eFlashArea).width());
		so.addVariable("stageHeight", SearchConfig.EXPAND_HEIGHT);
		so.addVariable("saved_id", CookieManager.getShelfSavedId().replace(/%/g, '%25'));
		so.addVariable('is_member', CookieManager.isMember());
		so.write(TopConfig.ID_E_FLASH_AREA);
		
		var moved = function(){
			self.isShelfChanging = false;
			self._activateTab(mode);
			GlobalValues.searchMode = mode;
		}
		
		shClearShelfSWF();
		setTimeout(moved, 2000);
	},
	
	
	_shrinkShelf:function(mode){
		this.isShelfChanging = true;
		
		var self = this;
		var eswf = getSwfElement(TopConfig.EXTERNAL_E_SHELF);
		var targetWidth = (mode == SearchMode.ASSOC) ? SearchConfig.ASSOC_WIDTH : SearchConfig.MATCH_WIDTH;
		var prevWidth = this.flashArea.parentNode.offsetWidth - targetWidth;
		eswf.shrinkShelf(prevWidth);
		
		var moved = function(){
			self.lockBtn.style.display = "";
			var expandSwf = getSwfElement(TopConfig.EXTERNAL_E_SHELF);
			expandSwf.height = SearchConfig.DEFAULT_HEIGHT;
		
			var childLen = self.assocLeft.childNodes.length;
			for(var i = 0; i < childLen; i++){
				try {
					self.assocLeft.childNodes[i].style.display = "";
				}catch(e){}
			}
			childLen = self.matchLeft.childNodes.length;
			for(i = 0; i < childLen; i++){
				try {
					self.matchLeft.childNodes[i].style.display = "";
				}catch(e){}
			}
			
			self.header.style.position = "";
			self.headerLayer.style.height = "";
			self.docBody.style.paddingTop = "";
			self.docBody.style.display = "";
			self.flashArea.style.display = "";
			self.eFlashArea.style.display = "none";
			self.assocLeft.style.width = (mode == SearchMode.ASSOC) ? SearchConfig.ASSOC_WIDTH + "px" : SearchConfig.MATCH_WIDTH + "px";
			self.matchLeft.style.width = SearchConfig.MATCH_WIDTH + "px";
			self.assocLeft.style.display = (mode == SearchMode.ASSOC) ? "" : "none";
			self.matchLeft.style.display = (mode == SearchMode.ASSOC) ? "none" : "";
			
			self.isShelfChanging = false;
			self._activateTab(mode);
			GlobalValues.searchMode = mode;
			
			//clear the expanded shelf
			var topEFlashArea = document.getElementById(TopConfig.ID_E_FLASH_AREA);
			while(topEFlashArea.childNodes.length > 0){
				topEFlashArea.removeChild(topEFlashArea.lastChild);
			}
			shLoadShelfSWF();
		}
		
		setTimeout(moved, 2000);
	},
	
	
	_toggleAssocMatch:function(mode){
		trace(GlobalValues.searchMode);
		var self = this;
		var currentIsAssoc = (GlobalValues.searchMode == SearchMode.ASSOC);
		
		if(!currentIsAssoc){
			this.assocLeft.style.display = (currentIsAssoc)?'none':'';
			this.matchLeft.style.display = (currentIsAssoc)?'':'none';
		}
		
		var targetId = (currentIsAssoc) ? SearchConfig.ID_ASSOC_LEFT : SearchConfig.ID_MATCH_LEFT;
		var nextId = (currentIsAssoc) ? SearchConfig.ID_MATCH_LEFT : SearchConfig.ID_ASSOC_LEFT;
		var target = document.getElementById(SearchConfig.ID_ASSOC_LEFT);
		var gotoWidth = (currentIsAssoc) ? SearchConfig.MATCH_WIDTH : SearchConfig.ASSOC_WIDTH;
		
		//execute when moving finished
		var moved = function(){
			self.assocLeft.style.display = (currentIsAssoc)?'none':'';
			self.matchLeft.style.display = (currentIsAssoc)?'':'none';
			
			self._activateTab(mode);
			
			GlobalValues.searchMode = mode;
		}
		
		this._tweenSearchAreaWidth(target, gotoWidth, moved);
	},
	
	
	_tweenSearchAreaWidth:function(target, gotoWidth, callback){
		var self = this;
		var intervalId;
		var widthStr = target.style.width;
		var index = widthStr.indexOf("px");
		var widthNum = parseInt(widthStr.substring(0, index));
		
		var moving = function(){
			widthNum += (gotoWidth - widthNum) / 4;
			target.style.width = widthNum + "px";
			
			if(Math.abs(gotoWidth - widthNum) < 10){
				clearInterval(self.intervalId);
				target.style.width = gotoWidth + "px";
				if(callback) callback();
			}
		}
		
		this.intervalId = setInterval(moving, 30);
	},
	
	
	_activateTab:function(mode){
		var currentTab = this.tabArr[GlobalValues.searchMode];
		var nextTab = this.tabArr[mode];
		var currentImg = currentTab.btn.getElementsByTagName("IMG")[0];
		var nextImg = nextTab.btn.getElementsByTagName("IMG")[0];
		
		currentTab.btn.className = currentTab.btn.className.split(' cur').join('');
		nextTab.btn.className = nextTab.btn.className + ' cur';
		currentImg.src = currentTab.img + ".png";
		nextImg.src = nextTab.img + "-cur.png";
	},
	
	
	setMatchCondValue:function(type, arr, text, order){
		this.matchCondition.setConditionValue(type, arr, text, order);
	},
	
	setAssocText:function(text){
		if(!text) text = "";
		this.assocCondition.setText(text);
	},
	
	_reset:function(){
		this.matchCondition.reset();
	}
}




var MatchSuperCondition = {
	inputIndexTable:new Array(),
	
	sortInputArr:function(){
		if(!this.lists || this.lists.length == 0) return;
		var newArr = new Array();
		var len = this.lists.length;
		for(var i = 0; i < len; i++){
			newArr.push(this.lists[i]);
		}
		
		newArr.sort(function(a,b){
							 return a.tabIndex - b.tabIndex;
							 });
		
		this.lists = newArr;
	},
	
	checkNumber:function(target){
		if(!Validation.isNumber(target.value)){
			$(target).parent("P").addClass(SearchConfig.CLS_SET_ERROR);
			return false;
		}else{
			$(target).parent("P").removeClass(SearchConfig.CLS_SET_ERROR);
			return true;
		}
	},
	
	checkISBN:function(target){
		if(!Validation.isISBN(target.value)){
			$(target).parent("P").addClass(SearchConfig.CLS_SET_ERROR);
			return false;
		}else{
			$(target).parent("P").removeClass(SearchConfig.CLS_SET_ERROR);
			return true;
		}
	},
	
	setInputValue:function(arr, order){
		var options = this.order.options;
		var len = options.length;
		for(var i = 0; i < len; i++) {
			var option = options[i];
			if(option.value == order) {
				this.order.selectedIndex = i;
				break;
			}
		}
		
		if(!arr || arr.length == 0) return;
		var len = this.lists.length;
		for(var i = 0; i < len; i++){
			if(arr[i]) this.lists[i].value = arr[i];
			else this.lists[i].value = "";
			
			/*for(var j = 0; j < len; j++){
				if(this.lists[j].tabIndex == i){
					this.lists[j].value = arr[i];
					break;
				}
			}*/
		}
	},
	
	clearValues:function(){
		var len = this.lists.length;
		for(var i = 0; i < len; i++){
			var input = this.lists[i];
			input.value = "";
		}
		
		var selectBoxs = this.container.getElementsByTagName("SELECT");
		len = selectBoxs.length;
		for(i = 0; i < len; i++) {
			var selectBox = selectBoxs[i];
			selectBox.selectedIndex = 0;
		}
	},
	
	checkValues:function(){
		var len = this.lists.length;
		for(var i = 0; i < len; i++){
			var input = this.lists[i];
			if(input.value) return true;
		}
		
		return false;
	},
	
	getOrder:function(){
		var options = this.order.getElementsByTagName("OPTION");
		//trace(options);
		return options[this.order.selectedIndex].value;
	}
}




/**
* a class which has book detail info
**/
var MatchBookCondition = function(container){
	this.container = document.getElementById(SearchConfig.ID_MATCH_BOOK_LIST);
	this.lists = this.container.getElementsByTagName("INPUT");
	this.radiosTitleFullMatch = [this.lists[1], this.lists[2]];
	this.radiosCreatorFullMatch = [this.lists[4], this.lists[5]];

	this.title = this.lists[0];
	this.author = this.lists[3];
	
	this.ISBN = this.lists[6];
	this.publisher = this.lists[7];
	this.pubYearStart = this.lists[8];
	this.pubYearEnd = this.lists[9];
	
	this.mediaType = this.container.getElementsByTagName("SELECT")[0];
	this.order = this.container.getElementsByTagName("SELECT")[1];
	
	this.sortInputArr();
	this.lists.splice(0, 4);
	
	this.getDetails = function(){
		var error = false;
		if(!this.checkNumber(this.pubYearStart)){
			error = true;
		}
		if(!this.checkNumber(this.pubYearEnd)){
			error = true;
		}
		if(!this.checkISBN(this.ISBN)){
			error = true;
		}
		
		if(error) return false;
		
		var isTitleFullMatch   = (this.radiosTitleFullMatch[0].checked) ? "1" : "";
		var isCreatorFullMatch = (this.radiosCreatorFullMatch[0].checked) ? "1": "";
		
		var mediaTypeStr = "";
		var selectedIndex = this.mediaType.selectedIndex;
		if(selectedIndex == 1) {
			mediaTypeStr = "S";
		} else if(selectedIndex == 2) {
			mediaTypeStr = "B";
		}
		
		var returnArr = new Array(this.title.value, this.author.value, this.ISBN.value, this.publisher.value, this.pubYearStart.value, this.pubYearEnd.value, isTitleFullMatch, isCreatorFullMatch, mediaTypeStr);
		trace(returnArr);
		for(var i=0; i < returnArr.length; i++){
			returnArr[i] = returnArr[i].split(String.fromCharCode(8203)).join("");
		}
		return returnArr;
	}
	
	var self = this;
	this.clearValues = function(){	
		var f = MatchSuperCondition.clearValues;
		f.call(this);
		
		this.radiosTitleFullMatch[0].checked = false;
		this.radiosTitleFullMatch[1].checked = false;
		this.radiosCreatorFullMatch[0].checked = false;
		this.radiosCreatorFullMatch[1].checked = false;
	},
	
	this.setDefaultValues = function() {
		this.radiosTitleFullMatch[1].checked = true;
		this.radiosCreatorFullMatch[1].checked = true;
	}
	
	this.hasBlankRadioButton = function() {
		var result = ((this.radiosTitleFullMatch[0].checked == false && this.radiosTitleFullMatch[1].checked == false) ||
		              (this.radiosCreatorFullMatch[0].checked == false && this. radiosCreatorFullMatch[1].checked == false));
		return result;
	}
	
	this.setInputValue = function(arr, order) {
		var f = MatchSuperCondition.setInputValue;
		f.call(this, arr);
		
		(arr[6] == 1) ? 
			this.radiosTitleFullMatch[0].checked = true :
			this.radiosTitleFullMatch[1].checked = true;
			
		(arr[7] == 1) ? 
			this.radiosCreatorFullMatch[0].checked = true :
			this.radiosCreatorFullMatch[1].checked = true;
		
		var mediaType = arr[8];
		switch(mediaType) {
			case "S" :
				this.mediaType.selectedIndex = 1;
				break;
			case "B" :
				this.mediaType.selectedIndex = 2;
				break;
			default : 
				this.mediaType.selectedIndex = 0;
				break;
		}
		
		this.order.selectedIndex = order;
	}
	
	/*setInputValue:function(arr){
		if(!arr || arr.length == 0) return;
		var len = this.lists.length;
		for(var i = 0; i < len; i++){
			if(arr[i]) this.lists[i].value = arr[i];
			else this.lists[i].value = "";
		*/	
			/*for(var j = 0; j < len; j++){
				if(this.lists[j].tabIndex == i){
					this.lists[j].value = arr[i];
					break;
				}
			}*/
		//}
	//},
}

MatchBookCondition.prototype = MatchSuperCondition;




/**
* a class which has works detail info
**/
var MatchWorksCondition = function(container){
	this.container = document.getElementById(SearchConfig.ID_MATCH_WORKS_LIST);
	this.lists = this.container.getElementsByTagName("INPUT");
	this.title = this.lists[0];
	this.author = this.lists[1];
	this.order = this.container.getElementsByTagName("SELECT")[0];
	
	this.getDetails = function(){
		var returnArr = new Array(this.title.value, this.author.value);
		for(var i=0; i < returnArr.length; i++){
			returnArr[i] = returnArr[i].split(String.fromCharCode(8203)).join("");
		}
		return returnArr;
	}
}

MatchWorksCondition.prototype = MatchSuperCondition;




/**
* a class which has person detail info
**/
var MatchPersonCondition = function(container){
	this.container = document.getElementById(SearchConfig.ID_MATCH_PERSON_LIST);
	this.lists = this.container.getElementsByTagName("INPUT");
	this.name = this.lists[0];
	this.bornYearStart = this.lists[1];
	this.bornYearEnd =this.lists[2];
	this.deadYearStart = this.lists[3];
	this.deadYearEnd = this.lists[4];
	this.order = this.container.getElementsByTagName("SELECT")[0];
	
	
	this.getDetails = function(){
		var error = false;
		if(!this.checkNumber(this.bornYearStart)){
			error = true;
		}
		if(!this.checkNumber(this.bornYearEnd)){
			error = true;
		}
		if(!this.checkNumber(this.deadYearStart)){
			error = true;
			
		}
		if(!this.checkNumber(this.deadYearEnd)){
			error = true;
		}
		
		if(error) return false;
		
		var returnArr = new Array(this.name.value, this.bornYearStart.value, this.bornYearEnd.value, this.deadYearStart.value, this.deadYearEnd.value);
		for(var i=0; i < returnArr.length; i++){
			returnArr[i] = returnArr[i].split(String.fromCharCode(8203)).join("");
		}
		return returnArr;
	}
}

MatchPersonCondition.prototype = MatchSuperCondition;




/**
* a class which operates associative search area
**/
var MatchCondition = function(){
	this.defaultFlg		= true;
	this.matchCondFlg	= false;
	this.matchCondIndex	= -1;
	this.matchCondArea	= document.getElementById(SearchConfig.ID_MATCH_COND);
	this.matchSearchBtn	= document.getElementById(SearchConfig.ID_MATCH_SEARCH_BTN);
	this.matchTextArea	= document.getElementById(SearchConfig.ID_MATCH_TEXT_AREA);
	
	var matchCondTabsArea = this.matchCondArea.getElementsByTagName('UL')[0];
	
	this.matchCondBtn		= matchCondTabsArea.getElementsByTagName('LI')[3].getElementsByTagName('A')[0];
	this.matchCondFormAreas	= $("."+SearchConfig.CLS_MATCH_CONDLIST);
	this.matchCondTabs		= matchCondTabsArea.getElementsByTagName('LI');
	
	this.bookCond	= new MatchBookCondition();
	this.worksCond	= new MatchWorksCondition();
	this.personCond	= new MatchPersonCondition();
	this.condArr = new Array(this.bookCond, this.worksCond, this.personCond);
	
	this.DEFAULT_WORD_ARR	= ["検索するワードを入力してください", "検索する作品名を入力してください", "検索する人物名を入力してください"];
	
	this.COLOR_BLUR		= "#ccc";
	this.COLOR_FOCUS	= "#000";
	
	//clear all condition panels
	var listLen = this.matchCondFormAreas.length;
	for(var i = 0; i < listLen; i++){
		this.matchCondFormAreas[i].style.display = "none";
	}
	
	this._setListener();
	this.setMatchCond(0);
	this._setDefaultText();
	this._clearValuesExceptCurrentCond();
	this.bookCond.setDefaultValues();
}

MatchCondition.prototype = {
	_setListener:function(){
		var self = this;
		this.matchSearchBtn.onclick = function(){
			self._matchSearch();
		}
		
		//set detail setting listener
		var bookStr = "#"+SearchConfig.ID_MATCH_BOOK_LIST+" input";
		var workStr = "#"+SearchConfig.ID_MATCH_WORKS_LIST+" input";
		var personStr = "#"+SearchConfig.ID_MATCH_PERSON_LIST+" input";
		$("#"+SearchConfig.ID_MATCH_TEXT_AREA+","+bookStr+","+workStr+","+personStr).keydown(function(e){
			if ( e.keyCode == 13 ) {
				self._matchSearch();
				return false;
			}
		});
		
		this.matchCondBtn.onclick = function(){
			self._setDetailActive(!self.matchCondFlg);
		}
		
		this.matchCondTabs[0].onclick = function(){
			self.setMatchCond(0);
		}
		this.matchCondTabs[1].onclick = function(){
			self.setMatchCond(1);
		}
		this.matchCondTabs[2].onclick = function(){
			self.setMatchCond(2);
		}
		this.matchTextArea.onfocus = function(){
			this.style.color = self.COLOR_FOCUS;
			if(self.defaultFlg){
				self.defaultFlg = false;
				this.value = "";
			}
		}
		this.matchTextArea.onblur = function(){
			if(this.value==""){
				self.defaultFlg = true;
				
				this.value = self.DEFAULT_WORD_ARR[self.matchCondIndex];
				this.style.color = self.COLOR_BLUR;
			}
		}
	},
	
	_matchSearch:function(){
		var freeword = this.matchTextArea.value.split(String.fromCharCode(8203)).join("");
		var detail, temp;
		
		if(freeword == this.DEFAULT_WORD_ARR[this.matchCondIndex]) freeword = "";
		
		switch(this.matchCondIndex){
			case 0:
				if(!this.bookCond.checkValues() && freeword == "") return;
				
				detail = this.bookCond.getDetails();
				if(!detail){
					alert("入力に間違いがあります");
					return;
				}
				HistoryManager.setURL(SearchManager, SearchManager.matchBookKeywordSearch, freeword, detail, this.bookCond.getOrder());
				break;
			case 1:
				if(!this.worksCond.checkValues() && freeword == "") return;
				
				detail = this.worksCond.getDetails();
				
				//swap freeword and title
				temp = detail[0];
				detail[0] = freeword;
				freeword = temp;
				
				HistoryManager.setURL(SearchManager, SearchManager.matchWorksKeywordSearch, freeword, detail, this.worksCond.getOrder());
				break;
			case 2:
				if(!this.personCond.checkValues() && freeword == "") return;
				
				detail = this.personCond.getDetails();
				if(!detail){
					alert("入力に間違いがあります");
					return;
				}
				
				//swap freeword and title
				temp = detail[0];
				detail[0] = freeword;
				freeword = temp;
				
				HistoryManager.setURL(SearchManager, SearchManager.matchCreatorKeywordSearch, freeword, detail, this.personCond.getOrder());
				break;
			default:
				break;
		}
	},
	
	//activate or deactivate
	_setDetailActive:function(active){
		if(this.matchCondFlg == active) return;
		
		this.matchCondFlg = active;
		var condLen = this.matchCondFormAreas.length;
		var img = this.matchCondBtn.getElementsByTagName("IMG")[0];
		
		if(!this.matchCondFlg){
			img.src = SearchConfig.IMG_MATCH_COND_BTN_OFF;
			for(var i = 0; i < condLen; i++){
				this.matchCondFormAreas[i].style.display = "none";
			}
			$(this.matchCondBtn.parentNode.parentNode).addClass(SearchConfig.CLS_SET_CLOSED);
		}
		else{
			img.src = SearchConfig.IMG_MATCH_COND_BTN_ON;
			this.matchCondFormAreas[this.matchCondIndex].style.display = "";
			$(this.matchCondBtn.parentNode.parentNode).removeClass(SearchConfig.CLS_SET_CLOSED);
		}
	},
	
	_setFreeText:function(text){
		if(!text || text == ""){
			this._setDefaultText();
			this._clearValuesExceptCurrentCond();
			return;
		}
		
		this.defaultFlg = false;
		this.matchTextArea.style.color = this.COLOR_FOCUS;
		this.matchTextArea.value = text;
	},
	
	_setDefaultText:function(){
		var self = this;
		this.defaultFlg = true;
		this.matchTextArea.style.color = this.COLOR_BLUR;
		this.matchTextArea.value = this.DEFAULT_WORD_ARR[this.matchCondIndex];
	},
	
	_clearValuesExceptCurrentCond:function() {
		var len = this.condArr.length;
		for(var i = 0; i < len; i++){
			if (i == this.matchCondIndex) continue;
			this.condArr[i].clearValues();
		}
	},
	
	//set selected detail panel
	setMatchCond:function(index){
		if(this.matchCondIndex == index) return;
		
		if(this.DEFAULT_WORD_ARR[this.matchCondIndex] == this.matchTextArea.value)
			this.matchTextArea.value = this.DEFAULT_WORD_ARR[index];
			
		this.matchCondIndex = index;
		
		var target = this.matchCondTabs[index];
		var input = target.getElementsByTagName("INPUT")[0];
		input.checked = true;
		
		var condLen = this.matchCondFormAreas.length;
		for(var i = 0; i < condLen; i++){
			var display = (i == index && this.matchCondFlg) ? "" : "none";
			var className = (i == index) ? "cur" : "";
			this.matchCondFormAreas[i].style.display = display;
			this.matchCondTabs[i].className = className;
		}
		
		if(this.bookCond.hasBlankRadioButton()) {
			this.bookCond.setDefaultValues();
		}
	},
	
	setConditionValue:function(type, arr, text, order){
		var index, temp;
		var detailArr = copyArray(arr);
		switch(type){
			//case SearchMode.MATCH_BOOK:
			case ResultType.MATCH_BOOK:
				index = 0;
				break;
			//case SearchMode.MATCH_WORKS:
			case ResultType.MATCH_WORKS:
				index = 1;
				temp = detailArr[0];
				detailArr[0] = text;
				text = temp;
				break;
			//case SearchMode.MATCH_PERSON:
			case ResultType.MATCH_PERSON:
				index = 2;
				temp = detailArr[0];
				detailArr[0] = text;
				text = temp;
				break;
			default:
				index = 0;
				break;
		}
		
		var arrFlg = false;
		for(var i = 0; i < detailArr.length; i++){
			if(detailArr[i] && detailArr[i] != "") arrFlg = true;
		}
		
		var cond = this.condArr[index];
		this._setFreeText(text);
		cond.setInputValue(detailArr, order);
		this._setDetailActive(arrFlg);
		this.setMatchCond(index);
	},
	
	_clearCurrentCondition:function(){
		if(this.matchCondIndex == -1) return;
		var currentCondition = this.condArr[this.matchCondIndex];
		currentCondition.clearValues();
		if(currentCondition.setDefaultValues) {
			currentCondition.setDefaultValues()
		}
		
		this._setDefaultText();
	},
	
	reset:function(){
		var len = this.condArr.length;
		for(var i=0; i<len; i++) {
			var currentCondition = this.condArr[i];
			currentCondition.clearValues();
			if(currentCondition.setDefaultValues) {
				currentCondition.setDefaultValues();
			}
		}
	}
}





/**
* a class which operates associative search area
**/
var AssocCondition = function(){
	this.firstFlg	= true;
	this.textBtn	= document.getElementById(SearchConfig.ID_ASSOC_TEXT_BTN);
	this.textArea	= document.getElementById(SearchConfig.ID_ASSOC_TEXT_AREA);
	this.todayBtn	= document.getElementById(SearchConfig.ID_ASSOC_TODAY_BTN);
	this.todayLink	= document.getElementById(SearchConfig.ID_ASSOC_TODAY_LINK);
	
	this.MAX_FLASH_COUNT = 8;
	this.flashCount = 0;
	this.flashId;
	this.animationIntervalId;
	this.flashFlg = true;
	this.btnImgArr	= new Array(
								"/externals/images/btn-association-01.gif",
								"/externals/images/btn-association-01-ovr.gif"
								);
	
	this.textArea.innerHTML = "";
	this.textArea.value = "";
	this._setListener();
	this._setTodayLink();
	
	this.open();
}

AssocCondition.prototype = {
	_setListener:function(){
		var self = this;
		this.textBtn.onclick = function(){
			//SearchManager.assocTextSearch(self.textArea.value);
			HistoryManager.setURL(SearchManager, SearchManager.assocTextSearch, self.textArea.value.split(String.fromCharCode(8203)).join(""));
		}
		
		$(this.todayBtn).click(function(){
			WhatIsTodayManager.getToday();
		});
		
		$(this.textBtn).mouseover(function(){
			self.flashFlg = false;
			clearInterval(self.flashId);
		});
		
		$(this.textArea).keydown(function(e){
			if ( e.keyCode == 13 ) {
				HistoryManager.setURL(SearchManager, SearchManager.assocTextSearch, self.textArea.value.split(String.fromCharCode(8203)).join(""));
				return false;
			}
		});
		
		Observer.addEventListener(GlobalEvent.TODAY_LOADED, WhatIsTodayManager, self.loadedToday, self);
	},
	
	_setTodayLink:function(){
		var linkStr = WhatIsTodayManager.getTodayLink();
		this.todayLink.href = linkStr;
	},
	
	loadedToday:function(){
		var todayStr = WhatIsTodayManager.todayData;
		
		if(!todayStr) return;
		if(this.firstFlg && !GlobalValues.todayPlayFlg) return;
		
		/*
		if(this.firstFlg){
			this.firstFlg = false;
			this.textArea.value = "";
			this.showTodayAnimation(todayStr);
			return;
		}
		*/
		
		this.textArea.value = "";
		this.textArea.value = todayStr;
	},
	
	showTodayAnimation:function(str){
		
		var count = 0;
		var self = this;
		
		var animating = function(){
			self.textArea.value += str.charAt(count);
			count++;
			if(count == str.length) animated();
		}
		
		var animated = function(){
			self.startFlashTextBtn();
			clearInterval(self.animationIntervalId);
		}
		
		animationIntervalId = setInterval(animating, 30);
	},
	
	open:function(){
		var self = this;
		$(this.textArea).fadeIn(1000, function(){self.opened(self);});
	},
	
	opened:function(self){
		//WhatIsTodayManager.getToday();
		$(self.textBtn).fadeIn(500);
	},
	
	setText:function(text){
		this.textArea.value = text;
	},
	
	_clearText:function() {
		this.setText("");
		clearInterval(self.animationIntervalId);
	},
	
	startFlashTextBtn:function(){
		var self = this;
		
		var flashing = function(){
			self.flashCount++;
			self.textBtn.firstChild.src = self.btnImgArr[self.flashCount % 2];
			if(self.flashCount == self.MAX_FLASH_COUNT){
				clearInterval(self.flashId);
			}
		}
		
		if(this.flashFlg) this.flashId = setInterval(flashing, 500);
	}/*,
	
	flashTextBtn:function(){
		
	}*/
}


