var busy = false, key, spec = 0, load = false, curtab = 'Employer', formSpecs = false;
var user = 0;
var services = [];
var tabs = {"Employer":"\u0417\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0443","Virtuzor":"\u0418\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044e"};
var timeOut = 1;

var specLinks = {"2":["3","126","5"],"6":["7","4","9","10","12","127","92","121","14","11","122","15","99","8","16"],"52":["53","54","13","56","55","57","58"],"17":["18","19","96","97","20"],"21":["22","90","23","24","25","66","27","28","29"],"42":["43","44","45","46","47","125","26","48","49","50","51"],"30":["123","31","32","33","91","101","34","103","107","35","36","131","37","109","38","39","40","133","124","94","98","112","41"],"104":["60","61","105","62","111"],"72":["73","74","75","76","77","78"],"59":["63","64","65","67","68","69","70","71"],"113":["114","106","116","117","118"],"79":["80","130","81","129","82","83","119","84","132","110","85","120","102","86","87","88","89"]};

$(document).ready(function(){
	reqGetSecondSpecs(6);
	$('img[id^="run"]').hide();
	if (!document.location.hash.length) {
		key = document.location.href.replace(/#[a-z0-9=&]+/i, '');
		document.location.href = key + '#' + curtab;
	}
	$.historyInit(pageload);
	if (document.location.hash.length) {
		pageload(document.location.hash.replace(/#[a-z0-9=&]+/i, ''));
		load = true;
	}
});

function pageload(hash) {
	if (hash) {
		hash = hash.split('=');
		if (hash.length == 2 && hash[0] == 'spec' && !isNaN(hash[1])) {
			curtab = 'Virtuzor';
			spec = parseInt(hash[1]);
			formSpecs = true;
		} else if ($.inArray(hash[0], ['Employer', 'Virtuzor']) >= 0) {
			curtab = hash[0];
		}
		if (load || curtab == 'Virtuzor') {
			reqGetContent(curtab);
		}
	} else if (load) {
		history.back();
	}
}

function reqMenu(hash) {
	$.historyLoad(hash);
	return;
}

function reqGetContent(tab) {
	if (busy) return;
	key = document.location.href.replace(/#[a-z0-9=&]+/i, '');
	document.location.href = key + '#' + tab;
	for (key in tabs) {
		$('#menu' + key).removeClass('act');
		$('#menu' + key).text(tabs[key]);
	};
	$('#menu' + tab).addClass('act');
	$('#menu' + tab).html('<span>' + tabs[tab] + '</span>');

	busy = true;
	$('#run').show();
	postData = ['act=getContent', 'tab=' + tab];
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'html',
		success: function(html) {
			$('#content').html(html);
			curtab = tab;
			lefted = false;
			$('#run').hide();
			busy = false;
			if (curtab == 'Virtuzor' && formSpecs) {
				formSpecs = false;
				reqSpecs();
			}
		}
	});
	return;
}

var lefted = false;;

function reqSelectService(spec, cleft, cright) {
	if (busy) return;
	busy = true;
	$('#run').show();
	postData = ['act=selectService', 'parent=' + spec, 'cleft=' + cleft, 'cright=' + cright];
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'html',
		success: function(html) {
			$('li[id^="liservice"]').removeClass('ch_act');
			$('#stepTwoServices').html(html);
			$('#liservice' + spec).addClass('ch_act');

			$('#employerContent').addClass('invis_ch');
			sortSelectedServices();
			$.each(services, function() {
				$('#ch' + this.service).check();
			});

			if (!lefted) goLeft(0);

			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function goLeft(pos) {
	if (pos == 230) pos = 229;
	if (pos <= 229) {
		$('#picLeft').css({'margin-left': (0 - pos)});
		$('.ch_2').width(691 + pos);
		window.setTimeout('goLeft(' + (pos + 10) + ')', timeOut);
	} else {
		lefted = true;
	}
	return;
}

function goRight(pos) {
	if (pos == 230) {
		pos = 229;
		$('#stepTwoServices').empty();
		$('#stepTwoSelected div').empty();
		$('#employerContent').attr('class', 'general');
	}
	if (pos <= 229) {
		$('#picLeft').css({'margin-left': (pos - 229)});
		$('.ch_2').width(920 - pos);
		window.setTimeout('goRight(' + (pos + 10) + ')', timeOut);
	} else {
		lefted = false;
	}
	return;
}

function closeStepTwo() {
	services = [];
	$('#run').show();
	$('li[id^="liservice"]').removeClass('ch_act');
	goRight(0);
	$('#run').hide();
	return;
}

function reqProjectAdd() {
	if (busy || !services.length) return;
	if (user) {
		pageTracker._trackPageview('/ajaxevents/addjobsrvindexbutauth/')
	} else {
		pageTracker._trackPageview('/ajaxevents/addjobsrvindexbut/')
	}
	var curr = [];
	$.each(services, function() {
		curr.push(this.service);
	});
	postData = ['act=projectAdd', 'services=' + curr.join(',')];
	busy = true;
	$('#run').show();
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'json',
		success: function(data) {
			if (data.ok) {
				document.location.href = '/add-project/';
			}
			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function checkService(service) {
	if (busy) return;
	busy = true;
	$('#run').show();
	postData = ['act=checkService', 'service=' + service];
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'json',
		success: function(data) {
			if ($('#ch' + service).is(':checked')) {
					services.push({'service': service, 'html': data.html});
			} else {
				var prev = services;
				services = [];
				$.each(prev, function() {
					if (this.service != service) {
						services.push({'service': this.service, 'html': this.html});
					}
				});
			}
			sortSelectedServices();
			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function sortSelectedServices() {
	var cols = [[], [], []];
	i = 0;
	$.each(services, function() {
		var mod = i % 3;
		cols[mod].push(this.html);
		i ++;
	});
	$('#stepTwoSelected div').empty();
	for (i = 0; i < 3; i ++) {
		$('#stepTwoSelected div').eq(i).html(cols[i].join(''));
	}
	if (services.length) {
		$('#stepTwoSelected').show();
		$('#button').removeClass('gr_but');
	} else {
		$('#stepTwoSelected').hide();
		$('#button').removeClass('gr_but').addClass('gr_but');
	}
	return;
}

function reqGetSecondSpecs(firstSpec) {
	if (busy) return;
	busy = true;
	$('#runVirtuzors').show();
	postData = ['act=getSecondSpecs', 'spec=' + firstSpec];
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'json',
		success: function(data) {
			$('li[id^="lispec"]').removeClass('ch_act');
			$('#specsList').html(data.specs);
			$('#specsListUsers').html(data.users);
			$('#lispec' + firstSpec).addClass('ch_act');
			$('#runVirtuzors').hide();
			busy = false;
		}
	});
	return;
}

function reqSpecs() {
	if (busy) return;
	busy = true;
	$('#run').show();
	pageTracker._trackPageview('/ajaxevents/regindexvir/');
	postData = ['act=getSpecs', 'type=radio'];
	$.ajax({
		type: 'post',
		url: '/ajax/user.index.ajax.php',
		data: postData.join('&'),
		dataType: 'html',
		success: function(html) {
			popupShow(html);
			if (spec) { presetSpec(spec.toString()); }
			$('#run').hide();
			busy = false;
		}
	});
	return;
}

function reqToggle(id) {
	if ($('#span' + id).attr('class') == 'open') {
		$('#span' + id).attr('class', 'hidden');
		$('#ul' + id).hide();
	} else {
		$('#span' + id).attr('class', 'open');
		$('#ul' + id).show();
	}
	return;
}

function reqClear(pfx) {
	$('input[id^="' + pfx + '"]:checked').uncheck();
	$('span[id^="select"]').attr('class', '');
	return;
}

function specClickRadio(id) {
	if ($('#spec' + id).is(':checked')) {
		$('span[id^="select"]').attr('class', '');
		for (key in specLinks) {
			if ($.inArray(id, specLinks[key]) >= 0) {
				$('#select' + key).attr('class', 'selected');
			}
		}
	}
	return;
}

function presetSpec(id) {
	reqClear('spec');
	for (key in specLinks) {
		if ($.inArray(id, specLinks[key]) >= 0) {
			$('#span' + key).attr('class', 'open');
			$('#ul' + key).show();
			$('#select' + key).attr('class', 'selected');
		}
	}
	$('#spec' + id).check();
	return;
}

function reqSelectSpecMain() {
	if (busy) return;
	busy = true;
	$('#run').show();
	var prev = spec;
	var curr = $('input[id^="spec"]:checked');
	if (!curr.length) {
		spec = 0;
		popupHide();
	} else {
		spec = parseInt($(curr[0]).val());
		$.historyLoad('spec=' + spec);
		var postData = ['act=regetSpec', 'spec=' + spec];
		$.ajax({
			type: 'post',
			url: '/ajax/user.index.ajax.php',
			data: postData.join('&'),
			dataType: 'json',
			success: function(data) {
				popupHide();
				document.location.href = '/registration/virtuzor/';
				$('#run').hide();
				busy = false;
			}
		});
	}
	return;
}