// checks if search phrase was changed since last complete page reload
//function submitWithCheck ( category, page, order, direction ) {
function submitWithCheck(category, phrase) {
	if ( $('#qsearch').val() == defaultQ ) {
		// clear if default
		$('#qsearch').val('');
	} else {
		q = ( (phrase == null || phrase == '') && $('#qsearch') != null ) ? $('#qsearch').val() : (phrase == null ? '' : phrase);
	}
	if ( category == null )  {
		document.com_search.action = compath+'search.html'+location.hash;
		document.com_search.submit();
	} else {
		submitSearch(category, 1, '', '' , '' );
	}
	return false;
}

function submitSearch(category, page, order, direction, appendQuery) {
	// get params
	page = (page == null) ? 1 : page;
	category = (category == null) ? 'forum' : category;
	
	// change master tabs
	categories = new Array('user', 'group', 'forum');
	catLen = categories.length;
	
	// reset tabs to unselected
	for ( var i=0; i<catLen; i++ ) {
		if ( $('#tab_' + categories[i]).length != 0 ) {
			$('#tab_' + categories[i]).removeClass();
		}
	}
	// select tab
	if ( $('#tab_' + category).length != 0 ) {
		$('#tab_' + category).addClass('sel');
	}
	
	order = (order == null) ? '' : order;
	direction = (direction == null) ? '' : direction;

	// add filters
	if ( category == 'user' ) {
		filters = new Array('realname', 'username', 'gamertag', 'age_from', 'age_to', 'city', 'country', 'interest', 'connection', 'birthmonth', 'birthday');
		$('#forumBasicOpts').hide();
	} else {
		filters = new Array('forumid', 'author', 'group_property', 'member_count');
		$('#forumBasicOpts').show();
	}
	filterLen = filters.length;
	params = {};
	
	for ( var i=0; i<filterLen; i++ ) {
		if ($('#'+filters[i]).length != 0 && $('#'+filters[i]).val() != '') params[filters[i]] = $('#'+filters[i]).val();
	}
	// bd is special
	if ( params['birthday'] != null && params['birthmonth'] != null ) {
		params['bd'] = 100*params['birthmonth'] + Number(params['birthday']);
		delete params['birthday'];
		delete params['birthmonth'];
	}
	

	// we have radioboxes for forum
	if ( category == 'forum' ) {
		params['searchtype'] = $('input[name=searchtype]:checked').val();
	}
	// radiobox for user gender
	else if ( category == 'user' ) {
		var gender = (document.forms.user_search == null ) ? '' : getRadioValue(document.forms.user_search.gender);
		if ( gender ) params['gender'] = gender;
	}
	
	// adding order and direction as part of filter for simple code
	if (order != null && order != '') params['order'] = order;
	if (direction != null && direction != '') params['direction'] = direction;

	// show loading screen
	loading(true);
	
	if ( appendQuery != null && appendQuery)
	{
		urlSearch += location.search.substring(1);
	}

	// load search results
	sendSearchRequest( urlSearch, category, q, page, params );
	
	return false;
}

function sendSearchRequest(url, category, phrase, page, filter) {
	jQuery.extend(filter, {'category': category, 'q': q, 'page': page});

	$.ajax({
			type: 'GET',
			url: url,
			data: filter,
			complete: function() { loading(false); },
			error: function(a,b,c) { alert(b); },
			success: function(msg) {
				$('#resultList').html(msg);
				// init country autocomplete field if necessary
				if ( category == 'user' ) countryInit();
				setScrollAnimation('#resultList');
			} });
}

/*
function sendRequest( category, phrase, page, filter ) {
	loading( true );
	jQuery.extend(filter, {'category': category, 'phrase': phrase, 'page': page });

	$.ajax({
			type: 'GET',
			url: urlCommSearch,
			data: filter,
			complete: function() { 
				categories = new Array('user','group','forum');
				catLen = categories.length;

				// change colors to def and hide categfilters					
				for ( var i=0; i<catLen; i++ ) {
					if ( $('#search_'+categories[i]).length != 0 ) $('#search_'+categories[i]).css('color', 'blue');
					//Element.hide('filter_'+categories[i]);
				}

				loading(false);

				// set color on active categ
				$('#search_'+category).css('color','red');
				
				// display filters of active categ
				//Element.show('filter_'+category);
			 },
			error: function(a,b,c) { alert(b); },
			success: function(msg) {
				$('#resultList').html(msg);
	} });
}
*/

function countryInit() { 
	$("#country").autocomplete(countries, {
		minChars: 2,
		max: 12,
		delay: 100,
		autoFill: true,
		mustMatch: true,
		matchContains: true,
		width: 320,
		scroll: false,
		scrollHeight: 300,
		onKeyPress: function(){},
		formatItem: function(row, i, max) {
			return row.name + " [" + row.id + "]";
		},
		formatMatch: function(row, i, max) {
			return row.name;
		},
		formatResult: function(row) {
			return row.name;
		}
	});
};


function loading(bool) {
	if (bool) {
		$('#fields').css('display', 'none');
		$('#loading').css('display', '');
		return;
	}
	$('#fields').show();
	$('#loading').hide();
}