//jQuery(document).ready(function() {
//jQuery(function() {
$(function() {  

  // HIDE ABOUT IF WINDOW TO SMALL
  if ($(this).width() <= 1024) {
    $('#sidebar').hide()
  }
  
  $(window).resize(function(){
    $(this).width() <= 1024 ? $('#sidebar').hide() : $('#sidebar').show()
  });
  
  // SEARCH
  $('#top-sf-submitter').each(function(){
    $(this).click(function(e){
      $('#top-sf').submit()
      e.preventDefault()   
    })
  }),
  
  // Toggle next p
  $("a.toggle_next").each(function(){
    var link = $(this)
    link.click(function(e){
      link.parent().next('p').toggle('fast')
      e.preventDefault()   
    })
  }),

  // ABOUT
  $('#sidebar a').each(function(){
    var link = $(this)
    
    // Background
    link.children('img').each(function(){
      $(this).css('display', 'none')
      link.css('background-image', 'url('+$(this).attr('src')+')')
    })

    // Links    
    link.click(function(e){
      pageTracker._trackPageview(link.attr('href'))
      $.ajax({
        url: link.attr('href'),
        type: 'GET',
        beforeSend: function() {
          $("#about:empty").html("<span class='status'>Henter…</span>")
        },
        success: function(html){
          $("#about").html(html)
          ui.bind_close_about_overlay()
        }
      });      
      e.preventDefault()      
    })
  })

  ui.bind_artist_details_links()

  // OVERLAY
  ui.bind_close_about_overlay()
  ui.bind_enlarge_links()


  // NEWS UPCOMING
  if ($('#news li').length > 1)
    $('#news li:first').addClass('active')

  $('#news li a').each(function() {
    $(this).click(function(e){      
      $('#news li.active').removeClass('active')
      $(this).parent().addClass('active')

      $('#news div.headlines').hide()
      $("#news div.headlines").eq($("#news li a").index(this)).show()

      e.preventDefault()
    }) 
  })   

  // NEWS IMAGES
  $('#news #stories .story img').each(function(){    

    var news_img = $(this)    
    
    $(this).prev('h2').children('a').hover(function(){    

      $('#news #stories p').html(news_img.clone().show())
    }, function(){
      //alert("out")
    })
    
  })
  
  // WORKS BY CATEGORY
  $('.works a.work-link').each(function(){
    var link = $(this)
    // Links    
    link.click(function(e) {
      if (link.next().hasClass('details')) {
        link.next().toggle('fast')
      } else {
        pageTracker._trackPageview(link.attr('href'))
        $.ajax({
          url: link.attr('href'),
          type: 'GET',
          success: function(html){
            if (!link.next().hasClass('details')) {
              link.after("<div class='details' style='display: none'><div>")
              link.next().html(html)
              link.next().show('fast')
              
              ui.bind_artist_details_links()
            }         
          }
        });
      }
      e.preventDefault()      
    })    

    /*link.mouseout(function(e){
      $(this).removeClass('over')
    })

    link.mouseover(function(e){
      $(this).addClass('over')
    })*/
    
  })
  
  // WORKS IN LIST
  $('.work-results a.work-link').each(function(){
    var link = $(this)
    
    link.click(function(e){      
      if (link.parents('div:first').next('.details').length == 0) {
        pageTracker._trackPageview(link.attr('href'))
        $.ajax({
          url: link.attr('href'),
          type: 'GET',
          success: function(html) {
            if (!link.parents('div:first').next().hasClass('details')) {
              link.parents('div:first').after("<div class='details' style='display: none'><div>")
              link.parents('div:first').next().html(html)
              link.parents('table:first').find('td:last').hide()
              link.parents('div:first').next().show('fast')
              
              ui.bind_artist_details_links()
            }
          }
        })
      } else {        
        link.parents('div:first').next('.details:visible').hide('fast')
        link.parents('div:first').next('.details:hidden').show('fast')
        link.parents('table:first').find('td:last').toggle(0)
      }
      e.preventDefault()      
    })
  })
  
  $('#order-select').change(function(e){
    $('#hidden_order').val($(this).val())    
    $('#search-works').submit()
  })
  
  
  // CATEGORY SELECTOR
  $('#category-select').each(function(){
    
    $(this).change(function(e){
      var category = $(this).val();
      $('#search-works').each(function(){
        
        var action = ((/\/en\//.test(this.action)) ? ['', 'en', 'collections'] : ['', 'samlingerne'])
        
        var q = $(this).children(':text').val()
        var o = $('#hidden_order').val()
        
        if (category != '') action.push(category)
        if (q != '') 
          action.push('soeg?order='+o+'&q='+q)
        else
          action.push('?order='+o)
        

        this.action = action.join('/')
        
        pageTracker._trackPageview(this.action)
        document.location = this.action
      })
    })
  })
  
  
  
  $('span.toc-more a').click(function(e){
    $(this).html($(this).html()=='+' ? '-' : '+')
    $(this).parent().next().toggle()
    e.preventDefault()  
  })
    
})


ui = {
  
  // Close about overlay
  bind_close_about_overlay: function() {
    $('#about a.closer').click(function(e){
      $('#about').empty()
      e.preventDefault()
    })    
  },
  
  
  bind_large_images: function() {
    $('table.work td.image a').click(function(e){      
      $(this).parents('.details').hide('fast')
      $(this).parents('.details').prev('div').find('td:last').show(0)
      e.preventDefault()
    })
  },
  
  bind_enlarge_links: function() {
    $('a.enlarge').click(function(e){
      
      var parts = new String(this).split('/')
      var win_name = parts[parts.length-2]
      
      pageTracker._trackPageview($(this).attr('href'))      
      
      window.open(this, win_name, 'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width=732,height=800');
      e.preventDefault();      
    })
  },
  
  bind_artist_details_links: function() {
    $('span.more a').each(function(){
      var link = $(this)
      
      link.unbind('click')
      
      link.click(function(e){
        
        var details = link.parents('span.more:first').nextAll('.artist-details:first')

        if (details.length == 1) {        
          if (link.html() == '+') {
            details.show('fast')
            link.html('-')
          } else {
            details.hide('fast')
            link.html('+')          
          }
        }
        e.preventDefault()
      })
    })    
   
  }
    
}



