jQuery(function($){

	$(document).ready(function() {
		
		Cufon.replace('.catalog-category-view .page-title h1, .catalog-product-view .page-title h1, .cms-page-view .page-title h1, .product-view h1');
		Cufon.replace('#slideshow .title', { lineHeight: '10px' });

		if ($('#exhibitor-products').length) {
			$('<span></span>').appendTo('#exhibitor-products a');
		};
		
		if ($('.products-grid').length) {
			$('<span></span>').appendTo('.products-grid a');
		};
		
		if ($('.block-recommended-products').length) {
			$('<span class="hover"></span>').appendTo('.block-recommended-products li a');
		};				

		$("#exhibitor-products span, .product-image span, .block-recommended-products .hover").animate({
			opacity: 0
		}, 0);
		
		$("#exhibitor-products a").live({
			mouseenter: function() {
				$('span', this).stop().animate({
					opacity: 0.3
				}, 200);
			},
			mouseleave: function() {
				$('span', this).stop().animate({
					opacity: 0
				}, 200);
			}
		});
		
		$(".product-image").live({
			mouseenter: function() {
				$('span', this).stop().animate({
					opacity: 0.3
				}, 200);
			},
			mouseleave: function() {
				$('span', this).stop().animate({
					opacity: 0
				}, 200);
			}
		});	
		
		$(".block-recommended-products li a").live({
			mouseenter: function() {
				$('.hover', this).stop().animate({
					opacity: 0.3
				}, 200);
			},
			mouseleave: function() {
				$('.hover', this).stop().animate({
					opacity: 0
				}, 200);
			}
		});			
		
		$.preload('#exhibitor-products a img', {
			onFinish: preload(),
			threshold: 1
		});		
		
		function preload() {
			$('#exhibitor-products a img').each(function () {
				$(this).fadeIn(500);
			});
		}
		
		$('#exhibitor-controls, .more-views ul').lavaLamp({
			fx: 'easeOutSine',
			speed: 300
		});

		if($.browser.msie && (parseInt($.browser.version, 10) < 9)) {
			$('.more-views li:last-child').addClass('last');
		}
		
		$(".more-views li:not('.selectedLava') a").animate({
			opacity: 0.5
		}, 0);
		
		$('.more-views a').live('click', function(e) {
			e.preventDefault();
			var currImg = $('.product-image img.current');
			currImg.after('<img class="new" src="' + $(this).attr('href') + '" width="440" height="440" />');
			currImg.fadeOut(500, function () {
				currImg.remove();
			});
			$('img.new').fadeIn(500, function () {
				$('img.new').removeClass('new').addClass('current');
			});			
			$(this).parent('li').siblings().removeClass('selectedLava');
			$(this).parent('li').addClass('selectedLava');
			$(this).parent('li').siblings().children('a').stop().animate({
				opacity: 0.5
			}, 0);
		});
		
		$(".more-views li:not('.selectedLava') a").live({
			mouseenter: function() {
				$(this).stop().animate({
					opacity: 1
				}, 200);
			},
			mouseleave: function() {
				$(this).stop().animate({
					opacity: 0.5
				}, 200);
			}
		});		
		
		$('#pause').bind('click', function (e) {
			e.preventDefault();
			$('#slideshow .images').cycle('pause');					
		});
		
		$('#play').bind('click', function (e) {
			e.preventDefault();
			$('#slideshow .images').cycle('resume');					
		});

		$('#slideshow .images').cycle({
			fx: 'fade',
			before: onBefore
		});

		function onBefore() {
			$(this).css({
				display: 'block',
				opacity: 1
			}).find('strong').css({
				marginTop: 0,
				top: ($(this).height() - $(this).find('strong').height()) / 2
			});	
		}		
		
		showExhibitor('cat-1');
		
		$('#exhibitor-controls a').live('click', function(e) {
			e.preventDefault();
			$.scrollTo($('#exhibitor-products'), 500);
			$(this).parent('li').siblings().removeClass('selectedLava');
			$(this).parent('li').addClass('selectedLava');
			var cat = ($(this).attr('id'));
			if($.browser.msie && (parseInt($.browser.version, 10) < 9)) {	
				$('#exhibitor-products ul').css('display', 'none');
				$('#exhibitor-products ul').trigger("destroy");
				$('#exhibitor-products .'+cat).css('display', 'block');
			} else {
				$('#exhibitor-products ul').fadeOut(200, function () {
					$('#exhibitor-products ul').trigger("destroy");
				});
				setTimeout(function () {
					$('#exhibitor-products .'+cat).fadeIn(200, function () {
						showExhibitor(cat);					
					});
				}, 200);				
			}
		});		
		
	});
	
	function showExhibitor(cat){
		$("#exhibitor-products ."+cat).carouFredSel({
			items : {
				visible			: 6
			},
			scroll : {
				easing	        : 'easeOutSine', 
				items			: 1,
				duration		: 500,
				pauseDuration	: 2000
			},
			circular	: false,
			infinite	: true,
			auto 		: false,
			prev : {   
				button  : "#exhibitor-nav .prev",
				key     : "left"
			},
			next : {
				button  : "#exhibitor-nav .next",
				key     : "right"
			}
		});
	}
	
	$('.colorbox').colorbox();	

	$('.top-links .block-cart').parent().addClass('mini-cart');

	if(!($('.block-cart .summary').length)) {
		$('.block-cart').addClass('no-items');
	}

	$('.mini-cart').live({
		mouseenter: function () {
			$('.block-cart').css('display', 'block');
		},
		mousehover: function () {
			$('.block-cart').css('display', 'block');
		},		
		mouseleave: function () {
			$('.block-cart').css('display', 'none');			
		}		
	});
	
	$('.block-layered-nav dt').bind('click', function () {
		$(this).toggleClass('open').next('dd').toggle();
	});
	
	$('.block-nav h2').bind('click', function () {
		$(this).toggleClass('open').next('ul').toggle();
	});	
	
	
	if($('#gmap').length) {
		initialize();
	}

	function initialize() {

		var latlng = new google.maps.LatLng(40.55091,14.242926);
		var center = new google.maps.LatLng(40.55091,14.242926);
		var options = {
			zoom: 15,
			center: center,
			scrollwheel: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("gmap"), options);

		// Viale Matteotti 2d - Capri
		
		var matteotti = new google.maps.LatLng(40.548025,14.243943);	
		var matteottiMarker = new google.maps.Marker({
			position: matteotti,
			map: map
		});
		var matteottiTip = $('#matteotti').html();
		var matteottiOptions = {
			alignBottom: true,
			content: matteottiTip
		};
		var matteottiIb = new InfoBox(matteottiOptions);
		
		// Via Federico Serena 28 - Capri
		
		var serena = new google.maps.LatLng(40.549083,14.244397);	
		var serenaMarker = new google.maps.Marker({
			position: serena,
			map: map
		});
		var serenaTip = $('#serena').html();
		var serenaOptions = {
			alignBottom: true,
			content: serenaTip
		};
		var serenaIb = new InfoBox(serenaOptions);
		
		// Via Camerelle 10 - Capri
		
		var camerelle = new google.maps.LatLng(40.549915,14.245673);	
		var camerelleMarker = new google.maps.Marker({
			position: camerelle,
			map: map
		});
		var camerelleTip = $('#camerelle').html();
		var camerelleOptions = {
			alignBottom: true,
			content: camerelleTip
		};
		var camerelleIb = new InfoBox(camerelleOptions);
		
		// Via Axel Munthe, 26 - Anacapri
		
		var anacapri = new google.maps.LatLng(40.556966,14.224226);	
		var anacapriMarker = new google.maps.Marker({
			position: anacapri,
			map: map
		});
		var anacapriTip = $('#anacapri').html();
		var anacapriOptions = {
			alignBottom: true,
			content: anacapriTip
		};
		var anacapriIb = new InfoBox(anacapriOptions);
		
		// Corso Italia, 117 - Sorrento
		
		var sorrento = new google.maps.LatLng(40.626321,14.375738);	
		var sorrentoMarker = new google.maps.Marker({
			position: sorrento,
			map: map
		});
		var sorrentoTip = $('#sorrento').html();
		var sorrentoOptions = {
			alignBottom: true,
			content: sorrentoTip
		};
		var sorrentoIb = new InfoBox(sorrentoOptions);
	
		// Aeroporto Capodichino - Napoli
		
		var capodichino = new google.maps.LatLng(40.884026,14.289179);	
		var capodichinoMarker = new google.maps.Marker({
			position: capodichino,
			map: map
		});
		var capodichinoTip = $('#capodichino').html();
		var capodichinoOptions = {
			alignBottom: true,
			content: capodichinoTip
		};
		var capodichinoIb = new InfoBox(capodichinoOptions);
		
		// Centro Comm. Le Vele - Viale Marconi SNC - Des. del Garda
		
		var levele = new google.maps.LatLng(45.467898,10.536432);	
		var leveleMarker = new google.maps.Marker({
			position: levele,
			map: map
		});
		var leveleTip = $('#levele').html();
		var leveleOptions = {
			alignBottom: true,
			content: leveleTip
		};
		var leveleIb = new InfoBox(leveleOptions);
		
		// Via della Tartana SNC - Positano
		
		var positano = new google.maps.LatLng(40.628252,14.487426);	
		var positanoMarker = new google.maps.Marker({
			position: positano,
			map: map
		});
		var positanoTip = $('#positano').html();
		var positanoOptions = {
			alignBottom: true,
			content: positanoTip
		};
		var positanoIb = new InfoBox(positanoOptions);
		
		// Aeroporto di Fiumicino - Roma
		
		var fiumicino = new google.maps.LatLng(41.7955, 12.2528);	
		var fiumicinoMarker = new google.maps.Marker({
			position: fiumicino,
			map: map
		});
		var fiumicinoTip = $('#fiumicino').html();
		var fiumicinoOptions = {
			alignBottom: true,
			content: fiumicinoTip
		};
		var fiumicinoIb = new InfoBox(fiumicinoOptions);		
		

		$("#fiumicinoListener").bind('click', function (e) {
			e.preventDefault();
			openFiumicino();
			$(this).parent().addClass('active');
		});
		
		google.maps.event.addListener(fiumicinoMarker, "click", function (e) {
			openFiumicino();
			$("#fiumicinoListener").parent().addClass('active');
		});
		
		

		$("#matteottiListener").bind('click', function (e) {
			e.preventDefault();
			openMatteotti();
			$(this).parent().addClass('active');
		});
		
		google.maps.event.addListener(matteottiMarker, "click", function (e) {
			openMatteotti();
			$("#matteottiListener").parent().addClass('active');
		});
		
		$("#serenaListener").bind('click', function (e) {
			e.preventDefault();
			openSerena();
			$(this).parent().addClass('active');			
		});
		
		google.maps.event.addListener(serenaMarker, "click", function (e) {
			openSerena();
			$("#serenaListener").parent().addClass('active');
		});
		
		$("#camerelleListener").bind('click', function (e) {
			e.preventDefault();
			openCamerelle();
			$(this).parent().addClass('active');			
		});
		
		google.maps.event.addListener(camerelleMarker, "click", function (e) {
			openCamerelle();
			$("#camerelleListener").parent().addClass('active');
		});		
		
		$("#anacapriListener").bind('click', function (e) {
			e.preventDefault();
			openAnacapri();
			$(this).parent().addClass('active');			
		});
		
		google.maps.event.addListener(anacapriMarker, "click", function (e) {
			openAnacapri();
			$("#anacapriListener").parent().addClass('active');			
		});			
		
		$("#sorrentoListener").bind('click', function (e) {
			e.preventDefault();
			openSorrento();
			$(this).parent().addClass('active');			
		});

		google.maps.event.addListener(sorrentoMarker, "click", function (e) {
			openSorrento();
			$("#sorrentoListener").parent().addClass('active');
		});			
		
		$("#capodichinoListener1").bind('click', function (e) {
			e.preventDefault();
			openCapodichino();
			$(this).parent().addClass('active');			
		});

		google.maps.event.addListener(capodichinoMarker, "click", function (e) {
			openCapodichino();
			$("#capodichinoListener1").parent().addClass('active');			
		});
		
		$("#capodichinoListener2").bind('click', function (e) {
			e.preventDefault();
			openCapodichino();
			$(this).parent().addClass('active');			
		});

		google.maps.event.addListener(capodichinoMarker, "click", function (e) {
			openCapodichino();
			$("#capodichinoListener2").parent().addClass('active');			
		});		
		
		$("#leveleListener").bind('click', function (e) {
			e.preventDefault();
			openLevele();
			$(this).parent().addClass('active');			
		});

		google.maps.event.addListener(leveleMarker, "click", function (e) {
			openLevele();
			$("#leveleListener").parent().addClass('active');
		});
		
		$("#positanoListener").bind('click', function (e) {
			e.preventDefault();
			openPositano();
			$(this).parent().addClass('active');			
		});
		
		google.maps.event.addListener(positanoMarker, "click", function (e) {
			openPositano();
			$("#positanoListener").parent().addClass('active');			
		});
				
		
		function openFiumicino() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();		
			matteottiIb.close();	
			fiumicinoIb.open(map, fiumicinoMarker);			
			map.panTo(fiumicino);
			$('#shops .active, #showroom').removeClass('active');
			$('#factory').addClass('active');			
		}
		
		function openMatteotti() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();		
			fiumicinoIb.close();
			matteottiIb.open(map, matteottiMarker);			
			map.panTo(matteotti);
			$('#shops .active, #showroom').removeClass('active');
			$('#factory').addClass('active');			
		}
		
		function openSerena() {
			matteottiIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();	
			fiumicinoIb.close();
			serenaIb.open(map, serenaMarker);			
			map.panTo(serena);
			$('#shops .active, #factory').removeClass('active');			
			$('#showroom').addClass('active');						
		}		
		
		function openCamerelle() {
			serenaIb.close();
			matteottiIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();		
			fiumicinoIb.close();
			camerelleIb.open(map, camerelleMarker);			
			map.panTo(camerelle);			
			$('#shops .active, #factory').removeClass('active');	
			$('#showroom').addClass('active');					
		}	
		
		function openAnacapri() {
			serenaIb.close();
			camerelleIb.close();			
			matteottiIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();	
			fiumicinoIb.close();
			anacapriIb.open(map, anacapriMarker);			
			map.panTo(anacapri);			
			$('#shops .active, #factory').removeClass('active');			
			$('#showroom').addClass('active');									
		}			
		
		function openSorrento() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			matteottiIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			positanoIb.close();	
			fiumicinoIb.close();
			sorrentoIb.open(map, sorrentoMarker);			
			map.panTo(sorrento);			
			$('#shops .active, #factory').removeClass('active');			
		}		
		
		function openCapodichino() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			matteottiIb.close();			
			leveleIb.close();
			positanoIb.close();	
			fiumicinoIb.close();
			capodichinoIb.open(map, capodichinoMarker);			
			map.panTo(capodichino);			
			$('#shops .active, #factory').removeClass('active');			
		}		
		
		function openLevele() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			matteottiIb.close();
			positanoIb.close();	
			fiumicinoIb.close();
			leveleIb.open(map, leveleMarker);
			map.panTo(levele);
			$('#shops .active, #factory').removeClass('active');			
		}		
		
		function openPositano() {
			serenaIb.close();
			camerelleIb.close();			
			anacapriIb.close();
			sorrentoIb.close();			
			capodichinoIb.close();			
			leveleIb.close();
			matteottiIb.close();
			fiumicinoIb.close();
			positanoIb.open(map, positanoMarker);			
			map.panTo(positano);			
			$('#shops .active, #factory').removeClass('active');			
		}		
		
	}

});
