var busy = false, i, key, load = false, currPage = 1, pages = 0, spec = 0;
var selected = {'service': 0, 'mode': ''};
var user = 0, person = 0;
var sort = {'type':'default', 'dir':'bot'};
var lastSort = {'rating':'bot', 'projects':'bot', 'recommends':'bot'};
var order = {'up':'asc', 'bot':'desc'};
var options = {'services': {'list': 'pop', 'display': 'visible'}, 'features': {'list': 'pop', 'display': 'visible'}};

$(document).ready(function(){
	if ($('#page').length) {
		$('#run').show();
		currPage = parseInt($('#page').val());
		pages = parseInt($('#pages').val());
		spec = parseInt($('#spec').val());

		$(document).keypress(function(e){
			var code = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
			if (e.ctrlKey) {
				if (code == 37 && currPage > 1) {
					reqLoadPage(currPage - 1);
				}
				if (code == 39 && currPage < pages) {
					reqLoadPage(currPage + 1);
				}
			}
		});

		if (document.location.hash.length) {
			key = document.location.hash.replace(/#/i, '').split('&');
			currPage = parseInt(key[0].replace(/page=/, ''));
			if (key.length > 4 && (key[4] == 'services' || key[4] == 'features')) {
				selected.service = parseInt(key[3].replace(/service=/, ''));
				selected.mode = key[4];
				options[selected.mode]['list'] = 'selected';
			}
		} else {
			var hash = [];
			hash.push('page=' + currPage);
			hash.push('sort=' + sort.type);
			hash.push('dir=' + sort.dir);
			hash.push('service=' + selected.service);
			hash.push(selected.mode);
			key = document.location.href.replace(/#[a-z0-9=&]+/i, '');
			document.location.href = key + '#' + hash.join('&');
		}

		$.each(options, function(mode, curr) {
			reqFilterList(mode);
		});

		$.historyInit(pageload);
		load = true;
		$('#run').hide();
	}
});

function pageload(hash) {
	if (hash) {
		hash = hash.split('&');
		var page = parseInt(hash[0].replace(/page=/, ''));
		sort.type = hash[1].replace(/sort=/, '');
		sort.dir = hash[2].replace(/dir=/, '');
		if (hash.length > 4 && (hash[4] == 'services' || hash[4] == 'features')) {
			selected.service = parseInt(hash[3].replace(/service=/, ''));
			selected.mode = hash[4];
			options[selected.mode]['list'] = 'selected';
		}		
		reqGetItems(page);
	} else if (load) {
		history.back();
	}
}

function reqLoadPage(page) {
	var hash = 'page=' + page + '&sort=' + sort.type + '&dir=' + sort.dir + '&service=' + selected.service + '&' + selected.mode;
	key = document.location.href.replace(/#[a-z0-9=&]+/i, '');
	document.location.href = key + '#' + hash;
	$.historyLoad(hash);
	if (page > 1) { $('#konkurs').hide(); } else { $('#konkurs').show(); }
	return;
}

function reqGetItems(page) {
	if (busy) return;
	busy = true;
	$('#run').show();
	for (key in lastSort) {
		if (key == sort.type) {
			$('#' + key + ' a').attr('class', sort.dir + '_range');
		} else {
			$('#' + key + ' a').attr('class', lastSort[key] + '_range_inact');
		}
	}
	var postData = ['act=loadPage', 'spec=' + spec, 'page=' + page, 'sort=' + sort.type, 'dir=' + order[sort.dir], 'selected=' + selected.service];
	$.ajax({
		type: 'post',
		url: '/ajax/virtuzors.ajax.php',
		data: postData.join('&'),
		dataType: 'html',
		success: function(html) {
			$('#tabContent').html(html);
			currPage = page;
			window.scroll(0, 0);
			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function sortUsers(type) {
	if (sort.type == type) {
		if (lastSort[type] == 'up') {
			lastSort[type] = 'bot';
		} else {
			lastSort[type] = 'up';
		}
	} else {
		sort.type = type;
	}
	sort.dir = lastSort[type];
	reqLoadPage(1);
	return;
}

function reqFilterList(mode) {
	var post = ['act=getFilterList'];
	post.push('spec=' + spec);
	post.push('mode=' + mode);
	post.push('list=' + options[mode].list);
	post.push('display=' + options[mode].display);
	if (mode == selected.mode) {
		post.push('selected=' + selected.service);
	}
	$.ajax({
		type: 'post',
		url: '/ajax/virtuzors.ajax.php',
		data: post.join('&'),
		dataType: 'json',
		success: function(data) {
			options[mode] = data.options;
			if (data.ok) {
				$('#spec' + mode.charAt(0).toUpperCase() + mode.substr(1)).html(data.html);
				$('#spec' + mode.charAt(0).toUpperCase() + mode.substr(1)).show();
			} else {
				$('#spec' + mode.charAt(0).toUpperCase() + mode.substr(1)).hide();
			}
		}
	});
}

function reqFilter(mode, value) {
	if (busy) return;
	busy = true;
	$('#run').show();
	var post = ['act=setFilter', 'mode=' + mode, 'value=' + value];
	$.ajax({
		type: 'post',
		url: '/ajax/virtuzors.ajax.php',
		data: post.join('&'),
		dataType: 'json',
		success: function(data) {
			if (data.ok) {
				options[mode].display = value;
				reqFilterList(mode);
			}
			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function servicesList(mode, list) {
	options[mode].list = list;
	reqFilterList(mode);
	return;
}

function serviceSelect(service, mode) {
	selected.service = parseInt(service);
	selected.mode = mode;
	$.each(options, function(mode, curr) {
		if (mode == selected.mode) {
			options[mode]['list'] = 'selected';
			reqFilterList(mode);
		} else {
			options[mode]['list'] = 'pop';
			$('#spec' + mode.charAt(0).toUpperCase() + mode.substr(1)).hide();
		}
	});
	return;
}

function serviceUnSelect(mode) {
	selected.service = 0;
	selected.mode = '';
	options[mode]['list'] = 'pop';
	$.each(options, function(mode, curr) {
		reqFilterList(mode);
		$('#spec' + mode.charAt(0).toUpperCase() + mode.substr(1)).show();
	});
	reqLoadPage(1);
	return;
}
function reqSpecs(userType) {
	if ($('#spec').length) {
		spec = parseInt($('#spec').val());
	}
	if (userType == 'virtuzor') {
		document.location.href = '/#spec=' + spec;
	} else if (userType == 'employer') {
		if (spec) {
			setcookie('tempProjectSpec', spec, Math.round(new Date().getTime() / 1000) + 3600 * 24 * 14, '/', ".virtuzor.ru");
		}
		document.location.href = '/add-project/';
	}
	return;
}
