function changeLang(lang) {

	if (lang == 'rus') {
		$('rus').disabled = true;
		$('eng').disabled = false;
	} else {
		$('rus').disabled = false;
		$('eng').disabled = true;
	}
	return false;
}


Init = function (){
	$$('a').each ( function (a){
		a.onfocus = function(){this.blur();};
	});

	new Ajaxer($$('.menu a'));
}
window.onload = Init;


var Ajaxer = Class.create();
Ajaxer.prototype = {

	initialize: function (links) {
		this.links = links;
		this.content = 'content';
		this.links.each (
			function(a) {

				if (!a.href.match('#')) {
					Event.observe(a, 'click', this.show.bind(this, a));
					a.onclick = function () {return false;}

				}
			}.bind(this)
		)
	},
	show: function(a) {
		this.links.each( function (href) {
			if (href == a) {
				href.className = 'current'
			} else {
				href.className = href.className.replace('current', '');
			}
		})
		new Ajax.Updater(
			this.content,
			a.href,
			{
				evalScripts: true
			}

		);
	}


}

var Work = Class.create();

Work.prototype = {

	initialize: function (list, id) {

		this.list = list;
		this.id   = id;
		this.item = false;

		this.prev  = $$('.left').first();
		this.next  = $$('.right').first();
		this.links = $$('.projectList a');


		this.imagepath = '/images/works/';
		this.filename  = $('filename');
		this.name	   = $('name');
		this.year	   = $('year');
		this.how 	   = $('how');
		this.url       = $('url');
		this.who       = $('who');

		Event.observe(this.prev, 'click', this.prevElement.bind(this));
		this.next.onclick = function () {return false;}
		Event.observe(this.next, 'click', this.nextElement.bind(this));
		this.prev.onclick = function () {return false;}
		this.links.each (
			function(a) {
				Event.observe(a, 'click', this.showId.bind(this, a));
				a.onclick = function () {return false;}
			}.bind(this)
		)
	},

	showId: function(link) {
		this.id = link.href.match(/(\d+)\.html/)[1]
		this.findItems();
		this.show(this.item);
	},

	findItems: function() {
		var cnt = 0;

		this.list.each (
			function(item) {
				if (item.id == this.id) {
					this.item = cnt;
				}
				cnt ++;
			}.bind(this)
		)
	},

	prevElement: function () {
		this.findItems();

		var prev = this.item-1;
		if (prev <0) {
			prev = this.list.length-1;
		}
		this.show(prev);
	},

	nextElement: function () {
		this.findItems();

		var next = this.item+1;
		if (next > this.list.length-1) {
			next = 0
		}
		this.show(next);
	},


	show: function(pos) {
		this.item = pos
		this.element = this.list[this.item];
		this.id = this.element.id;

		this.hideEffect();

	},
	hideEffect: function() {
		//this.filename.hide();
		img = new Image();
		img.src = this.imagepath + this.element.filename;
		new Effect.Opacity('container',
		{ duration: 2.0,
		  transition: Effect.Transitions.linear,
		  from: 1.0, to: 0,
		  afterFinish: function () {
			//$('load').style.display = 'block';

			this.showItem();
		  }.bind(this)
		 }
		 );
	},

	showItem: function() {
		this.name.textContent = this.element.name;
		this.year.textContent = this.element.year;
		this.how.textContent  = this.element.how;
		this.who.textContent  = this.element.who;
		this.url.textContent  = this.element.url.replace('http://', '');
		this.url.href  = this.element.url;
		this.filename.src = this.imagepath + this.element.filename;
		this.filename.onload = function () {
		    this.showEffect();
		}.bind(this)
	},

	showEffect: function() {
		 new Effect.Opacity('container',
		    { duration: 2.0,
		      transition: Effect.Transitions.linear,
		      from: 0, to: 1,

		      afterFinish: function () {
				//this.filename.show()
				//this.filename.onload = function () {
				//}.bind(this);


		      }.bind(this)
		     }
		);


	}


}
