//<![CDATA[

// GOOGLE ANALYTICS
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25272658-1']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// FINE GOOGLE ANALYTICS
/************************************/
// Usate per lo scroll delle gallerie
var speed = 50;
var pic, numImgs, arrLeft, i, totalWidth, n, myInterval;
//
var VillaBaiana = {

  // Utilizzare una variabile per fare riferimento al file SWF incorporato.

	DOMready: function () {
		
		if ($('.loadPhoto').length > 0) { VillaBaiana.loadGallery(); };
	  	if ($('.media').length > 0) { VillaBaiana.virtualTour(); };
		if ($('#fotografie').length > 0) { VillaBaiana.photoSlide(); VillaBaiana.dragGallery(); };
		if ($('.pranziNozze').length > 0) { VillaBaiana.photoSlide(); VillaBaiana.pranziNozze(); };
		if ($('.panoramica').length > 0) { VillaBaiana.panoramica(); };
		if ($('#mappa').length > 0) { VillaBaiana.mappa(); };
		if ($('.leggi').length > 0) { VillaBaiana.fancybox(); };

	}, // fine DOMReady
	
	// metodo usato per ottenere il percorso giusto in base alla posizione
	returnPath: function () {
	  var path = location.pathname;
	  var li = path.length;
	  var lf = path.replace(/\//g, '').length;
	  // Windows var n = li-lf-2;
	  var n = li - lf - 1;
	  var resultPath = "";
	
	  for (var i = 0; i < n; i++) {
		resultPath += "./";
	  }
	
	  return resultPath
	}, // fine returnPath
	
	toolTip: function (){
	  $('.toolTip').tooltip({ showURL: false });
	}, /* fine toolTip */
	
	
	fancybox: function() {
		$(".leggi").fancybox({
			'width'				: '75%',
			'height'			: '75%',
			'autoScale'     	: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'type'				: 'iframe'
		});
	},
	
	loadGallery: function() {
		$('.loadPhoto').unbind();
		$('.loadPhoto').toggle
						(
						 
						function() // prima funzione svolta
						{
							var obj = $(this);
							var myUrl = "data/photo-gallery.php?id="+obj.attr('id');
							//alert(myUrl);
							// carico il contenuto tramite ajax
							$.ajax({
								type: 'POST',
								url: myUrl,				
								success: function (data) {
									$('#contenuti').before(data);
									$('.gallery').slideToggle();
									//VillaBaiana.closeGalleryBtn();
									VillaBaiana.photoSlide();
									VillaBaiana.dragGallery();
									obj.text('Chiudi Galleria Fotografica');
								},
								error: function(xhr){
									//window.location = "pippo.html";
									alert('ajax error, '+xhr.status)
								}
							});
							return false;
						},
						
						function() // prima funzione svolta
						{
							 var obj = $(this);
							 
							 VillaBaiana.closeGallery();
							 obj.text('Galleria Fotografica');
						}
						);
	},

	closeGallery: function()
	{
		$('.gallery').slideToggle('slow', function(){ $('.gallery').remove()} );
		$('#btnCloseGallery').remove();
	},

	closeGalleryBtn: function()
	{
		$('body').append('<img id="btnCloseGallery" src="img/btn-close.png" width="42" height="42" alt="Chiudi la Galleria" />');
		$('#btnCloseGallery').click( function(){ VillaBaiana.closeGallery(); });
	},
	dragGallery: function ()
	{
		// Drag della galleria fotogafrica
		$("#fotografie").scrollview({grab:"images/openhand.cur",	grabbing:"images/closedhand.cur"});
	},
	
	mappa: function ()
	{
		$("#mappa").gMap({ markers: [{ latitude: 45.64131, longitude: 10.09368, icon: { image: "img/marker.png",
                                      iconsize: [75, 76],
                                      iconanchor: [35,45],
                                      infowindowanchor: [12, 0] }}],
						 zoom: 15
						 
						 });
	},

	photoSlide: function ()
	{
		pic = $("#fotografie ul li").children("img");
		numImgs = pic.length;
		arrLeft = new Array(numImgs);
	
		for (i=0;i<numImgs;i++)
		{
			totalWidth=0;
			for(n=0;n<i;n++)
			{
				totalWidth += $(pic[n]).width();
			}
		
			arrLeft[i] = totalWidth;
			$(pic[i]).css("left",totalWidth);
		}
	
		myInterval = setInterval("VillaBaiana.flexiScroll()", speed);
		//$('#imageloader').hide();
		$(pic).show();			
	},
	
	flexiScroll: function ()
	{
		for (i=0;i<numImgs;i++){
			arrLeft[i] -= 1;		
	
			if (arrLeft[i] == -($(pic[i]).width())){	
				totalWidth = 0;	
				for (n=0;n<numImgs;n++){
					if (n!=i){	
						totalWidth += $(pic[n]).width();
					}			
				}	
				arrLeft[i] =  totalWidth;	
			}					
			$(pic[i]).css("left",arrLeft[i]);
		}
	},
	
	virtualTour: function ()
	{
		$('.media').media({ width:720, height:420, autoplay:true, bgColor:'' });
		$('.item').click(function() {
			  $('#gallery').slideDown('slow', function() {
				// Animation complete.
			  });
		});
	},	
	
	
	/* Le sale*/
	
	panoramica: function ()
	{
				
		$('li.torchio .btnPanoramica').click(
			function () {
				video = $('li.torchio .panoramica');
				video.slideToggle();
				video.flash(
					{
						swf: 'virtual-tour/sala-del-torchio.swf',
						width: 720,
						height: 420
					}
				);
				
				return false;
			})
		
		$('li.cedro .btnPanoramica').click(
			function () {
				video = $('li.cedro .panoramica');
				video.slideToggle();
				video.flash(
					{
						swf: 'virtual-tour/sala-del-cedro1.swf',
						width: 720,
						height: 420
					}
				);
				
				return false;
			})
		
		$('li.loggia .btnPanoramica').click(
			function () {
				video = $('li.loggia .panoramica');
				video.slideToggle();
				video.flash(
					{
						swf: 'virtual-tour/sala-della-loggia.swf',
						width: 720,
						height: 420
					}
				);
				
				return false;
			})

	},	
	
	
	
	
	
	
	
	
	
	
	pranziNozze: function () {
		$('.pranzo').toggle();
		
		$('.btnPranzo').click(
			function () {
				$(this).next().slideToggle();
				return false;
			})
	},
	
	
	virtual: function () {
		$('.pranzo').toggle();
		
		$('.btnPranzo').click(
			function () {
				$(this).next().slideToggle();
				return false;
			})
	},
	
	popup: function () {}

} // chiusura classe VillaBaiana

///////////////////////////////////////////////////////

$(document).ready(function() { VillaBaiana.DOMready() });
//]]>
