/* A TARGET */
(function($) {
  $.fn.target = function() {
    return this.each(function(index, elm) {
      if ($(this).attr("class").substring(0, 7) == 'target-') {
        $(this).attr("target", '_' + substr($(this).attr("class"), 7));
      }
    });
  }
})(jQuery);

$(document).ready(function() {
  $("a").target();

  $("body").noContext();
  
  /* SEARCH */
  $("div.search_container div.button").bind("click", function() {  	
  	searchSite();
  });
  
  $("div.search_container div.input input").bind("keyup", function(e) {
  	if (e.keyCode == 13) {
  		searchSite();
  	}
  });
  
  function searchSite() {
  	if ($("div.search_container div.input input").val().length > 0) {
  		window.location.href = '/search/' + $("div.search_container div.input input").val() + '/';
  	}
  }
  
  /* MENU */
  $("div.top div.right").find("td").each(function() {
    $(this).bind("mouseover mouseout click", function(evt) {
      if (evt.type == 'mouseover') {
        if (!$(this).hasClass($(this).attr("id") + "_over")) {
          $(this).addClass($(this).attr("id") + "_over");
        }
      } else if (evt.type == 'mouseout') {
        if (!$(this).hasClass($(this).attr("id") + "_selected")) {
          $(this).addClass($(this).attr("id"));
          $(this).removeClass($(this).attr("id") + "_over");
        }
      } else if (evt.type == 'click') {
        window.location.href = $(this).find("a").attr("href");
      }
    });
  });

  /* Winkelwagen */
  $(".add_to_cart").bind("click", function() {
    var artikel_nr_tmp = substr($(this).attr("id"), strpos($(this).attr("id") , "_") + 1);

    if (artikel_nr_tmp) {
      var artikel_nr = new Array();
      var wci_amount = new Array();

      artikel_nr.push(artikel_nr_tmp);
      wci_amount.push(1);

      var list = {
        insert : true,
        artikel_nr: artikel_nr,
        wci_amount: wci_amount
      }

      insertIntoCart(list);
    }
  });

  function insertIntoCart(values) {
    $.POST('/webshop/cart/', values);
  }

  /* Inloggen */
  $("div.login div.form div.button").bind("click", function() {
    $(this).parents("form").eq(0).trigger("submit");
  });

  /* Printen */
  $("div#print").children("a").eq(0).bind("click", function() {
    window.print();
  });
});

