function hideTellProblem(){
	$('#tellProblemForm').hide();
	$('#tellProblemFeed').hide();
	$('#tellProblemLink').show();
}

function showTellProblem(){
	$('#tellProblemForm').show();
	$('#tellProblemFeed').hide();
	$('#tellProblemLink').hide();
}

function sendTellProblem(SITE_URL){

	$.ajax( {
		type : "POST",
		url  : SITE_URL + 'ajax/ajax_tell_problem.php',
		data : $('#tellProblemFrm').formSerialize(),
		success : function(data) {
			if(data == 0){
				hideTellProblem();
				$('#tellProblemFeed').show();

				$('#tellProblemName').val('');
				$('#tellProblemEmail').val('');
				$('#tellProblemMessage').val('');
			}

			if(data == '1' || data == '3'){
				$('#tellProblemEmail').addClass('err_field');
			}else{
				$('#tellProblemEmail').removeClass('err_field');
			}

			if(data == '2' || data == '3'){
				$('#tellProblemMessage').addClass('err_field');
			}else{
				$('#tellProblemMessage').removeClass('err_field');
			}
		}
	});
}

var sort_key   = '';
var sort_order = '';
var SITE_URL   = '';
var MIN_SEARCH_CHARS = 3;

function initSortByField(order, key){
	// remove class
	if(sort_key != '' && sort_order != ''){
		$('#sortLink_' + sort_key).removeClass('sort_' + sort_order);
	}

	sort_key   = key;
	sort_order = order;

	// set class
	$('#sortLink_' + sort_key).addClass('sort_' + sort_order);
}

function sortByField(type, q, p, key){
	var order = 'asc';

	if(sort_key == key && sort_order == 'asc'){
		order = 'desc';
	}

	initSortByField(order, key);

	searchResult(type, q, p, order, key);
}

function searchResult(type, q, p, order, key){
	var url = SITE_URL + 'ajax/';

	switch(type){
		case "tag":
			url += 'ajax_tag.php';
			url += '?urlsafe=' + q;
			break;
		case "provider":
			url += 'ajax_video_provider.php';
			url += '?urlsafe=' + q;
			break;
		case "category":
			url += 'ajax_category.php';
			url += '?urlsafe=' + q;
			break;
		default:
			url += 'ajax_result.php';
			url += '?q=' + q;
			break;
	}

	url += '&p=' + p;
	url += '&order=' + order;
	url += '&key=' + key;

	ajaxSearchResult(url);
}

function ajaxSearchResult(url){
	$('#searchResult').html('<img src="' + SITE_URL + 'ajax/loading.gif" border=0>');


	$.ajax( {
		type : "GET",
		url  : url,
		success : function(data) {
			$('#searchResult').html( data );
		}
	});
}

function runSearch(form){
	// get query
	var q = form.q.value;

	// remove spec chars
	q = q.replace(/[^\w\x7F-\xFF\s]/g, ' ');

	// length limit
	if(q.length > 64){
		q = q.substring(0, 64);
	}

	// replace short words
	q = ' ' + q + ' ';
	q = q.replace(/ +/g, '  ');

	var re = new RegExp("\\s(\\S{1,"+(MIN_SEARCH_CHARS - 1)+"})\\s", "g");
	q = q.replace(re, ' ');

	// remove extra spaces
	q = q.replace(/^ +/, '');
	q = q.replace(/ +$/, '');
	q = q.replace(/ +/g, '+');

	var url = SITE_URL + 'video/' + q + '/index.html';

	document.location.href = url;

	return false;
}

