$(function(){
	var cat = "";
	jQuery.easing.def = "easeOutExpo";
	speed = 1000;
	
	$('#grid-content').masonry({
		columnWidth: 260, 
		animate: true,
		itemSelector: '.box:not(.invis)',
		animationOptions: {
			duration: speed,
			queue: false
		}
	});
	
	if(jQuery.browser.msie) {
		$('div.box , div.box *').css({'opacity' : 'inherit', 'filter' : 'inherit'});
		$('#grid-content div.box img').css('filter','');
	}
	
	if ($('#link_tutte.current').size() > 0) {
		$('#sx2 a').click(function(e) {
			e.preventDefault();
			$('#sx a').removeClass('current');
			$(this).addClass('current');
			$('#viewer').css('left','-100%')
			$('#result').css('display','none');
			$('#result').html('');
			cat = $(this).attr('rel');
			catName = $(this).html();
			_gaq.push(['_trackEvent', 'Category', catName]);
			_gaq.push(['_trackPageview', '/'+cat+'/']);
			if (cat != 'tutte') {
				location.hash = cat+'/';
				$('#grid-content div.box').not($('.'+cat)).addClass('invis').fadeOut(speed);
				$('#grid-content div.'+cat).removeClass('invis').fadeIn(speed);
			} else {
				cat = "";
				location.hash = '';
				$('#sx a[rel=tutte]').addClass('current');
				$('#grid-content div.box').removeClass('invis').fadeIn(speed);
			}
			$('#grid-content').masonry();
		});
	} else {
		$('#sx2 a').not('#link_tutte').each(function(e) {
			$(this).attr('href','#'+$(this).attr('href'));
		});
	}
	
	$('a.image , a.text').hover(
		function(e) {
			$(this).children('span.title').animate({top:'0px',opacity:'0.7'}, speed/2)
			$(this).children('span.zoom').animate({bottom:'0px',opacity:'0.7'}, speed/2)
		},function(e) {
			$(this).children('span.title').animate({top:'-40px',opacity:'1'}, speed/2)
			$(this).children('span.zoom').animate({bottom:'-40px',opacity:'1'}, speed/2)
		}
	).click(
		function(e) {
			e.preventDefault();
			$('#result').css('display','none');
			$('#result').html('');
			$('#loading').css('opacity','1');
			permalink = $(this).attr('rel');
			location.hash = 'view/'+permalink+'/';
			itemName = $(this).children('span.title').html();
			_gaq.push(['_trackEvent', 'Item', itemName]);
			_gaq.push(['_trackPageview', 'view/'+permalink+'/']);
			$('#viewer').css('left','0px');
			$('#viewer2').css('left','-580px');
			$('#viewerBg').css('opacity','0');
			$('#viewerBg').animate({opacity:'0.8'}, speed);
			$('#viewer2').animate({left:'220px'}, speed, function() {
				$.get('contents.php', { permalink: permalink },
					function(data) {
						$('#loading').animate({opacity:'0'}, speed/2, function() {
							$('#result').html(data);
							$('#result').fadeIn(speed/2);
							$('#gallery').cycle({
								pause: 1,
								pauseOnPagerHover: 1,
								delay: -2000,
								speed: speed,
								next: '#next',
								prev: '#prev'
							});
							$('#fbShare').stop().popupWindow({ centerScreen:1 });
						});
					});
				}
			);
		}
	);
	
	if ($('#result.open').size() > 0) {
		$('#result').css('display','none');
		$('#loading').css('opacity','1');
		$('#viewer').css('left','0px');
		$('#viewer2').css('left','-580px');
		$('#viewerBg').css('opacity','0');
		$('#viewerBg').animate({opacity:'0.8'}, speed);
		$('#viewer2').animate({left:'220px'}, speed, function() {
			$('#loading').animate({opacity:'0'}, speed/2, function() {
				$('#result').fadeIn(speed/2);
				$('#gallery').cycle({
					pause: 1,
					pauseOnPagerHover: 1,
					delay: -2000,
					speed: speed,
					next: '#next',
					prev: '#prev'
				});
				$('#fbShare').stop().popupWindow({ centerScreen:1 });
			});
		});
	}
	
	if (location.hash != "" && location.hash != "#") {
		params = location.hash.slice(1).split('/');
		if (params[1] == "" && params[0] != 'view') {
			$('#link_tutte.current').removeClass('current');
			$('#sx2 a[rel|="'+params[0]+'"]').addClass('current');
			$('#grid-content div.box').not($('.'+params[0])).addClass('invis').fadeOut(speed);
			$('#grid-content div.'+params[0]).removeClass('invis').fadeIn(speed);
		} else if (params[0] == 'view' && params[1] != "") {
			$('#result').css('display','none');
			$('#result').html('');
			$('#loading').css('opacity','1');
			permalink = params[1];
			$('#viewer').css('left','0px');
			$('#viewer2').css('left','-580px');
			$('#viewerBg').css('opacity','0');
			$('#viewerBg').animate({opacity:'0.8'}, speed);
			$('#viewer2').animate({left:'220px'}, speed, function() {
				$.get('contents.php', { permalink: permalink },
					function(data) {
						$('#loading').animate({opacity:'0'}, speed/2, function() {
							$('#result').html(data);
							$('#result').fadeIn(speed/2);
							$('#gallery').cycle({
								pause: 1,
								pauseOnPagerHover: 1,
								delay: -2000,
								speed: speed,
								next: '#next',
								prev: '#prev'
							});
							$('#fbShare').stop().popupWindow({ centerScreen:1 });
						});
					});
				}
			);
		}
	}
	$('#grid-content').masonry();
	
	$('#close , #viewerBg').click(
		function(e) {
			if (cat != "") {
				location.hash = cat+'/';
				_gaq.push(['_trackPageview', cat+'/']);
			} else {
				location.hash = '';
				_gaq.push(['_trackPageview', '']);
			}
			$('#viewer2').animate({left:'-580px'}, speed);
			$('#viewerBg').delay(speed/2).animate({opacity:'0'}, speed, function () { $('#viewer').css('left','-120%') });
			$('#result').css('display','none');
			$('#result').html('');
		}
	);
	
	$(document).mousemove(function(e){
		$('#sx , body , .box').css('background-position',e.pageX/10+'px '+e.pageY/10+'px');
		$('#viewer2').css('background-position',e.pageX/20+'px '+e.pageY/20+'px');
	});

});

