//ajustar anchocontenedor slider
function anchoSlider() {
	var slides = $('#sliderProductos .scrollable .items').find('> div').length;
	var slides = slides + 1;
	var ancho = slides * 725;
	var anchopix = ancho+'px';
	
	$('#sliderProductos .scrollable .items').css('width',anchopix);
	
}


$('document').ready(function(){
	
		
	//Funcion para mostrar u ocultar el texto "Search..."
	$('#searchInput')
		.focus( function() {
			 if($(this).attr('value') == 'Search...' ) { $(this).attr('value',''); }
		}) 
		.blur( function() {
			if($(this).attr('value') == '') { $(this).attr('value','Search...'); } 
	});
	


	

	//Hover Menú
	
	$('#mainNav > li > a').hover(function(){
	
		var contenido = $(this).text();
		
		//$(this).append('<span></span>');

		$(this).find('> span')
			.text(contenido)
			.fadeIn(300);
			
	}, function() {
	
		$(this).find('> span')
			.fadeOut(200, function(){
				$(this).remove();
			});
	});
	
	
	$('#mainNav li').hover(function(){
		$(this).find('> ul, > div').fadeIn(300);
	}, function() {
		$(this).find('> ul, > div').fadeOut(300);
	});
	
	
	$('#mainNav li ul li:only-child').parent().append('<li><a href="#"> </a></li>');

	
   /*	$('#mainNav ul.marcas li a img').hover(function(){
		var src = $(this).attr('src').replace('-grey','');
		$(this).attr('src',src);
	},function(){
		var src = $(this).attr('src').replace('.jpg','-grey.jpg');
		$(this).attr('src',src);
	});
	         */
	

	
	
	//Mostrar submenu sidebar
	$('.sidebar ul li:has(ul) > a ').click(function(e){
		$(this).parent().find('ul').slideToggle(300);
		e.preventDefault();
	});
	
	
	//Documentos relacionados
	$('#docRel > li > a').click(function(e){
		$(this).parent().find('> ul').slideToggle(300);
		if($(this).parent().hasClass('open')){
			$(this).parent().removeClass('open');	
		} else {
			$(this).parent().addClass('open');
		};
		e.preventDefault();
	});




	/*Hover modulo
	$('#noticias .modulo a:has(img)').append('<span class="moduloImg"></span>');
	
	$('#noticias .modulo .textos .titulo, #noticias .modulo .textos .ver').hover(function(){
		$(this).parent().parent().find('.moduloImg').fadeIn(300);
	}, function(){
		$(this).parent().parent().find('.moduloImg').fadeOut(300);
	});
	
	$('#noticias .modulo a:has(img)').hover(function(){
		$(this).parent().find('.moduloImg').fadeIn(300);
	}, function(){
		$(this).parent().find('.moduloImg').fadeOut(300);
	});
	        */





	
	
		//ACCORDION BUTTON ACTION (ON CLICK DO THE FOLLOWING)
	$('.accordion .modulo a.titulo').click(function() {

		//REMOVE THE ON CLASS FROM ALL BUTTONS
		$('.accordion .modulo a.titulo').removeClass('on');
		  
		//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
	 	$('.accordion .modulo .contenedor_mapas').slideUp('normal');
			//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
			
		
		

   
   
		//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
		if($(this).next().is(':hidden') == true) {
			
			//ADD THE ON CLASS TO THE BUTTON
			$(this).addClass('on');
			  
			//OPEN THE SLIDE
			$(this).next().slideDown('normal');
		 } 
		  
	 });
	  



	//CORRECCION SLIDER
	
	
	//Slider Productos
	anchoSlider();	
	
	$(".scrollable").scrollable({
		circular: true
	}).autoscroll({ 
		autoplay: true,
		interval: 4000
	}).navigator();
	
	
	/*
	//Slider Productos
	anchoSlider();	
	
	var root = $(".scrollable").scrollable({circular: true}).autoscroll({ autoplay: true, interval: 4000 }).navigator();
	
	window.api = root.data("scrollable");
	
	$('#sliderProductos .navi a').click(function(){
		window.api.stop();
	});

*/


	//Listado productos 
	$('#listadoProductos .modulo a:has(img)').append('<span class="productImg"></span>');
	
	$('#listadoProductos .modulo').hover(function(){
		$(this).find('.productImg').stop().fadeIn(500);
	}, function(){
		$(this).find('.productImg').stop().fadeOut(500);
	});




	// pestañas
	$("ul.tabs").tabs("div.panes > div", {effect: 'slide'});	
	
	$('a#irGaleria').click(function(e){
		
		var api = $("ul.tabs").data('tabs');
		api.click(3);
		e.preventDefault();
	});
	



	//Slider producto detalle galeria
	$('#sliderGaleriaMin').tabs('#sliderGaleria > img', {
		effect: 'fade', 
		fadeOutSpeed: 0, 
		rotate: true
	}).slideshow({
		autoplay: false,
		interval: 2
	});
	
	
	
	//Contacto
	$('#cotizacionBtn').click(function(e){
		$('#cotizacion').slideDown(400);
		$('#sugerencias').slideUp(400);
	});
	
	$('#sugerenciasBtn').click(function(e){
		$('#cotizacion').slideUp(400);
		$('#sugerencias').slideDown(400);
	});
	
	//Buscar Productos
	$('#buscarProductoBtn').click(function(e){
		$('#buscarProducto').slideToggle();
		e.preventDefault();
	});
	
	//Custom select
    $('select.style').customStyle();
	
	
});
