Event.observe(document, 'dom:loaded', function() {
	ContributorsSelector.init()
	SwitchLanguages.init()
	EditingStyle.init()
	FloatProjectWidth.init()
	ShowYears.init()
	Navigation.init()
})
Event.observe(window, 'load', function() {
	ShowHideAbout.init()
})

var SwitchLanguages = {
	init: function() {
		this.update = this.update.bind(this)
		this.update()
	},
	update: function(){
		$$('.switchLanguages').each(function(langSwitch) {
			if (!langSwitch.links) {
				langSwitch.links = langSwitch.select('p.switch a')
				langSwitch.links.each(function(link){
					link.container = langSwitch
					link.field = langSwitch.down('input.' + link.lang) || langSwitch.down('textarea.' + link.lang)
					link.selectLang = function() {
						this.container.links.invoke('unselectLang')
						this.field.show()
						this.addClassName('current')
					}
					link.unselectLang = function() {
						this.field.hide()
						this.removeClassName('current')
					}
					link.observe('click', link.selectLang.bindAsEventListener(link))
				})
				langSwitch.links[0].selectLang()
			}
		})
	}
}

var ContributorsSelector = {
	init: function() {
		if (this.initData()) {
			this.showCurrentLink.observe('click', this.showCurrentContributors.bind(this))
			this.showAllLink.observe('click', this.showAllContributors.bind(this))
			this.showCurrentContributors()
		}
	},
	showCurrentContributors: function() {
		this.contributors.each(function(contributor) {
			contributor.hasClassName('current') ? contributor.show() : contributor.hide()
		})
		this.showCurrentLink.addClassName('current')
		this.showAllLink.removeClassName('current')
	},
	showAllContributors: function() {
		this.contributors.each(function(contributor) {
			contributor.show()
		})
		this.showCurrentLink.removeClassName('current')
		this.showAllLink.addClassName('current')
	},
	initData: function() {
		if ($('showCurrentContributors') && $('showAllContrubutors')) {
			this.showCurrentLink = $('showCurrentContributors')
			this.showAllLink = $('showAllContrubutors')
			this.contributors = $$('.contributor')
			return true
		}
	}
}

var PercentDraggable = Class.create(Draggable, {
	currentDelta: function() {
		return this.element.positionedOffset()
	},
})


