	
	setTimeout(function() {
	
		jQuery('[module_for=tickets], [module_for=digitalDownloads]').each(function() {
	
			qty = jQuery(this).find('input[type=number]').val();
		
			jQuery(this).find('input[type=number]').hide().parent().append(qty);
	
		});
		
	}, 3000);
	
	var baseUrl = 'assets/ecommerce/data_handler_ajax.php';
	var App = angular.module('onlineShop', ['ngDialog','ngSanitize']);

	//SERVICE TO trigger cart items update
	//Watches for changes in this.itemCount and loads json on change
	App.service('ShoppingList', function() {
		this.itemCount = 0;
		this.addQty = function (qty) { 
			this.itemCount  = this.itemCount + qty;
			return this.itemCount;
		};
	});

	/* SERVICE TO trigger client info update
	 * Watches for changes in this.updated and loads json on change
	 * Gets changed when client fills in info in product */
	 App.service('registeredList', function() {
	 	this.updated = 0;
	 	this.update_info = function (updateItem) { 
	 		this.updated = this.updated + updateItem;
	 		return this.updated;
	 	};
	 });


	/******Catogory Controller used in display_files/button.php ***********/
	 App.controller('categoryControler', function($scope, $http, ShoppingList) {
	 	$scope.added = '';
	 	$scope.pricing = '';
	 	$scope.itemQty = 1;
	 	$scope.categorySelected = '';
	 	$scope.hide_qty = hide_qty;

	 	/* GETS DATA FROM eccommerce_display.php function button_json() that gets called by data_handler.php  */
	 	//console.log(baseUrl + '?display=button&item_id=' + button_id + '&module=' + current_module);
		
		
		$http({
		    method: 'GET',
		    url: baseUrl + '?display=button&item_id=' + button_id + '&module=' + current_module
		    
		 }).then(function (response){

		    $scope.item = response.data;
		    console.log($scope.item);

		    $scope.selectedItem = 0;
		    //if item has variations
		    if($scope.item.categories != 'none' ) {

			    //Set default option to 1st one as well as default price
			    $scope.pricing = $scope.item.categories[0].price;
			    $scope.categorySelected = $scope.item.categories[0].name;

			    //if item has more than 7 it will display in a dropdown. The first one must be selected by default
			    if($scope.item.categories.length > 7 ) {
				    $scope.dropdown_button= $scope.item.categories[0];
			    }
		    }
		    else {
			    //Set price as normal price
			    $scope.pricing = $scope.item.price;
		    }
			
		 },function (error){
		 
		    console.log('error line 93');
		    
		});




	 	//This will change the price and variation according to the option they choose when options are in button form
	 	$scope.changeValue = function(value,itemIndex,catName) {
	 		$scope.pricing = value;
	 		$scope.selectedItem = itemIndex;
	 		$scope.categorySelected = catName;
	 	}

	 	//An html decode function to decode items that are displayed inside the dropdowns
	 	$scope.htmlDecode = function(text){ 
	 		var e = document.createElement('div'); 
	 		e.innerHTML = text; 
	 		return e.childNodes[0].nodeValue; 
	 	}

	 	//This will change the price and variation according to the option they choose when options are in dropdown form
	 	$scope.selectChange = function(price,name) {
	 		$scope.pricing = price;
	 		$scope.categorySelected = name;
	 	}

	 	$scope.addToCart = function(el) {
	 		
	 		//Calls float fucntion that takes the item to the basket
	 		if($('#cart_sidebar').length !=0 ) {
	 			itemFloat(el);
	 		}
	 		
	 		var url = baseUrl + '?action=add&module=' + current_module + '&item_id=' + $scope.item.item_id + "&qty=" + $scope.itemQty;
	 		
	 		//If an option is selected append it to url
	 		if($scope.categorySelected  != '' || $scope.categorySelected  != 'undefined' ) {
	 			
	 			url +="&option=" + encodeURIComponent($scope.categorySelected);
	 		}
			//Send query as ajax

			//console.log(url);
			
			
			$http({
			    method: 'GET',
			    url: url

			 }).then(function (response){

			    //Set text to 'Items Added'
	 			$scope.added = $scope.itemQty + " Added to " + $scope.item.cart_name;
	 			
	 			//Trigger cart reload
	 			ShoppingList.addQty($scope.itemQty);

			 },function (error){

			    console.log('error line 155');

			});


		
	 	}

	 });


	/******Cart Controller used in display_files/cart.php ***********/
