	
	var Tools = new Tools();	
	
	
	// initalize the db and call construct functions
	init().done(function(){
		 //loadDemoDb();
		  switch($.querystring().action) {
				case "categories":
					getAllCategory();
				break;
				case "shoplists":
					getAllShopLists();
					break;	
				case "shoplists":
					goShopping();
					break;
				case "aboutus":
					aboutus();
					break;	
				default:
					goShopping();
			}
		  
	});
	
	
	

	
	
	function getAllCategory() {
		$("#maintitle").html("Edit categories");
	 	shopit.indexedDB.getAllDataFrom('categories').done(function(){
	 		var i = 0;
	 		$("#list-of-categories").html("");
	 		$("#list-of-categories").append(Tools.getListItem("Categories"));
	 		$("#list-of-categories").append(Tools.getListItem("<input type='text' placeholder='New category name' id='newcategory' /><button id='addnew'>Add new</button>"));
	 		
	  		shopit.indexedDB.ret['categories'].sort(compare).forEach(function(item){
	  			$("#list-of-categories").append(Tools.getListItem("<div class='title'>"+item.name+"</div><div class='delete closed' id_category='"+item.id+"'><span class='glyphicon glyphicon-trash'></span> <span class='buttonname'></span></div>"));
	  			i++;
	  		});
	  		if(i==0) $("#list-of-categories").append(Tools.getListItem("<div class='title'>No category yet</div>"));
	  		 
	  		$(".title").click(function(){
	  		 	if($(this).parent().children(".delete").hasClass("opened"))
	  		 	{
	  		 		$(".delete").children(".buttonname").html("");
	  		 		$(".delete").removeClass('opened');
	  		 		$(".delete").addClass('closed');
	  		 	}
	  		 });
	  		
	  		 $(".delete").click(function(){
	  		 	if($(this).hasClass('closed'))
	  		 	{
	  		 		$(".delete").children(".buttonname").html("");
	  		 		$(".delete").removeClass('opened');
	  		 		$(".delete").addClass('closed');
	  		 		$(this).removeClass('closed');
	  		 		$(this).addClass('opened');
	  		 		$(this).children(".buttonname").html("Remove");
	  		 	}
	  		 	else
	  		 	{
	  		 		remove("categories",parseInt($(this).attr("id_category")));
	  		 		getAllCategory();
	  		 	}
	  		 });
	  		 
	  		
	  		 
	  		 $("#addnew").click(function(){
	  		 	if($("#newcategory").val()!="")
	  		 	{
	  		 		add("categories",{name:$("#newcategory").val()});
	  		 		getAllCategory();
	  		 	}
	  		 });

			
			
	  	});	
	  	
	  	
	 }
	 
	 function getAllShopLists() {
	 	shopit.indexedDB.getAllDataFrom('shoplists').done(function(){
	 		
	 		$("#maintitle").html("Edit the shoplist");
	 		
	 		
	 		var i = 0;
	 		$("#list-of-categories").html("");
	 		$("#list-of-categories").append(Tools.getListItem("Shop list"));
	 		

			var categories = new Array();
			
			shopit.indexedDB.getAllDataFrom('categories').done(function(){
				
			
					var list = "<input type='text' placeholder='New product name' id='nextProduct' /><input type='text' placeholder='Quantity' id='quantity' />";
					list += "<select id='selectedCategory'><option value='-1'>-- Select a category --</option><option value='0'>Uncategorized</option>";
		 			shopit.indexedDB.ret['categories'].sort(compare).forEach(function(item){
		 				categories[item.id] = item.name;
			  			list = list + "<option value='"+item.id+"'>"+item.name+"</option>";
		  			});
		 			list = list + "<option value='-2'>.. or make a new category >></option></select> <button id='addNewProduct'>Add it to the list!</button>";
		 			$("#list-of-categories").append(Tools.getListItem(list));
			
				
	 			
	 			$("#addNewProduct").click(function(){
		  		 	addNewProduct();
	  			});
	  			
	  			$("#selectedCategory").change(function(){
	  				if($(this).val()==-2) window.location.href = "?action=categories";
	  			});
	  		
	  			function addNewProduct()
	  			{
	  				if($("#nextProduct").val()!="" && $("#quantity").val()!="" && $("#selectedCategory").val()>=0)
		  		 	{
		  		 		add("shoplists",{name:$("#nextProduct").val(), quantity: $("#quantity").val(), id_category:$("#selectedCategory").val(), shopped:0});
		  		 		getAllShopLists();
		  		 	}
		  		 	else
		  		 	{
		  		 		
		  		 		if($("#nextProduct").val()=="") $("#nextProduct").css("border-color","red");
		  		 		else $("#nextProduct").css("border-color","#ccc");
		  		 		
		  		 		if($("#quantity").val()=="") $("#quantity").css("border-color","red");
		  		 		else $("#quantity").css("border-color","#ccc");
		  		 		
		  		 		if($("#selectedCategory").val()=="-1") $("#selectedCategory").css("border-color","red");
		  		 		else $("#selectedCategory").css("border-color","#ccc");
		  		 	}
	  			}
	  			
	  			var currentCategory = 0;
	  			var i = 0;
	  			var uncategorized = new Array();
	  			shopit.indexedDB.ret['shoplists'].sort(categoryCompare).forEach(function(item){
	  				i++;
	  				if(currentCategory != item.id_category)
	  				{
	  					currentCategory = item.id_category;
	  					if(categories[item.id_category]!=null && categories[item.id_category]!=0)
	  					$("#list-of-categories").append(Tools.getCategoryItem(categories[item.id_category]));
	  				}
	  				if(categories[item.id_category]!=null && categories[item.id_category]!=0)
		  			$("#list-of-categories").append(Tools.getListItem(item.name+"<div class='delete closed' id_product='"+item.id+"'><span class='glyphicon glyphicon-trash'></span> <span class='buttonname'></span></div>"));
		  			else
		  			{
		  				uncategorized[item.id] = item; 
		  			}
	  			});
	  			
	  			if(uncategorized.length>0) $("#list-of-categories").append(Tools.getCategoryItem("Uncategorized"));
	  
	  			uncategorized.sort(compare).forEach(function(item){
	  				$("#list-of-categories").append(Tools.getListItem(item.name+"<div class='delete closed' id_product='"+item.id+"'><span class='glyphicon glyphicon-trash'></span> <span class='buttonname'></span></div>"));
	  			});
	  			if(shopit.indexedDB.ret['shoplists'].length==0)  $("#list-of-categories").append(Tools.getListItem("No product added yet"));
	  			
	  			if(shopit.indexedDB.ret['shoplists'].length>0)  $("#list-of-categories").append(Tools.getListItem("<button class='danger' id='deleteTheList'>Delete the list</button>"));
	  			
	  			$("#deleteTheList").click(function(){
	  					shopit.indexedDB.clear("shoplists");
	  					getAllShopLists();
	  			});
	  			
	  			
	  			$(".delete").click(function(){
		  		 	if($(this).hasClass('closed'))
		  		 	{
		  		 		$(".delete").children(".buttonname").html("");
		  		 		$(".delete").removeClass('opened');
		  		 		$(".delete").addClass('closed');
		  		 		$(this).removeClass('closed');
		  		 		$(this).addClass('opened');
		  		 		$(this).children(".buttonname").html("Remove");
		  		 	}
		  		 	else
		  		 	{
		  		 		remove("shoplists",parseInt($(this).attr("id_product")));
		  		 		getAllShopLists();
		  		 	}
	  			 });
	 			
	 			});
			});	
	 	}
	 	
	 	
	 	function goShopping()
		{
			shopit.indexedDB.getAllDataFrom('shoplists').done(function(){
	 		
		 		$("#maintitle").html("Let's go shopping..");
		 	
		 		
		 		var currentCategory = 0;
	  			var i = 0;
	  			var uncategorized = new Array();
	  			var categories = new Array();
	  			var products = new Array();
	  			
		 		$("#list-of-categories").html("");
		 		
		 		shopit.indexedDB.getAllDataFrom('categories').done(function(){
		 			shopit.indexedDB.ret['categories'].sort(compare).forEach(function(item){
		 				categories[item.id] = item.name;
		  			});
		  			
		  			shopit.indexedDB.ret['shoplists'].sort(categoryCompare).forEach(function(item){
		  				products[item.id] = item;
		  				i++;
		  				if(currentCategory != item.id_category)
		  				{
		  					currentCategory = item.id_category;
		  					if(categories[item.id_category]!=null && categories[item.id_category]!=0)
		  					$("#list-of-categories").append(Tools.getCategoryItem(categories[item.id_category]));
		  				}
		  				var addClass = "";
		  				if(item.shopped==1) addClass="shoppedItem";
		  				if(categories[item.id_category]!=null && categories[item.id_category]!=0)
			  			$("#list-of-categories").append(Tools.getInteractiveListItem('<div class="productCheckBox'+addClass+'"><span class="glyphicon glyphicon-ok"></span></div><span class="pname">'+item.name+' ('+item.quantity+')</span>',item.id, addClass));
			  			else
			  			{
			  				uncategorized[item.id] = item; 
			  			}
	  				});
	  				

		  			if(uncategorized.length>0) $("#list-of-categories").append(Tools.getCategoryItem("Uncategorized"));
		  
		  			uncategorized.sort(compare).forEach(function(item){
		  				var addClass = "";
		  				if(item.shopped==1) addClass="shoppedItem";
		  				$("#list-of-categories").append(Tools.getInteractiveListItem('<div class="productCheckBox'+addClass+'"><span class="glyphicon glyphicon-ok"></span></div><span class="pname">'+item.name+' ('+item.quantity+')</span>',item.id, addClass));
		  			});
		  			if(shopit.indexedDB.ret['shoplists'].length==0)
		  			{
		  				 $("#list-of-categories").append(Tools.getListItem("No product added yet"));
		  				  $("#list-of-categories").append(Tools.getListItem("<button id='redirectToAddProducts'>Click here to add products</button>"));
		  			}
		  			
		  			$("#redirectToAddProducts").click(function(){
		  					window.location.href = "?action=shoplists";
		  			});
		  			
		 			$(".interactive-list-item").click(function(){
		  						if(products[$(this).attr("identifier")].shopped==0) products[$(this).attr("identifier")].shopped = 1;
		  						else products[$(this).attr("identifier")].shopped = 0;
		  						
		  		
		  						
		  						var request = update("shoplists",products[$(this).attr("identifier")], goShopping);
		  						
		  					
		  			});
		  			
		 		});
		 	
		 	
		 });
		}
		
		function aboutus()
		{
			 $("#maintitle").html("About us");
			 $("#list-of-categories").html("");
			 
			 $("#list-of-categories").append(Tools.getListItem("Made by: Tamas Szegedi, Óbudai Egyetem - Firefox OS kurzus"));
		}
			

	
	
		
	 

	
	