var Navigation = {
	init: function() {
		if (this.initData()) {
			this.hideMenuLink.observe('click', this.hideMenu)
			this.showMenuLink.observe('click', this.showMenu)
			this.switchMenu.setStyle({display: 'block'})
			this.navItems.each(function(item){
				item.originalText = item.innerHTML
				item.originalWidth = item.getWidth()
			}.bind(this))
			this.checkMenuSwitch()
			if ($('startpage')) {
				document.observe('click', this.hideMenu)
			}
		}
		Event.observe(window, 'resize', function(){
			Navigation.makeColumns()
		})
	},
	initData: function() {
		if ($('navigation') && $('hideMenu') && $('showMenu') && $('menuList')) {
			this.hideMenuLink = $('hideMenu')
			this.showMenuLink = $('showMenu')
			this.switchMenu = $('switchMenu')
			this.menuContainer = $('menuList')
			this.topMenuContainer = $('aboutFirmaLinks')
			this.makeColumns()
			if ($('navigationShade')) this.navigationShade = $('navigationShade')
			if ($$('div.project div.about')) this.projectAbout = $$('div.project div.about')[0]
			this.showMenu = this._showMenu.bindAsEventListener(this)
			this.hideMenu = this._hideMenu.bind(this)
			this.initDragging()
			return true
		}
	},
	makeColumns: function() {
		if ($('navigation')) {
			if ($('nav_23')) {
				$('nav_23').remove()
			}
			if ($('nav_37')) {
				$('nav_37').remove()
			}
			this.allLinks = $('menuHiddenList').childElements()
			this.newNavigation = $('menuList')
			this.newNavigation.innerHTML = ''
			this.topMargin = 214
			this.menuItemHeight = 30
			this.newAutoHeight = document.viewport.getDimensions().height - this.topMargin
			if (this.newAutoHeight < 348) {
				this.newAutoHeight = 348
			}
			$('navigationWrapper').setStyle({height: this.newAutoHeight + 'px'})
			this.itemsAtColumn = ((document.viewport.getDimensions().height - this.topMargin) / this.menuItemHeight).floor() - 2
			if (this.itemsAtColumn < 10) {
				this.itemsAtColumn = 10
			}
			var ul = ''
			this.allLinks.each(function(li, index) {
				if ((index % this.itemsAtColumn) == 0) {
					ul = new Element("ul")
					this.newNavigation.insert(ul)
				}
				ul.insert({bottom: '<li id="' + li.id + '" class="' + li.className + '">' + li.innerHTML + '</li>'})
			}.bind(this))
			this.navItems = [this.menuContainer.select('li'), $('aboutFirmaLinks').select('.wrapper')].flatten()
		}
	},
	initDragging: function(){
		if (this.menuContainer.hasClassName('draggable')) {
			this.navItems.each(function(item){
				if (item.id.match(/^nav_\d+$/)) {
					new PercentDraggable(item, {
						snap: function(x, y, draggable){
							var dimensions = document.viewport.getDimensions()
							var left = (x * 100 / dimensions.width).round() / 100 * dimensions.width
							var top = (y * 100 / dimensions.height).round() / 100 * dimensions.height
							return [left, top]
						},
						onStart:function(draggable, e){
							draggable.element.select('a').each(function(a){
								a.observe('click', Event.stop)
							}.bind(this))
						}.bind(this),
						onEnd:function(draggable, e){
							setTimeout(function(){
								draggable.element.select('a').each(function(a){
									a.stopObserving('click', Event.stop)
								}.bind(this))
							}.bind(this), 500)

							var dimensions = document.viewport.getDimensions()
							var left = (parseInt(draggable.element.getStyle('left')) * 100 / dimensions.width).round() + '%'
							var top = (parseInt(draggable.element.getStyle('top')) * 100 / dimensions.height).round() + '%'
							draggable.element.setStyle({left:left, top:top})

							var id = draggable.element.id.match(/\d+/)
							this.storePosition(id, left, top)
						}.bind(this)
					})
				}
			}.bind(this))
		}
	},
	_showMenu: function() {
		this.hideNavItems()
		this.showShade()
		this.menuContainer.setStyle({display: 'block'})
		this.topMenuContainer.setStyle({display: 'block'})
		this.checkMenuSwitch()
		$('navigation').setStyle({height: 'auto'})
		this.hideAbout()
		setTimeout(this.showNavItems.bind(this), 100)
	},
	_hideMenu: function() {
		this.hideNavItems()
		this.menuContainer.setStyle({display: 'none'})
		this.topMenuContainer.setStyle({display: 'none'})
		this.hideShade()
		this.showAbout()
		$('navigation').setStyle({height: '40px'})
		this.checkMenuSwitch()
	},

	showAbout: function() {
		if (this.projectAbout) new Effect.Opacity(this.projectAbout, {to: 1, duration: 0.1, transition: Effect.Transitions.linear})
	},
	hideAbout: function() {
		if (this.projectAbout) new Effect.Opacity(this.projectAbout, {to: 0, duration: 0.1, transition: Effect.Transitions.linear})
	},

	showShade: function() {
		if (this.navigationShade) {
			this.navigationShade.setStyle({display: 'block', opacity: 0})
			new Effect.Opacity(this.navigationShade, {from: 0, to: 0.65, duration: 0.1, transition: Effect.Transitions.linear})
		}
	},
	hideShade: function() {
		if (this.navigationShade) {
			new Effect.Opacity(this.navigationShade, {to: 0, duration: 0.1, transition: Effect.Transitions.linear, afterFinish:
				function(){
					this.navigationShade.setStyle({display: 'none'})
				}.bind(this)}
			)
		}
	},

	showNavItems: function() {
		this.navItems.each(function(item){
			item.addClassName('hide_extra')
			item.show()
			item.setStyle({opacity: 0})
			item.originalWidth = item.getWidth() - parseInt(item.getStyle('padding-left')) - parseInt(item.getStyle('padding-right'))
			item.setStyle({opacity: 1, width: 0})
			item.morph('width: '+item.originalWidth+'px', {duration: 0.5, transition: Effect.Transitions.linear, afterFinish:function(effect){
				item.removeClassName('hide_extra')
			}})
		})
		document.observe('click', this.hideMenu)
	},
	hideNavItems: function() {
		this.navItems.each(function(item){
			item.hide()
		})
		document.stopObserving('click', this.hideMenu)
	},
	checkMenuSwitch: function() {
		if (this.menuContainer.getStyle('display') == 'none') {
			this.showMenuLink.setStyle({display: 'block'})
			this.hideMenuLink.setStyle({display: 'none'})
		} else {
			this.showMenuLink.setStyle({display: 'none'})
			this.hideMenuLink.setStyle({display: 'block'})
		}
	}
}

