$(document).ready(function() {

	$("#form_search_free").submit(function(){
		return validate_search_free();
	});
	
	$("#form_search_by_product").submit(function(){
		return validate_search_by_product();
	});
	
	$("#form_search_by_production").submit(function(){
		return validate_search_by_production();
	});

	$("#form_search_by_product :input").change(function(){
		var _url = $("#form_search_by_product").attr('action');	
		var options = { 
			url: _url+'?'+Math.random(),
			target: '#count_search_by_product'
		}; 
		if(validate_search_by_product())
		{
			$('#form_search_by_product').ajaxSubmit(options);
		} else {
			$('#count_search_by_product').empty();
		}
	});

	$("#form_search_by_production :input").change(function(){
		var _url = $("#form_search_by_production").attr('action');
		var options = { 
			url: _url+'?'+Math.random(),
			target: '#count_search_by_production'
		}; 
		if(validate_search_by_production())
		{
			$('#form_search_by_production').ajaxSubmit(options);
		} else {
			$('#count_search_by_production').empty();
		}
	});

	/* fancybox */
	$("body.azienda ul.imgs:not(.imgs-3d) li a").fancybox();
	$("ul.fancy li a").fancybox();
	
	/* jQuery TOOLS | TAB */
	$("ul.tabs").tabs("figure.panes > a", {
		rotate: true
	}).slideshow({autoplay: true, interval: 5000});

	/* SLIDER */
	var settings = {
		marginLeft: 0,
		marginRight: 4,
		startPosition: 0
	};
	
	$(".slider").FlowSlider(settings);

 var productsList = $("ul.list-3d li");
	
	$("#product3DFilters input:checkbox").change(function() {
		_this = $(this);
	    _value = _this.attr('value');
	
	    var toShow = new Array()
	    	toHide = new Array(),
	    	checkedProducts = new Array(),
	    	checkedMaterials = new Array(),
	    	checkedStyles = new Array(),
	    	checkedSectors = new Array();
	    
		$("#tags-products input:checked").each(function(i,el){
			checkedProducts.push($(el).attr('value'));
		});
	
		$("#tags-materials input:checked").each(function(i,el){
			checkedMaterials.push($(el).attr('value'));
		});
	
		$("#tags-styles input:checked").each(function(i,el){
			checkedStyles.push($(el).attr('value'));
		});
	
		$("#tags-sectors input:checked").each(function(i,el){
			checkedSectors.push($(el).attr('value'));
		});
	
		productsList.each(function(i,e){
			_current = $(this);
			
			dataProducts = $(this).data('products').toString().split(",");
			dataMaterials = $(this).data('materials').toString().split(",");
			dataStyles = $(this).data('styles').toString().split(",");
			dataSectors = $(this).data('sectors').toString().split(",");
			
			statusProducts = false;
			statusMaterials = false;
			statusStyles = false;
			statusSectors = false;
	
			statusProducts = getStatus(statusProducts,checkedProducts,dataProducts);
			statusMaterials = getStatus(statusMaterials,checkedMaterials,dataMaterials);
			statusStyles = getStatus(statusStyles,checkedStyles,dataStyles);
			statusSectors = getStatus(statusSectors,checkedSectors,dataSectors);
	
			if(statusProducts && statusMaterials && statusStyles && statusSectors){
				toShow.push(_current);
			} else{
				toHide.push(_current);
			}
		});
		for (var i = 0; i < toHide.length; i++) {
				toHide[i].hide('fast');
		}
		for (var i = 0; i < toShow.length; i++) {
				toShow[i].show('fast');
		}
	
	});

	// welcomedesck
	
	$("#toggleTos").fancybox();
	/*
	$("#toggleTos").click(function(e){
		e.preventDefault();
		$(".tos").slideToggle();
	});
	*/

});

function getStatus(status,checked,data){
	if(checked.length == 0){
		status = true;
	} else {
		for (var i = 0; i < checked.length; i++) {
			if(jQuery.inArray(checked[i], data) !== -1)
			{
				status = true;
				break;
			}
		}
	}
	
	return status;
}

function validate_search_free(){
	if($("#CompanyQ").val()){
		return true;
	} else {
		return false;
	}
}

function validate_search_by_product(){
	if($("#CompanyProduct").val() || $("#CompanyMaterial").val() || $("#CompanyStyle").val() || $("#CompanySector").val()){
		return true;
	} else {
		return false;
	}
}

function validate_search_by_production(){
	if($("#CompanyStage").val() || $("#CompanyComuneId").val()){
		return true;
	} else {
		return false;
	}
}

