/**
 * 山大独自機能：特殊カテゴリ記事をラベルまたは発信対象別にDB抽出
 */

$(function(){
	$('.labelTrigger').click(function(){
		__switchLabelStyle($(this).attr('rel'));
		__multiRewriteList($(this).attr('rel'));
	});
	__fadeOutEmptySpCtgryListAtTopPage();
});

function __switchLabelStyle(clicked_code)
{
	$('.labelGroup li').each(function(){
		var class_attr = $(this).attr('class').replace(' current','');
		if(class_attr.indexOf(clicked_code)>=0){
			$(this).attr('class',class_attr + ' current');
		}else{
			$(this).attr('class',class_attr);
		}
	});
}

function __fadeOutEmptySpCtgryList(sp_id,response)
{
	if(!response) return;
	if(response.match(/[登録|ページ]がありません/)) {
		setTimeout(function(){
			$('#' + spCtgryDivIdMap[sp_id]).find('div').fadeOut('slow',function(){
				if(navigator.userAgent.indexOf('MSIE')>0) this.style.removeAttribute('filter');
			});
		},1000);
	}
}

function __fadeOutEmptySpCtgryListAtTopPage()
{
	for(var i=0; i<activeSpCtgry.length; i++){
		var sp_id = activeSpCtgry[i] + '';
		if(spCtgryDivIdMap[sp_id].match(/weekly|press[1-3]/)) continue;
		var sp_ctgry_name = spCtgryDivIdMap[sp_id];
		var response = $('#' + sp_ctgry_name + 'Inner').html();
		__fadeOutEmptySpCtgryList(sp_id,response);
	}
}

//発信対象別抽出（単体書き換えルーチン）
function __getArticleListByTargetRange(sp_id,data)
{
	$.ajax({
		url : BASE_DIR + '/index.php/index/ajax-rewrite-spctgry-list',
		type : "post",
		data : data,
		beforeSend: function(e) {
			$('#' + spCtgryDivIdMap[sp_id]).show();
			$('#' + spCtgryDivIdMap[sp_id] + 'Inner').html('<p><img src="' + BASE_DIR + '/img/finish-loader.gif" alt="" />更新中...</p>');
		},
		success: function(response) {
			var innerNode = $('#' + spCtgryDivIdMap[sp_id] + 'Inner');
			innerNode.hide();
			innerNode.html(response);
			innerNode.fadeIn('slow');
			if(response.match(/[登録|ページ]がありません/)) {
				setTimeout(function(){
					$('#' + spCtgryDivIdMap[sp_id]).find('div').fadeOut('slow',function(){
						if(navigator.userAgent.indexOf('MSIE')>0) this.style.removeAttribute('filter');
					});
				},1000);
			}
		}
	});
}
//発信対象別抽出（一斉）
function __multiRewriteList(target_code)
{
	for(var i=0; i<activeSpCtgry.length; i++){
		var sp_id = activeSpCtgry[i] + '';
		if(spCtgryDivIdMap[sp_id].match(/weekly|press[1-3]/)) continue;
		var data = {'cid':activeSpCtgry[i],'target_range':target_code,'token':token};
		__getArticleListByTargetRange(sp_id,data);
	}
}
//発信対象個別ページからのコール
function __multiRewriteListAtTargetBranch(target_code,update_node)
{
	$.ajax({
		url : BASE_DIR + '/index.php/index/ajax-get-empty-list-wrapper',
		type : "post",
		success: function(response) {
			$('#' + update_node).html(response);
			__multiRewriteList(target_code);
		}
	});
}

function __getArticleListByBranch(branchnews_cid,update_node)
{
	$.ajax({
		url : BASE_DIR + '/index.php/index/ajax-get-branch-list',
		type : 'post',
		data : {
			b_id : branchnews_cid
		},
		success : function(response){
			if(response=='failure'){
				
			}else{
				var update_obj = $('#' + update_node);
				update_obj.hide();
				update_obj.html(response);
				update_obj.fadeIn('slow');
			}
		}
	});
}