var ShowHideAbout = {
	init: function() {
		$A(document.getElementsByClassName('slide')).each(function(slideElement){
			if (slideElement) {
				slideElement.wrapper = $('wrapper')
				slideElement.wrapperHeight = slideElement.wrapper.getHeight()
				slideElement.titleHeight = slideElement.wrapper.down('h2').getHeight() + 4


				slideElement.slideBlocks = function() {
					if (this.className == 'switch opened') {
						this.removeClassName('opened')
						this.addClassName('closed')
						new Effect.Morph(slideElement.wrapper, {
							style: 'height: '+ slideElement.titleHeight +'px;', duration: 0.5,
							afterFinish: function(){ this.animation = true }.bind(this)
						})
					} else if (this.className == 'switch closed') {
						this.removeClassName('closed')
						this.addClassName('opened')
						new Effect.Morph(slideElement.wrapper, {
							style: 'height: '+ slideElement.wrapperHeight +'px;', duration: 0.5,
							afterFinish: function(){ this.animation = true }.bind(this)
						})
					}
				}

				slideElement.select('.switch').each(function(switchObj){
					switchObj.observe('click', slideElement.slideBlocks)
					switchObj.observe('mousedown', function(e){e.stop()})
				}.bind(this))
			}
		})
	}
}


var FloatProjectWidth = {
	init: function(){
		$A(document.getElementsByClassName('float')).each(function(container){
			elements = container.down('.new_style').childElements()
			var totalWidth = 0
			elements.each(function(element){
				if (element != "[object HTMLScriptElement]"){
					totalWidth = totalWidth + element.getWidth()
				}
			})
			container.setStyle({width: totalWidth + 'px'})
		})
	}
}

var EditingStyle = {
	init: function(){
		if (this.switcher = $('project_new_editing_style')) {
			this.switchStyle = this.switchStyle.bindAsEventListener(this)
			this.switcher.observe('change', this.switchStyle)
			this.switchStyle()
		}
	},
	switchStyle: function(){
		var newStyle = this.switcher.checked
		$$('.old_editing_style').invoke(newStyle ? 'hide' : 'show')
		$$('.new_editing_style').invoke(newStyle ? 'show' : 'hide')
	}
}

var AudioPlayer = {
	toggle: function(id){
		swfobject.getObjectById(id).toggle()
	}
	, onPlay: function(id){
		$(id).up('div.audio_player').down('input').value = 'Stop'
	}
	, onStop: function(id){
		$(id).up('div.audio_player').down('input').value = 'Play'
	}
}

var ShowYears = {
	init: function() {
		if(slideContainer = $('years')) {
			lastItem = slideContainer.down('ul').childElements().last()
			lastItem.addClassName('active')
			lastItem.down('.yearInfo').setStyle({height: lastItem.down('.wrapper').getHeight() + 'px'})
			duration = 0.5
		
			slideSwitch = function() {
				slideObject = this.up('li').down('.yearInfo')
				slideObjectHeight = slideObject.down('.wrapper').getHeight()
				heightSum = 0
				$A(slideContainer.getElementsByClassName('yearInfo')).each(function(slideUpElement){
					if (slideUpElement.getHeight() > 0) {
						slideUpElement.up('li').removeClassName('active')
						slideUp(slideUpElement)
					}
					heightSum = heightSum + slideUpElement.getHeight()
				})
				if (heightSum == 0) {
					slideDown()
				}
			},
		
			slideUp = function(slideUpElement) {
				this.animation = true
				if (!this.slideAnimation || this.animation == true) {
					this.animation = false
					this.slideUpAnimation = new Effect.Morph(slideUpElement, {
						style: 'height: 0px;', duration: duration,
						afterFinish: function(){
							if(slideUpElement != slideObject){
								slideDown()
							}
							this.animation = true
						}.bind(this)
					})
				}
			},
		
			slideDown = function() {
				this.animation = true
				if (!this.slideAnimation || this.animation == true) {
					scrolledHeight = document.viewport.getScrollOffsets().top
					topToYears = $('historyTitle').cumulativeOffset()[1]
					new Effect.Tween(null, scrolledHeight, topToYears, {duration: duration}, function(p){ scrollTo(0,p) })
					this.animation = false
					this.slideAnimation = new Effect.Morph(slideObject, {
						style: 'height: '+ slideObjectHeight + 'px;', duration: duration,
						afterFinish: function(){ 
							this.animation = true
							slideObject.up('li').addClassName('active')
						}.bind(this)
					})
				}
			}
		
			slideContainer.select('span.year').each(function(switchObj){
				switchObj.observe('click', slideSwitch)
				switchObj.observe('mousedown', function(e){e.stop()})
			})
		}
	}
}

