


var QooQoo = new Class({
	
	shoppingCart: null,
	pageScroller: null,
	
	initialize: function() {
		window.addEvent('domready', this.onDOMReady.bind(this));
		
		if(bertaGlobalOptions.environment == 'engine') {
			bertaEditor.addEvent(BertaEditor.EDITABLES_INIT, this.contentHeightUpdate.bind(this));
		}
		
		BertaGallery = QooQoo.BertaGallery;
	},
	
	onDOMReady: function() {
		this.entriesContainer = $('pageEntries');
		this.content = $('content');
		this.isMessySection = $('contentContainer').hasClass('messyLayout');
		this.entries = this.isMessySection ? $$('#pageEntries>div') : $$('#pageEntries>li');
		
		if(this.entriesContainer) {
			this.contentWidthUpdate();
			this.contentHeightUpdate();
		}
		
		if(!this.isMessySection) {
			if(bertaGlobalOptions.environment == 'site') {
				this.pageScroller = new QooQoo.PageScroller();
			}
		} else {
			$$('.mess').each(function(el) {
				if(!el.hasClass('xEntry')) el.adopt(new Element('div', {'class': 'xHandle'}));
			});
			$$('.xEntryMove').addClass('xHandle');
			$$('.xEntryToBack').addEvent('click', this.messyLayout_saveOrder.bindWithEvent(this));
		}
		
		this.shoppingCart = new QooQoo.ShoppingCart();
		if($('checkoutCompleteText')) {
			// clear the shopping cart
			this.shoppingCart.empty();
		}
		

	},
	
	messyLayout_saveOrder: function(event) {
		event.stop();
		
		var target = $(event.target);
		target = target.getParent('.xEntry');
		
		var entriesList = target.getParent('.xEntriesList');
		target.inject(entriesList, 'top');
		
		var newOrder = new Array();
		entriesList.getElements('.xEntry').each(function(el) {
			newOrder.push(el.getClassStoredValue('xEntryId'));
		});
		
		new Request.JSON({
			url: bertaEditor.options.updateUrl,
			data: "json=" + JSON.encode({
				section: bertaEditor.currentSection, entry: null, entryNum: null, 
				action: 'ORDER_ENTRIES', property: '', value: newOrder
			}),
			onComplete: function(resp) { 

			}.bind(this)
		}).post();
	},

	
	contentWidthUpdate: function() {
		if(!this.isMessySection) {
			var numEntries = this.entries.length > 0 ? this.entries.length : 1;
			//numEntries += (3 - ((numEntries % 3) ? (numEntries % 3) : 3));

			this.entriesContainer.setStyle('width', (numEntries * 310) + 'px');
			if(numEntries >= 3 && Browser.Engine.webkit) {
				$$('#content>a.xCreateNewEntry').addClass('hidden');
			} else {
				$$('#content>a.xCreateNewEntry').removeClass('hidden');
			}
		}
	},
	contentHeightUpdate: function() { 
		var maxHeight = 0;
		if(this.isMessySection) {
			if(this.entries) this.entries.each(function(e) {
				var y = e.getPosition(this.content).y;
				var h = e.getSize().y;
				if(y+h > maxHeight) maxHeight = y + h;
			});
			if(maxHeight < 250) maxHeight = 250;
			this.content.setStyle('height', maxHeight + 20 + 'px');
		} else {
			if(this.entries) this.entries.each(function(e) {
				var y = e.getSize().y;
				if(y > maxHeight) maxHeight = y;
				/*if(e.getClassStoredValue('xEntryId') == 1) {
					console.debug(y, e.getStyle('height'), maxHeight);
				}*/
			
			});
			if(maxHeight < 250) maxHeight = 250;
			this.content.setStyle('height', maxHeight + 20 + 'px');
		}
	}

	
	
});


