var currentTab = -1;

function $( id ) {
	return document.getElementById( id );
}

function toggle_worldwide(el) {
	var obj = document.getElementById(el);
	if(obj) {
		if(obj.className == "") obj.className = "active";
			else obj.className = "";
	}
}

function clickTab( nm ) {
	if (nm == 'next') nm = currentTab + 1;
	if (nm > tabs.length - 1) nm = 0;
	if (nm < 0 ) nm = 0;
	if (nm == currentTab) return;
	var preload = new Image();
	preload.onload = function() {
		$('splash-root').style.backgroundImage = "url("+tabs[ nm ]['bg']+")";
		$('splash-text-value').innerHTML = tabs[ nm ]['title'];
		$('splash-lead').innerHTML = tabs[ nm ]['lead'];
		$('splash-logo').innerHTML = '<img src="'+tabs[ nm ]['logo']+'" alt="'+tabs[ nm ]['title']+'"/>';
		$('tab-'+nm).className = 'active';
		if (currentTab >= 0) {
			$('tab-'+currentTab).className = '';
		}
		currentTab = nm;
	}
	preload.src = tabs[ nm ]['bg'];
}

function randomSplash() {

	var nm = Math.round(Math.random() * (tabs.length - 1));
	$('splash-text').style.display = 'block';
	clickTab( nm );

}

var activatedNews = 0;

function getNews( x ) {

	if (x > 0 && activatedNews != x) {
		ajax.feedback = updateNews;
		$('news-'+activatedNews).className = '';
		activatedNews = x;
		ajax.process( 'press/'+x+'.htm', 'GET');
	}

}

function updateNews( response ) {

	var txt = response.getText();
	if (txt) {
		$('latest-news').innerHTML = txt;
		$('news-'+activatedNews).className = 'active';
	}

}

function activateServices( str ) {

	str = str.replace(/\s+/, '');
	var ids = str.split(',');
	for (var i = 0; i < ids.length; i++) {
		$('service-'+ids[i]).className = 'active';
	}

}

function deactivateServices( str ) {
	str = str.replace(/\s+/, '');
	var ids = str.split(',');
	for (var i = 0; i < ids.length; i++) {
		$('service-'+ids[i]).className = '';
	}

}

function vacancyResponce( response ) {

	$('cv-status').innerHTML = '<p class="form-result">' + response.getText() + '</p>';
}


function getUserSelection() {
	var str = "";
	if (window.getSelection) {
		str = window.getSelection();
	} else if (document.getSelection) {
		str = document.getSelection();
	} else if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		str = range.text;
	} else {
		str = "Sorry, this is not possible with your browser.";
	}
	return str;
}

function addMistake( text ) {

	//var ajax = new ajaxRequest();
	ajax.addPostVar('mistake', 1 );
	ajax.addPostVar('title', location.href );
	ajax.addPostVar('text', text );
	ajax.addPostVar('name', prompt('Your Name?') );
	ajax.addPostVar('comment', prompt('Your Comment?') );

	ajax.feedback = function( response ) {
		alert( response.getText() );
	}

	ajax.process('?addMistake', 'post');

}

function checkMistake( e ) {

	if (!e) e = window.event;
	var key = e.keyCode || e.witch;
	if (e.ctrlKey && key == 13) {
		var str = getUserSelection();
		if (typeof str == 'object') {
			str = str.toString();
		}
		if (str) {

			addMistake( str );

		}
	}

}

document.onkeyup = function(e) {
	checkMistake( e );
}
