/* FIX DEI PNG */
$(function(){
	$(document).pngFix({ blankgif: img_dir+'/pixel.gif' });
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
});

/* SLIDE IN HOMEPAGE */
function setSlider(xmlUrl) {
	var flashvars = {
		sliderXML: xmlUrl
	};
	var params = {
		allowfullscreen: 'true',
		allowscriptaccess: 'sameDomain'
	};
	var attributes = {
		id: 'SLIDER',
		name: 'SLIDER'
	};
	
	swfobject.embedSWF('/contents/instance1/images/slider.swf', 'slider', '870', '80', '9.0.0', false, flashvars, params, attributes);
}

/* FILMATO YOUTUBE */
function youtube(id,W,H,container) {
	var params = {
		allowfullscreen: 'true',
		allowscriptaccess: 'sameDomain',
		wmode: 'transparent'
	};
	var attributes = {
		id: container,
		name: 'YOUTUBE'
	};
	
	swfobject.embedSWF('http://www.youtube.com/v/'+id+'&fmt=22&showinfo=0&fs=1', container, W, H, '9.0.0', false, false, params, attributes);
}

/* VIDEO PLAYER */
function video(contenitore,url,link_url,link_label,w,h,autoplay,videoimg) {

	var flashvars = {
		videoUrl: url,
		linkUrl: link_url,
		linkLabel: link_label,
		autoPlay: autoplay,
		videoImg: videoimg
	}
	
	var params = {
		allowfullscreen: "true",
		allowScriptAccess: "always",
		align: "middle",
		swliveconnect: "true",
		bgcolor: "#ffffff",
		wmode: "transparent"
	};
	
	var attributes = {
		id: contenitore,
		name: contenitore
	};
	
	swfobject.embedSWF(img_dir+'/video-player.swf', contenitore, w, h, '9.0.0', false, flashvars, params, attributes);
}
/* VIDEO PLAYER POPUP NATALIZIO */
function videopopup(contenitore,url,link_url,link_label,w,h,autoplay,videoimg,fullscreen) {

	var flashvars = {
		videoUrl: url,
		linkUrl: link_url,
		linkLabel: link_label,
		autoPlay: autoplay,
		videoImg: videoimg
	}
	
	var params = {
		allowfullscreen: fullscreen,
		allowScriptAccess: "always",
		align: "middle",
		swliveconnect: "true",
		bgcolor: "#ffffff",
		wmode: "transparent"
	};
	
	var attributes = {
		id: contenitore,
		name: contenitore
	};
	
	swfobject.embedSWF(img_dir+'/video-player.swf', contenitore, w, h, '9.0.0', false, flashvars, params, attributes);
}


/* ITALIA CLICCABILE */
function italy(node,xsl,producttype) {
	
	var flashvars = {
		'nodeId': node,
		'xsl': xsl,
		'producttype': producttype
	};
	
	var params = {
		allowfullscreen: "true",
		allowScriptAccess: "always",
		align: "middle",
		swliveconnect: "true",
		bgcolor: "#ffffff",
		wmode: "transparent"
	};
	
	var attributes = {
		id: 'italia',
		name: 'Italia'
	};
	
	swfobject.embedSWF(img_dir+'/italia.swf', 'italia', 590, 421, '9.0.0', false, flashvars, params, attributes);
}

/* MENU A SCOMPARSA HEADER */
$(function(){
	$('li.nav-top-li').hover(
		function(){ $(this).children('div.sottomenu').show(); },
		function(){ $(this).children('div.sottomenu').hide(); }
	);
});

/* new homepage */
$(function(){
	
	if ($('#news-container').length) {
		$.get('/site/bigmat/news.jsp?size=4', function(data) {
			$('#news-container').html(data);
		});
	}
	
	if ($('#eventi-container').length) {
		$.get('/site/bigmat/eventi.jsp?size=4', function(data) {
			$('#eventi-container').html(data);
		});
	}
	
	if ($('#news-container').length) {
		$.get('/site/bigmat/speciali.jsp?size=1', function(data) {
			$('#speciale-container').html(data);
		});
         }
	 
	/*if ($('#partner').length) {
		var urlXML = "/contents/instance1/static/partner-data.xml";
		$.ajax({
			url: urlXML,
			dataType: 'xml',
			success: function(data,textStatus){
				var count = data.getElementsByTagName("element").length;
				if(count == 0) {
					$("#partner").hide();
				}
			},
			error: function(){
				$('#partner').html('<h2 style="text-align: center;">Servizio non disponibile</h2>');
			}
		});		
        }	*/
});


