$(document).ready(function() {

	// Gestione target blank links con doctype strict
	// This is how it works:
	// <a href="http://www.tripwiremagazine.com/" rel="external">tripwiremagazine.com</a>
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});

	// gestione IMG HOVER
	$(".mHover").each(function() {
		var src = $(this).attr('src');
		var extension = src.substring(src.lastIndexOf('.'), src.length);
		$(this).mouseover(function() {
			$(this).attr('src', src.replace(extension,'-on' + extension));
		}).mouseout(function() {
			$(this).attr('src', src);
		});
	});

//	$('hr.styleme').each(function() {
//		var hrId = $(this).attr('id');
//		$(this).removeAttr('id').removeClass('styleme').wrap('<div class="hr" id="' + hrId + '">');
//	});

	if ($('.bgimage').length && $.fn.bgimage) {
		$('.bgimage').bgimage();
	}

	if (!$.browser.msie) {
		$('body').css({overflow: 'auto'});
	}
	
	if ($('body').hasClass('home')) {
		$('<div id="homeLeft"></div>').insertBefore($('#menuBar'));
	}

	$(window).bind('resize', function() {
		var _elements = $('#homeLeft, #menuBar, #content, #sideGallery');
		_elements.each(function() {
			$(this).height('auto');
		});

		var setToHeight = Math.max.apply(null, $.makeArray(_elements.add($('#menuBar')).add($(window)).add($(document)).map(function() { return $(this).height(); })));

		_elements.each(function() {
			$(this).height(setToHeight - (parseInt($(this).css('padding-top'), 10) + parseInt($(this).css('padding-bottom'), 10)));
		});
	}).trigger('resize');

	$('#contentBackground, #sideGalleryBackground').css({opacity: .68});

	if ($.fn.fancybox) {

		$('[rel*=gallery]').fancybox({
			    autoDimensions: false
			  , autoScale: false
			  , centerOnScroll: true
			  , height: 600
			  , hideOnOverlayClick: false
			  , margin: '0 45px 55px'
			  , overlayOpacity: 1
			  , overlayColor: '#ffffff'
			  , padding: 0
			  , scrolling: 'no'
			  , titleShow: false
			  , transitionIn: 'none'
			  , transitionOut: 'none'
			  , width: 800
			  , header: '<img alt="" id="logoGallery" src="/skin/website/img/logoGallery.png" style="display: block; margin: 0 auto; padding: 20px 0;" />'
//			  , onClosed: function() {
//			    	$('fancybox-navigation').clearQueue();
//			    	$('fancybox-content').clearQueue();
//		    		$('fancybox-wrap').clearQueue();
//		    		$('fancybox-outerwrap').clearQueue();
//			    }
		});

		if (location.hash && location.hash.substr(1)) {
			var idAuto = location.hash.substr(1);
			$('a[rel*=gallery]').map(function() {
				return (new RegExp('\/.*' + idAuto + '\/[^\/]*').test($(this).attr('href')) ? this : null);
			}).trigger('click');
		}
//		$($('[rel*=gallery]').get(0)).trigger('click');
	}

	if (typeof SWFObject !== undefined) {
		if ($('body').hasClass('home')) {
			var so = new SWFObject('/skin/website/swf/3.swf', 'video', '600', '400', '8', '#dad8da');
			so.addParam('wmode', 'transparent');
			so.addParam('id', 'video');
			so.addVariable('indirizzo_video', '/skin/website/swf/home_uniques_201111.flv');
			so.addVariable('skin_video',      '/skin/website/swf/SkinOverPlaySeekMute.swf');
			so.write('frameInner');
		} else {
		
			//fallback for tooltip plugin
			if (!('tooltip' in $.fn)) {
				$.fn.tooltip = function () {
					return this;
				};
			}
			
			$('[rel*=video]')
			.each(function() {
				$(this).data('titleAttr', $(this).attr('title'));
				$(this).find('img').each(function() {
					$(this).css({'background-image': 'url(' + $(this).attr('src') + ')'});
					$(this).attr({'src': '/skin/website/widgets/playOverThumb.png'});
				});
			})
			.bind('click', function(event) {
				$('#videoTitle').text($(this).data('titleAttr')).show();
				var so = new SWFObject('/skin/website/swf/player_video.swf', 'video', '551', '310', '8', '#000000');
				so.addParam('wmode', 'transparent');
				so.addParam('id', 'video');
				so.addVariable('indirizzo_video', $(this).attr('href'));
				so.addVariable('skin_video',      '/skin/website/swf/SkinOverPlaySeekMute.swf');
				so.write('videoContainer');
				event.preventDefault();
			})
			.tooltip({
				showURL: false
			})
			.eq(0).trigger('click');
		}
	}
});


$(window).bind('load', function() {
	$(window).trigger('resize');
});

// gestione dropdown submenu news
jQuery(function($) {
	var $news_menu = $('.level1.news') || [],
		$submenu;
	
	if (!$news_menu.length) {
		return false;
	}
	
	$submenu = $news_menu.find('> ul').hide();
	
	$news_menu.find('> a').click(function (e) {
			e.preventDefault();
			$submenu.stop(true, true).slideToggle('fast');	
	});	
});
