$(document).ready(function() {

  $(".togglecats").click(function(e) {
  
    e.preventDefault();
    
    $(".toggle").toggle();
  
  });

  $(".button").button();
  
  $(".search_button").button({icons:{primary:"ui-icon-search"}});
  
  $(".cart_button").button({icons:{primary:"ui-icon-cart"}});
  
	$("a.product_picture").fancybox({"hideOnContentClick":true});
  
  function product_fill_data(id) {
  
    $("#copydetails").fadeIn("fast");
    
    $.ajax({
      url:"/seller_ajax_details.php",
      data:"id="+id,
      dataType:"json",
      success:function(data) {
        
        $("form")[0].reset();
        
        $("textarea[name=author]").val(data.author);
        $("textarea[name=publisher]").val(data.publisher);
        $("textarea[name=publishyear]").val(data.publishyear);
        $("textarea[name=name]").val(data.name);
        $("textarea[name=name_en]").val(data.name_en);
        $("textarea[name=extrainfo]").val(data.extrainfo);
        $("textarea[name=extrainfo_en]").val(data.extrainfo_en);
        $("input[name=isbn]").val(data.isbn);
        $("input[name=exlibrisnimi]").val(data.exlibrisnimi);
        $("input[name=painos]").val(data.painos);
        $("select[name=type_id]").val(data.type_id);
        $("select[name=binding_id]").val(data.binding_id);
        
        $("select[name=cat1]").val(data.cat1);
        
        if(data.pageamount > 0) $("input[name=pageamount]").val(data.pageamount);
        
        $(".copyok").show();
        
        $(".autogrow").autogrow();

        var content = $("#cat-type-"+data.type_id).html();
      
        $("#cat2-sel").html(content);
        
        $("select[name=cat2]").val(data.cat2);

      }
      
    });
  
  }
  
  /*$("#product_type").change(function() {
  
    var val = $(this).val();
  
  });*/
  
  $(".copyfromname").click(function(e) {
  
    var val = $("textarea[name=name]").val();
    
    $("textarea[name=name_en]").val(val);
    
    $(".autogrow").autogrow();
  
    e.preventDefault();
    
    return false;
  
  });
  
  $("#product_author").autocomplete({source:"/seller_ajax_author.php",minLength:2});
  
  $("#product_publisher").autocomplete({source:"/seller_ajax_publisher.php",minLength:2});
  
  $("#quicksearch").autocomplete({source:"/search_ajax_name.php",minLength:2});
  
  $("#product_name").autocomplete({
    source:"/seller_ajax_name.php",
    minLength:2,
    select:function(event,ui) {
      product_fill_data(ui.item.id);
    }
  });
  
  $(".autogrow").autogrow();
  
  /* ** */
  
  function add_to_cart(product_id) {

    $.ajax({
      type:"GET",
      url:"cart.php",
      data:"action=add&id="+product_id,
      success:function(data) {
        if(data == "E_KPL" || data == "E_NUM") {
          $("#cart").effect("highlight",{},1500);
          return false;
        }
        $("#cart_items").html(data);
        if($("#cart").is(":hidden")) $("#cart").effect("bounce",{times:3},350);
        else $("#cart").effect("highlight",{},1500);
      }
    });

  }
  
  $(".add_to_cart").click(function() {

    var product_id = $(this).attr("rel");
    
    add_to_cart(product_id);

  });

});

(function($) {

  $.fn.autogrow = function() {

    this.filter('textarea').each(function() {

      var $this = $(this), minHeight  = $this.height(), lineHeight = $this.css('lineHeight');

      var shadow = $('<div></div>').css({
        position:   'absolute',
        top:        -10000,
        left:       -10000,
        width:      $(this).width(),
        fontSize:   $this.css('fontSize'),
        fontFamily: $this.css('fontFamily'),
        lineHeight: $this.css('lineHeight'),
        resize:     'none'
      }).appendTo(document.body);

      var update = function() {

        var val = this.value.replace(/</g, '&lt;')
          .replace(/>/g, '&gt;')
          .replace(/&/g, '&amp;')
          .replace(/\n/g, '<br/>');

        shadow.html(val);
        
        $(this).css('height', Math.max(shadow.height() + 15, minHeight));

      }

      $(this).change(update).keyup(update).keydown(update);

      update.apply(this);

    });

    return this;

  }

})(jQuery);
