/*!
 * Kigind
 *
 *
 */

(function(window, document, undefined){

	var Kigind = function() {
		
		// internal items
		var fn = this;
				
		// create the cart object
		fn.cart = {};
		
		// add to cart
		fn.cart.addProduct = function( id, amount, price, variation, callback ) {
			// the data
			var params = $.param({
				'GUID': 'PASSED',
				'action': 'addItem',
				'pid': id,
				'amount': amount,
				'price': price,
				'variation': variation
			});
			
			// make the request
			$.ajax({
				type: 'GET', 
				url: '/controls/basket.ashx',
				data: params,
				success: function(response) {
					// update the basket amount
					$("#basketAmount").html( parseInt($("#basketAmount").text()) + parseInt(amount) );
					
					// fire the callback
					!callback || callback.call(this, response);
				}
			});
		}
		
		// remove product
		fn.cart.removeProduct = function( uid, callback ) {
			$.ajax({
				url: '/controls/basket.ashx', data: 'GUID=PASSED&action=remItem&uid='+uid,
				success: function(response) {
					!callback || callback.call(this, response);
				}
			});	
		}
		
		// update a product
		fn.cart.updateProduct = function( uid, amount, callback ) {
			// the params
			var params = $.param({
				'GUID': 'PASSED',
				'action': 'update',
				'uid': uid,
				'amount': amount
			});
			
			$.ajax({
				url: '/controls/basket.ashx', data: params,
				success: function(response) {
					!callback || callback.call(this, response);
				}
			});			
		}
		
		// the products
		fn.products = {}
		
		// bind product functions
		fn.products.bindEvents = function() {
			// check if we are on a button
			if ( !$(".add-to-basket").length ) {
				return;
			}
			
			// bind the click
			$(".add-to-basket").bind('click', function(){
				// get the info
			    var id = $("#ctl00_cphPage_lblPartNumber").text();
				var amount = $("#amount").val();
				var price = $("#ctl00_cphPage_lblPrice").text().replace('DKK ','');
				var variation = '';
				var addProduct = true;
				
				// check for a variation
				$(".variationlist").each(function(i, el){
					// make sure something is selected
					if ( !el.selectedIndex ) {
						addProduct = false;
						alert(el.options[0].text);
						return;
					}
					
					// add the item
					variation += (variation ? ', ' : '') + el.options[0].text.split(' ')[1]+': ' + el.options[el.selectedIndex].text;
				});
				
				// check to see if the product should be added
				if ( addProduct ) {
					$(".add-to-basket").fadeTo('fast', 0.5).html('Tilføjer');
					
					kigind.cart.addProduct(id, amount, price, variation, function(){
						$(".add-to-basket").fadeTo('fast', 1).html('Læg i kurv');
					});
				}
			});
		}
		
		// the order object
		fn.order = {};
		
		fn.order.count = function() {
			var amount = 0;
			
			$(".order-table .item .amount input").each(function(i,el){
				amount += parseInt( $(el).val() );
			});
			
			return amount;
		}
		
		fn.order.bindEvents = function() {
			// check for order
			if ( !$(".order-table").length ) {
				return;
			}
			
			$(".order-table .item").each(function(i, el){
				// the updater
				$(el).children('.info').children('.amount').children('input').bind('keyup', function(){
					var rel = $(this).attr('rel');
					var price = parseFloat($(el).children('.info').children('.unit_price').html().replace('kr. ', '').replace(',','.').replace(',',''));
					var amount = parseInt($(this).val());
					var total = addCommas(price*amount);
					total = 'kr. ' + ( total.indexOf(',') > -1 ? total : total + ',00' );
					
					// fade out
					$(el).fadeTo('fast', 0.5, function(){
						// set the new total
						kigind.cart.updateProduct( rel, amount, function(){
							$(el).children('.info').children('.total_price').html(total);
							$(el).fadeTo('fast', 1);
							
							// update the basket amount
							$("#basketAmount").html( fn.order.count() );
						});
					});
				});
				
				// the remover
				$(el).children('.controls').children('.remove_product').bind('click', function(){
					var rel = $(this).attr('rel');
					
					$(el).fadeTo('fast', 0.5, function(){
						fn.cart.removeProduct( rel, function(){
							$(el).animate({ height: 0, margin: 0, opacity: 0 }, 'fast', function(){
								$(el).remove();
								
								// update the basket amount
								$("#basketAmount").html( fn.order.count() );
							});
						});						
					});
					
					return false;
				});
			});
		}	
		
		
		// the bootstrap
		fn.bootstrap = function() {
			kigind.products.bindEvents();
			kigind.order.bindEvents();
		}
		
		// return the internal object
		return fn;
		
	}
	
	window.kigind = new Kigind();

})(this, document);

$(document.body).ready(function(){
	kigind.bootstrap();
});
