
function moduleSearch() {
  url = 'index.php?route=product/search';

  var filter_keyword = $('#filter_keyword').attr('value')

  if (filter_keyword) {
    url += '&keyword=' + encodeURIComponent(filter_keyword);
  }

  var filter_category_id = $('#filter_category_id').attr('value');

  if (filter_category_id) {
    url += '&category_id=' + filter_category_id;
  }

  location = url;
}
$('.switcher').bind('click', function() {
  $(this).find('.option').slideToggle('fast');
});
$('.switcher').bind('mouseleave', function() {
  $(this).find('.option').slideUp('fast');
});


function getURLVar(urlVarName) {
  var urlHalves = String(document.location).toLowerCase().split('?');
  var urlVarValue = '';

  if (urlHalves[1]) {
    var urlVars = urlHalves[1].split('&');

    for (var i = 0; i <= (urlVars.length); i++) {
      if (urlVars[i]) {
        var urlVarPair = urlVars[i].split('=');

        if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
          urlVarValue = urlVarPair[1];
        }
      }
    }
  }

  return urlVarValue;
}

function getUrlParam(name) {
  var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return "";
  else
    return results[1];
}

$(document).ready(function () {

$('#search .inside input').keydown(function(e) {
  if (e.keyCode == 13) {
    moduleSearch();
  }
});


  $('.faq-top a').attr('onclick','scrollToTop();return false');
  $('#add_to_compare').removeAttr('onclick');
  $('#add_to_compare').click(function () {
  $('#module_compare').show('bounce','',500);
    $.ajax({
      type: 'post',
      url: 'index.php?route=module/compare/callback',
      data: $('#product :input'),
      dataType: 'html',
      success: function (html) {
        $('#module_compare .middle').html(html);

      },
      complete: function () {
        var image = $('#image').offset();
        var compare  = $('#module_compare').offset();

        $('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

        params = {
          top : compare.top + 'px',
          left : compare.left + 'px',
          opacity : 0.0,
          width : $('#module_compare').width(),
          height : $('#module_compare').height()
        };

        $('#temp').animate(params, 'slow', false, function () {
          $('#temp').remove();
        });
      }
    });
  });

 $('.compare_remove').live('click', function () {
    if (!confirm('Bevestigen?')) {
      return false;
    }
    $(this).removeClass('compare_remove').addClass('cart_remove_loading');
    $.ajax({
      type: 'post',
      url: 'index.php?route=module/compare/callback',
      dataType: 'html',
      data: 'remove=' + this.id,
      success: function (html) {
        $('#module_compare .middle').html(html);
        if (getUrlParam('route').indexOf('checkout') != -1) {
          window.location.reload();
        }
      }
    });
  });

  $('#newsletter-form').submit(function() {
    $.ajax({
      type: 'POST',
      url: 'index.php?route=account/newsletter/subscribe',
      data: 'name='+$('input[name="name"]').val()+'&email='+$('input[name="email"]').val(),
      success: function(data) {
        if (data =='success') {
          $("#newsletter-form").html('');
          $('#response').addClass('success').removeClass('error');
          $('#response').html('U heeft zich ingeschreven voor onze nieuwsbrief.');
        }
        else {
          $('#response').html(data);
      }
    }
  });
  return false;
  });

});

  function popup(link) {
        if ($('#dialog').length==0) {
          $(link).parent().append('<div id="dialog" style="display:none;"></div>');
        }
        $('#dialog').dialog(
          {
            hide: 'slide',
            show: 'slide',
            modal: 'true',
            width: '600',
            title: $(link).text(),
            open: function(event, ui) { $('.ui-widget-overlay').bind('click', function(){$("#dialog").dialog('close'); }); },
            maxWidth:'800'
          });
        $.ajax({
        url: $(link).attr('href'),
        data: 'ajax=true',
        success: function(data) {
                $('#dialog').html(data);
                $('#dialog').dialog('open');

          }
        });
    return false;
  }

function scrollToTop () { // run this code on page load (AKA DOM load)
  /* set variables locally for increased performance */
    $('html, body').animate({scrollTop:0}, 'slow');
}
function add_to_compare(product_id) {
  $('#module_compare').show('bounce','',500);
    $.ajax({
      type: 'get',
      url: 'index.php?route=module/compare/callback',
      data: 'product_id='+product_id,
      dataType: 'html',
      success: function (html) {
        $('#module_compare .middle').html(html);

      },
      complete: function () {
        var image = $('#image').offset();
        var compare  = $('#module_compare').offset();

        //$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

        params = {
          top : compare.top + 'px',
          left : compare.left + 'px',
          opacity : 0.0,
          width : $('#module_compare').width(),
          height : $('#module_compare').height()
        };

        $('#temp').animate(params, 'slow', false, function () {
          $('#temp').remove();
        });
      }
    });
  }

