function initCart()
{
  jQuery('.add_to_cart, .remove_from_cart').click(function(){
    var id = jQuery(this).attr('rel');
    jQuery.ajax({
      url: this.href + '?type=ajax',
      type: 'GET',
      dataType: 'json',
      success: function(data)
      {
        if(data.status == 'success')
        {
          jQuery('#' + id).remove();

          updateCart(data.total_amount, data.total_price);
        }

        if(data.message)
          showMessage(data.message);
      }
    });
    return false;
  });

  jQuery('form#order_form').submit(function(){
    jQuery(this).ajaxSubmit({
      dataType: 'json',
      data: { type: 'ajax' },
      success:function(data)
      {
        if(data.status == 'success')
        {
          updateCart(data.total_amount, data.total_price);
        }

        if(data.message)
          showMessage(data.message);
      }
    });

    return false;
  });
}

function updateCart(total_amount, total_price)
{
  jQuery('#order_total_price').html(total_price);

  if(total_amount == 0)
    jQuery('#cart_product_list').html('<p>Ваша корзина пуста.</p>');
}

function showMessage(message)
{
  alert(message);
}

jQuery.fn.accordion = function(options) {
    // options
    var SLIDE_DOWN_SPEED = 'fast';
    var SLIDE_UP_SPEED = 'fast';
    var startClosed = options && options.start && options.start == 'closed';
    var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on - 1 : 0;
    return this.each(function() {
        jQuery(this).addClass('accordion'); // use to activate styling
        jQuery(this).find('ul').not('.current ul').hide();

        /*jQuery(this).find('li').click(function() {
            var current = jQuery(this.parentNode).find('li ul:visible');
            var next = jQuery(this).find('ul');
            if(next[0] == current[0])
              return;

            if (current[0] != next[0]) {
                current.slideUp(SLIDE_UP_SPEED);
            }
            if (next.is(':visible')) {
                next.slideUp(SLIDE_UP_SPEED);
            } else {
                next.slideDown(SLIDE_DOWN_SPEED);
                return false;
            }
        });*/
        if (!startClosed) {
            var elem = jQuery(this).find("ul > ul > li.current").parents('ul');
            elem.prev().addClass('current');
            elem.prev().css('color','#fff');
            if(!elem.get()[0])
            {
              jQuery(this).find('ul:first').slideDown(SLIDE_DOWN_SPEED);
              return;
            }
            elem.slideDown(SLIDE_DOWN_SPEED);
        }
    });
};

/* */
function initApp()
{
  initCart();

  jQuery("a.fancy").fancybox({
    'hideOnContentClick':true,
    'overlayShow':true,
    'zoomSpeedIn':100,
    'zoomSpeedOut':100,
    'hideOnOverlayClick':true
  });

  jQuery("#gbadd").click(function () {
    jQuery("#gbaddf").toggle("slow");
  });

  jqueryslidemenu.buildmenu("myslidemenu", arrowimages, 1);

  jQuery('.lnavig').accordion({start: 'closed'});
}

/* */
var MapWin;
function OpenMap(url, width, height)
{
    var h=height+60;
    if (MapWin) { MapWin.close(); }
    MapWin=window.open ("", "Photos", "width="+width+",height="+h+",resizable=0");
    MapWin.document.open();
    MapWin.document.writeln("<HTML><head><link href=/st.css rel=stylesheet type=text/css><title>����������</title></head><BODY leftmargin=0 topmargin=0 oncontextmenu='return false' ondragstart='return false'><center><img src="+url+" width="+width+" height="+height+" border=0><br><br><a class=book onclick='window.close();' href='javascript:window.close();' target=_parent>�������</a><br><br></center></BODY></HTML>");
    MapWin.document.close();
    MapWin.focus();
}

var BookWin;
function OpenWin(url, width, height) {
  var h=height+140;
  var w=width+20;
  if (BookWin) { BookWin.close(); }
  BookWin=window.open (url, "Book", "width="+w+",height="+h+",resizable=1,scrollbars=yes");
  BookWin.focus();
}

