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


  var myTimer = {}

  // TOP MENU
  $("#menu span a").each(function(){
    var link = $(this)
    var m = $("#menu_"+link.attr('class'))

    // Move into place
    if (m.hasClass('artists')) {
      m.css('left', link.position().left-55)
    }

    // HOVER
    link.hover(
      function(){
        $.clearTimer(myTimer)
        if ($("#menu_"+link.attr('class')+":visible").length < 1) {
          m.stop()
          
          var fade = ($("#menu ul.l1:visible").length < 1) ? true : false          
          $("#menu ul.l1").hide()          

          fade ?  m.fadeIn('fast') : m.show()          
        }
      }, 
      function(){
        myTimer = $.timer(1600,function(){
          m.fadeOut('fast')  
        })        
      }
    )      
  })
  
  // SUB MENU
  $("#menu ul.l1").each(function(){
    var m = $(this)

    m.hover(
      function(){
        $.clearTimer(myTimer)
      }, 
      function(){
        myTimer = $.timer(1600,function(){
          m.fadeOut('fast')
        })        
      }
    )
  })
  
  
  $("p.more a").click(function(e){
    var link = $(this)

    if (link.parents('.details').siblings('.read-more').length == 0) {
      link.parents('.details').after('<div class="read-more" id="'+link.attr('class')+'"></div>')      
      $.ajax({
        type: "GET",
        url: link.attr('href'),
        success: function(html){
          link.parents('.details').siblings('.read-more').html(html)
        }
      })      
    } 
    
    ui.go_to_link(link)
    
    e.preventDefault()      
  })

  $("p.show-all a").click(function(e){
    var link = $(this)
        
    $.ajax({
      type: "GET",
      url: link.attr('href'),
      success: function(html){
        link.parents('.read-more:first').append(html)
        link.parent().remove()
      }
    })      
    
    e.preventDefault()      
  })
  
  
  // MENU LINKS
  $("#menu li a:not(.ticket), .event a").click(function(e){
    //$(".l0 li.current").removeClass('current')
    //link.parent().addClass('current')

    var m = $(this).parents('ul:first')
    if (m.hasClass('artists')) {
      $.clearTimer(myTimer)
      m.fadeOut('fast')      
    }
    
    ui.go_to_link($(this))
    // Find position    

    e.preventDefault()      
  })
  
  // HOME
  $('#home').click(function(e){
    ui.scroll_to($("#pages"), 600)
    window.location.hash = ''
    pageTracker._trackPageview($(this).attr('href'))
    e.preventDefault()
  })    


  // CATEGORIES
  $(".categories a").click(function(e){
    $(".categories a.current").removeClass('current')
    $(this).addClass('current')
    
    var category = $(this).attr('id')
    if (category == 'all') {
      $(".events div.event").fadeIn('fast')
    } else {      
      window.location.hash = $(this).attr('href')
      $(".events div.event:not(."+category+")").hide()
      $(".events div.event."+category).fadeIn('fast')
    }
    e.preventDefault()
  })
  
  
  $("a.newsletter").click(function(e){
    
    $.ajax({
      type: "GET",
      url: $(this).attr('href'),
      success: function(html){
        $('div.newsletter').remove()
        $('#menu').append(html)  			  
        ui.bind_newsletter_close()
        ui.bind_newsletter_form()
      }
    })

    e.preventDefault()          
  })

  
  ui.bind_newsletter_close()
  ui.bind_newsletter_form()
  
  
  // Auto scroll to direct inks  
  if (window.location.hash != '') {
    var url = window.location.hash.substr(1,window.location.hash.length)
        
    if (url.search(/^\/nyhedsbrev/) != -1) {
      $("a.newsletter").trigger('click')
    } else {
      $.ajax({
        type: "GET",
        url: '/id'+url,
        success: function(html){
          var column = $("#"+html)
          ui.scroll_to(column, 0)
        }
      })
    }
  }
  
  // Auto open newsletter
  $("a.newsletter").trigger('click')
  
})



ui = {
  
  
  go_to_link: function(link) {

    window.location.hash = link.attr('href')

    var column = $("#"+link.attr('class'))
    ui.scroll_to(column, 600)
    
    pageTracker._trackPageview(link.attr('href'))
  },
  
  bind_newsletter_form: function() {
    
    $("div.newsletter .options input:radio").click(function(e){
      $(this).parent().find("label.selected").removeClass('selected')
      $(this).next().addClass('selected')    
    })
    
    $("div.newsletter form").submit(function(e){
      
      $('div.newsletter input.ctrl').val('Sender…')
      
      $.ajax({
        type: "POST",
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success: function(html){
          $('div.newsletter').remove()
          $('#menu').append(html)
          ui.bind_newsletter_close()
          ui.bind_newsletter_form()
        }
      })
      e.preventDefault()
    })
  },
  
  // Close newsletter
  bind_newsletter_close: function() {
    $('a.close-newsletter').click(function(e){
      $('div.newsletter').remove()
      e.preventDefault()
    })    
  },
  
  scroll_to: function(obj, duration) {
    $.scrollTo(obj, {duration: duration, offset: {left: -201, top: -256}})
  }
  
}