function PbanFaq(){
var _this = this;
this.init = function(){
//dialog準備
$( "#answer" ).dialog({
autoOpen:false,
modal:true,
width:520,
height:400,
buttons:{
"解決した":function(){ _this.resolved(this); },
"お問い合せページへ":function(){ _this.notResolved(this); }
},
hide:{effect:"fade"},
close:function(event, ui){
//window.location.hash='';
}
});
//全選択と選択解除
_this._initSlctAllCheckbox();
//初期検索実行
_this.doSearch(false);
//URL指定コンテンツ表示
_this._openHashContent();
}
this.fireSearchTrigger = function(eventSrc, scroll){
try{
var selectAllElem = $(eventSrc).parents('FIELDSET.category').find('input.selectAll');
_this._initSlctAllCheckbox(selectAllElem);
}catch(e){
//握りつぶす
}
_this.doSearch(scroll);
}
this.doSearch = function(scroll){
var chks = $( "input[name=cat2ids]" );
var valArray = new Array();
chks.each(
function(){
var domobj = $(this).get()[0];
if( domobj.checked ) {
valArray.push( domobj.value );
}
}
);
var searchKey = $( "#query" ).get()[0].value;
$.ajax({
type:"POST",
url:"/pb/faqSearch.do;jsessionid=37E89C48AC6A300E930F26FC26441128?method=getQuestions&seeAll=true",
data:{cat2ids:valArray, "query":searchKey},
dataType:"json",
success:function(msg) {
_this.displayQs( msg, searchKey );
_this.displayKeyword(msg, searchKey);
if(scroll){
_this.doScroll();
}
},
error:function(req,status,err) {
$( "#msg" ).html("[" + status + "] " + err);
$( "#questions" ).html( "" );
$( ".seeAll" ).html( "" );
}
});
}
this.displayQs = function(json, keywords){
if( json.qarray.length == 0) {
$( "#msg" ).html("[検索キーワード:" + keywords + "]を含む情報は見つかりませんでした。");
$( "#questions" ).html( "" );
$( ".seeAll" ).html( "" );
} else {
$( "#msg" ).html( "" );
//set questions list
var contents = '';
for(var i in json.qarray){
if(!json.qarray[i].cat1){
alert("エラーが発生しました。\n" + JSON.stringify(json.qarray[i]));
continue;
}
contents += '
';
contents += json.qarray[i].q;
contents += '';
$( "#questions" ).html(contents);
$( ".question" ).click(
function(event){
var targetId = event.srcElement ? event.srcElement.id : event.target.id;
return _this.dialogOpen(targetId);
}
);
}
}
}
this.displayKeyword = function(json, keywords){
if( json.qarray.length == 0) {
$('#keyword4results').text('');
}else{
if(keywords){
$('#keyword4results').text('[検索キーワード:' + keywords + ']');
}else{
$('#keyword4results').text('');
}
}
}
this.doScroll = function(){
var fixwid = window.innerWidth;//スクロールバー含む幅を取得
//ウィンドウ幅で追従箇所の高さ取得
var hheight = 0;
if(fixwid > 767){
var hheight = $('header').height() + $('.topicPath').height() - $('nav').height();
}else{
var isFixed = $('header').css('position') == 'fixed';
if(isFixed){
hheight = $('header').height();
}else{
hheight = 2*$('header').height();
}
}
var position = $('#results').offset().top-hheight;
$("html, body").animate({scrollTop:position}, 1000, "swing");
}
this.dialogOpen = function(faqid){
//set hash
window.location.hash='q' + faqid;
$.ajax({
type:"POST",
url:"/pb/faqSearch.do;jsessionid=37E89C48AC6A300E930F26FC26441128?method=getQandA",
data:{'faqid':faqid},
dataType:"json",
success:function( msg ) {
var contents = "- " + msg.q + "
" + "- " + msg.a + "
";
$( "#answer" ).html( contents );
$( "#answer" ).get()[0].setAttribute('currentid', faqid); //will refer to 'currentid'
},
error:function(req,status,err) {
var contents = "[" + status + "]" + err;
$( "#answer" ).html( contents );
$( "#answer" ).currentId = null;
}
});
//open dialog
$( "#answer" ).dialog( "open" );
return false;
}
this._openHashContent = function(){
var _hash = window.location.hash;
if(_hash != ''){
if(_hash.indexOf('#') == 0){
_hash = _hash.substr(1);
}
if(_hash.indexOf('q') == 0){
_hash = _hash.substr(1);
}
if(_hash != '')
_this.dialogOpen(_hash);
}
}
this.resolved = function(dlog){
$.ajax({
type:"POST",
url:"/pb/faqSearch.do;jsessionid=37E89C48AC6A300E930F26FC26441128?method=resolved",
data:{faqid:$( "#answer" ).get()[0].getAttribute('currentid')},
dataType:"json",
success:function(msg){},
error:function(req,status,err){}
});
$(dlog).dialog("close");
}
this.notResolved = function(dlog){
$.ajax({
type:"POST",
url:"/pb/faqSearch.do;jsessionid=37E89C48AC6A300E930F26FC26441128?method=notresolved",
data:{faqid:$( "#answer" ).get()[0].getAttribute('currentid')},
dataType:"json",
success:function(msg){},
error:function(req,status,err){}
});
location="/form/contact_form.html";
}
/*
* 全選択/選択解除チェックボックスの初期化
*/
this._initSlctAllCheckbox = function(triggerCheckbox){
//引数省略時は全selectAllエレメントを対象とする
if(triggerCheckbox == null){
triggerCheckbox = $('.selectAll');
}
//一旦onChagne解除
triggerCheckbox.off('change');
/*
* 対象checkboxの状態に応じた、初期値の設定
* 一つでもチェックが外れていたら、トリガーのcheckboxはOFF
* 全部チェックされていたら、トリガーのcheckboxはON
*/
triggerCheckbox.each(function(elem){
var trigger = $(this);
var targets = $(this).parents('FIELDSET.category').find('input[name=cat2ids]');
var currentStatus = _this._currentStatus(targets);
if(currentStatus == 1){
this.checked = true;
}else{
this.checked = false;
}
});
//ハンドラーの(再)設置
_this._setHandleForSlctCtrl(triggerCheckbox);
}
/*
* 全選択/選択解除ボタンのハンドラ―設置
*/
this._setHandleForSlctCtrl = function(triggerCheckbox){
triggerCheckbox.on('change', function(){
var trigger = $(this);
var targets = $(this).parents('FIELDSET.category').find('input[name=cat2ids]');
var currentStatus = _this._currentStatus(targets);
if(currentStatus == 0){
_this._checkAll(targets, true);
}else{
_this._checkAll(targets, false);
}
_this.doSearch(false);
});
}
this._currentStatus = function(checkboxes){
var status = 1; //「全部チェックされている」を初期値として検査開始する
for(var i=0; i < checkboxes.length; i++){
if(!checkboxes.get(i).checked){
status = 0;
break;
}
}
return status;
}
this._checkAll = function(targets, checked){
targets.each(function(){this.checked = checked;});
}
}