App.controller('cartController', function($scope ,$http, ShoppingList) {
	
	//Set up defaults.
	$scope.total_items = '';
	$scope.total_price = '';
	$scope.shoplist = '';
	$scope.shoplist.cart_name = "Shopping Cart";
	$scope.shoplist.terms_and_conditions = "";
	$scope.shoplist.terms_and_conditions_submit_disabled = "false";
	$scope.toggle = false;

	//Visibility fix. It will make the sidebar color the websites back color if it is white/transparent
	if($('#cart_sidebar').length !=0) {
		var back_cart_color= $('#cart_sidebar').css('background-color');
		if(back_cart_color =='rgba(0, 0, 0, 0)')
		{
			var body_back = $('body').css('background-color');
			$('#cart_sidebar').css('background-color',body_back);
		}
	}
	$http({
	    method: 'GET',
	    url: baseUrl + '?display=cart'

	 }).then(function (response){

	    $scope.shoplist = response.data;
		$scope.total_items = $scope.shoplist.shop_total_items;
		$scope.total_price = $scope.shoplist.shop_total_price;

	 },function (error){

	    console.log('error line 155');

	});
		
	//Set Data & Totals
	//This watches ShoppingList.itemCount and if it sees that the count has changed it will refresh the cart
	$scope.$watch( function () { return ShoppingList.itemCount; }, function ( itemCounter ) {
		$http({
		    method: 'GET',
		    url: baseUrl + '?display=cart'

		 }).then(function (response){

		    $scope.shoplist = response.data;
				//console.log(baseUrl + '?display=cart');
				//console.log($scope.shoplist );
				$scope.total_items = $scope.shoplist.shop_total_items;
				$scope.total_price = $scope.shoplist.shop_total_price;
		 },function (error){

		    console.log('error line 218');

		});
	
	
	
	});

	
	//Simply toggles the preview if the cart displays at the top of the content
	$scope.togglePreview = function() {
		if($scope.toggle == false) {
			$scope.toggle = true;
		}
		else{
			$scope.toggle = false;
		}
	}

	//function that removes item from cart.
	$scope.remove_item = function(item_id,module,option) {
		url = baseUrl + '?action=update&module=' + module + '&item_id=' + item_id + '&qty=all';
		
		//If the item has options then append option to url
		if(option  != '' || option  != 'undefined') {
			url +="&option=" + encodeURIComponent(option);
		}
		
		if(module == 'tickets') {
	
			ticketUrl = 'assets/tickets/store_qty.php?update_qty=true&item_id='+item_id+'&qty=0';
			
			$http({
			    method: 'GET',
			    url: ticketUrl

			 }).then(function (response){

			 },function (error){

			    console.log('error line 258');

			});
		
	
		}

			$http({
			    method: 'GET',
			    url: url

			 }).then(function (response){

			    ShoppingList.addQty(1);
			    
			 },function (error){

			    console.log('error line 275');

			});
			
			
		}
	});

/******quickview Button  controller used in display_files/quick view.php ***********/
App.controller('quick_view_button', function($scope ,$http, ShoppingList,ngDialog) {
	$scope.item_id = 0;


	//function that opens quickview popup.
	$scope.open_quick_view = function (item_id,module,hide_qty) {
		$scope.module = module;
		$scope.hide_qty = hide_qty;
		
		
		
		
		
		
		$http({
		    method: 'GET',
		    url: baseUrl + '?display=quick_view&item_id=' + item_id + '&module=' + module

		 }).then(function (response){

		    $scope.item = response.data;
			ngDialog.open({
				template: 'quick_view_template',
				controller: 'quick_view_button_controller',
				className: 'ngdialog-theme-default',
				scope: $scope //Gets item details and sends it through to the quickview controller
			});
		 },function (error){

		    console.log('error line 300');

		});
	};
});

