$(document).ready(function(){
	
		//Set default open/close settings
		$('.acc_produtos').hide(); //Hide/close all containers
		//$('.acc_designacao:first').addClass('active').next().show(); //Add "active" class to first designacao, then show/open the immediate next container
		
		//On Click
		$('.acc_designacao').click(function(){
			if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
				$('.acc_designacao').removeClass('active').next().slideUp(); //Remove all .acc_designacao classes and slide up the immediate next container
				$(this).toggleClass('active').next().slideDown(); //Add .acc_designacao class to clicked trigger and slide down the immediate next container
			}
			return false; //Prevent the browser jump to the link anchor
		});
                 
                 $("a#show-panel").click(function(){
                        $("#lightbox, #lightbox-panel").fadeIn(300);
                 });
                 $("a#close-panel").click(function(){
                        $("#lightbox, #lightbox-panel").fadeOut(300);
                 });
                            
		$(".seleccionar").fancybox({
			onStart		:	function() {
				//return window.confirm('Continue?');
			},
			onCancel	:	function() {
				alert('Cancelado!');
			},
			onComplete	:	function() {
				//alert('Completed!');
			},
			onCleanup	:	function() {
				//return window.confirm('Tem a certeza que pretende fechar?');
			},
			onClosed	:	function() {
				//alert('Closed!');
			}
		});
		
		$(".encomendar").fancybox({
			onStart		:	function() {
				//return window.confirm('Tem a certeza que deseja encomendar os produtos?');
			},
			onCancel	:	function() {
				alert('Cancelado!');
			},
			onComplete	:	function() {
				//alert('Completed!');
			},
			onCleanup	:	function() {
				//return window.confirm('Tem a certeza que pretende cancelar a encomenda dos produtos?');
			},
			onClosed	:	function() {
				//window.location = "/produtos/telhas";
				//alert('Closed!');
			}
		});
		
		$("#googleMaps").fancybox({
			'width'				: '42%',
			'height'			: '67%',
			'autoScale'     	: true,
			'padding'			: 0,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
		$('#encomendar').hide();
		$("#inclinacao").change( function () {
			var valInclinacao = $("option:selected", this).val();
			var valArea = $("#area").val();
						
			if(valArea != '' && valInclinacao != '')
			{
					$('#encomendar').show('slow');
					$.getJSON('/produtos/calcula',{area: valArea,inclinacao: valInclinacao, ajax: 'true'},
					function(j){
					//alert(j);
						for (var i = 0; i < j.length; i++) {
								$('#qtdTTL_'+j[i].produto_id).html(j[i].quantidadeTelhas + '');
								$('#precoTTL_'+j[i].produto_id).html(j[i].precoTelhas + ' €');
							}
						});
			}
			else
			{
				$('#encomendar').hide('slow');
			}
		});
			
		$("#area").blur(function(){
				var valArea = $("#area").val();
				var valInclinacao = $("#inclinacao").val();
						
			if(valArea != '' && valInclinacao != '')
			{	
				$('#encomendar').show('slow');
				$.getJSON('/produtos/calcula',{area: valArea,inclinacao: valInclinacao, ajax: 'true'},
				function(j){
					//alert(j);
					for (var i = 0; i < j.length; i++) {
							$('#qtdTTL_'+j[i].produto_id).html(j[i].quantidadeTelhas + '');
							$('#precoTTL_'+j[i].produto_id).html(j[i].precoTelhas + ' €');
						}
					});
			}
			else
			{
				$('#encomendar').hide('slow');
			}
		});
		
		

		$('#top-categorias').children().hover(function() {
				$(this).siblings().stop().fadeTo(500,0.5);
			}, function() {
				$(this).siblings().stop().fadeTo(500,1);
			});

});
