$(function(){
  $('.flash-message').live('click', function(){ $(this).fadeOut() })
  $('form.normal .radios input:checked').parent().addClass('selected')
  $('form.normal .radios input').change(function(){
    $('input[name='+$(this).attr('name')+']').parent().removeClass('selected')
    $(this).parent().addClass('selected')
  })

  $('#homepage-big-banners > ul a').click(function(){
    var url = $($(this).attr('href')).find('a').attr('href')
    if(url)window.location = url

    return false;
  })

  $('#homepage-big-banners a.big-banner-swf').each(function(){
    swfobject.embedSWF( $(this).attr('href'), $(this).attr('id'), "100%", "240", "9.0.0","/expressInstall.swf", {}, {salign: 'TL', wmode:'transparent'}, {})
  })


  $('#homepage-big-banners').tabs({
    event: 'mouseenter'
  }).tabs('rotate', 5000)

  $('#categories-tree')
    .accordion({active:'div.category-selected', collapsible:true, header: '.header', autoHeight: false})
    .find('.header').hover(
      function(){$(this).find('span.image').stop().animate({ opacity: 1 },'fast' ) },
      function(){$(this).find('span.image').animate({ opacity: 0.7 },'fast' ) }
    )

  $('#sort_brand, #sort_sport, #sort_category').change(function(){this.form.submit()})

  if($('#product-images .small-thumbs').size())
    $('#product-images').tabs({fx: { opacity: 'toggle' }});

  $('a.gallery-image,a.store-feedback,a.product-feedback').colorbox({ scrolling: false, transition:"fade"})


  $('#product-images .thumb').hover(
    function(){
      $('#big-image').clearQueue().show().css('background-image','url('+escape($(this).find('a').attr('href'))+')')
      var zoomer = $(this).find('.zoomer').clearQueue().css('opacity',0).css('display','').animate({opacity:0.2},'fast')
      $(this).mousemove(function(e){
        zoomer.css('left', Math.max(Math.min(e.pageX-$(this).offset().left-62,123),0))
        zoomer.css('top', Math.max(Math.min(e.pageY-$(this).offset().top-90,180),0))
        $('#big-image').css('background-position', -zoomer.position().left + 'px '+ -zoomer.position().top*2 + 'px')
      })
    },
    function(){
      $(this).unbind('mousemove')
      $('#big-image').hide()
      $(this).find('.zoomer').hide()
    }
  ).click(function(){
    $.fn.colorbox({href: $(this).find('a').attr('href')})
    return false
  })

  var add_product = function(){
    var self = $(this)
    self.addClass('progress')
    $.post($(this).attr('action'), self.serialize(), function(data){
      $('#cart').html(data).find('.small-products').effect('highlight')
      self.removeClass('progress')
    })
    return false
  }

  $('#product-page').bind('submit',add_product)
  $('#cart .delete-item').live('submit',add_product)

  $('#cart-list-update').remove()
  $('#cart-list input,#cart-list select').live('change',function(){
    var form = $(this).closest('form')
    $.post(form.attr('action'),form.serialize(), function(data){ $('#cart-items-content').html(data).effect('highlight') })
  })

  $('#cart-list .delete-button').live('click', function(){
    var form = $(this).closest('form')
    $(this).closest('tr').remove()
    $.post(form.attr('action'),form.serialize(), function(data){ $('#cart-items-content').html(data).effect('highlight') })
    return false
  })

  $('#cities-list a, #cities-map a').click(function(){ $.scrollTo($(this).attr('href'), 'normal'); return false; })
  $('#brands-list .links-list a').click(function(){ $.scrollTo($(this).attr('href').match(/(#.*)$/)[1], 'normal'); return false; })

  var toggle_tr = function(tr){
    var visible = tr.find('.excerpt:visible,.full-text:visible')
    var hidden = tr.find('.excerpt:hidden,.full-text:hidden')
    visible.fadeOut('fast', function(){ hidden.fadeIn('normal') })
  }

  $('.brands-list a.toggle-link').click(function(){
    $('.brands-list .description .full-text:visible').each(function(){toggle_tr($(this).closest('tr'))})
    toggle_tr($(this).closest('tr'))
    return false
  })

  $('#feedback-form-content,#feedback-product-content,#feedback-store-content').live('submit',function(){
    var self = $(this).closest('.container.iframe,.feedback-sidebar')
    self.addClass('progress')
    $.post($(this).attr('action'), $(this).serialize(), function(data){
      self.replaceWith(data).removeClass('progress')
      $.fn.colorbox.resize()
    })
    return false
  })

  $('.has-dropdown').hover(function(){
    $(this).addClass('hover');
    $(document.body).addClass('ie_zindexfix')
  },function(){
    $(this).removeClass('hover');
    $(document.body).removeClass('ie_zindexfix')
  })
})