/******button that displays when quickview is not in use. used in display_files/quick_button.php ***********/
App.controller('quick_add_button', function($scope ,$http, ShoppingList) {
			$scope.init = function(id, module)
			{
				$scope.item_init_id = id;
				$scope.item_module = module; 
				
				var sendUrl = baseUrl + '?display=button&item_id=' + $scope.item_init_id+ '&module=' + $scope.item_module;
				
				//Gets data for the button 
				$http({
				    method: 'GET',
				    url: sendUrl

				 }).then(function (response){

				   $scope.item = response.data;
				    $scope.pricing = $scope.item.price;

				 },function (error){ console.log('error line 317'); });
		
				

			};

			$scope.added = '';

			//function to add item to cart
			$scope.addToCart = function(el) {

				//Displays floating icon
				if($('#cart_sidebar').length !=0 ) {
						itemFloat(el)
				}

				var url = baseUrl + '?action=add&module=' + $scope.item_module + '&item_id=' + $scope.item_init_id + "&qty=1";
			 //Sends add to cart ajax
			 
			    $http({
				    method: 'GET',
				    url: url

				 }).then(function (response){

				  $scope.added = "1 Added to " + $scope.item.cart_name;
			 		//trigger refresh cart
			 		ShoppingList.addQty(1);

				 },function (error){ console.log('error line 347'); });
				 
				 
			 }

	});

/******controller that gets used in the popup : javascript.php ***********/
App.controller('quick_view_button_controller', function($scope ,$http, ShoppingList) {


	var body_back = $('body').css('background-color');
	$('.ngdialog-content').css('background-color',body_back);
	var ng_back = $('.ngdialog-content').css('background-color');
	var text_color = $('.ngdialog-content').css('color');
	$('.ngdialog-content').css('border-color',text_color);
	$('.ngdialog-content').css('border-width','3px');
	$('.ngdialog-content').css('border-style','solid');

	$scope.pricing = '';
	$scope.itemQty = 1;
	$scope.added = '';
	$scope.selectedItem = 0;

	//if item has variations
		if($scope.item.categories != 'none' ) {
			//Set default option to 1st one as well as default price
			$scope.pricing = $scope.item.categories[0].price;
			$scope.categorySelected = $scope.item.categories[0].name;

			//if item has more than 7 it will display in a dropdown. The first one must be selected by default
			if($scope.item.categories.length > 7 ) {
				$scope.dropdown= $scope.item.categories[0];
			}
		}
		else {
			//Set price as normal price
			$scope.pricing = $scope.item.price;
		}

		//This will change the price and variation according to the option they choose when options are in button form
		$scope.changeValue = function(value,itemIndex,catName) {
			$scope.pricing = value;
			$scope.selectedItem = itemIndex;
			$scope.categorySelected = catName;
		}

			//An html decode function to decode items that are displayed inside the dropdowns
		$scope.htmlDecode = function(text){ 
			var e = document.createElement('div'); 
			e.innerHTML = text; 
			return e.childNodes[0].nodeValue; 
		}

		//This will change the price and variation according to the option they choose when options are in dropdown form
		$scope.selectChange = function(price,name)  {

			$scope.pricing = price;
			$scope.categorySelected = name;
		}

		$scope.addToCart = function(el) {
		//Calls float fucntion that takes the item to the basket
			if($('#cart_sidebar').length !=0) {
					itemFloat(el)

				}

			$scope.added = '';
			var url = baseUrl + '?action=add&module='+ $scope.module +'&item_id=' + $scope.item.item_id + "&qty=" + $scope.itemQty;

			//If an option is selected append it to url
			if($scope.categorySelected  != '' &&  typeof $scope.categorySelected  !== 'undefined') {
				url +="&option=" + encodeURIComponent($scope.categorySelected)
			}
			
			
			$http({
			    method: 'GET',
			    url: url

			 }).then(function (response){

				  //Set text to 'Items Added'
				$scope.added = $scope.itemQty + " Added to " + $scope.item.cart_name;
				//Trigger cart reload
				ShoppingList.addQty($scope.itemQty);

			    },function (error){ console.log('error line 434'); });
				 
				 
		}
	});


