$(document).ready(function () {

	$('#profilo').hide();	
	$('#firma').hide();
	$('#reset').hide();
	$('#categories').hide();

 	$('#cat_btn').click(function () { 
		$('#categories').slideToggle("slow");
		return false;
    });

	$('#dash_btn').click(function () { 
		$('#dashboard').slideToggle("slow");
		return false;
    });
	
	$('#carrello_img').click(function(){
		if((location.pathname.length) > 20){
			var pathname = location.pathname.substr(0,20);
			location.href = location.protocol + '//'+location.hostname+ ":"+location.port + pathname + "checkout.html";
		}else{
			location.href = location.protocol + '//'+location.hostname+ ":"+location.port + location.pathname + "checkout.html";
		}
	})
	
	$('.choice').click(function(){
		
		id = $(this).attr("id");
		$('#categories').hide();
		$('.grid_16.show_prodotti.cat').html('');
		$('#reset').fadeIn('slow');
		$('#reset').click(function(){
			$('.grid_16.show_prodotti.random').fadeIn("slow");
			$('.grid_16.show_prodotti.cat').fadeOut('slow');
			
			$(this).fadeOut('slow');
		});
		
		switch(id){
			case '1agamma':
				$.getJSON("cgi-bin/object/mostra_index_prodotti.php?getProdotti=1",
				    function(data){
					if(data != null){
					    $.each(data, function(i,item){
							$('.grid_16.show_prodotti.random').fadeOut("slow");						
							
							var div_cont = document.createElement('div');
							div_cont.setAttribute("class", "draggable ui-draggable");
							div_cont.setAttribute("code", data[i].cod_1 +"/"+data[i].cod_2);
							div_cont.setAttribute("price", data[i].prezzo);
							div_cont.setAttribute("id", data[i].nome);
							div_cont.innerHTML = data[i].nome;
							
							var a = document.createElement('a');
							a.setAttribute("class", "fancybox");
							a.setAttribute("title", data[i].descr);
							a.setAttribute("href", data[i].link);
							
							var img = document.createElement('img');
							img.setAttribute("src", data[i].link_thumb);
							
							a.appendChild(img);
							
							var p = document.createElement('p');
							p.setAttribute("class", "description");
							p.innerHTML = data[i].descr;
							
							var input = document.createElement('input');
							input.setAttribute("class", "how_many");
							input.setAttribute("type", "text");
							input.setAttribute("name", "how_many");
							input.setAttribute("value", "");
							
							div_cont.appendChild(a);
							div_cont.appendChild(p);
							div_cont.appendChild(input);
							$(div_cont).draggable({ helper: "clone", opacity: "0.5" });
							$('.grid_16.show_prodotti.cat').append(div_cont);
							
							$('a.fancybox').fancybox();
						})
					}else{
						$('.grid_16.show_prodotti.random').html("<p class=\"red\">Il database di prodotti è vuoto</p>");
					}
				})
			
				break;
				
			case '4agamma':
				$.getJSON("cgi-bin/object/mostra_index_prodotti.php?getProdotti=4",
				    function(data){
					    $.each(data, function(i,item){
							$('.grid_16.show_prodotti.random').fadeOut("slow");
							
							var div_cont = document.createElement('div');
							div_cont.setAttribute("class", "draggable ui-draggable");
							div_cont.setAttribute("code", data[i].cod_1 +"/"+data[i].cod_2);
							div_cont.setAttribute("price", data[i].prezzo);
							div_cont.setAttribute("id", data[i].nome);
							div_cont.innerHTML = data[i].nome;
							
							var a = document.createElement('a');
							a.setAttribute("class", "fancybox");
							a.setAttribute("title", data[i].descr);
							a.setAttribute("href", data[i].link);
							
							var img = document.createElement('img');
							img.setAttribute("src", data[i].link_thumb);
							
							a.appendChild(img);
							
							var p = document.createElement('p');
							p.setAttribute("class", "description");
							p.innerHTML = data[i].descr;
							
							var input = document.createElement('input');
							input.setAttribute("class", "how_many");
							input.setAttribute("type", "text");
							input.setAttribute("name", "how_many");
							input.setAttribute("value", "");
							
							div_cont.appendChild(a);
							div_cont.appendChild(p);
							div_cont.appendChild(input);
							$(div_cont).draggable({ helper: "clone", opacity: "0.5" });
							$('.grid_16.show_prodotti.cat').append(div_cont);
							
							$('a.fancybox').fancybox();
						})
					}
				)
			
				break;
				
			case 'confezionati':
				$.getJSON("cgi-bin/object/mostra_index_prodotti.php?getProdotti=c",
				    function(data){
					    $.each(data, function(i,item){
							$('.grid_16.show_prodotti.random').fadeOut("slow");
							
							var div_cont = document.createElement('div');
							div_cont.setAttribute("class", "draggable ui-draggable");
							div_cont.setAttribute("code", data[i].cod_1 +"/"+data[i].cod_2);
							div_cont.setAttribute("price", data[i].prezzo);
							div_cont.setAttribute("id", data[i].nome);
							div_cont.innerHTML = data[i].nome;
							
							var a = document.createElement('a');
							a.setAttribute("class", "fancybox");
							a.setAttribute("title", data[i].descr);
							a.setAttribute("href", data[i].link);
							
							var img = document.createElement('img');
							img.setAttribute("src", data[i].link_thumb);
							
							a.appendChild(img);
							
							var p = document.createElement('p');
							p.setAttribute("class", "description");
							p.innerHTML = data[i].descr;
							
							var input = document.createElement('input');
							input.setAttribute("class", "how_many");
							input.setAttribute("type", "text");
							input.setAttribute("name", "how_many");
							input.setAttribute("value", "");
							
							div_cont.appendChild(a);
							div_cont.appendChild(p);
							div_cont.appendChild(input);
							$(div_cont).draggable({ helper: "clone", opacity: "0.5" });
							$('.grid_16.show_prodotti.cat').append(div_cont);
							
							$('a.fancybox').fancybox();
						})
					}
				)
			
			
				break;
		}
	})

		$.getJSON("cgi-bin/object/mostra_index_sottpr.php?prodotti=true",
		    function(data){
			    $.each(data, function(i,item){
					var div_cont = document.createElement('div');
					div_cont.setAttribute("class", "draggable ui-draggable");
					div_cont.setAttribute("code", data[i].cod_1 +"/"+data[i].cod_2);
					div_cont.setAttribute("price", data[i].prezzo);
					div_cont.setAttribute("id", data[i].nome);
					div_cont.innerHTML = data[i].nome;
					
					var a = document.createElement('a');
					a.setAttribute("class", "fancybox");
					a.setAttribute("title", data[i].descr);
					a.setAttribute("href", data[i].link);
					
					var img = document.createElement('img');
					img.setAttribute("src", data[i].link_thumb);
					
					a.appendChild(img);
					
					var p = document.createElement('p');
					p.setAttribute("class", "description");
					p.innerHTML = data[i].descr;
					
					var input = document.createElement('input');
					input.setAttribute("class", "how_many");
					input.setAttribute("type", "text");
					input.setAttribute("name", "how_many");
					input.setAttribute("value", "");
					
					div_cont.appendChild(a);
					div_cont.appendChild(p);
					div_cont.appendChild(input);
					$(div_cont).draggable({ helper: "clone", opacity: "0.5" });
					$('.grid_16.show_prodotti.random').append(div_cont);
					
					$('a.fancybox').fancybox();
					
				})
			}
		)

			function mostra(pagina){
				$('.grid_16.show_prodotti.random').html('');
				$.each(conserva, function(i,item){
					$('.grid_16.show_prodotti.random').append(conserva[pagina][i]);					
				});
			}
				

	$('#footer').click(function(){
		$('#firma').toggle("slow");
		return false;
	});
	
		var COOKIE_NAME = top.location.host + "/hashcode";
		var USER = top.location.host + "/user";
		var options = { path: '/', expires: 10 };
							
				if($.cookie(COOKIE_NAME) != null){
				nospazi=$.cookie(USER).replace(/\s+$|^\s+/g,"");
				$.ajax({
			   		type: "POST",
			   		url: "cgi-bin/object/check_login.php",
			   		data: "name=" + nospazi,
			   		success: function(msg){
						if(msg != null || msg != undefined){
							$('#hello').html('Salve, ' + msg);
							$('#hello').hover(function(){
								$(this).html('<a href="#">Log Out</a>');
								$(this).click(function(){
									$.cookie(COOKIE_NAME, null, options);
									location.reload();
									$(this).html('Salve, Utente!');
									$('#toggle').show();
									$('.sep').show();
									$('#profilo').hide();
								})
							},function(){
								$(this).html('Salve, '+ msg);
							}
							)
							$('#non_autenticato').hide();
							$('#toggle').hide();
							$('.sep').hide();
							$('#profilo').show();
   	  					}
					}
				})
				}


	$('#submit_login').click(function(){
		var user = $('#username').val();
		var pwd = $('#pwd').val();
		
		$.ajax({
	   		type: "POST",
	   		url: "cgi-bin/object/login.php",
	   		data: "username=" + user + "&pwd=" +pwd,
	   		success: function(msg){
				if(msg == null || msg == 1){
					alert("non sei un utente registrato.");
				}else if(msg == 2){
					alert("non hai attivato ancora l'account. Controlla la tua mail.");
				}else{
					array = msg.split("-");
					$.cookie(COOKIE_NAME, array[1] , options);	
					$.cookie(USER, array[0] , options);	
					location.reload();
				}
			}
	 	});
		
		
		return false;
	})
	
	
});