/* MAP */
$(function(){
if ($('#mappa').length > 0) {
	colours = [
		{ featureType: "administrative", elementType: "all", stylers:		[ { hue: "#30383c" }, { saturation: -30 }, { lightness: 0 } ]},
		{ featureType: "landscape", elementType: "all", stylers:			[ { hue: "#30383c" }, { saturation: -30 }, { lightness: 0 } ] },
		{ featureType: "poi", elementType: "all", stylers:					[ { hue: "#30383c" }, { saturation: -30 }, { lightness: 0 } ] },
		{ featureType: "road", elementType: "all", stylers:					[ { hue: "#30383c" }, { saturation: -50 }, { lightness: 0 } ] },
		{ featureType: "transit", elementType: "all", stylers:				[ { hue: "#30383c" }, { saturation: -30 }, { lightness: 0 } ] },
		{ featureType: "water", elementType: "all", stylers:				[ { hue: "#30383c" }, { saturation: -30 }, { lightness: 0 } ] } 
	];
	latlng = new google.maps.LatLng(46.472227,11.325638);
	myOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP, sensor: "false", center: latlng, disableDefaultUI: true };
	map = new google.maps.Map(document.getElementById("mappa"), myOptions);
	styledMapOptions = { map: map, name: "Map"}
	myStyledMap = new google.maps.StyledMapType(colours,styledMapOptions);
	map.mapTypes.set('colouredMap', myStyledMap);
	map.setMapTypeId('colouredMap');
	marker = new google.maps.Marker({ map: map, icon: 'img/maplogo.png', shadow: 'img/maplogoshadow.png', position: latlng, title: "Level - Piazza Fiera 1 - Bolzano" });
	google.maps.event.addListener(map, 'click', function() { openmap (); });
	google.maps.event.addListener(marker, 'click', function() { openmap (); });
}
});

function openmap () {
	$('#result').css('display','none');
	$('#result').html('');
	$('#loading').css('opacity','1');
	$('#viewer').css('left','0px');
	$('#viewer2').css('left','-580px');
	$('#viewerBg').css('opacity','0');
	$('#viewerBg').animate({opacity:'0.8'}, speed);
	$('#viewer2').animate({left:'220px'}, speed, function() {
		$('#loading').animate({opacity:'0'}, speed/2, function() {
			$('#result').html('<div id="mappa2" style="width: 728px; height: 468px;"></div>');
			$('#result').fadeIn(speed);
			var myOptions = { zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP, sensor: "false", center: latlng, panControl: true, zoomControl: true, mapTypeControl: false, scaleControl: false, streetViewControl: false };
			map2 = new google.maps.Map(document.getElementById("mappa2"), myOptions);
			map2.mapTypes.set('colouredMap', myStyledMap);
			map2.setMapTypeId('colouredMap');
			map2.setOptions({ mapTypeControl: false });
			marker2 = new google.maps.Marker({ map: map2, icon: 'img/maplogo.png', shadow: 'img/maplogoshadow.png', position: latlng, title: "Level - Piazza Fiera 1 - Bolzano" });
			marker2.setAnimation(google.maps.Animation.BOUNCE);
		});
	});
}

/* Analytics */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1211084-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);
})();