App.controller('cartCheckoutController', function($scope ,$http, ShoppingList,registeredList, ngDialog ) {
	$scope.total_items = '';
	$scope.total_price = '';
	$scope.toggle = false;
	$scope.checkout_step = 'basket';
	$scope.client_details = null;
	$scope.terms_text = 'empty';
	$scope.found_entry = false;
	$scope.shipping_index = 0;
	$scope.$watch( function () { return ShoppingList.itemCount; }, function ( itemCounter ) {
	
	    $http({
		method: 'GET',
		url: baseUrl + '?display=cart&add_shipping=true'

	     }).then(function (response){

		      $scope.shoplist = response.data;
			//console.log(data);
			$scope.total_items = $scope.shoplist.shop_total_items;
			$scope.total_price = $scope.shoplist.shop_total_price;

			$scope.initial_total = $scope.total_price ;

			if($scope.shoplist.shipping_options != 'false')
			{

				$scope.shipping_rate_basket = $scope.shoplist.shipping_options[$scope.shipping_index];
				$scope.total_price = parseFloat($scope.initial_total) + parseFloat($scope.shipping_rate_basket.shipping_price);
				$scope.total_price = $scope.total_price.toFixed(2);
			}

		},function (error){ console.log('error line 434'); });

	});

	$scope.update_item = function(item_id,module,option,quantity) {
	
	
	console.log(baseUrl + '?action=update&module=' + module + '&item_id=' + item_id + '&qty=' + quantity+'-update');
	
		url = baseUrl + '?action=update&module=' + module + '&item_id=' + item_id + '&qty=' + quantity;
		if($scope.categorySelected  != '' || $scope.categorySelected  != 'undefined') {
			url +="&option=" + encodeURIComponent(option);
		}
		
		if(module == 'tickets') {
		
			ticketUrl = 'assets/tickets/store_qty.php?update_qty=true&item_id='+item_id+'&qty='+quantity;
			

			$http({
				method: 'GET',
				url: ticketUrl

			}).then(function (response){


			   },function (error){ console.log('error line 434'); });
		   
			
		
		}
		
		if(quantity != null) {
		
		    $http({
			    method: 'GET',
			    url: url

		    }).then(function (response){

			     ShoppingList.addQty(1);

		       },function (error){ console.log('error line 434'); });
		       
		} else {
		
		    console.log('quantity is NULL'); 
		}

	}



	$scope.add_shipping = function() {

		$scope.shipping_index = $('.shop_text_box')[0].selectedIndex;
		$scope.total_price = parseFloat($scope.initial_total) + parseFloat($scope.shipping_rate_basket.shipping_price);
		$scope.total_price = $scope.total_price.toFixed(2);

	}
	
	$scope.remove_item = function(item_id,module,option) {

	    console.log(baseUrl + '?action=update&module=' + module + '&item_id=' + item_id + '&qty=all');
	    
		url = baseUrl + '?action=update&module=' + module + '&item_id=' + item_id + '&qty=all';
		if(option  != '' || option != 'undefined') {
			url +="&option=" +encodeURIComponent(option);
		}
		
		$http({
			method: 'GET',
			url: url

		}).then(function (response){

			 ShoppingList.addQty(1);

		   },function (error){ console.log('error line 434'); });

		  
		
		if(module == 'tickets') {
	
			ticketUrl = 'assets/tickets/store_qty.php?update_qty=true&item_id='+item_id+'&qty=0';
		
			
			$http({
				method: 'GET',
				url: ticketUrl

			}).then(function (response){

			
			   },function (error){ console.log('error line 434'); });

	
		}
			
	}
	
	$scope.add_coupon = function() {

		url = baseUrl + '?action=add_coupon&module=coupon&coupon='+$('.coupon_textbox').val();
		
		$http({
			method: 'GET',
			url: url

		}).then(function (response){

			 ShoppingList.addQty(1);

		   },function (error){ console.log('error line 434'); });

		
		
	}

		$scope.change_step = function(step) {

			$scope.checkout_step = step;

			if(step =='form') {

				$scope.update_information();
			}

			if(step =='summary') {
				
				this.submit_data();
				

				if($scope.details.eft_method == true) {
					$scope.payment_method ='eft';
				}
				else if($scope.details.quote_method == true) {
					$scope.payment_method ='quote';
				}
				else if($scope.details.paypal_method == true) {
					$scope.payment_method ='paypal';
				}
				else if($scope.details.payu_method == true) {
					$scope.payment_method ='payu';
				}
				else if($scope.details.payfast_method == true) {
					$scope.payment_method ='payfast';
				}
				else if($scope.details.yoco_method == true) {
					$scope.payment_method ='yoco';
				}
				else if($scope.details.paytoday_method == true) {
					$scope.payment_method ='paytoday';
				}

				var temp_total = parseFloat($scope.total_price);
				if($scope.client_details.client_shipping_name != null) {


					//temp_total += parseFloat($scope.shipping_rate_basket.shipping_price);
				}
				
				if($scope.total_price !='NA') {
					$scope.summary_total = temp_total.toFixed(2);
				}
				else {
					$scope.summary_total = 'NA'
				}
				window.location.hash="backTop"; 
			}

			if(step =='thankyou') {

				if($scope.payment_method =='payfast') {
					window.location='wcheckout.php?method=payfast';
				}
				else if($scope.payment_method =='payu'){
					window.location='wcheckout.php?method=payu';
				}
				else if($scope.payment_method =='paypal'){
					window.location='wcheckout.php?method=paypal';
				}
				else if($scope.payment_method =='yoco'){
					window.location='wcheckout.php?method=yoco';
				}
				else if($scope.payment_method =='paytoday'){
					window.location='wcheckout.php?method=paytoday';
				}
				else {
				
				    $http({
					    method: 'GET',
					    url: baseUrl + '?action=process_sale&payment_method=' + $scope.payment_method

				    }).then(function (response){

					     $http({
						method: 'GET',
						url: baseUrl + '?display=thankyou'

						}).then(function (response2){

							$scope.thankyou_text = response2.data.thankyou_text;


						   },function (error){ console.log('error line 635'); });
				       

				       },function (error){ console.log('error line 638'); });



				}
			}
		}
		
		$scope.check_terms = function() {
		
			if($scope.shoplist.terms_and_conditions != '') {
			
				chcked = $('#terms_agreed:checked').length;
				
				if(chcked) {
				
					$('.shop_button_forward[for=contact]').attr('disabled', false);
				
				} else {
				
					$('.shop_button_forward[for=contact]').attr('disabled', true);
				
				}
			
			}
		
		}
		
		$scope.submit_data = function() {

			if($scope.shipping_rate_basket != null) {
				$scope.client_details.client_shipping = $scope.shipping_rate_basket.shipping_id;
			}
			
			$http({
			    method: 'POST',
			    url: baseUrl + "?action=post_form",
			    data: $.param($scope.client_details),
			    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			}).then(function (response){

				$scope.returned = response.data;


			},function (error){ console.log('error line 722'); });


		}

		$scope.update_information = function() {
			
		    $http({
			method: 'GET',
			url: baseUrl + '?display=checkout_form'

		    }).then(function (response){

			    $scope.details = response.data;
			    $scope.client_details = $scope.details.client_details;
			    $scope.terms_text = $scope.details.shipping_text;
			    //console.log($scope.details);
			    if($scope.client_details.client_email != null) {
				    $scope.found_entry = true;
			    }

			    if($scope.details.shipping_options != null) {

				    for(var i = 0; i < $scope.details.shipping_options.length; i++) {
					    var obj = $scope.details.shipping_options[i];

					    if(obj.shipping_name == $scope.client_details.client_shipping_name){
						    $scope.shipping_rate = obj;
					    }
				    }

				    if($scope.shoplist.block_shipping == true) {

					    $scope.client_details.client_shipping_name = null;
				    }

				    if($scope.client_details.client_shipping_name == null) {
					    $scope.shipping_rate = null;
				    }

			    }


		    },function (error){ console.log('error line 722'); });



			
		}

		$scope.payment_select = function(payment) {

			$scope.payment_method = payment;

		}

		$scope.item_id = 0;
		$scope.already_registered = function () {

			ngDialog.open({
				template: 'already_registered',
				controller: 'details_controller',
				className: 'ngdialog-theme-default',
				scope: $scope
			});
		};  

		$scope.share_popup = function () {

			ngDialog.open({
				template: 'share_cart',
				controller: 'share_controller',
				className: 'ngdialog-theme-default',
				scope: $scope
			});
		};  


		$scope.$watch( function () { return registeredList.updated; }, function ( itemCounter ) {
			
			if(registeredList.updated != 0) {
				$scope.update_information();
			}
		});

		$scope.check_info = function() {
			
			url = baseUrl + '?action=set_registration&name=' + $scope.client_details.client_name + '&email=' + $scope.client_details.client_email;
			
			$scope.request_url = url;
			
			if($scope.found_entry == false) {
				//alert($scope.client_details.client_email);
				//console.log(url);
				
				$http({
					method: 'GET',
					url: url

				}).then(function (data_sent){

					if(data_sent.valid == true)
					{
						registeredList.update_info(1);
						$scope.found_entry  = true;
					}

				   },function (error){ console.log('error line 786'); });


			}
		};

	});
