var _ = {
	timer : null,
	div_flag: 1,
	events: {
		expand: function(e) {
			if (!e) e = event;
			return {e: e, o: e.srcElement || e.target};
		}
	},
	services: {
		current: {},
		images: {},
		colors : {},
		mouseover: function(e) {
			e = _.events.expand(e);
			var num = e.o.id.substr(1), current = _.services.current[num];
			if (!current) current = 0;
			var id = _.services.all[num][current];
			_.services.fade(num, id);
		},
		mouseout: function(e) {
			e = _.events.expand(e);
			var num = e.o.id.substr(1), current = _.services.current[num], id = _.services.all[num][current];
			$('#serv_'+ num + '_' + id).fadeOut(500);
			if (_.services.timer) clearTimeout(_.services.timer);
			_.services.in_run = false;
		},
		fade: function(num, id) {
			var current = _.services.current[num];
			if (!current) current = 0;
			current++;
			if (current < 1) current =_.services.all[num].length - 1;
			else if (current > _.services.all[num].length - 1) current = 0;
			var next_id = _.services.all[num][current];
			_.services.current[num] = current;
			$('#serv_'+ num + '_' + next_id)[0].style.display = 'none';
			$('#serv_'+ num + '_' + next_id).fadeIn(500);
			$('#serv_'+ num + '_' + next_id)[0].style.zIndex = '30';
			if (!_.services.in_run) {
				_.services.in_run = true;
				_.services.timer = setTimeout('_.services.fade('+ num +','+ next_id +');', 1000);
				return;
			}
			$('#serv_'+ num + '_' + id)[0].style.display = 'block';
			$('#serv_'+ num + '_' + id)[0].style.zIndex = '20';
			$('#serv_'+ num + '_' + id).fadeOut(500);
			_.services.timer = setTimeout('_.services.fade('+ num +', '+ next_id +')', 1000);
			_.services.in_run = true;
		}
	},
	contacts: {
		flag: 0,
		mouseclick: function () {
			var o = $('#g_map')[0], t1 = $('#see_map')[0], t2 = $('#see_vcg_office')[0];
			if (_.contacts.flag) {
				o.style.display = t2.style.display = 'none';
				t1.style.display = 'inline';
				_.contacts.flag = 0;
			}
			else {
				o.style.display = 'block';
				t1.style.display = 'none';
				t2.style.display = 'inline';
				_.contacts.flag = 1;
			}
		}
	},
	works: {
		current: 1,
		flags: {
			1: 1,
			2: 0,
			3: 0
		},
		images: {
			active: 0,
			all: []
		},
		tabs: 0,
		middle: function (e) {
			e = _.events.expand(e);
			var id = e.o.id.substr(1);
			_.works.current = id;
			_.works.flags[id] = 1;
			for (var i = 0; i < _.works.tabs; i++) {
				document.getElementById('middle'+ i).style.display = (_.works.flags[i] && _.works.current == i ? 'block' : 'none');
			}
		},
		moveImage: function (e) {
			e = _.events.expand(e);
			var id = e.o.id;
			var active = _.works.images.active;
			document.getElementById('small_'+ active).style.backgroundPosition = '0 0';
			if (id === 'left_pointer') active--;
			if (id === 'right_pointer') active++;
			if (id === 'case_top') active++;
			if (active < 0) active =_.works.images.all.length - 1;
			else if (active > _.works.images.all.length - 1) active = 0;
			_.works.images.active = active;
			_.works.show(_.works.images.active);
		},
		show: function(num) {
			document.getElementById('small_'+ _.works.images.active).style.backgroundPosition = '0 0';
			var ext = _.works.images.all[num].split('.').pop();
			if (ext == 'swf') {
				$('#case_top')[0].innerHTML = '<div id="swf'+ num +'"><a href="http://www.adobe.com/go/getflashplayer/"><img src="/_/get_flash_player.gif" alt="Get Adobe Flash player"></a></div>';
				swfobject.embedSWF(''+ _.works.images.all[num] +'', 'swf'+ num, '930', '407', '9.0.0', '/_/expressinstall.swf', {}, {});
			} else {
				$('#case_top')[0].innerHTML = '<img height="407" src="'+ _.works.images.all[num] +'" alt="">';
			}
			document.getElementById('small_'+ num).style.backgroundPosition = '-5px 0';
			_.works.images.active = num;
		},
		next: function(e) {
			e = _.events.expand(e).e;
			var active = _.works.images.active, widz = document.getElementById('case_top').firstChild.offsetWidth, pos_x = e.offsetX ? (e.offsetX) : e.pageX - document.getElementById('small_'+ active).offsetLeft;
			document.getElementById('small_'+ active).style.backgroundPosition = '0 0';
			active = active + (widz * 0.4 > pos_x ? - 1 : 1);
			if (active < 0) active =_.works.images.all.length - 1;
			else if (active > _.works.images.all.length - 1) active = 0;
			_.works.images.active = active;
			_.works.show(_.works.images.active);
		}
	},
	filter: {
		show: function(id) {
			document.getElementById(id).style.display = 'block';
		},
		hide: function(id) {
			document.getElementById(id).style.display = 'none';
		}
	},
	about: {
		mouseover: function(e) {
			e = _.events.expand(e);
			var num = e.o.id.substr(1), image = $('#s'+ num)[0];
			_.about.current = image.src;
			image.src = image.src.replace('_grey.', '.');
		},
		mouseout: function(e) {
			e = _.events.expand(e);
			var num = e.o.id.substr(1), image = $('#s'+ num)[0];
			image.src =_.about.current;
		},
		all: []
	},
	first_page: {
		count: null,
		all: {},
		current: 0,
		auto_flag: true,
		opacity: 1,
		filter: 'alpha(opacity=100)',
		show_next: function() {
			if (!_.first_page.auto_flag) return;
			var current = _.first_page.current, id = _.first_page.all[current];
			_.first_page.hide();
		},
		hide: function() {
			var current = _.first_page.current, id = _.first_page.all[current];
			$('#fi'+ id)[0].style.display = 'block';
			_.first_page.shadow_block(current);
			current++;
			if (current < 1) current =_.first_page.all.length - 1;
			else if (current > _.first_page.all.length - 1) current = 0;
			$('#fi'+ _.first_page.all[current])[0].style.display = 'block';
			$('#fi'+ _.first_page.all[current])[0].style.zIndex = '1';
			_.first_page.show();
			_.first_page.current = current;
			_.first_page.highlight_block(current);
		},
		show: function() {
			var current = _.first_page.current, id = _.first_page.all[current];
			$('#fi'+ id)[0].style.display = 'block';
			$('#fi'+ id)[0].style.zIndex = '10';
			$('#fi'+ id).fadeOut(500);
		},
		auto_off: function() {
			clearInterval(_.first_page.timer);
			_.first_page.auto_flag = false;
		},
		click: function(e) {
			var current = _.first_page.current, number = _.first_page.all[current];
			e = _.events.expand(e);
			var id = e.o.id;
			id = id.split('_')[1];
			if (id == current) {
				_.first_page.auto_off();
				return;
			}
			_.first_page.shadow_block(current);
			$('#fi'+ number).fadeOut(150);
			_.first_page.auto_off();
			_.first_page.current = id;
			_.first_page.show_click();
		},
		show_click: function(id) {
			var current = _.first_page.current, id = _.first_page.all[current];
			_.first_page.highlight_block(current);
			$('#fi'+ id).fadeIn(150);
		},
		highlight_block: function(id) {
			if ($('#fblock'+ id)[0]) $('#fblock'+ id)[0].setAttribute('class', 'fblock_hover');
			if ($('#fade_'+ id)[0]) $('#fade_'+ id)[0].setAttribute('class', 'fade_hover');
		},
		shadow_block: function(id) {
			if ($('#fblock'+ id)[0]) $('#fblock'+ id)[0].setAttribute('class', 'block_for_first');
			if ($('#fade_'+ id)[0]) $('#fade_'+ id)[0].setAttribute('class', 'block_for_first_inner');
		}
	}
};

