
var SuggestHideTimeout;
var GLOBAL_Field="Title";
var GLOBAL_Card_Source="";

var BasicSuggestSelect='<select id="Suggest%GLOBAL_Field%" size="10" onChange="SelectCard(); SuggestHide(); return true;" onFocus="clearTimeout(SuggestHideTimeout);" onBlur="SuggestHide();"></select>';

function GetCardInfo_Timer(type)
{
    GLOBAL_Field=type;
	clearTimeout(timeoutID);
	if ($("form#CardSearchForm input[name="+GLOBAL_Field+"]").val())
		{timeoutID=setTimeout("GetCardsInfo()", 1000);
		}
	else if (GLOBAL_Field=="Type")
		{timeoutID=setTimeout("TypeShortSuggest()", 500);
		}
}

function GetCardsInfo()
{
// window.open('/cards/card_search_suggest.phtml?field='+GLOBAL_Field+'&keyword='+$('#'+GLOBAL_Field).val(),'fff');
	$.get("/cards/card_search_suggest.phtml",
		{field: GLOBAL_Field,
		 keyword: $('#'+GLOBAL_Field).val()
		}, AjaxHandleServerResponse
	);
}

function AjaxHandleServerResponse(data)
{
  var result=data.documentElement.childNodes;

  if ((result.length > 0) && (result[0].firstChild.data != 'OK'))
   {$('#'+GLOBAL_Field).css('color', 'green');
	$('#'+GLOBAL_Field+'Select').html(BasicSuggestSelect.replace('%GLOBAL_Field%', GLOBAL_Field));

	for (i=0; i < result.length; i++) $("<option></option>").attr("value", result[i].firstChild.data).html(result[i].firstChild.data).appendTo($("#Suggest"+GLOBAL_Field));
	$("#Suggest"+GLOBAL_Field).get(0).selectedIndex=-1; //элемент не должен быть выделен, иначе не сработает onChange()
   }
  else if ((result.length == 1) && (result[0].firstChild.data == 'OK'))
   {$('#'+GLOBAL_Field).css('color', 'green');
   }
  else
   {$('#'+GLOBAL_Field).css('color', 'black');
	SuggestHide();
   }
}

function SuggestHide()
{
SuggestHideTimeout=setTimeout('$("#TitleSelect").html(" "); $("#TypeSelect").html(" ");', 200);
}

function TypeShortSuggest()
{
 if (!$("form#CardSearchForm input[name=Type]").val())
   {
	GLOBAL_Field="Type";
    $("#TypeSelect").html(BasicSuggestSelect.replace('%GLOBAL_Field%', GLOBAL_Field));


	$("#TypeShort option").each(function(){$("<option></option>").attr("value", this.value).html(this.firstChild.data).appendTo($("#SuggestType"));});

	$("#SuggestType").get(0).selectedIndex=-1;
   }
}

function SelectCard()
{
	$("#"+GLOBAL_Field).val($("#Suggest"+GLOBAL_Field).val());
}

function GrpAdvancedSelector(Grps)
{
	if (!$("#GrpAdvancedSelector").html())
		{$("#GrpAdvancedSelector").load("/cards/cards_grps_advanced.phtml", {Grps: Grps});
		};

	if ($("#GrpAdvancedSelectorLink_Compact").html()=='компактный')
		{$("#GrpAdvancedSelector").show();
		 $("#GrpAdvancedSelectorLink_Full").html('развернутый');
		 $("#GrpAdvancedSelectorLink_Compact").html('<a href="#" onClick="GrpAdvancedSelector(\''+Grps+'\'); return false;">компактный</a>');
		 $("#Grp").css('disabled', true);
		 $("#GrpAdvanced").val('Y');
		 $("#Grp").hide();
		 $("#BannedSelector").hide();
		}
	else {$("#GrpAdvancedSelector").hide();
		 $("#GrpAdvancedSelectorLink_Compact").html('компактный');
		 $("#GrpAdvancedSelectorLink_Full").html('<a href="#" onClick="GrpAdvancedSelector(\''+Grps+'\'); return false;">развернутый</a>');
		 $("#Grp").css('disabled', false);
		 $("#GrpAdvanced").val('');
		 $("#Grp").show();
		 if ($("#FullSearch").val()=='Y')
			{$("#BannedSelector").show();
			}
		 }
}

function FullSearchDisplay()
{
if ($("#FullSearch").val()!='Y')
 {
	$("#PDU").find(".cards-FullSearch2").show();
	$("#FullSearchSelectorLink").html('<img src="/images2/design/icons/search_minus.gif" width="10" height="10" border="0" vspace="2" hspace="5" align="middle">компактный поиск');
	$("#FullSearch").val('Y');
 }
else
 {
	$("#PDU").find(".cards-FullSearch2").hide();
	$("#FullSearchSelectorLink").html('<img src="/images2/design/icons/search_plus.gif" width="10" height="10" border="0" vspace="2" hspace="5" align="middle">развернутый поиск');
	$("#FullSearch").val('');
 }
}

function SearchColor(new_color)
{
	color=$("form#CardSearchForm input[name=Color]").val();
	if (color.indexOf(new_color) >= 0) {color=color.replace(new_color, '');}
	else {color=color+new_color;}
	$("form#CardSearchForm input[name=Color]").val(color);
}

