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

  var doc = $(this)
    
  // Open overlay
  $('#overlays li').click(function(e){         
    var link = $(this).children('a:first')
    $.ajax({
      url: link.attr('href'),
      type: 'GET',
      success: function(html){
        try { $.epiclock('destroy') } catch(e) { null }
        
        $('#overlay').remove()
        $("#layout").append(html)
        
        $('#overlay').css('top', $(document).scrollTop())
      
        $('div.swf').each(function(){
          $(this).hide()
        })
        
        ui.bind_item_td_links()
        ui.bind_coin_td_links()        
        ui.bind_item_links()
        ui.bind_close_overlay()
        
        pageTracker._trackPageview(link.attr('href'))
      }
    })
    e.preventDefault()
  })
  
  $('a.form-submitter').click(function(e){    
    $(this).parent().submit()  
    e.preventDefault()
  })


  // Open item
  ui.bind_item_links()
  ui.bind_item_td_links()
  ui.bind_coin_td_links()  
  
  
  // Close overlay
  ui.bind_close_overlay()
  ui.bind_close_item()
  
  ui.bind_zoom_item()
    
})

var ui = {
  
  bind_close_overlay: function() {
    $('#overlay a.close-overlay').click(function(e){
      $('#overlay').remove()
      
      $('div.swf').each(function(){
        $(this).show()
      })
      
      e.preventDefault()
    })    
  },

  bind_close_item: function() {
    $('#overlay a.close-item').click(function(e){
      var link = $(this)
      $.ajax({
        url: link.attr('href'),
        type: 'GET',
        success: function(html){
          var top = $('#overlay').position().top
          $('#overlay').remove()
          $("#layout").append(html)          
          $('#overlay').css('top', top)
          
          ui.bind_item_td_links()
          ui.bind_coin_td_links()          
          ui.bind_item_links()
          ui.bind_close_overlay()
          
          pageTracker._trackPageview(link.attr('href'))
        }
      })      
      e.preventDefault()
    })    
  },
  
  bind_coin_td_links: function() {
    $("table.coins td a").click(function(e){
      var link = $(this)
      $.ajax({
        url: link.attr('href'),
        type: 'GET',
        success: function(html){
          var top = $('#overlay').position().top
          $('#overlay').remove()
          $("#layout").append(html)          
          $('#overlay').css('top', top)

          ui.bind_item_links()
          ui.bind_close_item()
          ui.bind_zoom_item()
          
          pageTracker._trackPageview(link.attr('href'))
        }
      })              
      e.preventDefault();
    })
  },
  
  bind_item_td_links: function() {
    $("table.items td").each(function(){
      var td = $(this)

      var link = td.children("a:first")
      var info_div = td.children("div:first")
      if (link && info_div) {        
        info_div.click(function(e){
          $.ajax({
            url: link.attr('href'),
            type: 'GET',
            success: function(html){
              var top = $('#overlay').position().top
              $('#overlay').remove()
              $("#layout").append(html)              
              $('#overlay').css('top', top)

              ui.bind_item_links()
              ui.bind_close_item()
              ui.bind_zoom_item()
              
              pageTracker._trackPageview(link.attr('href'))
            }
          })        
        })
      }

      td.hover(
        function () {
          $(this).children('div:first').show()
        }, 
        function () {
          $(this).children('div:first').hide()
        }
      )
    })
  },
  
  bind_item_links: function() {
    $('a.item').click(function(e){         
      var link = $(this)
      $.ajax({
        url: link.attr('href'),
        type: 'GET',
        success: function(html){
          var top = $('#overlay').position().top
          $('#overlay').remove()
          $("#layout").append(html)          
          $('#overlay').css('top', top)
          
          ui.bind_item_links()
          ui.bind_item_td_links()
          ui.bind_coin_td_links()
          ui.bind_close_overlay()
          ui.bind_close_item()   
          ui.bind_zoom_item()       
          
          pageTracker._trackPageview(link.attr('href'))
        }
      })
      e.preventDefault()
    })  
  },
  
  bind_zoom_item: function() {
    $('#item-image').hover(
      function () {
        $(this).children('a:last').children('img').show()
      }, 
      function () {
        $(this).children('a:last').children('img').hide()
      }
    )    
  }
  
}