$(document).ready(function() {
	if ($('.info-block').length > 0 || $('.img-holder-mini').length > 0 || $('.mini-photo').length > 0) {
		$('.info-block .img-holder a, .img-holder-mini a, .mini-photo a').hover(function() {
			$(this).parent().css('opacity','1.0');
		}, function() {
			$(this).parent().css('opacity','0.8');
		});
	}
	if ($('#map').length > 0) {
		createMap2();
	}
	
	
	if ($('.mini-photo').length > 0) {
		$('.mini-photo a').fancybox();
	}
	
	if ($('#richiedi-preventivo').length > 0) {
		$('#richiedi-preventivo').validate({
			rules: {
				Nome: "required",
				Cognome: "required",
				Telefono: "required",
				Richiesta: "required",
				Email: {
					required: true,
					email: true
				}
			}
		});
	}
	
	if ($('#proponi-candidatura').length > 0) {
		$('#proponi-candidatura').validate({
			rules: {
				Nome: "required",
				Cognome: "required",
				Recapito: "required",
				Qualifica: "required",
				Email: {
					required: true,
					email: true
				}
			}
		});
	}	
	
	
	if ($('#proponi-progetto').length > 0) {
		$('#proponi-progetto').validate({
			rules: {
				Nome: "required",
				Cognome: "required",
				Telefono: "required",
				Mobile: "required",
				Richiesta: "required",
				Email: {
					required: true,
					email: true
				}
			}
		});
	}
	
	
	
	if ($('#allega').length > 0) {
		var typeUpload='UPLOAD';
		if ($('form.sceglici-form').attr('id') == 'proponi-candidatura') {
			typeUpload='CURRICULA';
		}
		$("#allega").uploadify({
			'uploader'       : '/img/uploadify.swf',
			'checkScript'	 : '/include/check.php',
			'script'         : '/include/uploadify.php',
			'cancelImg'      : '/img/cancel.png',
			'buttonImg'   	 : '/img/scegli-file.png',
			'folder'         : '../static',
			'auto'           : true,
			'multi'          : true,
			'onComplete' : function(event, ID, fileObj, response, data) {
				setTimeout(function() {
					$.post('/api',{m: 'attachment.put', name: fileObj.name, type: typeUpload, text: 'data'}, function(fid) {
						$('#allegati').append('<li id="'+fid+'"><span>'+fileObj.name+'</span> <a>[Rimuovi]</a></li>');
						var i=0;
						var ids=new Array();
						$("#allegati li").each(function() {
							ids[i]=$(this).attr('id');
							i++;
						});
						$('input[name=FileAllegati]').val(ids.join('*')+'*');
					});
				},300);
		    }
		});
		$("#allegati a").live('click',function() {
			var filetodel=$(this).prev('span').html();
			var fileLi=$(this).parent('li');
			var FID=fileLi.attr('id');
			$.post('/api',{m: 'attachment.del', id: FID}, function() {
				fileLi.remove();
			});
		});
	}
	
	
	if ($('#searchbar').length > 0 || $('#bigsearch').length > 0) { 
		var searchText="Cerca nel Sito.";
		var searchInput=$('input[name=cerca]');
		//alert(searchInput.hasClass('no-change'));

			searchInput.not('.no-change').val(searchText).focus(function() {
				if ($(this).val() == searchText) { $(this).val(''); }
			}).blur(function() {
				if ($(this).val() == '') { $(this).val(searchText); }
			});
		
		$('#searchbar, #bigsearch').submit(function() {
			var searchInput=$(this).children('input[name=cerca]');
			if (searchInput.val().length < 4) {
				alert("Scrivi una parola di almeno 3 caratteri.");
				searchInput.focus();
				return false;
			} else {
				if (searchInput.val() == searchText) {
					searchInput.focus();
					return false;
				} else {
					return true;
				}
			}
		});
	}
	
	if ($('#images-holder img').length > 0 && $('#billboard').length > 0) {
		if ($('#images-holder img.active').length < 1) {
			$('#billboard div.holder').css('background','url('+$('#images-holder img:first').attr('src')+') no-repeat center center');
			$('#images-holder img:first').addClass('active');
		}
		setInterval(function() {			
			var active=$('#images-holder img.active');
			var next=active.nextAll('#images-holder img').not('.active');
			if (next.length == 0) {
				next=$('#images-holder img:first');
			} else {
				next=active.next();
			}
			if ($('#billboard div.holder *').length < 1) {
				var targetOpacity='0';
			} else {
				var targetOpacity='1.0';
			}
			
			$('#billboard div.holder').animate({
				opacity: targetOpacity
			},200,function() {
				next.addClass('active');
				active.removeClass('active');
				$(this).css('background','url('+next.attr('src')+') no-repeat center center').animate({ opacity:'1.0' },200);
			});			
		},6000);
	}
	
});

function createMap() {
	var latlng = new google.maps.LatLng(40.640769,14.87284);
	var myOptions = {
		zoom: 13,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map"),
	myOptions);
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map, 
		title:"Studio GeoTask"

	});
}


function createMap2() {
	var punti = new Array("40.640769,14.87284", "40.340069,18.170881", "37.066455,15.284428", "40.316853,15.632146");	
	var start = new google.maps.LatLng(parseFloat(punti[0].split(",")[0]),parseFloat(punti[0].split(",")[1]));
	var myOptions = {
		zoom: 13,
		center: start,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map"),
	myOptions);
	var latlng = new Array();
	var latlngbounds =  new google.maps.LatLngBounds();
	
	for (i=0;i<punti.length;i++) {
		var point = new google.maps.LatLng(parseFloat(punti[i].split(",")[0]),parseFloat(punti[i].split(",")[1]));
		var marker = new google.maps.Marker({
			  position: point
		  });
		latlng.push(point);
		marker.setMap(map);
		latlngbounds.extend(point);
	 }
	 map.fitBounds(latlngbounds);
}
