$(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);
		});
	});

	if ($(".media-header").length > 0) {
		var fileName = $(".media-header").attr("rel");
		$(".media-header").media({
	    	width:     "100%",
	    	height:	   342,
	    	autoplay:  true,
	 		bgColor:   '#000000',
	    	src:       '/skin/website/swf/'+fileName+'.swf',
	    	params:    { wmode: 'transparent' }, // object params/embed attrs
	    	flashvars: { color: colorFlash, 
	    				 title: titleFlash, 
    				 	 voci_animazione: vociFlash
				 	   },
	    	caption:   false // supress caption text
		});
	}

	if ($(".media-home").length > 0) {
		$(".media-home").media({
    		width:     "100%",
	    	height:	   "100%",
	    	autoplay:  true,
	 		bgColor:   '#000000',
	    	src:       '/skin/website/swf/intro.swf',
	    	params:    { wmode: 'transparent' }, // object params/embed attrs
	    	caption:   false // supress caption text
		});
	}
	
	initializeSong();

	$('#menu-generic .menu li:last').css('margin-top', '15px');
	
	$("#mute_play").click(function() {
		var divMutePlay = $(this).children("div"); 
		if(divMutePlay.hasClass("mute")) {
			$.get("/playSong", function(){
				divMutePlay.removeClass("mute");
				divMutePlay.addClass("play");
				$("#player_song").html('<a style="display: none;" class="media-mp3" href="/skin/website/mp3/relax.mp3"></a>');
				initializeSong();
			});		
		} else if(divMutePlay.hasClass("play")) {
			$.get("/muteSong", function(){
				divMutePlay.removeClass("play");
				divMutePlay.addClass("mute");
				$(".media-mp3").remove();
			});
		}
	})
	if ($.browser.msie) {
		if ($('#scrollableList').length > 0) {
			var currentScroll = 0;
			var interval = null;
			$('.scrollable').parent().append('<div style="height: 18px; width: 40px; background: url(/skin/website/widgets/scroll_up.png) no-repeat 0 0; display: block; position: absolute; top: -28px; right: 370px;" id="up"></div>');
			$('.scrollable').parent().append('<div style="height: 18px; width: 40px; background: url(/skin/website/widgets/scroll_down.png) no-repeat 0 0; display: block; position: absolute; top: 290px; right: 370px;" id="down"></div>');
			$('#down').bind('mouseover',function(){
				currentScroll += 100;
				if (currentScroll > $('.scrollable').attr('scrollHeight')) currentScroll = $('.scrollable').attr('scrollHeight');  
				$('.scrollable').animate({scrollTop: currentScroll},500);
				interval = setInterval(function(){
					currentScroll += 100;
					if (currentScroll > $('.scrollable').attr('scrollHeight')) currentScroll = $('.scrollable').attr('scrollHeight');  
					$('.scrollable').animate({scrollTop: currentScroll},500);
				},500);		
			});
			$('#down').bind('mouseleave',function(){
				if (interval != null) {
					clearInterval(interval);
					$('.scrollable').stop();
					interval = null;
				}
			});
			$('#up').bind('mouseover',function(){
				currentScroll -= 100;
				if (currentScroll < 0) currentScroll = 0;
				$('.scrollable').animate({scrollTop: currentScroll},500);
				interval = setInterval(function(){
					currentScroll -= 100;
					if (currentScroll < 0) currentScroll = 0;
					$('.scrollable').animate({scrollTop: currentScroll},500);
				},500);
			});
			$('#up').bind('mouseleave',function(){
				if (interval != null) {
					clearInterval(interval);
					$('.scrollable').stop();
					interval = null;
				}
			});
		}
	} else {
		if ($('#scrollableList').length > 0) { 
			$('#scrollableList').hoverscroll({vertical: true, width: 560, height: 280, arrows: false, fixedArrows: false});
		}
	}
});

function view_youtube_video(me) {
	if ($('.image-spacer iframe').length) return;
	if($('#mute_play>div').hasClass("play")) {
		$('#mute_play').click();
	}
	var code = $(me).attr('data-video');
	var ytcode = '<iframe style="position:absolute;" width="510" height="253" src="http://www.youtube.com/embed/'+code+'?rel=0&autohide=1" frameborder="0" allowfullscreen></iframe>';
	$('.image-spacer img').fadeOut(3000);
	$('.image-spacer').prepend(ytcode);
}

function initializeSong() {
	if ($(".media-mp3").length > 0) {
		$(".media-mp3").media({
			width:     "1",
	    	height:	   "1",
	    	autoplay:  true,
	    	bgColor:   '#000000',
	 		mp3Player: '/skin/website/swf/player.swf',
	 		params:    { wmode: 'transparent' }, // object params/embed attrs
 		 	flashvars: { song: '/mp3/relax.mp3' } 
		});
	}	
}