QooQoo.BertaGallery = new Class({
	Implements: Options,
	Extends: BertaGallery,
	
	numSmallSizeImagesInRow: 1,
	
	initialize: function(container, options) {
		this.setOptions(options);

		this.numSmallSizeDisplayed = 1;
		
		this.attach(container);
		
		if(this.numSmallSizeDisplayed < this.numSmallSizeImagesInRow)
			this.loadFirst();
		else
			this.qooqoo_createMultiBox();
	},
	
	loadNext: function() {
		if(this.navContainer) {
			if(this.numSmallSizeDisplayed <= this.numSmallSizeImagesInRow) {
				var nextLi = this.getNext();
				if(nextLi) {
					this.nav_highlightItem(nextLi);
					this.numSmallSizeDisplayed++;
					var aEl = nextLi.getElement('a');
					this.load(aEl.get('href'), aEl.getClassStoredValue('xType'), 
							  aEl.getClassStoredValue('xW'), aEl.getClassStoredValue('xH'), 
							  aEl.getClassStoredValue('xVideoHref'), nextLi.getElement('.xGalleryImageCaption').get('html'));
					
				} else {
					this.qooqoo_createMultiBox();
				}
			} else {
				this.qooqoo_createMultiBox();
			}
		}
	},
	
	layout_update: function() {
		if(this.type == 'row') {
			var totalHeight = 0, maxWidth = 0, itmSize;
			this.imageContainer.getChildren('.xGalleryItem').each(function(item) {
				itmSize = item.getSize();
				totalHeight += itmSize.y;
				if(itmSize.x > maxWidth) maxWidth = itmSize.x;
			});
			this.imageContainer.setStyle('height', totalHeight + 'px');
			this.imageContainer.setStyle('width', maxWidth + 'px');

			window.qooQoo.contentHeightUpdate();
		}
	},
	
	qooqoo_createMultiBox: function() {
		if(this.navContainer) { 
			this.entry = this.container.getParent('.xEntry');
			var mbId = 'multiBox' + this.entry.getClassStoredValue('xEntryId');
		
			this.imageContainer = this.container.getElement('div.xGallery');
		
			this.navContainer.getElements('li').each(function(li, idx) {
				var a = li.getElement('a');
				a.set('href', a.getClassStoredValue('xOrigHref'));
				a.set('id', mbId + '_' + idx);
				a.addClass(mbId);
			
				var titleHTML = '';
				if(this.entry.getElement('h2')) 
					titleHTML += '<h2>' + this.entry.getElement('h2').get('html') + '</h2>';
				if(this.entry.getElement('.description')) {
					var dimsDesc = this.entry.getElement('.description').get('html')
					if(dimsDesc) titleHTML += '<p class="sub">' + dimsDesc + '</p>';
				}
				a.set('title', titleHTML);
			
				var descEl = li.getElement('.xGalleryImageCaption');
				descEl.addClass(mbId + '_' + idx);
				if(descEl.get('html') != '') new Element('HR').inject(descEl, 'top');
			
			}.bind(this));
		
			this.mbBox = new multiBox(mbId, {
				openFromLink: false,
				overlay: new overlay(),
				descClassName: 'xGalleryImageCaption'
			});
			
			this.qooqoo_addClickEvents();
		}
	},
	
	qooqoo_addClickEvents: function() {
		var imageDivs = this.imageContainer.getElements('div.xGalleryItem');
		imageDivs.setStyle('cursor', 'pointer');
		imageDivs.addEvent('click', this.qooqoo_openPopup.bindWithEvent(this));
	},
	
	qooqoo_openPopup: function(event) {
		var imageDIV = $(event.target);
		if(imageDIV.get('tag') != 'div') imageDIV = imageDIV.getParent('div.xGalleryItem');
		var imageDIVs = this.imageContainer.getElements('div.xGalleryItem');
		var idx = imageDIVs.indexOf(imageDIV);
		
		this.mbBox.open(this.mbBox.content[idx]);
	}
	
});


