// 文字コード：UTF-8

// 外部から参照するデータ
var cdsc_card_data = new Array();	// 検索用カードデータ
var cdsc_card_hit = new Array();	// 各カードが検索にヒットしたか( 0:ヒットしてない / 1:ヒットした )
var cdsc_card_hit_num = 150;		// 検索にヒットしたカード数

// 内部処理用のデータ
var cdsc_card_data_flag = 0;	// カードデータを取得したか( 0:未取得 / 1:取得済 )

var CDSC_CARD_TYPE_NUM = 5;	// カードタイプのボタンの数
var cdsc_card_type = 0;		// カードタイプ( 0:すべて / 1:前衛 / 2:後衛 / 3:マジック / 4:スーパー )

var CDSC_SERIES_NUM = 6;	// シリーズのボタンの数
var cdsc_series = 0;		// シリーズ( 0:すべて / 1:WQ / 2:MS / 3:FL / 4:LI / 5:LE )

var CDSC_RARITY_NUM = 9;	// レア度のボタンの数
var cdsc_rarity = 0;		// レア度( 0:すべて / 1～8:レア度 )

var cdsc_display_option_flag = 0;	// 検索条件の「オプション」以下の項目の表示フラグ( 0:非表示 / 1:表示 )

// カードデータを取得
function cdsc_get_card_data()
{
	var url = document.URL.split("#")[0];
	new Ajax.Request(url, {
		method: "post",
		parameters: "get_card_data=1",
		onSuccess: cdsc_get_card_data_success
	});
}
// 取得成功時の処理
function cdsc_get_card_data_success(obj)
{
	var res = eval( '(' + obj.responseText + ')' );
	cdsc_card_data = res.d;
	cdsc_card_data_flag = 1;
	cdsc_search();
}

// カードタイプをセット
function cdsc_set_card_type(val)
{
	var button_id;
	
	if( (val < 0) || (CDSC_CARD_TYPE_NUM <= val) ){
		return;
	}
	
	for(var i = 0; i < CDSC_CARD_TYPE_NUM; i++){
		button_id = "cdsc_card_type_" + i;
		document.getElementById(button_id).className = "card_type button_off";
	}
	button_id = "cdsc_card_type_" + val;
	document.getElementById(button_id).className = "card_type button_on";
	cdsc_card_type = val;
	
	// 検索実行
	cdsc_search();
}

// シリーズをセット
function cdsc_set_series(val)
{
	var button_id;
	
	if( (val < 0) || (CDSC_SERIES_NUM <= val) ){
		return;
	}
	
	for(var i = 0; i < CDSC_SERIES_NUM; i++){
		button_id = "cdsc_series_" + i;
		document.getElementById(button_id).className = "series button_off";
	}
	button_id = "cdsc_series_" + val;
	document.getElementById(button_id).className = "series button_on";
	cdsc_series = val;
	
	// 検索実行
	cdsc_search();
}

// レア度をセット
function cdsc_set_rarity(val)
{
	var button_id;
	
	if( (val < 0) || (CDSC_RARITY_NUM <= val) ){
		return;
	}
	
	for(var i = 0; i < CDSC_RARITY_NUM; i++){
		button_id = "cdsc_rarity_" + i;
		document.getElementById(button_id).className = "rarity button_off";
	}
	button_id = "cdsc_rarity_" + val;
	document.getElementById(button_id).className = "rarity button_on";
	cdsc_rarity = val;
	
	// 検索実行
	cdsc_search();
}

