/*
 * jQuery UI Autocomplete HTML Extension
 *
 * Copyright 2010, Scott González (http://scottgonzalez.com)
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * http://github.com/scottgonzalez/jquery-ui-extensions
 */
(function($) {

  var proto = $.ui.autocomplete.prototype,
          initSource = proto._initSource;

  function filter(array, term) {
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), "i");
    return $.grep(array, function(value) {
      return matcher.test($("<div>").html(value.label || value.value || value).text());
    });
  }

  $.extend(proto, {
    _initSource: function() {
      if (this.options.html && $.isArray(this.options.source)) {
        this.source = function(request, response) {
          response(filter(this.options.source, request.term));
        };
      } else {
        initSource.call(this);
      }
    },

    _renderMenu: function(ul, items) {
      $.each(items, function(index, item) {
        ul.append("<li class='ui-autocomplete-category'><img src='" + item.img + "'><a class='ac-link' href='" + item.value + "'>" + item.label + "</a></li>");
      });

    },


    _renderItem_fake: function(ul, item) {
      return $("<li></li>")
              .data("item.autocomplete", item)
              .append($("<a></a>")[ this.options.html ? "html" : "text" ](item.label))
              .appendTo(ul);
    }
  });

})(jQuery);

var render_cart_info = function (price, quantity) {
    $('#cart-price').text(price || '0');
    $('#cart-quantity').text( quantity || '0');

//    $.cookie('total_price', price, {path:'/'});
//    $.cookie('total_quantity', quantity, {path:'/'});

};

$(function() {
  render_cart_info($.cookie('total_price'), $.cookie('total_quantity'));
  $('#kit-products input[type="checkbox"]').click(function() {
    var price = parseInt($('#main-product-price').val());
    $('#kit-products input:checked').each(function(i) {
      price += parseInt($(this).val());
    });
    $('#main-product-form .price').text(price + ' Р.');
  });

  $(window).scroll(function() {
    if ($(this).scrollTop() > 400) {
      $('#to-top, #top-to-top, #fantomas').fadeIn('slow');
    } else {
      $('#to-top, #top-to-top, #fantomas').fadeOut('slow');

    }
  });

  $('.ac-link').live('click', function() {
    window.location = $(this).attr('href');
  });

  $('#to-top, #top-to-top').click(function() {
    $('html, body').animate({scrollTop:0}, 'slow');
    return false;
  });

  $('#navigation').jmenu();

  $('.collapser').click(function() {

    $(this).next('.collapsable').slideToggle();
  })

  $('.more-news').click(function(e){
    e.preventDefault();
    $(this).prev().slideToggle('slow');
    $(this).remove();
  })

  $('#q').autocomplete({source: '/search_suggest/', html:true});

  $('.product-pictures').scrollable();
  $('#related-products').scrollable();

  $('.product-pictures img[rel]').overlay();
  $('.product-pictures .picture-zoom').overlay();
  $('.hint-tooltip *[title], img[title].hint, *.hint-span[title]').tooltip();
  $('.product-buttons a[title]').tooltip({position:'center left', tipClass: 'tooltip tooltip-right'});
  $('.contacts-hint').tooltip({position:'bottom center', tipClass: 'tooltip tooltip-bottom'});
  $('.picture-overlay').draggable();

  $(".product-buttons a[rel], .cart-details a[rel]").overlay({

    mask: {
      color: '#fff',
      loadSpeed: 200,
      opacity: 0.3
    },

    onBeforeLoad: function() {

      // grab wrapper element inside content
      var wrap = this.getOverlay().find(".contentWrap");

      // load the page specified in the trigger
      wrap.load(this.getTrigger().attr("href"));
    },

    onLoad: function() {
      $('.options-overlay').draggable();
    }

  });
  $('#show-extended-filters').click(function(e) {
    e.preventDefault();
    $('.extended-filters').slideToggle(500);
  });
  $('#goto-extended-filters').click(function(e) {
    e.preventDefault();
    $.scrollTo($('.bread-crumbs'));
    $('.extended-filters').slideToggle(500);
  });
  $('#clear-filters').click(function (e) {
    e.preventDefault();
    $(':input', '.filters')
            .not(':button, :submit, :reset, :hidden')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');

  });
  $('#go-to-product').click(function(e) {
    e.preventDefault();
    $.scrollTo($('.brands-list, .series-block'));
  });


  // $('#id_field_processed_square_value_0').change(function(){
  //   $('#id_field_power_heat_value_0').val($(this).val()/10);
  // });
  //$('#id_field_processed_square_value_1').change(function(){
//    $('#id_field_power_heat_value_1').val($(this).val()/10);
//  });

//  $('#id_field_power_heat_value_0').change(function(){
  //   $('#id_field_processed_square_value_0').val($(this).val()*10)
//  });
//  $('#id_field_power_heat_value_1').change(function(){
//    $('#id_field_processed_square_value_1').val($(this).val()*10)
//  });
});