$(document).ready(function() {
	var nodes = $('.div_for_tr_image_a'), i = nodes.length - 1;
	for (; i >= 0; --i) {
		nodes[i].onmouseover = _.services.mouseover;
		nodes[i].onmouseout = _.services.mouseout;
	}
	nodes = $('.map');
	i = nodes.length - 1;
	for (; i >= 0; --i) {
		nodes[i].onclick = _.contacts.mouseclick;
	}
	nodes = $('.case_m_i');
	i = nodes.length - 1;
	for (; i >= 0; --i) {
		nodes[i].onclick = _.works.middle;
	}
	if ($('#left_pointer').length) $('#left_pointer')[0].onclick = _.works.moveImage;
	if ($('#right_pointer').length) $('#right_pointer')[0].onclick = _.works.moveImage;
	if ($('#case_top').length) $('#case_top')[0].onclick = _.works.moveImage;

	if ($('#small_menu').length) {
		nodes = $('#small_menu')[0];
		var so = _.works.images.all.length, html = '<table width="100%" id="c_menu"><tr><td align="center"><table>';
		for (i = 0; i < so; i++) {
			html += '<td class="case_td"><div id="small_'+ i +'" class="case_c_'+ (i ? 'i' : 'a') +'" onclick="_.works.show('+ i +')">&nbsp;</div></td>'
		}
		html += '</table></td></tr></table>';
		nodes.innerHTML = html;
		_.works.show(0);
	}

	if ($('#case_top').length) {
		$('#case_top')[0].onclick = _.works.next;
	}

	if ($('.about_image_one').length) {
		nodes = $('.about_image_one');
		i = nodes.length - 1;
		for (; i >= 0; --i) {
			nodes[i].onmouseover = _.about.mouseover;
			nodes[i].onmouseout = _.about.mouseout;
		}
	}
	if ($('.big_b').length) {
		var first = _.first_page.all[0];
		$('#fi'+ first)[0].style.display = 'block';
		if (!_.first_page.timer) _.first_page.timer = setInterval('_.first_page.show_next();', 5000);
		setTimeout('_.first_page.highlight_block(0);', 100);
	}
	if ($('.block_for_first').length) {
		nodes = $('.block_for_first');
		i = nodes.length - 1;
		for (; i >= 0; --i) nodes[i].onclick = _.first_page.click;
	}
	nodes = document.getElementsByTagName('A');
	var total = nodes.length, domain = location.hostname.toLowerCase();
	if (domain.indexOf('www.') != -1) domain = domain.replace('://www.', '://');
	for (i = 0; i < total; i++) {
		if (nodes[i].href.indexOf('mailto:') != -1) continue;
		if (nodes[i].href.indexOf(domain) == -1) nodes[i].onclick = openWindow;
	}

	// Toggle Vacancies in News->Careers
	if ($('#toggler_1').length) {
		var o1 = $('#vacancy_1'), o2 = $('#vacancy_2'), o3 = $('#vacancy_3'), o4 = $('#vacancy_4'), o5 = $('#vacancy_5'), o6 = $('#vacancy_6'), o7 = $('#vacancy_7');
		$('#toggler_1')[0].onclick = function() {
			o1.toggle('slow');
			o2.hide('slow');
			o3.hide('slow');
			o4.hide('slow');
			o5.hide('slow');
			o6.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_2')[0].onclick = function() {
			o2.toggle('slow');
			o1.hide('slow');
			o3.hide('slow');
			o4.hide('slow');
			o5.hide('slow');
			o6.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_3')[0].onclick = function() {
			o3.toggle('slow');
			o2.hide('slow');
			o1.hide('slow');
			o4.hide('slow');
			o5.hide('slow');
			o6.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_4')[0].onclick = function() {
			o4.toggle('slow');
			o1.hide('slow');
			o2.hide('slow');
			o3.hide('slow');
			o5.hide('slow');
			o6.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_5')[0].onclick = function() {
			o5.toggle('slow');
			o1.hide('slow');
			o2.hide('slow');
			o3.hide('slow');
			o4.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_6')[0].onclick = function() {
			o6.toggle('slow');
			o1.hide('slow');
			o2.hide('slow');
			o3.hide('slow');
			o4.hide('slow');
			o5.hide('slow');
			o7.hide('slow');
		};
		$('#toggler_7')[0].onclick = function() {
			o7.toggle('slow');
			o1.hide('slow');
			o2.hide('slow');
			o3.hide('slow');
			o4.hide('slow');
			o5.hide('slow');
			o6.hide('slow');
		};
	}
});