function SearchRarity(new_rarity)
{
	rarity=$("form#CardSearchForm input[name=Rarity]").val();
	if (rarity.indexOf(new_rarity) >= 0) {rarity=rarity.replace(new_rarity, '');}
	else {rarity=rarity+new_rarity;}
	$("form#CardSearchForm input[name=Rarity]").val(rarity);
}

function SearchGrp(new_grp)
{
	new_grp=new_grp+'-';
	grp=$("form#CardSearchForm input[name=Grps]").val();
	if (grp.indexOf(new_grp) >= 0) {grp=grp.replace(new_grp, '');}
	else {if (grp=='') {grp='-';} grp=grp+new_grp;}
	$("form#CardSearchForm input[name=Grps]").val(grp);
}

function SearchBanned()
{
	if ($("#Grp").val().indexOf('-') == 0)
		{$("#Banned").attr({disabled : ""});
		}
	else
		{$("#Banned").get(0).selectedIndex=0;
		 $("#Banned").attr({disabled : "true"});
		}
}

function ShortUrl()
 {
	var temp='';
	var help=this.document.forms.CardSearchForm.elements;

	// эти пары не должны образоваться, но вдруг...
	if (!help.doRus.checked){help.doRusOnly.checked=false};
	if (!help.doOracle.checked){help.doOracleOnly.checked=false};
	if (help.doSearchOracle.checked){help.doOracle.checked=true};
	// ---------------------------------------------------------

	help.Grps.value=help.Grps.value.replace(RegExp("^\-+"), "");
	help.Grps.value=help.Grps.value.replace(RegExp("\-+$"), "");
	help.Grps.value=help.Grps.value.replace(RegExp("\-+"), "-");

	var url='?';
	var FullSearchList=new Array('MultiColor', 'Mana', 'ManaSign', 'Power', 'PowerSign', 'Toughness', 'ToughnessSign', 'Banned', 'Number');
	var FullSearchLine='-'+FullSearchList.join('-')+'-';
	var InverseCheckboxList_ASC=new Array('doRus','doRusOnly','doOracle','doOracleOnly','doFlavor','doTornament','doArtist');
	var InverseCheckboxListLine_ASC='--'+InverseCheckboxList_ASC.join('-')+'--';
	var InverseCheckboxList_DSC=new Array('doRusOnly','doOracleOnly','doSearchOracle');
	var InverseCheckboxListLine_DSC='--'+InverseCheckboxList_DSC.join('-')+'--';

	var counter=0;

	for (var i=0; i < help.length; i++)
		{
			if (InverseCheckboxListLine_ASC.indexOf('-'+help[i].name+'-') > 0)
				{help[i].value=(help[i].checked=='')?'N':'';
				}
			if (InverseCheckboxListLine_DSC.indexOf('-'+help[i].name+'-') > 0)
				{help[i].value=(help[i].checked=='')?'':'Y';
				}
			if (!((help[i].name == 'FullSearch') ||
				(help[i].name == 'GrpAdvanced') ||
				(help[i].name == 'TypeShort') ||
				((FullSearchLine.indexOf('-'+help[i].name+'-') > 0) && help.FullSearch.value=='') ||
				(!help.GrpAdvanced.value && (help[i].name == 'Grps')) ||
				(help.GrpAdvanced.value && (help[i].name == 'Grp')) ||
				(help.GrpAdvanced.value && (help[i].name == 'Banned')) ||
				((help[i].name == 'Banned') && (help.Grp.value.indexOf('-') < 0)) ||
				(help[i].value == '') ||
				(help[i].name == '')))
					{
temp=help[i].value;
temp=temp.replace(new RegExp('&', 'g'), '%26');
temp=temp.replace(new RegExp(' +', 'g'), '+');
url+=((counter++ > 0)?'&':'')+help[i].name+'='+temp;
					}
		}
	if (url=='?')
		{alert('Пожалуйста, укажите параметры поиска.');
		}
	else{window.open('/cards/search.phtml'+((url=='?')?'':url), '_top');
		}
}

function ShowCardVersion(card_source, card_target, doShowFields)
{
 	$("#"+card_source).load("/cards/card_info_table.phtml?card="+card_target+((doShowFields)?"&"+doShowFields:""));
}

function BlockCheckboxSubfield(field, subfield)
{
	if ($("#"+field).attr("checked"))
		{$("#"+subfield).attr("disabled", false);
		}
	else
		{$("#"+subfield).attr("checked", false).attr("disabled", true);
		}
}

function doSearchOracleAction()
{
	if ($("#doSearchOracle").attr("checked"))
		{$("#doOracle").attr("checked", true).attr("disabled", true);
		 BlockCheckboxSubfield('doOracle', 'doOracleOnly');
		}
	else
		{$("#doOracle").attr("disabled", false);
		}
}

function doSortByAction(variant)
{
	if (($("#SortBy"+variant).val()=="title_rus") || ($("#SortBy"+variant).val()=="type_rus"))
		{$("#doRus").attr("checked", true);
		 BlockCheckboxSubfield('doRus', 'doRusOnly');
		}
	else if ($("#SortBy"+variant).val()=="type_oracle")
		{$("#doOracle").attr("checked", true);
		 BlockCheckboxSubfield('doOracle', 'doOracleOnly');
		}
	else if ($("#SortBy"+variant).val()=="")
		{$("#doORusOnly").attr("checked", false)
		}
}