 # Class
 #
 class vtexTotalizers
-	constructor: (@element, options) ->
+	constructor: (context, options) ->
 		@options = $.extend {}, $.fn.vtexTotalizers.defaults, options
+		@context = context
 
-		@init()
-
-	init: =>
-		@options.$template = $ """
-		<div class="amount-items-in-cart amount-items-in-cart-loading">
-			<div class="cartInfoWrapper">
-				<span class="title"><span id="MostraTextoXml1">Resumo do Carrinho</span></span>
-				<ul class="cart-info">
-					<li class="amount-products">
-						<strong><span id="MostraTextoXml2">Total de Produtos:</span></strong> <em class="amount-products-em">0</em>
-					</li>
-					<li class="amount-items">
-						<strong><span id="MostraTextoXml3">Itens:</span></strong> <em class="amount-items-em">0</em>
-					</li>
-					<li class="amount-kits">
-						<strong><span id="MostraTextoXml4">Total de Kits:</span></strong> <em class="amount-kits-em">0</em>
-					</li>
-					<li class="total-cart">
-						<strong><span id="MostraTextoXml5">Valor Total:</span></strong> R$ <em class="total-cart-em">0,00</em>
-					</li>
-				</ul>
-			</div>
-		</div>
-		"""
-
-		$(@element).after @options.$template
-
-		@selectors = {
-			amountProducts: $('.amount-products-em', @options.$template)
-			amountItems: $('.amount-items-em', @options.$template)
-			totalCart: $('.total-cart-em', @options.$template)
-		}
+		@select =
+			amountProducts: => $('.amount-products-em', @context)
+			amountItems: => $('.amount-items-em', @context)
+			totalCart: => $('.total-cart-em', @context)
 
 		@getCartData()
+		@bindEvents
 
+	bindEvents: =>
 		$(window).on 'cartUpdated', (event, cartData) =>
 			if (cartData)
 				@setCartData(cartData)
 			else
 				@getCartData()
 
-	formatCurrency: (value) ->
-		if value is '' or not value? or isNaN value
-			num = 0.00
-		else
-			num = value / 100
-		parseFloat(num).toFixed(2).replace('.', ',').toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.')
-
 	getCartData: =>
-		$(@options.$template).addClass 'amount-items-in-cart-loading'
+		$(@context).addClass 'amount-items-in-cart-loading'
 
 		$.ajax({
 			url: '/api/checkout/pub/orderForm/'

			type: 'POST'
		})
		.done (data) =>
			$(@options.$template).removeClass 'amount-items-in-cart-loading'
		.success (data) =>
			@setCartData data
		.fail (jqXHR, textStatus, errorThrown) =>

		total = 0
		for subtotal in data.totalizers
			total += subtotal.value if subtotal.id is 'Items'
		totalCart = _.formatCurrency(total / 100)

		@select.amountProducts().text amountProducts
		@select.amountItems().text amountItems
		@select.totalCart().text totalCart

#
# Plugin
