jQuery.fn.linkAcc = function() {

  return this.each( function() {

        $(this).find('a').each( function() {
            $(this).click( function() {
             var needOff = $(this).parent().attr('id');

             $(document).find( 'small[id=' + needOff + ']').each( function() {
                if ( $(this).hasClass('active' ) ) {
                    // включаем активный
                    $(this).css('display', '' );
                } else {
                    $(this).css('display', 'none' );
                    // выключаем неактивный
                }
             });

             $(document).find( 'small[id!=' + needOff + ']').each( function() {

                if ( $(this).hasClass( 'active' ) ) {               
                  // выключаем активный
                  $(this).css('display', 'none');
                } else {
                  // включаем неактивный
                  $(this).css('display', '');
                }
             });

            });
        });        

  });
};
changeCount = function( obj, action ) {

        // Получаем parent тэг
        var parent = $(obj).parent();
        // Получаем тэг с количеством
        var count  = $(parent).find('input[name=count]');

        if ( action == 'minus' ) {
            if ( count.val() > 1 ) {
                count.val( count.val() - 0 - 1 );
            }
        } else {
            count.val( count.val() - 0 + 1 );
        }

	cartTable = $('table[class=order-items]');

	if ( cartTable.length > 0 ) {

		var tableString = $(obj).parent();
		var gds_id = $(tableString).find('input[name=gds_id]');

		if ( gds_id.length > 0 ) {
                      $.ajax( {
                                type:     "get",
                                url:      "/util/",
                                dataType: "json",
                                data:     "action=ajax_cart&gds_id=" + gds_id.val()  + "&gds_count="+ count.val() ,
                                async:    "false",
                                success: function( data, textStatus ) {
                                        if ( data.result == true ) {
                                                // Обновнляем корзину
                                                var cartDiv = $('div[class=cart]');
                                                var footer  = $(cartDiv).find('.footer');
                                                cartDiv.find('.item').remove();
                                                $(footer).before( data.cart_html );
                                                $(footer).html('<strong class="price">Итого: <br/>' + data.totalsum + '' + '</strong><a href="/cart/order"><img src="/images/copyline/img/cart_submit.png" alt="Оформить"/></a>');
                                                $(obj).parent().parent().parent().find('span[class=summ]').text( data.sum );
                                                $('span[class=crn]').text( data.totalsum );
                                        }
                                }
			});
		}
	}

	cartForm = $('form[name=cart]');
	if ( cartForm.length > 0 ) {
		var gds_id = $(obj).siblings('input[name=gds_id]');
		if ( gds_id.length > 0 ) {
			$.ajax({
				type:     "get",
				url:      "/util/",
				dataType: "json",
				data:     "action=ajax_cart&gds_id=" + gds_id.val()  + "&gds_count="+ count.val() ,
				async:    "false",
				success: function( data, textStatus ) {
					if ( data.result == true ) {
						// Обновнляем корзину
						var cartDiv = $('div[class=cart]');
						var footer  = $(cartDiv).find('.footer');
						cartDiv.find('.item').remove();
						$(footer).before( data.cart_html );
						var linkOrder = $(footer).find('a');
                                                $(footer).html('<strong class="price">Итого: <br/>' + data.totalsum + '' + '</strong><a href="/cart/order"><img src="/images/copyline/img/cart_submit.png" alt="Оформить"/></a>');
						$(obj).parent().parent().parent().find('span[class=summ]').text( data.sum );
						// Обновляем общую сумму заказа
					        $(cartForm).find('span[class=crn]').text( data.totalsum2 );
						//alert( textStatus );
					}
				},
				error:   function( data ) {
				}
			});
		}
	}
}

mapLinks = function( obj ) {

    var arr = $('a[id=map]');
 //   alert( arr );
//    $(obj).parent().html('asdsad');
}

jQuery.validator.addMethod( "phoneCode", function(value, element) {
	return false;
}, "Test" );

jQuery.validator.addMethod( "inn", function( value, element ) {
        if ( $(':radio[name=f_typeofclient]').filter(":checked").val() == 1 ) {
          if ( value.length < 10 ) {
            return false;
          } else {
            return true;
         }
        } else {
          return true;
        }
}, "Укажите ИНН" );

jQuery.validator.addMethod( "company", function( value, element ) {
        if ( $(':radio[name=f_typeofclient]').filter(":checked").val() == 1 ) {
          if ( value.length < 3 ) {
            return false;
          } else {
            if ( value == '' ) return false;
            return true;
         }
        } else {
          return true;
        }
}, "Укажите название компании" );


$(document).ready( function() {

        $('a[id=map]').click( function() {
            mapLinks( $(this) );
        });

        $('.sheme_link').linkAcc();
	$('span[class=minus]').click( function()  {
		changeCount( this, 'minus' );
	});

        $('span[class=plus]').click( function()  {
		changeCount( this, 'plus' );
	});



/* validate */



















});

