$(document).ready( function () {
  // Remove the non-js zipcheck input form.
  $('#zipcheck_input').hide()

  // Only of the url points to www.xs4all.nl (and not shop.xs4all.nl) install zipcheck overlay
  $('.bestel a').each( function () {
    if(this.href.match(/postcodecheck.php/))
      install_zipcheck_overlay(this)
  })

  $('#new_zipcheck').each( function () {
      install_zipcheck_overlay(this)
  })

  // If this page has unavaiable abo's, show the 'not available' element
  $('table.index_abo td.unavailable').each( function () {
    $(this).find('.bestel').hide()
    $(this).find('.not_bestel').show()
  })

  $('#mini_zipcheck form').submit( function () {
    if(window.s)
    {
      if(window.tracking_vars)
      {
        if(window.tracking_vars.eVar3)
          window.s.eVar3 = window.tracking_vars.eVar3
        if(window.tracking_vars.products)
          window.s.products = window.tracking_vars.products
      }

      s.events = 'event20,event23'
      window.s.t()
    }

    $('#zipcheck_submit').fadeOut(500, function () {
      $('#zipcheck_loading').fadeIn(500)
    })

  })
})

function install_zipcheck_overlay(element)
{
  // If the abo form and zipcheck overlay are missing, do nothing
  if(!($('#form_abo')[0] && $('#zipcheck_overlay')[0]))
    return false

  // make link click go to overlay
  $(element).click( function (e) {
    // If the page contains zipcheckoutput, go forth!
    if(element.id != 'new_zipcheck' && $('#zipcheck_output').length)
      return true

    // set abo type in form and headtext
    var abo_type = $(this).attr('rel').toLowerCase()
    if($(this).attr('rel') && $('#form_abo')[0])
      $('#form_abo').val(abo_type)
  
    if(abo_type)
    {
      $('#zipcheck_headtext').html(
        "Controleer hier of <b>ADSL " + abo_type.charAt(0).toUpperCase()  + abo_type.substr(1) + "</b> op uw adres beschikbaar is."
      )
    }

    // set style and size of overlay and zipcheck block
    $('#zipcheck_overlay').css({
      'width'  : $(document).width(),
      'height' : $(document).height()
    }).show().click( function () { hide_zipcheck_overlay() })



    $('#mini_zipcheck').css({
      'left' : Math.floor(
        ($(document).width() - $('#mini_zipcheck').width()) / 2
      ) + 'px',
      'top'  : Math.floor(
        (($(window).height() - $('#mini_zipcheck').height()) / 2) + $(window).scrollTop()
      ) + 'px'
    }).fadeIn()

	  $('#js_zipcode').focus()

    $('#mini_zipcheck .close').click( function () { hide_zipcheck_overlay() })
    e.stopPropagation()
    return false
  })
}

function hide_zipcheck_overlay()
{
  $('#mini_zipcheck').fadeOut('slow')
  $('#zipcheck_overlay').fadeOut('slow')
}

