﻿function MiniCart(ip, serverPath) {
	this.Cart
	this.Id

	if (typeof MiniCart.initialized == "undefined") {

		this.Id = readCookie('CartId')

		MiniCart.prototype.highlight = function () {
			var self = this
			if ($('#quantity').text() > 0) {
				$.each(self.Cart.CartItems, function (index, item) {
					$('#cart-detail-table').append('<tr class="item"><td style="text-align:center;">' + item.Description + '</td></tr>')
				})

				$("#cart-detail").show().delay(2000).slideUp(250, function () { $("#cart-detail-table .item").detach() })
			}
		}

		MiniCart.prototype.tryToLoadCart = function (Id) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/LoadCart?callback=?',
				{
					cartId: Id
				},
				function (json) {
					if (json) {
						self.Cart = json
						MiniCart.prototype.setMiniCartDisplayedQuantity(self.Cart.CartItems.length)
						$('#cart').trigger('loaded')
					}
					else {
						MiniCart.prototype.createCart(ip)
					}
				}
			)
		}

		MiniCart.prototype.createCart = function (ip) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/CreateCart?callback=?',
				{
					ipAddress: ip
				},
				function (json) {
					self.Cart = json
					self.Id = json.Id
					createCookie('CartId', json.Id, 30)
					MiniCart.prototype.setMiniCartDisplayedQuantity(self.Cart.CartItems.length)
					$('#cart').trigger('loaded')
				}
			)
		}

		MiniCart.prototype.setMiniCartDisplayedQuantity = function (quantity) {
			$('#quantity').text(quantity)
		}

		MiniCart.prototype.registerCartEvents = function () {
			var self = this
			$("#cart-items-label, #cart-container a ").mouseover(function () {
				if ($('#quantity').text() > 0) {
					$.each(self.Cart.CartItems, function (index, item) {
						$('#cart-detail-table').append('<tr class="item"><td style="text-align:center;">' + item.Description + '</td></tr>')
					})
					$('#cart-detail').show()
				}
			})

			$("#cart-items-label, #cart-container a").mouseout(function () {
				$("#cart-detail").hide()
				$("#cart-detail-table .item").detach()
			})
		}

		MiniCart.prototype.cleanCart = function (callback) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/CleanCart?callback=?',
				{
					cartId: self.Cart.Id					
				},
				function (json) {
					self.Cart = json
					MiniCart.prototype.setMiniCartDisplayedQuantity(self.Cart.CartItems.length)
					$('#cart').trigger('changed')
					if (callback)
						callback()
				}
			)
		}

		MiniCart.prototype.addToCart = function (reference, description, productid, datatype, priceregion, datasize, dataprovider, isuserremovable, callback) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/AddItemToCart?callback=?',
				{
					cartId: self.Cart.Id,
					cartItemReference: reference,
					cartItemProductId: productid,
					cartItemDescription: description,
					cartItemDataType: datatype,
					cartItemPriceRegion: priceregion,
					cartItemDataSize: datasize,
					cartItemManufacturer: dataprovider,
					cartItemIsUserRemovable: isuserremovable
				},
				function (json) {
					self.Cart = json
					MiniCart.prototype.setMiniCartDisplayedQuantity(self.Cart.CartItems.length)
					$('#cart').trigger('changed')
					if (callback)
						callback()
				}
			)
		}

		MiniCart.prototype.removeFromCart = function (productid, callback) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/RemoveItemFromCart?callback=?',
				{
					cartId: self.Cart.Id,
					cartItemProductId: productid
				},
				function (json) {
					self.Cart = json
					MiniCart.prototype.setMiniCartDisplayedQuantity(self.Cart.CartItems.length)
					$('#cart').trigger('changed')
					if (callback)
						callback()
				}
			)
		}

		MiniCart.prototype.replaceCartItem = function (oldproductid, newreference, newdescription, newproductid, newdatatype, newpriceregion, newdatasize, newmanufacturer, newisuserremovable, callback) {
			var self = this
			$.getJSON(serverPath +
				'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/ReplaceCartItem?callback=?',
				{
					cartId: self.Cart.Id,
					oldProductId: oldproductid,
					newReference: newreference,
					newProductId: newproductid,
					newDescription: newdescription,
					newDataType: newdatatype,
					newPriceRegion: newpriceregion,
					newDataSize: newdatasize,
					newManufacturer: newmanufacturer,
					newIsUserRemovable: newisuserremovable
				},
				function (json) {
					self.Cart = json
					$('#cart').trigger('changed')
					if (callback)
						callback()
				}
			)
		}

		MiniCart.prototype.addUsChartsPack = function (callback) {
			var self = this
			$.getJSON(serverPath +
			'/webserviceswcf/ChartsCatalogServices/ChartsCatalogServices.svc/jsonp/GetUsaPack?callback=?',
				{},
				function (json) {
					for (i = 0; i < json.length; i++) {
						if (i == json.length - 1)
							self.addToCart(json[i].ZoneName, json[i].Title, json[i].ProductId, json[i].DataType, json[i].PriceRegion, json[i].ZoneType, json[i].DataProvider, false, callback)
						else
							self.addToCart(json[i].ZoneName, json[i].Title, json[i].ProductId, json[i].DataType, json[i].PriceRegion, json[i].ZoneType, json[i].DataProvider, false)
					}
				}
			)
		}
		
		MiniCart.prototype.updateCartOwner = function (userId) {
			var self = this
			$.getJSON(serverPath +
			'/webserviceswcf/CartProviderService/CartProvider.svc/jsonp/UpdateCartOwner?callback=?',
				{
					cartId: self.Cart.Id,
					userId: userId
				},
				{}
			)
		}


		if (this.Id) {
			MiniCart.prototype.tryToLoadCart(this.Id)
		}
		else {
			MiniCart.prototype.createCart(ip)
		}
		MiniCart.initialized = true
	}
}





//SAMPLE CODE TO ADD Article to Cart
//<script type="text/javascript">
//$(function(){
//	$('#cart').bind('loaded', function(){
//		$('#addNoaaPackButton').click(function(){ 
//			miniCart.CleanCart(
//			function(){
//				miniCart.addToCart("T0MX", "MaxSea Time Zero Navigator", 4, {}, {}, {}, {}, false, 
//				function(){ 
//					miniCart.addUsChartsPack(
//					function() {
//						window.location = '/Store/SignIn/tabid/693/language/en-US/Default.aspx'
//					})
//				})
//			})
//		})
//	})
//})
//</script>