// 検索実行
function cdsc_search()
{
	// まだカードデータを取得していなければ先に取得してくる
	if(cdsc_card_data_flag == 0){
		cdsc_get_card_data();
		return;
	}

	// 検索処理
	var card_name  = document.getElementById("cdsc_card_name").value;
	var no_rarity8 = document.getElementById("cdsc_no_rarity8").checked;
	var speed_use  = document.getElementById("cdsc_speed_use").checked;
	var no_gb      = document.getElementById("cdsc_no_gb").checked;
	var mn_max_lv = 0;
	for(var i = 0; i <= 3; i++){
		if(document.getElementById("cdsc_mn_max_lv_" + i).checked){
			mn_max_lv = i;
			break;
		}
	}
	var mn_max_hp = 0;
	for(var i = 0; i <= 6; i++){
		if(document.getElementById("cdsc_mn_max_hp_" + i).checked){
			mn_max_hp = i;
			break;
		}
	}
	var mg_stone = 0;
	for(var i = 0; i <= 6; i++){
		if(document.getElementById("cdsc_mg_stone_" + i).checked){
			mg_stone = i;
			break;
		}
	}
	var mg_type = "";
	for(var i = 0; i <= 12; i++){
		if(document.getElementById("cdsc_mg_type_" + i).checked){
			mg_type = document.getElementById("cdsc_mg_type_" + i).value;
			break;
		}
	}
	
	cdsc_card_hit_num = 0;
	
	for(var i = 0; i < cdsc_card_data.length; i++){
		cdsc_card_hit[i] = 1;
		// カード名
		if(card_name.length > 0){
			if( (cdsc_card_data[i].name.indexOf(card_name, 0)    != 0) && 
			    (cdsc_card_data[i].kana.indexOf(card_name, 0)    != 0) && 
			    (cdsc_card_data[i].gb_name.indexOf(card_name, 0) != 0) && 
			    (cdsc_card_data[i].gb_kana.indexOf(card_name, 0) != 0) ){ 
				cdsc_card_hit[i] = 0;
				continue;
			}
		}
		// カードタイプ
		if( (cdsc_card_type > 0) && (cdsc_card_data[i].type != (cdsc_card_type - 1)) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( ((mn_max_lv > 0) || (mn_max_hp > 0)) && ((cdsc_card_data[i].type != 0) && (cdsc_card_data[i].type != 1)) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( ((mg_stone > 0) || (mg_type != "")) && (cdsc_card_data[i].type != 2) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		// シリーズ
		if( (cdsc_series > 0) && (cdsc_card_data[i].series != (cdsc_series - 1)) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		// レア度
		if( (cdsc_rarity > 0) && (cdsc_card_data[i].rarity != cdsc_rarity) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		// オプション
		if( no_rarity8 && (cdsc_card_data[i].rarity == 8) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( speed_use && (cdsc_card_data[i].spd_no == 1) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( no_gb && (cdsc_card_data[i].gb_name != "") ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if(mn_max_lv > 0){
			var mn_max_lv_temp = 0;
			if(cdsc_card_data[i].mn_info_max == 1){
				mn_max_lv_temp = cdsc_card_data[i].mn_lv1;
			} else if(cdsc_card_data[i].mn_info_max == 2){
				mn_max_lv_temp = cdsc_card_data[i].mn_lv2;
			} else if(cdsc_card_data[i].mn_info_max == 3){
				mn_max_lv_temp = cdsc_card_data[i].mn_lv3;
			} else if(cdsc_card_data[i].mn_info_max == 4){
				mn_max_lv_temp = cdsc_card_data[i].mn_lv4;
			}
			if(mn_max_lv_temp != mn_max_lv){
				cdsc_card_hit[i] = 0;
				continue;
			}
		}
		if( (mn_max_hp > 0) && (cdsc_card_data[i].mn_max_hp1 != mn_max_hp) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( (mg_stone > 0) && (cdsc_card_data[i].mg_stone != mg_stone) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		if( (mg_type != "") && (cdsc_card_data[i].mg_type != mg_type) ){
			cdsc_card_hit[i] = 0;
			continue;
		}
		
		// 検索にヒット
		cdsc_card_hit_num++;
	}
	
	cdsc_search_callback();
}

// 検索条件の「オプション」以下の項目の表示切り替え
function cdsc_display_option(obj)
{
	if(cdsc_display_option_flag == 0){
		cdsc_display_option_flag = 1;
		obj.innerHTML = "[-]";
		if(Prototype.Browser.IE){
			$("cdsc_mn_max_lv_row").style.display = "block";
			$("cdsc_mn_max_hp_row").style.display = "block";
			$("cdsc_mg_stone_row").style.display  = "block";
			$("cdsc_mg_type_row").style.display   = "block";
		} else {
			$("cdsc_mn_max_lv_row").style.display = "table-row";
			$("cdsc_mn_max_hp_row").style.display = "table-row";
			$("cdsc_mg_stone_row").style.display  = "table-row";
			$("cdsc_mg_type_row").style.display   = "table-row";
		}
	} else {
		cdsc_display_option_flag = 0;
		obj.innerHTML = "[+]";
		$("cdsc_mn_max_lv_row").style.display = "none";
		$("cdsc_mn_max_hp_row").style.display = "none";
		$("cdsc_mg_stone_row").style.display  = "none";
		$("cdsc_mg_type_row").style.display   = "none";
	}
}
