$(window).load(function (){
	var secmenuh = ($('div#sec-menu').outerHeight())*-1;
	$('div#sec-menu').css('top', secmenuh +'px');

	if ($.cookie('currentToggle') === 'on') {
		$('div#main-menu').css('left', '0px').show();
		$('div#sec-menu').css('top', '0px').show();
		$('div#main-content').show();
	}

$('div#home').click(function() {
		if ($.cookie('currentToggle') === 'on') {
			var currentH = ($('div#sec-menu').outerHeight())*-1;
			$('div#main-content').fadeOut(function() {
				$('div#sec-menu').animate({top: currentH}, function(){
					$('div#sec-menu').hide();
				});
				$('div#main-menu').animate({left: -728}, function(){
					$('div#main-menu').hide();
				});
			});
			$.cookie('currentToggle', 'off', { path: '/'});
		} else {
			$('div#main-menu').show(function (){
				$(this).animate({left: 0});
			});
			$('div#sec-menu').show(function (){
				$(this).animate({top: 0}, function() {
					$('div#main-content').fadeIn();	
				});
			});
			$.cookie('currentToggle', 'on', { path: '/'});
		}
	});

		var $container 	= $('#am-container'),
			$imgs		= $container.find('img').hide(),
			totalImgs	= $imgs.length,
			cnt			= 0;
				
	$imgs.each(function(i) {
		var $img	= $(this);
		$('<img/>').load(function() {
			++cnt;
			if( cnt === totalImgs ) {
				$imgs.show();
				$container.montage({
					liquid : false,
					fillLastRow	: false,
					fixedHeight : 80,
					margin: 2
				});
			}
		}).attr('src',$img.attr('src'));
	});

	// Equalize height
	var maxHeight = 0;
	$("div.half-box").each(function(){
	   if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
	});
	$("div.half-box").height(maxHeight);

	
	//// arrange mainmenu
	//// dichiaro la variabile per memorizzare la lunghezza totale dei list item
	//var lil = 0;
	//// Determino la lunghezza del menu meno il valore del padding laterale
	//var mml = ($('div#main-menu').width())-20;
	//// determino il numero totale di list item
	//var totli = $('ul#mainmenu li').length;
	//// sommo la lunghezze di ogni list item nella variabile
	//$('ul#mainmenu li').each(function (){
	//	lil += $(this).width();
	//});
	//// determino il valore di margine e lo applico
	//var lim = (mml-lil)/(totli-1);
	//// escludo l'ultimo list item
	//$('ul#mainmenu li:lt('+totli+')').css('margin-right' , lim+'px');


});	// load