// document.observe("dom:loaded", function() {
// 	init_events();
// });
//
// ival=10;
//
// src=Array();
// href=Array();
// w=Array();
// pos=Array();
// word=Array();
// var maxw=0;
// var maxi=0;
// var objects;
// var stylei=0;
// var asdqwe=0;
//
// function init_events() {
//
// 	menu=$('logo');
// 	if($('pdf')){new Draggable('pdf');}
//
// 	menu.observe('click', showmenu)
//
// 	node=$('menu3');
// 	objects = getElementsByClass("randomize",node,"div");
// 	for(i=0; i<objects.length; i++){
// 		if (objects[i].firstChild.innerHTML != undefined) {
// 			href[i]=objects[i].firstChild.getAttribute("href");
// 			word[i]=objects[i].firstChild.innerHTML;
// 		} else {
// 			word[i]=objects[i].innerHTML;
// 			stylei=i;
// 		}
// 		pos[i]=0;
// 		w[i]="";
// 		if (word[i].length > maxw) {
// 			maxw=word[i].length;
// 			maxi=i;
// 		}
// 	}
// }
//
//
// var z=0;
// function showmenu(){
// 	if (asdqwe != 1){
// 		for (j=0;j < objects.length; j++){
// 			pos[j]=0;
// 			w[j]="";
// 		}
//
// 		if ($('firmamenu').style.display == "block") {
// 			sh();
// 		} else {
// 			mchr = self.setInterval(randomize_chars, ival);
// 			mt = self.setTimeout(sh, ival+1);
// 		}
// 	}
// }
//
// function sh(){
// 	if ($('firmamenu').style.display == "block"){
// 		$('firmamenu').style.display="none"
// 		$('main_menu').style.display="none"
// 		if ($('pdf')) $('pdf').style.display="block"
// 	} else {
// 		$('firmamenu').style.display="block"
// 		$('main_menu').style.display="block"
// 		if ($('pdf')) $('pdf').style.display="none"
// 	}
// }
//
// function randomize_chars(){
// 	for(i=0; i<objects.length; i++){
// 		if (word[i].length >= pos[i]) {
// 			w[i]=word[i].substr(0,pos[i]);
// 			pos[i]=pos[i]+1;
// 		}
// 		z=createRandomUpperCaseLetter();
// 		if (word[i] != undefined) {
// 			if (word[i].length < pos[i]) z="";
// 			if (href[i]!= null) {
// 				objects[i].innerHTML="<a href='"+href[i]+"'>"+ w[i] + z +"</a>";
// 			} else {
// 				objects[i].innerHTML= w[i] + z;
// 			}
// 		}
// 		if (maxi == i && maxw < pos[i]){
// 			mchr = window.clearInterval(mchr);
// 		}
// 	}
// }
//
// function randomize_chars_to_delete(){
// 		i=0;
// 		object=$('randomize');
// 		for (var childItem in object.childNodes) {
// 			if (object.childNodes[childItem].nodeType == 1)	{
// 						if (word[i].length >= pos[i]) {
// 							w[i]=word[i];
// 							//.substr(0,pos[i]);
// 							pos[i]=pos[i]+1;
// 						} else {
// 				//			window.clearInterval(mchr)
// 						}
//
// 				z=createRandomUpperCaseLetter();
// 				if (word[i].length < pos[i]) z="";
//
// 				if (href[i]!= null) {
// 					object.childNodes[childItem].innerHTML="<a href='#test'>"+word[i]+"  "+ i + " "+ pos[i] + w[i] + z +"</a>";
// 				} else {
// 					object.childNodes[childItem].innerHTML= w[i] + z;
// 				}
//
// 				i++;
// 			}
// 		}
//
// }
//
//
//
// function Folding(target){
// 	menu_header=document.getElementById("menu2_header");
// 	target=document.getElementById("menu2");
// 	if (target.offsetWidth < 110){
// 		$('menu2').morph('background-color: #fff; width: 500px; height: 510px;',{duration: 0.5});
// 		return false;
// 	}
// 	if (target.offsetWidth >=480){
// 		$('menu2').morph('background-color: #fff; width: 100px; height: 100px;',{duration: 0.5});
// 		return false;
// 	}
//
// }
//
//
//
//
//
// function createRandomUpperCaseLetter() {
//    return String.fromCharCode(65 + Math.round(Math.random() * 25));
// }
//
//
//
//
// function getElementsByClass(className, node, tag) {
// 	if (node == null)
// 		node = document;
// 	if ( tag == null )
// 		tag = '*';
//     var all =  node.getElementsByTagName(tag);
//     var elements = new Array();
//     for( var e = 0; e < all.length; e++ ) {
//         var classes = all[e].className.split(/\s/g);
//         for( var c = 0; c < classes.length; c++ ) {
//             if( classes[c] == className ) {
//                 elements[elements.length] = all[e];
//             }
//         }
//     }
//     return elements;
// }
//
//
// function isdefined( variable)
// {
//     return (typeof(window[variable]) == "undefined")?  false: true;
// }
