Event.observe(document, 'dom:loaded', function(){
  SwitchLanguages.init()
  SwitchWorks.init()
  ArtistImage.init()
})

var SwitchWorks = {
  init: function() {
    if ($('smallWorks') && $('largeWork')) {
      this.largeWork = $('largeWork')
      this.largeWork.img = this.largeWork.down('img')
      this.largeWork.description = this.largeWork.down('div')
      this.works = $$('#smallWorks li')
      this.works.each(function(work){
        work.largeWorkSrc = work.down('a').href
        this.preload(work.largeWorkSrc)
        work.largeWorkDescription = work.down('div')
        work.observe('click', this.showWork.bindAsEventListener(this))
      }.bind(this))
      this.doShowWork(this.works[0])
    }
  }
  ,preload: function(src) {
    var img = new Image()
    img.src = src
  }
  ,showWork: function(event) {
    event.stop()
    this.doShowWork(event.findElement('li'))
  }
  ,doShowWork: function(work) {
    this.largeWork.img.src = work.largeWorkSrc
    this.largeWork.description.innerHTML = work.largeWorkDescription.innerHTML
    this.works.invoke('removeClassName', 'current')
    work.addClassName('current')
  }
}

var SwitchLanguages = {
	init: function() {
		this.update = this._update.bind(this)
		this.update()
	},
	_update: function(){

		$$('div.switchLanguagesInput').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('li.' + link.lang) || langSwitch.down('div.' + 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 ArtistImage = {
  init: function() {
    if ($('artists')) {
      this.artistsCollection = $$('#artists li')
      this.artistsCollection.each(function(artist){
        artist.down('a').observe('mouseover', this.showImage)
      }.bind(this))
    }
  },
  showImage: function() {
    this.imagePath = this.next('span').innerHTML
    $('photos').innerHTML = ''
    if (this.imagePath != '') {
      this.newImage = new Element('img', {src: this.imagePath})
      $('photos').insert(this.newImage)
    }
  }
}