QooQoo.PageScroller = new Class({
	
	btnLeft: null,
	btnRight: null,
	
	currentPage: 0,
	numPages: 0,
	
	entriesContainerTween: null,
	
	initialize: function() {
		this.entries = $$('#pageEntries>li');
	
		this.btnLeft = $('contentArrowLeft');
		this.btnRight = $('contentArrowRight');
		this.currentPage = 0;
		this.numPages = Math.ceil(this.entries.length / 3);
		if(!this.numPages) this.numPages = 1;
		
		this.entriesContainerTween = new Fx.Tween($('pageEntries'), {
			'duration': 500,
			'transition': 'quint:in:out'
		});
		
		this.btnLeft.addEvent('click', this.onBtnLeftClick.bindWithEvent(this));
		this.btnRight.addEvent('click', this.onBtnRightClick.bindWithEvent(this));
		this.updateButtons();
	},
	onBtnLeftClick: function(event) {
		event.stop();
		this.scroll(-1);
	},
	onBtnRightClick: function(event) {
		event.stop();
		this.scroll(1);
	},
	updateButtons: function() {
		this.btnLeft.setStyle('display', this.currentPage > 0 ? 'block' : 'none');
		this.btnRight.setStyle('display', (this.currentPage < this.numPages - 1) ? 'block' : 'none');
	},
	
	scroll: function(direction) {
		var targetX;
		if(direction > 0) {
			if(this.currentPage < this.numPages - 1) {
				this.currentPage++;
				targetX = this.currentPage * 930;
				this.scroll_to(targetX);
			}
		} else {
			if(this.currentPage > 0) {
				this.currentPage--;
				targetX = this.currentPage * 930;
				this.scroll_to(targetX);
			}
		}
		
		this.updateButtons();
	},
	scroll_to: function(targetX) {
		this.entriesContainerTween.start('left', -targetX + 'px');
	}
	
	
});


