$(document).ready(function() {
	var isEng = document.location.pathname.match(/_eng/);

    $('.apple_overlay img').each(function() {
		$(this).css('margin-top', (526 - this.height) / 2);
    });
	// Setup overlays
    $('img[rel]').overlay({
        expose: {
			color: '#333',
			opacity: 0.9
		},
		effect: 'apple',
		closeOnClick: false
    }).css('cursor', 'pointer');
	// Setup smooth scrolling
	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('a[name=' + this.hash.slice(1) + ']');
			if ($target.length) {
				var targetOffset = $target.offset().top - ($('#glossarioPiccolo').offset().top - $('#glossarioPiccolo')[0].scrollTop);
				$('#glossarioPiccolo').animate({scrollTop: targetOffset}, 2000);
				return false;
			}
		}
	});
	url = '/dinamico/promozioni.php';
	if (isEng) {
		url = '/dinamico/promozioni_eng.php';
	}
	$('#contenutoPromo>div.load').load(url);
	url = '/dinamico/news.php';
	if (isEng) {
		url = '/dinamico/news_eng.php';
	}
	$('#contenutoNews>div.load').load(url, function(responseText, textStatus, XMLHttpRequest) {
		$('#contenutoNews>div.load').css('margin-top', '10px');
		$('#contenutoNews div.scrollable').scrollable({ vertical: true, size: 1 }).circular().autoscroll({ interval: 5000 });
	});
	// Click on soluzioni custom
	$('#devicestlc a[href*=#]').unbind('click').click(function() {
		if ($('#popupDevices').length == 0) {
			var msg = isEng ? "Update in progress" : "Settore in aggiornamento.";
			$('body').append('<div class="modal" id="popupDevices"><h2>' + msg + '</h2><p><button class="close">Ok</button></p></div>');
		}
		$('#popupDevices').overlay({
			expose: { color: '#333', opacity: 0.9 },
			closeOnClick: false,
			api: true
		}).load();
	});
	// Click on soluzioni custom
	$('a[href$=mappa.php], a[href$=mappa_eng.php]').css('display', 'none');
	// Search
	var searchValue = isEng ? 'Search' : 'Ricerca nel sito';
	$('#ricerca').focus(function() {
		if (this.value == searchValue) {
			this.value = '';
		}
	}).blur(function() {
		if (this.value == '') {
			this.value = searchValue;
		}
	});
});