App.controller('details_controller', function($scope ,$http, registeredList) {

	$scope.check_info = function() {
		url = baseUrl + '?action=set_registration&name=' + $scope.registered_name + '&email=' + $scope.registered_email;
		$scope.request_url = url;
		
		$http({
					method: 'GET',
					url: url

				}).then(function (data_sent){

					registeredList.update_info(1);

				   },function (error){ console.log('error line 807'); });
				   
		
	};
});
App.controller('share_controller', function($scope ,$http, registeredList) {
	
	$scope.client_message = "Hi " + $scope.share_receip_name + ', ' + $scope.share_own_name + "wants to share his Shopping cart with you.";

	$scope.share_cart = function() {
		url = baseUrl + '?action=share&share_email=' + $scope.share_email + '&share_receip_name=' + $scope.share_receip_name + '&share_own_name='+$scope.share_own_name + '&share_message' + $scope.client_message ;
		
		$http({
			method: 'GET',
			url: url

		}).then(function (response){

			$scope.vMessage = response.data.return_msg;

		   },function (error){ console.log('error line 827'); });


	};

});

angular.element(document).ready(function() {
	angular.bootstrap(document, ['onlineShop']);



});


var itemFloat = function(el) {

	if(el.currentTarget) {

		var pos = $(el.currentTarget).position();
	
	} else {
	
		var pos = $(el).position();
	
	}
	
	var style = "width:40px;height:40px;float:right;position:absolute;top:" + (pos.top) + "px;left:" + (pos.left) + "px;z-index:90000;"; 
	$('body').after('<div align="center" class="mover" style="'+style+'"><img src="assets/ecommerce/icons/cart_icon.png" width="40px" /></div>');
	
	var pos = $('#cart_sidebar').offset();
	
	$('.mover').animate({top: pos.top, left: pos.left}, 800, function() {

		$('#cart_sidebar').animate({right:'0px'}, 400, function() { $('#cart_sidebar').delay(500).animate({right: '-360px'}, 400); });
		$('.mover').remove();
	
	});

}



/* #################################END ECOMMERCE ############################## */


