window.addEvent('domready', function () {
  
  
  
  $$('a').each(function(el){
    el.addEvents({
      'mouseenter': function(e){
        if(!this.hasClass('selected')) this.morph({color: this.retrieve('over_color')})
      },
      'mouseleave': function(e){
        if(!this.hasClass('selected')) this.morph({color: this.retrieve('default_color')})
      }
    })
    
    el.store('default_color', el.getStyle('color'))
    if(el.hasClass('ob')){
      el.store('over_color', '#333333')
    }else if(el.hasClass('oo')){
      el.store('over_color', '#F6821F')
    }else if(el.hasClass('og')){
      el.store('over_color', '#BCCE00')
    }else{
      el.store('over_color', '#BCCE00')
    }
    
    el.set({'morph': {duration:400, transition:'quint:out'}}).addClass('noUnderline')
  })
  
  $$('.item a').each(function(el){
    el.getElement('.frame').set({'morph': { duration: 300, transition: 'expo:out' } })  
    if(n = el.getElement('.new'))n.set({'morph': { duration: 300, transition: 'expo:out' } })  
    if(p = el.getElement('.promo'))p.set({'morph': { duration: 300, transition: 'expo:out' } })  
    
    el.addEvents({
      'mouseenter': function(e){
        this.getElement('.frame').morph({top:-3, left:-3, padding:3})
        
        if(n = this.getElement('.new')) n.morph({top:-5, left:-5})
        if(p = this.getElement('.promo')) p.morph({top:-5, left:-5})
      },
      'mouseleave': function(e){
        this.getElement('.frame').morph({top:0, left:0, padding:0})
        
        if(n = this.getElement('.new'))n.morph({top:3, left:3})
        if(p = this.getElement('.promo'))p.morph({top:3, left:3})
      }
    })
  })
  
  if($('item')){
    $$('#imagesContainer .image').set({'morph':{ duration:1000, transition:'expo:out'}, 'styles': {opacity: 1} })
      
    image_z = 2
    $$('#thumbsContainer .thumb').addEvents({
      'mouseenter': function(e){
        if(this.hasClass('selected')) return 
        this.getElement('.frame').morph({top:-2, left:-2, padding:2})
      },
      'mouseleave': function(e){
        if(this.hasClass('selected')) return
        this.getElement('.frame').morph({top:0, left:0, padding:0})
      },
      'click': function(e){
        if(e)e.stop()
        if(this.hasClass('selected')) return
        this.getParent().getChildren('.selected').removeClass('selected').getElement('.frame').morph({top:0, left:0, padding:0})
        this.addClass('selected')
        
        $('image_' + this.get('id').replace('thumb_','')).set('styles', {display: 'block', opacity:0, 'z-index': image_z++ }).morph({opacity: 1})
        $('zoomContainer').empty().adopt(new Element('img', { src: this.get('href') }), new Element('img', {src: '/images/blank.gif', styles: {width:600, height:600, position:'absolute', top:0, left:0}}))
      }
    }).getElement('.frame').set({ 'morph': { duration: 300, transition: 'expo:out' } })
      
    
    
    /* ZOOM */
    $$('#imagesContainer .image').addEvents({
      'mouseenter': function(e){
        if(!this.hasClass('zoom'))return
        zoom = $('zoomContainer')
        zoom.set('styles', {'display': 'block', 'opacity': 0}).scrollTo(e.page.x - zoom.getPosition().x - zoom.getScroll().x, e.page.y - zoom.getPosition().y - zoom.getScroll().y).fade('in')
      }
    })
    $('zoomContainer').addEvents({
      'mouseleave': function(e){
        $('zoomContainer').fade('out')
      },
      'mousemove': function(e){
        
        x = (e.page.x - this.getPosition().x - this.getScroll().x) * 1.2 - 30
        y = (e.page.y - this.getPosition().y - this.getScroll().y) * 1.2 - 30
        x = x > 300 ? 300 : x; x = x < 0 ? 0 : x;
        y = y > 300 ? 300 : y; y = y < 0 ? 0 : y;
        this.scrollTo(x, y)
      }
      
    })
    
    $('thumbsContainer').getChildren()[0].fireEvent('click')
  }
  
  
  if($('sortForm')){
    $('sortSubmit').set('styles', {display: 'none'})
    $('sortSelect').addEvent('change', function(e){
      $('sortForm').submit();
    })
  }
  
  if($('collectionsTrigger')){
    //Cookie.dispose('collections')
    
    var collections = $('collectionsContainer')
    var collections_height = collections.getHeight()
    collections.set('morph', { duration:500, transition:'expo:out', onComplete:equalHeights})
    
    switch(Cookie.read('collections')){
      case 'closed':
        collections.set('styles', {height: 0})
        $('collectionsTrigger').addClass('down')
      break
      case 'opened':
        
      break
      default:
        $('collectionsTrigger').fireEvent.delay(2000, $('collectionsTrigger'), 'click')
      break
    }

    $('collectionsTrigger').addEvent('click', function(e){
      if(e)e.stop()
      if(collections.getHeight()){
        collections.morph({height: 0})
        this.addClass('down')
        Cookie.write('collections', 'closed', {path: '/'})
      }else{
        collections.morph({height: collections_height})
        this.removeClass('down')
        Cookie.write('collections', 'opened', {path: '/'})
      }
    })
  }
  
  
  window.addEvent('load', function () {
    if($('sidebar')){
      sidebar_height = $('sidebar').getHeight()
      equalHeights()
    }
  })
  
  function equalHeights(){
    //console.debug(sidebar_height)
    sb = $('sidebar')
    if($('content').getHeight() > sidebar_height-20) sb.set('styles', {height: $('content').getHeight()-20})
    else if(sb.getHeight() > sidebar_height) sb.set('styles', {height: sidebar_height})
  }
  
})