$(document).ready(function (){
	$.fn.preload = function(options) {
			var opts 	= $.extend({}, $.fn.preload.defaults, options);
			o			= $.meta ? $.extend({}, opts, this.data()) : opts;
			var c		= this.length,
				l		= 0;
			return this.each(function() {
				var $i	= $(this);
				$('<img/>').load(function(i){
					++l;
					if(l == c) o.onComplete();
				}).attr('src',$i.attr('src'));	
			});
		};
		$.fn.preload.defaults = {
			onComplete	: function(){return false;}
		};

	$(function() {
		var $tf_bg							= $('#tf_bg'),
				$tf_bg_images				= $tf_bg.find('img'),
				$tf_bg_img					= $tf_bg_images.eq(0),
				$tf_thumbs					= $('#tf_thumbs'),
				total								= $tf_bg_images.length,
				current							= 0,
				$tf_content_wrapper	= $('#tf_content_wrapper'),
				$tf_next						= $('#tf_next'),
				$tf_prev						= $('#tf_prev'),
				$tf_loading					= $('#tf_loading');
		
		//preload the images				
		$tf_bg_images.preload({
			onComplete	: function(){
				$tf_loading.hide();
				init();
			}
		});
		
		//shows the first image and initializes events
		function init(){
			//get dimentions for the image, based on the windows size
			var dim	= getImageDim($tf_bg_img);
			//set the returned values and show the image
			$tf_bg_img.css({
				width		: dim.width,
				height	: dim.height,
				left		: dim.left,
				top			: dim.top
			}).show();
		
			//resizing the window resizes the $tf_bg_img
			$(window).bind('resize',function(){
				var dim	= getImageDim($tf_bg_img);
				$tf_bg_img.css({
					width		: dim.width,
					height	: dim.height,
					left		: dim.left,
					top			: dim.top
				});
			});

			//click the arrow down, scrolls down
			$tf_next.bind('click',function(){
				if($tf_bg_img.is(':animated'))
					return false;
					scroll('tb');
			});
			
			//click the arrow up, scrolls up
			$tf_prev.bind('click',function(){
				if($tf_bg_img.is(':animated'))
				return false;
				scroll('bt');
			});
		}
		
		//show next / prev image
		function scroll(dir){
			//if dir is "tb" (top -> bottom) increment current, 
			//else if "bt" decrement it
			current	= (dir == 'tb')?current + 1:current - 1;
			
			//we want a circular slideshow, 
			//so we need to check the limits of current
			if(current == total) current = 0;
			else if(current < 0) current = total - 1;

			//we get the next image
			var $tf_bg_img_next	= $tf_bg_images.eq(current),
				//its dimentions
				dim				= getImageDim($tf_bg_img_next),
				//the top should be one that makes the image out of the viewport
				//the image should be positioned up or down depending on the direction
					top	= (dir == 'tb')?$(window).height() + 'px':-parseFloat(dim.height,10) + 'px';
					
			//set the returned values and show the next image	
				$tf_bg_img_next.css({
					width	: dim.width,
					height	: dim.height,
					left	: dim.left,
					top		: top
				}).show();
				
			//now slide it to the viewport
				$tf_bg_img_next.stop().animate({
					top 	: dim.top
				},1000);
				
			//we want the old image to slide in the same direction, out of the viewport
				var slideTo	= (dir == 'tb')?-$tf_bg_img.height() + 'px':$(window).height() + 'px';
				$tf_bg_img.stop().animate({
					top 	: slideTo
				},1000,function(){
				//hide it
					$(this).hide();
				//the $tf_bg_img is now the shown image
					$tf_bg_img	= $tf_bg_img_next;
				//show the description for the new image
					$tf_content_wrapper.children()
									   .eq(current)
								       .show();
		});
		}
		
		//animate the image to fit in the viewport
		function resize($img){
			var w_w	= $(window).width(),
				w_h	= $(window).height(),
				i_w	= $img.width(),
				i_h	= $img.height(),
				r_i	= i_h / i_w,
				new_w,new_h;
			
			if(i_w > i_h){
				new_w	= w_w;
				new_h	= w_w * r_i;
				
				if(new_h > w_h){
					new_h	= w_h;
					new_w	= w_h / r_i;
				}
			}	
			else{
				new_h	= w_w * r_i;
				new_w	= w_w;
			}
			
			$img.animate({
				width	: new_w + 'px',
				height	: new_h + 'px',
				top		: '0px',
				left	: '0px'
			},350);
		}
		
		//get dimentions of the image, 
		//in order to make it full size and centered
		function getImageDim($img){
			var w_w	= $(window).width(),
				w_h	= $(window).height(),
				r_w	= w_h / w_w,
				i_w	= $img.width(),
				i_h	= $img.height(),
				r_i	= i_h / i_w,
				new_w,new_h,
				new_left,new_top;
			
			if(r_w > r_i){
				new_h	= w_h;
				new_w	= w_h / r_i;
			}
			else{
				new_h	= w_w * r_i;
				new_w	= w_w;
			}


			return {
				width	: new_w + 'px',
				height	: new_h + 'px',
				left	: (w_w - new_w) / 2 + 'px',
				top		: (w_h - new_h) / 2 + 'px'
			};
			}
	});

	$('#sec-menu, #main-menu').hide();
	$('ul.questions li:gt(0)').prepend(' / ');

	//Tabbed navigation
	//Nascondo i pannelli eccetto il primo
	$('div.panes div:gt(0)').hide();
	//Aggiungo la classe current al primo pannello
	$('div.panes div:first').addClass('current');
	//Aggiungo un ID ad ogni pannello in base al loro indice
	$('div.panes div:not(#null)').each(function () {
		var paneID = 'pane-'+$('div.panes div:not(#null)').index(this);
		$(this).attr('id' , paneID);
	});
	//Imposto la funzione di sostituzione pannello
	$('ul.questions li a').each(function () {
		$(this).click(function () {
			//Determino la totalità dei trigger
			var totTrigger =  $('ul.questions li a');
			//Imposto l'indice del trigger selezionato e richiamo il suo pannello
			var selectTrigger = 'pane-'+totTrigger.index(this);
			selectPane = $('div#'+selectTrigger);
			//Imposto classe elemento lista
			$('.active').removeClass('active');
			$(this).addClass('active');
			//Imposto transizione
			$('.current').removeClass('current').fadeOut(function () {
				$(selectPane).addClass('current').fadeIn();
			});
			return false;
		});// click
	});// each

	$('div#logo').hover(function() {
		$('div#player').fadeIn();
	}, function () {
		$('div#player').fadeOut();
	});//hover

	// Wedding-menu-slide
	$('.ws-container').hover(function() {
		$(this).find('.ws-content').stop().animate({left: '0px'});
	}, function () {
		$(this).find('.ws-content').stop().animate({left: '-130px'});
	});//hover

	// Idee-menu-slide
	$('.id-data').click(function (){
		$(this).parent().find('.id-body').slideToggle();
	})

	// fancybox

	$('a.inline:gt(0)').hide();

	$('div#am-container a, a.inline').fancybox({
		overlayColor: '#000',
		overlayOpacity: 0.8,
		padding: 0,
		autoScale: true
	});

	// add slashes
	$('div.short').find('h2').append(' /');

	// Halfbox
	$('div.half-box:lt(1)').addClass('first');

	$('div#main-menu ul li:lt(4)').css('margin-right', '94px');


}); // ready


