
var tabslider = ( function ($) {

	var order = {
		articleId : 0,
		orgContainer : {
			postalcode	: 0,
			transportcosts	: 0,
			wastetype	: '',
			wastepricem3	: 0,
			containersize	: '',
			totalprice	: 0,
			description	: '',
			filling		: '',
			licenses	: '',
			alternatives	: [{}],
			selectablecontainers	: [{}],
			selectablewastetypes	: [{}]
		},
		newContainer : {
			postalcode	: 0,
			transportcosts	: 0,
			wastetype	: '',
			wastepricem3	: 0,
			containersize	: '',
			totalprice	: 0,
			description	: '',
			filling		: '',
			licenses	: '',
			alternatives	: [{}],
			selectablecontainers	: [1,2,3,4,5,6],
			selectablewastetypes	: [1,2,3,4,5,6,7]
		}
	},
	
	config = {

		navItems		: '#tabslider_nav li',
		navItemLinks		: '#tabslider_nav li a',
		activeTab		: 'li.activetab',
		contentContainer	: '#tabslider_content',
		activeTabClass		: 'activetab',

		postalcodeTab		: '#postalcode',
		wasteTab		: '#waste',
		containerTab		: '#container',
		orderTab		: '#order',
		button			: 'a.passive_link',
		altButton		: 'a.alt_container_link',

		itemContainer		: '.inner',
		postalcodehtml		: '#html_postalcode',
		wastetypehtml		: '#html_wastetype',
		containertypehtml	: '#html_containertype',
		pricehtml		: '#html_price',

		productPrice		: '#price_tag_webshop',
		productAdvicePrice	: '#price_tag_del',
		productImage		: '#product_image',
		productTabs		: '#product-tabs',
		productAlts		: '#product-alt',
		productDescription	: '#description',
		productRules		: '#rules',
		productLicenses		: '#licenses',

		anchor			: '#scroll_anchor'

	},

	privs = {

		isEmpty : function (mixed_var) {

			if (mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null
				|| mixed_var === false || typeof mixed_var === 'undefined' ) {
				return true;
			}

			if (typeof mixed_var == 'object') {
				for (var key in mixed_var) {
					return false;
				}
				return true;
			}
		},

		setAlternatives : function () {

			var html = '<div class="collectionContainer">',
				container = order.newContainer.alternatives;
			for (var i = 0, n = container.length; i < n; i++) {

				if (i > 3) break;

				html +=	'<div class="productCollection">';
				html +=	'	<div class="inner" id="alt_container_' + i + '">';
				html +=	'		<div class="TL"></div>';
				html +=	'		<div class="TC"></div>';
				html +=	'		<div class="TR"></div>';
				html +=	'		<div class="scrolltitle">';
				html +=	'			<h2 class="scrolltitle"><a class="passive_link alt_container_link" href="#order">' + container[i].size + ' m&#179;</a></h2>';
				html +=	'		</div>';
				if ( typeof container[i].adviceprice != 'undefined' && container[i].adviceprice.length > 0 ) {
					html +=	'		<p class="price"><del>' + container[i].adviceprice + '</del></p>';
				}
				html +=	'		<p class="altproduct_price">&euro; ' + parseFloat(container[i].price).toFixed(2) + '</p>';
				html +=	'		<div class="image">';
				html +=	'			<a class="passive_link alt_container_link" href="#order">';
				html +=	'				<img src="/uploads/images/order_' + container[i].img + '" alt="' + document.title + '" />';
				html +=	'			</a>';
				html +=	'		</div>';
				html +=	'		<p class="call-to-action">';
				html +=	'			<a class="passive_link button bestelBtn large alt_container_link" href="#order"><span>Kies</span><i><!-- --></i></a>';
				html +=	'		</p>';
				html +=	'		<div class="BL"></div>';
				html +=	'		<div class="BC"></div>';
				html +=	'		<div class="BR"></div>';
				html +=	'	</div>';
				html +=	'</div>';

			}

			html += '</div>';

			$(config.productAlts).empty().append(html).show();
		},

		getTotalprice : function () {

			if (!privs.isEmpty(order.newContainer.postalcode) && !privs.isEmpty(order.newContainer.wastetype)
				&& !privs.isEmpty(order.newContainer.containersize)){
				
				$.ajax({
					type: "POST",
					dataType: 'json',
					url: "includes/processors/containercalculator.php",
					data: "action=getProductUpdate&param=" + $.toJSON(order),
					success: function (data) {

						//overwrite orignal container settings, in case of a change
						$.extend(order.orgContainer, data.param.orgContainer);

						//adjust current object
						$.extend(order.newContainer, data.param.newContainer);

						//set price html
						//$(config.productPrice).html('&euro; ' + data.param.newContainer.totalprice);
						var totalprice = '0.00'.split(".");
						if ( typeof data.param.newContainer.totalprice == 'string' )
							totalprice = data.param.newContainer.totalprice.split(".");
						$(config.productPrice).html('&euro; ' + totalprice[0] + '<span>' + totalprice[1] + '</span>');
						
						//set advice price html
						if ( typeof data.param.newContainer.adviceprice != 'undefined' &&
							data.param.newContainer.adviceprice != false ) {
							$(config.productAdvicePrice).html('<del>' + data.param.newContainer.adviceprice + '</del>');
						}
						else {
							$(config.productAdvicePrice).html('');
						}

						//set alternative container options
						privs.setAlternatives();
						privs.setAlternativeVars();
						privs.showSelectableContainers();
						privs.showSelectableWastetypes();

					},
					error : function (e) {
						throw new Error(e);
					}
				});

			} else {
				return;
			}

		},

		setWastetype : function () {

			$(config.button, config.wasteTab).click( function () {

				var wastetype = $(this).parents(config.itemContainer).find('h2 a').html();

				if (wastetype !== '') {
					
					// update json
					order.newContainer.wastetype = wastetype;

					// set html
					$(config.wastetypehtml).html(wastetype);

					order.newContainer.description = "";
					order.newContainer.filling = "";
					order.newContainer.licenses = "";

					$.ajax({
						type: "POST",
						dataType: 'json',
						url: "includes/processors/containercalculator.php",
						data: "action=getProductUpdate&param=" + $.toJSON(order),
						success: function (data) {

							//adjust current object
							$.extend(order.newContainer, data.param.newContainer);
							order.newContainer.selectablecontainers = data.param.newContainer.selectablecontainers;
							privs.showSelectableContainers();
							order.newContainer.selectablewastetypes = data.param.newContainer.selectablewastetypes;
							privs.showSelectableWastetypes();

							//set product html
							var content = data.param.newContainer;
							$(config.productDescription, config.productTabs).html(content.description);
							$(config.productRules, config.productTabs).html(content.filling);
							$(config.productLicenses, config.productTabs).html(content.licenses);

							if ( typeof data.alertMessage !== 'undefined') {
								alert(data.alertMessage);
							}

						},
						error : function (e) {
							throw (e);
						}
					});

				} else {
					throw ('Afvaltype niet gedefinieerd');
				}

				// check if total price can be calculted
				privs.getTotalprice();

				//set active tab
				$('#tabslider').tabs('select',2);

				//prevent default event action
				return false;
			});

		},

		setContainersize : function () {

			$(config.button, config.containerTab).click( function () {

				var parent = $(this).parents(config.itemContainer);

				//get Container capacity
				var capacity = parent.find('h2 a').html();

				//get container image
				var imagesrc = parent.find('img').attr('src');

				// update json
				if (capacity !== '') {
					if (capacity.toLowerCase() === 'bigbag') {
						order.newContainer.containersize =  1;
					} else {
						order.newContainer.containersize =  parseInt(capacity);
					}
				} else {
					throw new Error('Container capaciteit niet gedefinieerd');
				}

				order.newContainer.description = "";
				order.newContainer.filling = "";
				order.newContainer.licenses = "";

				//updates for alternative containeroptions within tab 4
				$.ajax({
					type: "POST",
					dataType: 'json',
					url: "includes/processors/containercalculator.php",
					data: "action=getProductUpdate&param=" + $.toJSON(order),
					success: function (data) {

						//adjust current object
						$.extend(order.newContainer, data.param.newContainer);
						order.newContainer.selectablecontainers = data.param.newContainer.selectablecontainers;
						privs.showSelectableContainers();
						order.newContainer.selectablewastetypes = data.param.newContainer.selectablewastetypes;
						privs.showSelectableWastetypes();

						//set product html
						var content = data.param.newContainer;
						$(config.productDescription, config.productTabs).html(content.description);
						$(config.productRules, config.productTabs).html(content.filling);
						$(config.productLicenses, config.productTabs).html(content.licenses);

						if ( typeof data.alertMessage !== 'undefined') {
							alert(data.alertMessage);
						}
					},
					error : function (e) {
						throw new Error(e);
					}

				});

				// set html
				$(config.containertypehtml).html(capacity);

				//set product image
				$(config.productImage).find('img').attr('src', imagesrc.replace('order_', 'productImage_'));

				// check if total price can be calculted
				privs.getTotalprice();

				//set active tab
				$('#tabslider').tabs('select',3);

				//prevent default event action
				return false;
			});
			
		},

		setAlternativeVars : function () {

			$(config.altButton).click( function () {
				
				var id = $(this).parents('.inner').attr('id'),
					index = id.slice( (id.lastIndexOf('_') + 1), id.length);
					alt = order.newContainer.alternatives[index];

				$(config.button, config.containerTab).each( function () {
					if (parseInt(this.title) === alt.size) {
						$(this).click();
					}
				});
			});
		},

		showSelectableContainers : function () {
			for(var i = 1; i <= 6; i++) {
				if ( $.isArray(order.newContainer.selectablecontainers) && $.inArray(i, order.newContainer.selectablecontainers) != -1 ) {
					$('#container0' + i + '_select').show();
					$('#container0' + i + '_show').hide();
				}
				else {
					$('#container0' + i + '_select').hide();
					$('#container0' + i + '_show').show();
				}
			}
		},

		showSelectableWastetypes : function () {
			for(var i = 1; i <= 7; i++) {
				if ( $.isArray(order.newContainer.selectablewastetypes) && $.inArray(i, order.newContainer.selectablewastetypes) != -1 ) {
					$('#wastetype0' + i + '_select').css("display", "inline");
					$('#wastetype0' + i + '_show').css("display", "none");
				}
				else {
					$('#wastetype0' + i + '_select').css("display", "none");
					$('#wastetype0' + i + '_show').css("display", "inline");
				}
			}
		}

	};

	return {

		setArticleId : function (articleId) {
			order.articleId = parseInt(articleId);
		},

		//called from flash-event
		setPostalcode : function (postalcode) {

			// update json
			order.newContainer.postalcode = postalcode;

			// setHtml
			postalcode *= 100;
			postalcode += (' - ' + (postalcode + 99));
			$(config.postalcodehtml).html(postalcode);

			// check for a optional total price
			privs.getTotalprice();

			// set active tab
			$('#tabslider').tabs('select',1);

		},

		setWastetype : function (wastetype) {

			// update json
			order.newContainer.wastetype = wastetype;

			// set html
			$(config.wastetypehtml).html(wastetype);

			$.ajax({
				type: "POST",
				dataType: 'json',
				url: "includes/processors/containercalculator.php",
				data: "action=getProductUpdate&param=" + $.toJSON(order),
				success: function (data) {

					//adjust current object
					$.extend(order.newContainer, data.param.newContainer);
					order.newContainer.selectablecontainers = data.param.newContainer.selectablecontainers;
					privs.showSelectableContainers();
					order.newContainer.selectablewastetypes = data.param.newContainer.selectablewastetypes;
					privs.showSelectableWastetypes();

					//set product html
					var content = data.param.newContainer;
					$(config.productDescription, config.productTabs).html(content.description);
					$(config.productRules, config.productTabs).html(content.filling);
					$(config.productLicenses, config.productTabs).html(content.licenses);

					if ( typeof data.alertMessage !== 'undefined') {
						alert(data.alertMessage);
					}

				},
				error : function (e) {
					throw new Error(e);
				}
			});

			// check for a optional total price
			privs.getTotalprice();

			// set active tab
			$('#tabslider').tabs('select',2);

		},

		setContainersize : function (capacity) {

			// update json
			order.newContainer.containersize = capacity;

			//updates for alternative containeroptions within tab 4
			$.ajax({
				type: "POST",
				dataType: 'json',
				url: "includes/processors/containercalculator.php",
				data: "action=getProductUpdate&param=" + $.toJSON(order),
				success: function (data) {
					//adjust current object
					$.extend(order.newContainer, data.param.newContainer);
					order.newContainer.selectablecontainers = data.param.newContainer.selectablecontainers;
					privs.showSelectableContainers();
					order.newContainer.selectablewastetypes = data.param.newContainer.selectablewastetypes;
					privs.showSelectableWastetypes();

					if ( typeof data.alertMessage !== 'undefined') {
						alert(data.alertMessage);
					}

				},
				error : function (e) {
					throw new Error(e);
				}
			});

			// set html
			var containertype;
			if ( capacity == 1 )
				containertype = 'Bigbag';
			else
				containertype = capacity + ' m&#179;';
			$(config.containertypehtml).html(containertype);

			// check if total price can be calculted
			privs.getTotalprice();

			// set active tab
			$('#tabslider').tabs('select',3);

		},

		setProductimage: function(image) {

			$(config.productImage).find('img').attr('src', 'uploads/images/' + image);

		},

		setOrgContainer : function (container) {
			$.extend(order.orgContainer, container);
		},

		setNewContainer : function (container) {
			$.extend(order.newContainer, container);
			if ( container.postalcode > 0 )
				this.setPostalcode(container.postalcode);
			if ( container.wastetype != '' )
				this.setWastetype(container.wastetype);
			if ( container.containersize > 0 )
				this.setContainersize(container.containersize);
		},

		//replaces traditional addToBasket function
		placeOrder : function () {

			if (!privs.isEmpty(order.newContainer.postalcode) && !privs.isEmpty(order.newContainer.wastetype)
				&& !privs.isEmpty(order.newContainer.containersize)){

				$.ajax({
					type: "POST",
					dataType: 'json',
					url: "includes/processors/containercalculator.php",
					data: "action=getArticleId&param=" + $.toJSON(order) + "&orgid=" + $("#orgid").val(),
					success: function (data) {
						var articleid = data.articleId;
						var orgid = $("#orgid").val();
						var index = 0;
						if ( typeof order.newContainer.index !== 'undefined' ) index = order.newContainer.index;
						addToBasket(articleid, 0, 0, orgid, 1, index);
					},
					error : function (e) {
						throw new Error(e);
					}
				});

			} else {
				return;
			}
		},

		initiate : function () {
			privs.setWastetype();
			privs.setContainersize();
			privs.showSelectableContainers();
			privs.showSelectableWastetypes();
		}

	};

})(jQuery);

//triggered on body.onload
jQuery(function () {tabslider.initiate()});
