(function($) {
    $.fn.ellipsis = function(enableUpdating){
        var s = document.documentElement.style;
        if (!('textOverflow' in s || 'OTextOverflow' in s)) {
            return this.each(function(){
                var el = $(this);
                if(el.css("overflow") == "hidden"){
                    var originalText = el.html();
                    var w = el.width();
                    
                    var t = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'overflow': 'visible',
                        'max-width': 'inherit'
                    });
                    el.after(t);
                    
                    var text = originalText;
                    while(text.length > 0 && t.width() > el.width()){
                        text = text.substr(0, text.length - 1);
                        t.html(text + "...");
                    }
                    el.html(t.html());
                    
                    t.remove();
                    
                    if(enableUpdating == true){
                        var oldW = el.width();
                        setInterval(function(){
                            if(el.width() != oldW){
                                oldW = el.width();
                                el.html(originalText);
                                el.ellipsis();
                            }
                        }, 200);
                    }
                }
            });
        } else return this;
    };
})(jQuery);

$(document).ready(function() 
{
  var culture = $('html').attr("lang");

  // open links in iframe page
/*  $('a[target$=_blank]').click(function()
  {
    var link = $(this).attr("href");
    if (! $(this).hasClass('external'))
    {
      window.open("http://" + document.location.hostname + "/" + culture + "/redirect/?link=" + escape(link));
      return false;
    }
  }); */
  
    if ($.browser.msie) {
        $('div.shop_cart input[type=text]').keydown(function(e){
          if (e.keyCode == 13) {
            $(':submit').click();
            
            return false;
          }
        });
    }
    
    var aanbodtimer;
    $("#staffelkorting").hover(function(){
    	$('.shop_list').css('visibility', 'hidden');
        $('#center .tier_fade').css('visibility', 'visible');
        $("#center div.tier").stop(true, true).animate({opacity: 1}, 1000);
            clearTimeout(aanbodtimer);
    }, function(){
        aanbodtimer = setTimeout(function() {
        	$('.shop_list').css('visibility', 'visible');
        	$('#center .tier_fade').css('visibility', 'hidden');
            $("#center div.tier").stop(true, true).animate({opacity: 0}, 1500);
        }, 1000);
    });
  
  if (typeof coupon_msg !== 'undefined') {
      $('span.coupon input').focus(function(){
          if ($(this).attr('value')==coupon_msg) {
              $(this).attr('value', '');
              $(this).css('color', '#666');
          }
      });
      $('#shoppingcart span.coupon input').blur(function(){
          if ($(this).attr('value')=='') {
              $(this).attr('value', coupon_msg);
              $(this).css('color', '#aaa');
          }
      });
  }

  $('span.coupon input').blur();

  $("#shoppingcart .update").css('display', 'none');
  
  $("#other_adress").click(function(){
      if ($(this).is(':checked')) {
          $("#shipping_details").css('display', 'block');
      } else {
          $("#shipping_details").css('display', 'none');
          $('#shoppingcart_Delivery_address').val('');
          $('#shoppingcart_Delivery_zipcode').val('');
          $('#shoppingcart_Delivery_city').val('');
      }
  });
  
  $('a#show-contact-form').click(function(){
      $('form#contact-form').slideToggle('slow');
  });
  
  $('#sm_contact_interest_other').click(function(){
      $('#sm_contact_interest-other').attr('disabled', ! $(this).is(':checked'));
  });
  
  $("#select-culture").customSelect();
  
  $("#foot img[id^='brand-']").hover(function(){
      var g = $(this);
      var g_src = g.attr('src');
      
      var c = $('#hover-' + g.attr('id'));
      var c_src = c.attr('src');
      
      g.attr('src', c_src);
      c.attr('src', g_src);
  }, function(){
      var g = $(this);
      var g_src = g.attr('src');
      
      var c = $('#hover-' + g.attr('id'));
      var c_src = c.attr('src');
      
      g.attr('src', c_src);
      c.attr('src', g_src);
  });
  
  $('.faq-vraag').click(function(){
      $(this).next('.faq-antwoord').toggle(250);
      $(this).toggleClass('active');
  });
  $('.uitvouw-knop').click(function(){
      var found = false;
      var e = $(this).next('.uitvouw-blok');
      if ( ! e.attr('class')) {
          e = $(this).parent().next('.uitvouw-blok');
      }
      if (e.attr('class')) {
          $(this).toggleClass('active');
          e.toggle(250);
      }
  });
  
  if (category = $('input#category-id')) {
	  $('ul.categories > li.children').click(function(){
         var e = $(this);
         
         if ( ! e.find('ul').is(':visible')) {
             $.each(e.parent().find('li ul:visible'), function(i, v){
                 $(v).hide('fast');
             });
    
             e.find('ul').toggle('fast');
             
             return false;
         }
      });
  }
  
  $('.menu-productgroup').click(function(){
      $('ul.categories:visible').not($(this).next('ul.categories')).hide('fast');
      
      $(this).next('ul').toggle('fast');
  });
  
  $('ul.catalogs a').click(function(){
      var file = $(this).attr('href');
      
      if (file.match('\.pdf$')) {
          window.open("http://" + document.location.hostname + "/" + culture + "/pdf/?file=" + escape(file),
                  "pdfviewer","menubar=1,resizable=1,width=1000,height=650");
      }

      return false;
  });
  
  $('#left .news-item h2 a, #right .news-item h2 a').ellipsis();
  
  $('.fancy').click(function(){
	  var e = $(this);
	  var view = e.find('span').html();
	  
	  $('img#product-view-image').attr('src', view);
	  $('#product-fancy-image').attr('href', e.attr('href'));
	  $('.fancy').attr('rel', 'fancy_group');
	  e.removeAttr('rel');
	  return false;
  });
  
  $('#sample-submit').click(function(e){
	  var $step2 = $('#sample-step-two');
	  if ( ! $step2.is(':visible')) {
		  e.preventDefault();
		  // validate
		  var $nm = $('tr.sample-name');
		  var $inm = $('#sample_name, #quote_name');
          var $em = $('tr.sample-email');
          
          var valid = true;
          if ($inm.val()) {
        	  $nm.addClass('hidden');
          } else {
        	  $nm.removeClass('hidden');
        	  valid = false;
          }
          if (/.+\@.+\...+/.test($('#sample_email, #quote_email').val())) {
        	  $em.addClass('hidden');
          } else {
        	  $em.removeClass('hidden');
        	  valid = false;
          }
          if (valid) {
        	  // post
        	  $.post(document.location.href, {
        		  initials:	$('#sample_initials, #quote_initials').val(),
        		  name: 	$('#sample_name, #quote_name').val(),
        		  email: 	$('#sample_email, #quote_email').val()
        	  }, function(data){
        		  $('#sample-id').val(data);
        	  });
        	  // shop step 2
        	  $step2.slideDown(200);
        	  
        	  // trackevent
        	  if ($('#sample_email').length) {
        		  _gaq.push(['_trackEvent', 'test', 'stap2', $('#product-name').html()]);
        	  } else {
        		  _gaq.push(['_trackEvent', 'offerte', 'stap2', $('#product-name').html()]);
        	  }
          }
	  }
  });
  
});