function initialize() {
  var pvCastel = new google.maps.LatLng(44.109607, 10.411706);
  var pvChifenti = new google.maps.LatLng(44.005898, 10.563819);
  var mapCenterLat = pvChifenti.lat()+((pvCastel.lat()-pvChifenti.lat())/2);
  var mapCenterLng = pvChifenti.lng()+((pvCastel.lng()-pvChifenti.lng())/2);
	var mapCenter = new google.maps.LatLng(mapCenterLat, mapCenterLng);	
  var myOptions = {
    zoom: 11,
    center: mapCenter,
    navigationControl: true,
    scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("mappa"), myOptions);
  
  var markerCastel = new google.maps.Marker({
     position: pvCastel, 
     map: map
  });
  var markerChifenti = new google.maps.Marker({
  	position: pvChifenti,
  	map: map
  })
} // inizialize google maps

// centro la mappa su punto vendita
function centerCastel() {
	var pvCastel = new google.maps.LatLng(44.109607, 10.411706);
	var myOptions = {
    zoom: 16,
    center: pvCastel,
    navigationControl: true,
    scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("mappa"), myOptions);
  
  var markerFornoli = new google.maps.Marker({
      position: pvCastel, 
      map: map
  });
}

// centro la mappa su punto vendita
function centerChifenti() {
	var pvChifenti = new google.maps.LatLng(44.005898, 10.563819);
	var myOptions = {
    zoom: 16,
    center: pvChifenti,
    navigationControl: true,
    scaleControl: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("mappa"), myOptions);
  
  var markerFornoli = new google.maps.Marker({
      position: pvChifenti, 
      map: map
  });
}

























