// Custom Javascript

jQuery(document).ready(function() {
	
	// Secondary Navigation
	jQuery("ul.sf-menu").superfish({
		delay: 500,
		animation: {opacity:'show'},
		speed: 200,
		autoArrows: false,
		dropShadows: false
	}); 
	
	// Main Navigation
	jQuery('#nav ul.sf-menu').superfish({ 
		delay: 200,
		animation: {opacity:'show', height:'show'},
		speed: 'fast',
		autoArrows: false,
		dropShadows: false
	});


	
	// Tab Widget
	jQuery(".tabs").tabs({ 
		fx: { opacity: 'toggle' } 
	});
	
	// Input focus css change
	jQuery('input').focus( 
		function () {        
			if ($(this).attr('id') !== 'lupa') { 
				jQuery(this).css('border', '1px solid #d2d2d2');
				jQuery(this).css('colo	r', '#444444');  
			}
		}
	);
	
	// Input blur css change
	jQuery('input').blur( 
		function () {  
			if ($(this).attr('id') !== 'lupa') {
				jQuery(this).css({
					border: '1px solid #e0e0e0',
					color: '#999999'
				});  
			}          
		}
	);
	
	// Submit buttons
	jQuery('#respond input.btn').hover( 
		function () {
			jQuery(this).css({background: '#fff'});
		}, function () {
			jQuery(this).css({background: 'url(images/button_bg.gif) repeat-x'});
		}
	);   
	
	$('#lupa').click(function(){
		$('#buscador').submit();
	});
	
	
	$('a[href="#footer"]').smoothScroll();
	
	function mostrar_error_login() {
		$('#pedido').hide();
		$('.boton_compra').html('<p class="consejo">Para hacer un pedido tiene que estar registrado, <a href="' + base_url + 'alta/">haga click aqui</a> para darse de alta.</p>');
	}
	
	qtipBolsa = $('#bolsa').qtip({
      // content: $('#bolsaList'),
      content: {
          url: base_url+'proceso_bolsa/ver_bolsa',
          prerender: true
      },
      api: {
          beforeShow: function()  {
						this.loadContent(base_url+'proceso_bolsa/ver_bolsa', null, 'GET');
          },
          onContentUpdate: function() {
						var l = $('#bolsaList li').length;
						if (l === 0) {
							$('#numArticulosBolsa').text('0');
						} else {
							$('#numArticulosBolsa').text($('#bolsaList li').length);
						}
					
          }
      },
      show: { 
          effect: {
              type: 'slide',
              length: 300
          }, 
          when: { 
              event: 'mouseover' 
          } 
      },
      hide: {
    
          fixed: true
      },
      style: { 
          name: 'green',
          tip: 'topMiddle',
          border: {
              width: 3,
              radius: 3
          },
          width: 159
      },
      position: {
          corner: {
              target: 'bottomMiddle',
              tooltip: 'topMiddle'
          }
      }
  });

	anadir = function (boton) {
		var btnPulsado = boton;
		if($(btnPulsado).css('opacity') == '0.5')
			return;
			
		$(btnPulsado).css('opacity', 0.5).css('cursor', 'auto');
			
		var codigo = $(btnPulsado).attr('codigo');
		$.post(base_url+'proceso_bolsa/meter/', { codigo: codigo, cantidad: 1 },
		   function(data) {
		     	$(".qtip").css("display","none");
					$(qtipBolsa).qtip("show");
					$('#numArticulosBolsa').text($('#bolsaList li').length);
		   });
		

		
		$(btnPulsado).css('opacity', 1).css('cursor', 'pointer');
		
		return false;
	};
	
	$('#bolsaList .sacar').live('click', function(){
      var fila = $(this).parent().parent();
      var codigo = $(fila).attr('data-stock_id');
 
			$.post(base_url+'proceso_bolsa/sacar/', { codigo: codigo },
			   function(data) {
			     $(fila).fadeOut();
	          $.get(base_url+'proceso_bolsa/total', function(data) {
	            if(data == 0) 
	              $('.bolsaPagar').fadeOut();
	            $('#bolsaList .total strong').text(data + ' €');  
	          });
			   });
  });
	
	$('#pedido').click(function() {
		if (lo == 1) {
			anadir(this)
		} else {
			mostrar_error_login();
		}
		return false;
	});
	
	$('#mapa img.static').click(function() {
		$('#mapa').html('<iframe width="460" height="341" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=es&amp;geocode=&amp;q=Paseo+de+Madrid,+4,+28660+Boadilla+del+Monte,+Madrid,+Espa%C3%B1a&amp;sll=40.404068355893266,-3.8755415680534497&amp;sspn=0.009976,0.009645&amp;g=Paseo+de+Madrid,+28660+Boadilla+del+Monte,+Madrid,+Comunidad+de+Madrid,+Espa%C3%B1a&amp;ie=UTF8&amp;hq=&amp;hnear=Paseo+de+Madrid,+28660+Boadilla+del+Monte,+Madrid,+Comunidad+de+Madrid,+Espa%C3%B1a&amp;ll=40.404068355893266,-3.8755415680534497&amp;spn=0.002494,0.002411&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=es&amp;geocode=&amp;q=Paseo+de+Madrid,+4,+28660+Boadilla+del+Monte,+Madrid,+Espa%C3%B1a&amp;sll=40.404068355893266,-3.8755415680534497&amp;sspn=0.009976,0.009645&amp;g=Paseo+de+Madrid,+28660+Boadilla+del+Monte,+Madrid,+Comunidad+de+Madrid,+Espa%C3%B1a&amp;ie=UTF8&amp;hq=&amp;hnear=Paseo+de+Madrid,+28660+Boadilla+del+Monte,+Madrid,+Comunidad+de+Madrid,+Espa%C3%B1a&amp;ll=40.404068355893266,-3.8755415680534497&amp;spn=0.002494,0.002411&amp;z=14&amp;iwloc=A" style="color:#444;text-align:left;text-shadow: 0 1px 1px rgba(78, 103, 72, 0.7);">Ver mapa más grande</a></small>');
	});
	
	var busquedaLimpia = false;

	$("#busqueda").focus(function(){
	    if(!busquedaLimpia) {
	        $(this).val('');
	        busquedaLimpia = true;
	    }
	    // Select field contents
	    this.select();
	});
	

  $('#busqueda').bind('keypress', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 13) { //Enter keycode
      $(this).submit();
     }
  });
	


	$('#tagbox').tagdragon({'field':'busqueda','url':base_url+'ajax/articulos/',
      'onSelectedItem': function(val){
        window.location = base_url + 'producto/' + val.codigo + '/' + val.permalink;
      },      
      'onLoadList': function(val){
        $('#lupa').addClass('buscando');
      },      
      'onLoadedList': function(val){
        $('#lupa').removeClass('buscando');
      },
      'max': 30,
      'charMin': 3
    	
	});

	
	
	$('input[data-producto]').focus(function() {
		this.select();
	});
	
	
	$('input[data-producto]').blur(function() {
		if (lo == 1) {
			var codigo = $(this).attr('data-producto'); 
			var cantidad_inicial = $(this).attr('data-cantidad-inicial'); 
			var cantidad = $(this).val(); 
			if (cantidad < 1)
				cantidad = 1;
			var cantidad_final = cantidad - cantidad_inicial;
			$.post(base_url+'proceso_bolsa/meter/', { codigo: codigo, cantidad: cantidad_final },
			   function(data) {
						window.location.reload();
			});	
		}
	});
	
	var provinciaSeleccionada = $('#provincia').attr('data-selected');
  $('#provincia option').removeAttr('selected');
  $('#provincia option[value='+provinciaSeleccionada+']').attr('selected','selected');
  



	
	$('a[href="/legal.html"]').click(function(e) {
		e.preventDefault();
		$('#legal').reveal();
	});

	$('a[href="/privacidad.html"]').click(function(e) {
		e.preventDefault();
		$('#privacidad').reveal();
	});

}); 




