(function($) {
    $.fn.listenForChange = function(options) {
        settings = $.extend({
            interval: 200 // in microseconds
        }, options);

        var jquery_object = this;
        var current_focus = null;

        jquery_object.filter(":input").add(":input", jquery_object).focus( function() {
            current_focus = this;
        }).blur( function() {
            current_focus = null;
        });

        setInterval(function() {
            // allow
            jquery_object.filter(":input").add(":input", jquery_object).each(function() {
                // set data cache on element to input value if not yet set
                if ($(this).data('change_listener') == undefined) {
                    $(this).data('change_listener', $(this).val());
                    return;
                }
                // return if the value matches the cache
                if ($(this).data('change_listener') == $(this).val()) {
                    return;
                }
                // ignore if element is in focus (since change event will fire on blur)
                if (this == current_focus) {
                    return;
                }
                // if we make it here, manually fire the change event and set the new value
                $(this).trigger('change');
                $(this).data('change_listener', $(this).val());
            });
        }, settings.interval);
        return this;
    };
})(jQuery);

$(function () {
	$("#listTops a:first").click();
	
	$("input[name=chave]").focus( function () {
		if (!$(this).attr("focus")) {
			$(this).attr("focus", $(this).val())
			$(this).val("");
		}
	});
	$("input[name=chave]").blur( function () {
		if (!$(this).val()) {
			$(this).val($(this).attr("focus"));
			$(this).attr("focus", "");
		}
	});
	
});
$(document).ready(
  function() {
    $('.link_cancelar_encomenda').click(
      function() {
        if (confirm('Tem certeza que deseja cancelar a encomenda?')) {
          $.get(
            $(this).attr('href'), 
            function(data) {
              alert(data);
              window.location.reload();
            }
          );
        }
        return false;
      }
    );    
		$('.restringe').submit(
			function() {
				tb_show('','/catalogo/confirma_compra.php?&height=320&width=640', null); 
				return false;
			}
		);
    $(".real_link").click(
      function() {
        window.location.href = $(this).attr('href');
        return false;
      }
    );  
		$('#pesq_codpostal').click(
			function() {
				tb_show('','/plugins/codigospostais/codigos.php?&height=320&width=800');
				return false;
			}
		).focus(
			function() {
			}
		);
    $('.finaliza_list').click(
      function() {
				var thishref = $(this).attr('href');
				$.get(
					'/loja/existe_sem_stock_query.php',
					function(data) {
						if (data != "0") {
							var none=1; 
							$('#close-layer-ecard').click(); 
							tb_show('','/loja/existe_sem_stock.php?&height=480&width=900', null); 
						}
						else {
							window.location.href = thishref;
						}
					}
				);
        return false;
      }
    );
		$('input.txtPass[type="password"]').listenForChange();
		$('input.txtPass[type="password"]').change(
			function() {
				$('.txtPass').parents('.login').find('em').removeClass('txtPassword');
				$('.login em').hide();
			}
		);
  }
);

function initCustomContentFaqsAcc(){
	$('.contentFaqs.customAcc').each(function(){
						  					  
		var accContainer = $(this);							  
		var accTab = accContainer.find('.list > a')						  
		
		accTab.click(function() {
		
		/*var contentListBooksImg = content.find('.contentListBooks ul li a span.bookImg');*/
		//$('.contentListBooks ul').css( 'display' , 'none');
		
		if($(this).next('.top').is('.open')){
			/*contentListBooksImg.removeAttr('style');*/
			$(this).next('.top').slideUp('slow',function(){
			}).removeClass('open');
			accContainer.find('a').removeClass('active');
		}else{
			$(this).next('.top').find('.open').slideUp('slow');
			accContainer.find('a').removeClass('active').next('.top').slideUp('slow').removeClass('open');
			$(this).addClass('active');
			$(this).next('.top').slideDown('slow').addClass('open');
			
			//$(this).next('.top').slideDown('slow',function(){
//				/*contentListBooksImg.css('position','relative');*/
//				//$('.contentListBooks ul').css( 'display' , 'block');
//			}).addClass('open');
		}
		return false;
		}).next('.top').hide(); //fecha todos 
		//.filter(':first').click(); // o primeiro fica aberto
	});
}

$(document).ready(
	function() {
		if($('.contentFaqs.customAcc').length){
			initCustomContentFaqsAcc();
		}	
	}
);
function preenchelocalidade(codigopostal, localidade) {
	$('#realcp1').val(codigopostal);
	$('#reallocalidade').val(localidade);
}