QooQoo.ShoppingCart = new Class({
	
	Implements: Events,
	
	cart: null,
	cartElement: null,
	cartElItemsContainer: null,
	cartElItems: null,
	shippingPriceForCart: null,
	
	initialize: function() {
		this.cartElement = $('shoppingCart');
		this.cartContentsElement = $('shoppingCartContents');
		this.checkoutFormElement = $('paypalCheckoutForm');
    this.checkoutFormElementCC = $('ccCheckoutForm');
		this.cartElItemsContainer = this.cartElement.getElement('.numItemsContainer');
		this.cartElItems = this.cartElement.getElement('.numItems');
		
		// all pages init
		if(this.cartElement) {
			
			// read cart from cookie
			this.cart = Cookie.read('shopping_cart');
			if(this.cart) {
				this.cart = new Hash(JSON.decode(this.cart));
			} else {
				this.cart = new Hash();
			}
			
			// init cart element
			this.cartElement.removeClass('hidden');
			this.updateCartLink();
			
			// add events to "add to cart" buttons
			$$('.addToCart a').addEvent('click', this.onAddToCartClick.bindWithEvent(this));

      // add events to "pay with creditcard" form submit
			$$('#ccCheckoutForm').addEvent('submit', this.onccCheckoutFormSubmit.bindWithEvent(this));
		}
		
		// shopping cart page init
		if(this.cartContentsElement) {
			if(this.cart) {
				var tbody = this.cartContentsElement.getElement('tbody');
				var example = tbody.getElement('tr').dispose();
				var cItem, i = 0;
				this.cart.each(function(c, name) {
					cItem = example.clone();
					cItem.setClassStoredValue('checkoutItemId', ++i);
					cItem.getElement('.title span').set('html', name);
					cItem.getElement('.qty').set('html', c.qty);
					cItem.getElement('.price').set('html', c.price ? Number(c.price).numberFormat(2) : '0.00');
					cItem.inject(tbody);
				});
				
				$('shipping_select').addEvent('change', this.onShippingSelectChange.bindWithEvent(this));
				
				var dCode = $('appliedDiscountCode');
				var dPercent = $('appliedDiscountPercent');
				if(dCode && dCode.get('html') != '' && dPercent && dPercent.getProperty('title') != '') {
					this.cartDiscount = parseFloat(dPercent.getProperty('title'));
					this.cartDiscountCode = dCode.get('html');
				} else {
					this.cartDiscount = 0;
					this.cartDiscountCode = false;
				}
				//console.debug(this.cartDiscount, this.cartDiscountCode);
				
				this.updateCartContentsElement();
				this.updateCheckoutFormElement();
				this.cartContentsElement.getElements('a.add').addEvent('click', this.onCartUpdateAddClick.bindWithEvent(this));
				this.cartContentsElement.getElements('a.remove').addEvent('click', this.onCartUpdateRemoveClick.bindWithEvent(this));
				
			} else {
				this.cartContentsElement.addClass('hidden');
			}
			
		}
	},
	
	add: function(name, price, weight, qty) {
		if(this.cart.has(name)) {
			var item = this.cart.get(name);
			if(!item.qty) item.qty = parseInt(qty); else item.qty = parseInt(item.qty) + parseInt(qty);
			item.price = parseFloat(price);
			item.weight = parseFloat(weight);
			this.cart.set(name, item);
		} else {
			this.cart.set(name, {qty: parseInt(qty), price: parseFloat(price), weight: parseFloat(weight)});
		}
		this.save();
		this.updateCartLink();
	},
	empty: function() {
		this.cart.empty();
		this.save();
		this.updateCartLink();
		this.updateCartContentsElement();
		this.updateCheckoutFormElement();
	},
	update: function(name, byQty) {
		if(this.cart.has(name)) {
			if(!byQty) {
				this.cart.erase(name);
			} else {
				var item = this.cart.get(name);
				if(item.qty) item.qty = parseInt(item.qty) + parseInt(byQty);
				if(item.qty)
					this.cart.set(name, item);
				else
					this.cart.erase(name);
			}
			this.save();
			this.updateCartLink();
			this.updateCartContentsElement();
			this.updateCheckoutFormElement();
		}
	},
	save: function() {
		Cookie.write('shopping_cart', JSON.encode(this.cart.getClean()), {path: bertaGlobalOptions.paths.siteABSRoot});
	},
	
	
	

	
	onAddToCartClick: function(event) {
		event.stop();
		var addToCartConteiner = $(event.target).getParent('.addToCart');
		var title = addToCartConteiner.getElement('.cartTitle').get('html');
		var price = parseFloat(addToCartConteiner.getElement('.cartPrice').get('title'));
		var weight = parseFloat(addToCartConteiner.getElement('.cartWeight').get('html'));
		if(!weight) weight = 0;
		
		var optionSelect = addToCartConteiner.getElement('select');
		if(optionSelect) {
			title += ' ' + optionSelect.get('value');
		}
		this.add(title, price, weight, 1);
		
		var addedContainer = addToCartConteiner.getNext('.addedToCart');
		addedContainer.set('html', title + ' added!');
		this.afterToCartAdded(addToCartConteiner, addedContainer);
	},
	onCartUpdateAddClick: function(event) {
		event.stop();
		var td = $(event.target).getParent('td');
		this.update(td.getElement('span').get('html'), 1);
		
	},
	onCartUpdateRemoveClick: function(event) {
		event.stop();
		var td = $(event.target).getParent('td');
		this.update(td.getElement('span').get('html'), -1);
	},
	
	onShippingSelectChange: function(event) {
		var shippingPrice = Number($(event.target).get('value'));
		this.cartContentsElement.getElement('.shippingTotal').set('html', shippingPrice > 0 ? shippingPrice.numberFormat(2) : '');
		this.updateCartContentsElement();
		this.updateCheckoutFormElement();
	},

  onccCheckoutFormSubmit: function(event) {
    if (
      $('nname').get('value').trim() == '' ||
      $('phone').get('value').trim() == '' ||
      $('address').get('value').trim() == '' ||
      $('pk').get('value').trim() == '' ||
      $('email-reg').get('value').trim() == ''
    ) {
      alert('Please fill all mandatory fields');
      return false;
    }

    // post order
    var products = new Array();

		if(this.checkoutFormElement && this.cartContentsElement) {
			var tbody = this.cartContentsElement.getElement('tbody');	// first tbody element
			var title, cItem, checkoutIdx = 0;
			tbody.getElements('tr').each(function(tr) {
				title = tr.getElement('.title span').get('html');
				cItem = this.cart.get(title);
				products[checkoutIdx] = new Array(title, cItem.qty, cItem.price);

        checkoutIdx++;
			}.bind(this));

      var discountCode = 0,
          cartDiscount = 0;
      if(this.cartDiscountCode && this.cartDiscount > 0) {
        discountCode = this.cartDiscountCode;
        cartDiscount = this.cartDiscount;
      }

		}

    new Request({
      method: 'post',
      url: '/fdl/ajax.php',
      data: {
        'name' : $('nname').get('value'),
        'phone' : $('phone').get('value'),
        'address' : $('address').get('value'),
        'pk' : $('pk').get('value'),
        'email' : $('email-reg').get('value'),
        'notes' : $('notes').get('value'),
        'products' : products,
        'shipping' : shippingPriceForCart,
        'discountCode' : discountCode,
        'cartDiscount' : cartDiscount * 100
      },
      //onRequest: function() { alert('Request made. Please wait...'); },
      onComplete: function(response) {
        jQuery('#cc-fields').replaceWith(response);
        jQuery('#cc-submit-btn').remove();
        jQuery('#cc-total-lvl').remove();
        
        // popup link for cc payment
        jQuery('.popup-link').popupWindow({
          centerBrowser: 1,
          height: 650,
          width: 800
        });
      }
    }).send();

    return false;
  },
	
	
	afterToCartAdded: function(c1, c2) {
		new Fx.Tween(c1, {duration: 200}).set('opacity', 1).start('opacity', 0).chain(
			function() { 
				c1.addClass('hidden');
				c2.removeClass('hidden');
				new Fx.Tween(c2, {duration: 400}).set('opacity', 0).start('opacity', 1).wait(2000).chain(
					function() { 
						new Fx.Tween(c2, {duration: 200}).set('opacity', 1).start('opacity', 0).chain(
							function() { 
								c2.addClass('hidden');
								c1.removeClass('hidden');
								new Fx.Tween(c1, {duration: 500}).set('opacity', 0).start('opacity', 1);
							}
						)
					}
				);
			}
		);
	},
	
	
	
	updateCartLink: function() {
		var numItems = 0;
		this.cart.each(function(itm) {
			numItems += itm.qty;
		});
		
		this.cartElItems.set('html', numItems);
		this.cartElItemsContainer.removeClass('hidden');
	},
	
	updateCartContentsElement: function() {
    
		if(this.cartContentsElement) {
			var tbody = this.cartContentsElement.getElement('tbody');
			var sum = 0, weight = 0, itemsAdded = false, itmSum, itmWeight, title, cItem, checkoutIdx;
			tbody.getElements('tr').each(function(tr) {
				checkoutIdx = tr.getClassStoredValue('checkoutItemId');
				title = tr.getElement('.title span').get('html');
				cItem = this.cart.get(title);
				if(!cItem || !cItem.qty) {
					tr.destroy();
				} else {
					tr.getElement('.qty').set('html', cItem.qty ? cItem.qty : 0);
					itmSum = Number(cItem.price ? cItem.price : 0) * Number(cItem.qty ? cItem.qty : 0);
					tr.getElement('.sum').set('html', itmSum ? itmSum.numberFormat(2) : '0.00');
					sum += itmSum;
					
					itmWeight = Number(cItem.weight ? cItem.weight : 0) * Number(cItem.qty ? cItem.qty : 0);
					weight += itmWeight;
					
					itemsAdded = true;
				}
			}.bind(this));
			
			if(this.cartDiscount > 0 && sum > 0) {
				sum -= sum * this.cartDiscount;
			} 
			
			if(!itemsAdded) {
				this.cartContentsElement.addClass('hidden');
				$('shoppingCartEmpty').removeClass('hidden');
			} else {
				this.cartContentsElement.removeClass('hidden');
				$('shoppingCartEmpty').addClass('hidden');
			}
			
			shippingPriceForCart = -2;
			
			// get weight index
			var weights = String($('shipping_weights').get('value')).split(','), priceIndex = -1;
			for(var i = 0; i < weights.length; i++) {
				if(weight * 1000 < weights[i]) {
					priceIndex = i;
					break;
				}
			}
			
			// find price for the weight index
			if(priceIndex >= 0) {
				if($('shipping_select').get('value') != -1) {
					var shippingPrices = String($('shipping_select').get('value')).split(',');
					if(shippingPrices[i]) {
						shippingPriceForCart = parseFloat(shippingPrices[i]);
					} else {
						shippingPriceForCart = -1;
					}
				} else {
					shippingPriceForCart = -1;
				}
			}
			//console.debug('updateCartContentsElement: w=' + weight + '; shipping=' + shippingPriceForCart)
			
			// display the shipping price and add it to the total sum
			if(shippingPriceForCart >= 0) {
				this.cartContentsElement.getElement('.shippingTotal').set('html', shippingPriceForCart.numberFormat(2));
				this.cartContentsElement.getElement('tbody.total').removeClass('hidden');
				this.cartContentsElement.getElement('tbody.overweightError').addClass('hidden');
				this.cartContentsElement.getElement('tbody.shipping').removeClass('hidden');
				this.checkoutFormElement.removeClass('hidden');
        this.checkoutFormElementCC.removeClass('hidden');
				sum += shippingPriceForCart;
        $('content').setStyle('height', '620px'); // dirty fix
			} else {
				this.cartContentsElement.getElement('tbody.total').addClass('hidden');
				
				if(shippingPriceForCart == -2) {
					this.cartContentsElement.getElement('tbody.shipping').addClass('hidden');
					this.cartContentsElement.getElement('tbody.overweightError').removeClass('hidden');
				} else {
					this.cartContentsElement.getElement('tbody.shipping').removeClass('hidden');
					this.cartContentsElement.getElement('tbody.overweightError').addClass('hidden');
				}
				this.checkoutFormElement.addClass('hidden');
        this.checkoutFormElementCC.addClass('hidden');
			}
			
			this.cartContentsElement.getElement('.sumTotal').set('html', sum ? sum.numberFormat(2) : '0.00');
      var sumlvl = sum * 0.707;
      this.checkoutFormElementCC.getElement('#sumTotalLVL').set('html', sumlvl ? sumlvl.numberFormat(2) : '0.00');
		}
	},
	
	updateCheckoutFormElement: function() {
		// delete existing hidden fields
		if(this.checkoutFormElement) {
			var i = 1, found = true, el;
			while(found) {
				found = false;
				if(el = $('checkout_amount_' + i)) el.destroy();
				if(el = $('checkout_quantity_' + i)) el.destroy();
				if(el = $('checkout_shipping_' + i)) el.destroy();
				if(el = $('checkout_item_name_' + i)) {
					el.destroy();
					found = true;
				}
				i++;
			}
			
			var dEl = $('checkout_discount_rate_cart');
			if(dEl) dEl.destroy();
			
		//	var shEl = $('checkout_shipping');
			//if(shEl) shEl.destroy();
		}
		
		// create new hidden fields
		if(this.checkoutFormElement && this.cartContentsElement) {
			var tbody = this.cartContentsElement.getElement('tbody');	// first tbody element
			var itmSum, title, cItem, checkoutIdx = 0;
			var shippingPriceToAdd = shippingPriceForCart;
			//var shippingPrice = Number($('shipping_select').get('value'));
			tbody.getElements('tr').each(function(tr) {
				checkoutIdx++;
				title = tr.getElement('.title span').get('html');
				cItem = this.cart.get(title);
				//itmSum = Number(cItem.price ? cItem.price : 0) * Number(cItem.qty ? cItem.qty : 0);
				
				this.checkoutFormElement.adopt(
					new Element('input', {'type': 'hidden', 'name': 'item_name_' + checkoutIdx, 'id': 'checkout_item_name_' + checkoutIdx, 'value': title}),
					new Element('input', {'type': 'hidden', 'name': 'quantity_' + checkoutIdx, 'id': 'checkout_quantity_' + checkoutIdx, 'value': cItem.qty}),
					new Element('input', {'type': 'hidden', 'name': 'amount_' + checkoutIdx, 'id': 'checkout_amount_' + checkoutIdx,'value': cItem.price ? cItem.price.numberFormat(2) : '0.00'}),
					new Element('input', {'type': 'hidden', 'name': 'shipping_' + checkoutIdx, 'id': 'checkout_shipping_' + checkoutIdx,'value': shippingPriceToAdd})
				);
				
				if(shippingPriceToAdd > 0) shippingPriceToAdd = 0; // add the whole shipping price only to the first element in cart
			}.bind(this));
			
			if(this.cartDiscountCode && this.cartDiscount > 0) {
				checkoutIdx++;
				this.checkoutFormElement.adopt(
					new Element('input', {'type': 'hidden', 'name': 'item_name_' + checkoutIdx, 'id': 'checkout_item_name_' + checkoutIdx, 'value': 'Discount code "' + this.cartDiscountCode + '"'}),
					new Element('input', {'type': 'hidden', 'name': 'quantity_' + checkoutIdx, 'id': 'checkout_quantity_' + checkoutIdx, 'value': 1}),
					new Element('input', {'type': 'hidden', 'name': 'amount_' + checkoutIdx, 'id': 'checkout_amount_' + checkoutIdx,'value': '0.00'}),
					new Element('input', {'type': 'hidden', 'name': 'shipping_' + checkoutIdx, 'id': 'checkout_shipping_' + checkoutIdx,'value': '0.00'}),
					new Element('input', {'type': 'hidden', 'name': 'discount_rate_cart', 'id': 'checkout_discount_rate_cart', 'value': this.cartDiscount * 100})
				);
			}
			
			//new Element('input', { 'type': 'hidden', 'name': 'shipping', 'id': 'checkout_shipping', 'value': this.cartContentsElement.getElement('.shippingTotal').get('html') }).inject(this.checkoutFormElement);
			
		}
	}
	
	
});









Number.implement({

	/*
	Property: numberFormat
		Format a number with grouped thousands.

	Arguments:
		decimals, optional - integer, number of decimal percision; default, 2
		dec_point, optional - string, decimal point notation; default, '.'
		thousands_sep, optional - string, grouped thousands notation; default, ','

	Returns:
		a formatted version of number.

	Example:
		>(36432.556).numberFormat()  // returns 36,432.56
		>(36432.556).numberFormat(2, '.', ',')  // returns 36,432.56
	*/

	numberFormat : function(decimals, dec_point, thousands_sep) {
		decimals = Math.abs(decimals) + 1 ? decimals : 2;
		dec_point = dec_point || '.';
		thousands_sep = thousands_sep || ',';
	
		var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + ''); // returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals
		var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
		return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) + thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + 
				(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
	}


});





var qooQoo = new QooQoo();