function openWindow(e) {
	e = _.events.expand(e);
	var o = e.o;
	while (true) {
		if (o.tagName === 'A') break;
		if (!o || o.tagName === 'BODY') return false;
		o = o.parentNode;
	}
	window.open(o.href);
	return false;
}

function Tabs(func_name, container_id) {
	if (typeof func_name !== 'string') {
		alert('Error in Tabs.\nFunction\'s name should be a string.');
		return false;
	}
	if (typeof container_id !== 'string') {
		alert('Error in Tabs.\nContainer ID should be a string.');
		return false;
	}
	this.self = func_name;
	this.uid = container_id;
	this.containers = [];
	this.style = {
		table: '',
		firstRow: '',
		firstRowsCol: '',
		secondRow: '',
		secondRowsCol: '',
		activeTab: '',
		activeTabDiv: '',
		activeFirstTab: '',
		activeMiddleTab: '',
		activeLastTab: '',
		inactiveTab: '',
		inactiveTabDiv: '',
		inactiveFirstTab: '',
		inactiveMiddleTab: '',
		inactiveLastTab: '',
		activeDataContainer: '',
		inactiveDataContainer: ''
	};
	this.current = -1;

	this.$ = function(id) {
		return document.getElementById(id);
	};
	this.ini = function() {
		var o, i, so = this.containers.length, so_ = so - 1, html = '<table style="'+ this.style.table +'"><tr style="'+ this.style.firstRow +'">';
		//if (navigator.userAgent.indexOf('Chrome') == -1) {
		if (navigator.userAgent.indexOf('Firefox') != -1) {
			html += '<td></td>';
		}
		for (o in this.style) {
			i = this.style[o];
			if (i !== '' && i.charAt(i.length - 1) !== ';') {
				alert('Error in Tabs.\nCSS rule "'+ o +'" does not end with semicolon.');
				return false;
			}
		}
		for (i = 0; i < so; i++) {
			html += '<td id="tab_'+ this.containers[i].uid +'" style="'+ this.style.inactiveTab;
			if (i === 0) html += this.style.inactiveFirstTab;
			else if (i === so_) html += this.style.inactiveLastTab;
			else html += this.style.inactiveMiddleTab;
			html += '" onclick="'+ this.self +'.openTab('+ i +')"><div style="'+ this.style.inactiveTabDiv +'">'+ this.containers[i].name +'</div></td>';
		}
		if (so == 1) {
			var px_in_td;
			html += '<td style="width: 6px;';
			if (navigator.userAgent.indexOf('MSIE') != -1) {
				html+= 'border-bottom: 1px solid #e7e7e7;';
				px_in_td = 9;
			} else {
				px_in_td = 4;
			}
			html += '"><img src="/_/corner_l.png" alt="" style="margin-left: -'+ px_in_td +'px; margin-bottom: -4px"></td><td width="99%" style="'+ this.style.afterTabSpace;
		} else {
			html += '<td id="div_with_no_border" style="width: 4px; border-bottom: 1px solid #e7e7e7"><img src="/_/corner_l.png" alt="" style="margin-left: -4px; margin-bottom: -4px"></td><td width="99%" style="'+ this.style.afterTabSpace;
		}
		if (so > 1) html += 'padding:0';
		html += '"></td><td style="border-bottom: 1px solid #e7e7e7"></td></tr><tr style="'+ this.style.secondRow +'"><td colspan="'+ (so + 5) +'" style="'+ this.style.secondRowsCol +'">';
		for (i = so_; i >= 0; --i) {
			o = this.$(this.containers[i].uid);
			html += '<div id="'+ this.containers[i].uid +'" style="'+ this.style.inactiveDataContainer +'">'+ this.$(this.containers[i].uid).innerHTML +'</div>';
			o.parentNode.removeChild(o);
		}
		html += '</td></tr></table>';

		var tabs_o = document.createElement('DIV');
		tabs_o.id = this.uid;
		tabs_o.innerHTML = html;
		o = this.$(this.uid);
		o.parentNode.replaceChild(tabs_o, o);

		this.openTab(0);

		return this;
	};
	this.addTab = function(container_id, name) {
		var o = this.$(container_id);
		if (!o || !o.tagName || o.tagName !== 'DIV') {
			alert('Error in Tabs.\nContainer should be DIV.');
			return false;
		}
		this.containers.push({uid: container_id, name: name});
		return this;
	};
	this.openTab = function(id) {
		if (typeof id !== 'number') {
			alert('Error in Tabs.\nID should be a number.');
			return false;
		}
		if (this.current === id) return;

		var tab_style = this.style.activeTab;
		if (id === 0) tab_style += this.style.activeFirstTab;
		else if (id === this.containers.length - 1) {
			tab_style += this.style.activeLastTab;
			document.getElementById('div_with_no_border').style.borderBottom = '0';
		} else {
			document.getElementById('div_with_no_border').style.borderBottom = '1px solid #e7e7e7';
			tab_style += this.style.activeMiddleTab;
		}
		var o = this.$('tab_'+ this.containers[id].uid);
		o.setAttribute('style', tab_style);
		o.firstChild.setAttribute('style', this.style.activeTabDiv);

		this.setAttr(this.$(this.containers[id].uid), this.style.activeDataContainer);

		if (this.current > -1) {
			tab_style = this.style.inactiveTab;
			if (this.current === 0) tab_style += this.style.inactiveFirstTab;
			else if (this.current === this.containers.length - 1)  tab_style += this.style.inactiveLastTab;
			else tab_style += this.style.inactiveMiddleTab;
			o = this.$('tab_'+ this.containers[this.current].uid);
			o.setAttribute('style', tab_style);
			o.firstChild.setAttribute('style', this.style.inactiveTabDiv);

			this.setAttr(this.$(this.containers[this.current].uid), this.style.inactiveDataContainer);
		}
		this.current = id;
	};

	this.setAttr = function(o, css) {
		o.setAttribute('style', css);
		if ($.browser.msie && $.browser.version * 1 < 8) {
			css = css.split(';');
			var so = css.length - 1, i = 0, parts;
			for (; i < so ; i++) {
				parts = css[i].split(':');
				if (parts.length !== 2) continue;
				o.style[parts[0].replace(/^\s\s*/, '').replace(/\s\s*$/, '')] = parts[1].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			}
		}
	}
	return this;
}

// Preload images
var CacheElements = {
	o: null,
	storage: [],
	stored: [],
	store: function(src) {
		var es = this.storage, i = 0, so = es.length;
		for (; i < so; i++) {
			if (es[i] === src) return;
		}
		this.storage[so] = src;
		this.stored[so] = false;
	},
	now: function() {
		if (!this.o) {
			var div = document.createElement('DIV');
			div.style.position = 'absolute';
			div.style.top = '-9999px';
			div.style.left = '-9999px';
			this.o = document.body.appendChild(div);
			div = document.createElement('DIV');
			div.style.position = 'relative';
			div.id = 'elements_image_preloader';
			this.o = this.o.appendChild(div);
		}
		var i = 0, so = this.storage.length, img;
		for (; i < so; i++) {
			if (this.stored[i]) continue;
			img = document.createElement('IMG');
			img.style.position = 'absolute';
			img.style.top = 0;
			img.style.left = 0;
			img.src = this.storage[i];
			this.o.appendChild(img);
			this.stored[i] = true;
		}
	}
}