/* FADE IMMAGINI TESTATA */
function fadeimmagini() {
	var i=1;
	
	$('#testata').css('background-image','none');
	$('#testata img.img-header:first').show();
	
	var timer = setInterval(function(){
		$('#testata img.img-header:visible').fadeOut(2000);
		if ( i >= $('#testata img.img-header').length) i = 0;
		$('#testata img.img-header:eq('+i+')').fadeIn(3000);
		i++;
	},6000);
}


/* INIZIO SONDAGGI */
$(function(){
	$('#altro').hide();
	$('#associarsi .checkbox[name="multianswer.16.96.1.89"]').click(function(){
		if($(this).is(':checked')){
			$('#altro').slideDown('normal');
			$('#altro input[type="text"]').addClass('mandatory');
		} else {
			$('#altro').slideUp('normal');
			$('#altro input[type="text"]').removeClass('mandatory');
		}
	});
});

function checkInput(identificativo){
	$(identificativo + '.error-txt').html('');
	var a = '';
	var cr = '';
	var cc = '';

	$(identificativo + ' input[class*="mandatory"]').each(function(i){	
	
		var name = $(this).attr('name');
		var label = $('label[for="'+name+'"]').text();

		switch($(this).attr('type')){
			case 'text':
				if($(this).val().length == 0) a += '<div class="m-b-cinque">Il campo <strong>'+label+'</strong> non puņ essere vuoto</div>';
				break;
			case 'checkbox':
				var exps = /multianswer\.[0-9]+\.[0-9]+\.[0-9]+/;
				var sub_name = exps.exec(name);
				var label_checkbox = $('label[for*="'+sub_name[0]+'"]').text();
				if(!$('input[name*="'+sub_name[0]+'"]').is(':checked') && cc!=sub_name[0]) {
					a += '<div class="m-b-cinque">Il campo <strong>'+label_checkbox+'</strong> deve essere selezionato</div>'
				}
				cc = sub_name[0];
				break;
			case 'radio':
				var label = $('label[id="'+name+'"]').text();
				if(!$('input[name="'+name+'"]').is(':checked') && cr!=name) a += '<div class="m-b-cinque">Il campo <strong>'+label+'</strong> deve essere selezionato</div>';
				cr = name;
				break;
			default:
				break;
		}
	});
	
	$(identificativo + ' select[class*="mandatory"]').each(function(){
		var name = $(this).attr('id');
		var label = $('label[for="'+name+'"]').text();
		if($(this).val().length == 0) {
			a += '<div class="m-b-cinque">Il campo <strong>'+label+'</strong> deve essere selezionato</div>';
		}
	});
	
	if(!$(identificativo + ' input[name*="privacy"]').is(':checked')){
		a += '<div class="m-b-cinque">Devi accettare <strong>l\'informativa sulla privacy</strong></div>';
	}
	
	if (a){
		$(identificativo + ' .error-txt').html(a);
		$(identificativo + ' .error-js').show();
		location.href = '#errors';
	} else {
		$(identificativo).submit();
	}
}

function changeSurvey(id) {
	$('.error-txt').html('');
	$('.error-js').hide();
	$('.questionari').hide(); 
	$(id).show();
}
/* FINE SONDAGGI */



/* RICERCA PUNTO */
$(function(){
	$('#submit-regioni').click(function(){
		if($('#regione option:selected').val()!=''){
			$('#search-bigmat-region').submit();
		} else {
			alert('Devi selezionare almeno una regione per inziare la ricerca');
		}
	});
	
	$('#regione').change(function(){
		if($(this).children(':selected').val()!='') {
			$('#provincia-container').slideDown('normal');
			
			var regionId = $(this).children(':selected').val();
			
			var option = '<option value=""></option>';
			$.each(regioni.regioni, function(){
				if(this.id == regionId) {
					$.each(this.province, function(){
						option += '<option value="'+this.id+'">'+this.label+'</option>';
					})
				}
			});
			$('#provincia').html(option);
			
		} else {
			$('#provincia-container').slideUp('normal');
		}
	});
	$('#provincia').change(function(){
		if($(this).children(':selected').val()!=''){
			$(this).attr('name','idCardLevel');
			$('#regione').attr('name','param');
		} else {
			$(this).attr('name','param');
			$('#regione').attr('name','idCardLevel');
		}
	});
	
});

/* BOX FILIALI */
$(function(){
	$('.openAffiliate').click(function(){
		$(this).parent('div').next('.filiali').slideToggle('normal');
	});
});

