$(function() {
  $('body').bind('cbox_load', function() {
    // add close text next to the close button
    $('#cboxClose').wrap('<div id="cboxCloseWrapper"></div>').before('<span>close</span>')
    // when the close text is clicked, pass it off to the close button to close the colorbox
    $('#cboxCloseWrapper span').bind('click', function() { $('#cboxClose').click() })
  })

  var lightbox_width = '700px'

  // on help pages, replace the email link with a colorbox form
  function colorboxize_mail_link() {
    $('#help #mail-link').colorbox({
      href: '/concierge.php #contact-form',
      width: lightbox_width,
      onComplete: function() {
        if($('#sku').length > 0) $('#interested-in-field')[0].value = $('.product-name').text() + ' (' + $('#sku').text() + ')'
        // when the form is submitted track it as a page view in google analytics
        $('#contact-form').bind('submit.ga', function() {
          var form = $(this)
          _gaq.push(['_trackPageview', '/conceirge-submit.php'])
          form.unbind('submit.ga') // we're going to submit again, and don't want to run this code again
          setTimeout(function() { form.submit() }, 200) // submit again after a delay so that the data can be sent to GA
          return false
        })
      }
    })
  }

  colorboxize_mail_link()

  // load special order help page in a colorbox
  $('.product-info.special-order').find('#special-order-button, .price.poa:not(.limited) a, #inventory-status-special-order').each(function(i, e) {
    e = $(e)
    e.colorbox({
      href: (e.is('a') ? e.attr('href') : e.closest('form').attr('action')) + ' #help', // we want the actual URL used because it contains a product id
      width: lightbox_width,
      onComplete: colorboxize_mail_link
    })
  })

  function create_help_colorbox(selector, href) {
    $(selector).colorbox({href: href, width: lightbox_width, onComplete: function() {
      _gaq.push(['_trackPageview', href])
      colorboxize_mail_link()
      $('#click-to-call-button').click(function() {
        _gaq.push(['_trackPageview', '/click-to-call.php'])
        window.open("http://talk.callonclick.com/saleseu/callme/callme-pop.php?cid=00009469&language=EN&us=44","","width=430, height=490,status=1")
      })
    }})
  }

  create_help_colorbox('#limited-more-link, #limited-button, .price.limited a', '/help-limited.php #help')
  create_help_colorbox('#delivery-options-more-link',                           '/help-delivery-options.php #help')
  create_help_colorbox('#gift-wrap-link',                                       '/help-gift-wrap.php #help')
  create_help_colorbox('#help-link',                                            $('#help-link').attr('href') + ' #help')
  create_help_colorbox('#contact-us',                                           '/help.php #help')
  create_help_colorbox('#inventory-status-more-link',                           $('#inventory-status-more-link').attr('href') + ' #help')
  create_help_colorbox('#inventory-status-more-link-image',                     $('#inventory-status-more-link-image').attr('href') + ' #help')
  create_help_colorbox('.estimated-usd a',                                      '/help-usd.php #help')
  create_help_colorbox('#gold-menu',                                            '/help-gold.php #help')
  create_help_colorbox('#gift-finder-menu',                                     '/help-gift-finder.php #help')
  create_help_colorbox('#footer-help input',                                    '/help.php #help')

  $('#header-search-text, #list-subscribe input[type="text"]').enablePlaceholder()
  var random_slide = Math.floor(Math.random() * $('#homepage-slidedeck dd').length) + 1
  var homepage_slidedeck = $('#homepage-slidedeck dl').slidedeck({index: false, hideSpines: true, cycle: true, start: random_slide, speed: 300, autoPlay: true})
  $('#homepage-slidedeck').append(
    $('<div class="nav-buttons">')
      .append($('<a href="#" class="previous-link">&lt; previous</a>').click(function() { homepage_slidedeck.prev(); return false }))
      .append($('<a href="#" class="next-link">next &gt;</a>').click(function() { homepage_slidedeck.next(); return false }))
  )

  $('#locations .map-link').colorbox()

  if(!Modernizr.input.required) {
    $('form.foxycart:has(select[name=option])').submit(function(event) {
      if($(this).find('select[name=option]').val() === "") {
        event.preventDefault()
        event.stopImmediatePropagation()
        alert('Please select a "size & materials" option.')
      }
    })
  }

  var replacement = $('<select></select>').change(function() { location.href = this.options[this.selectedIndex].value })
  $('#order-products-by')
    .find('a').each(function(i, a) {
      var a = $(a)
      replacement.append('<option value="' + a.attr('href') + '" ' + (a.is('.active') ? 'selected' : '') + '>' + a.html() + '</option>')
    }).end()
    .find('.spacer, a').remove().end()
    .find('.label').after(replacement)
});

