document.documentElement.className += 'hasJS';

var origH = 0;
var ratio = 0;

function showImg(path) {
	var $imgfade = $('#imgfade');
	
	$imgfade.fadeOut(400, function() {
		var newImg = new Image();
		
		newImage.onload = function () {
			$imgfade.attr('src', path).fadeIn(600);
		};
		newImage.src = path;
	});
}

function showImgCarousel () {
	var imgPreload = new Image(),
		$imgfade = $('#imgfade'),
		imgPath = $(this).attr('href');
	
	imgPreload.onload = function () {
		$imgfade
		.attr('src', imgPath)
		.fadeIn();
		
	};
	imgPreload.src = imgPath;
	
}

function showEmbedCarousel () {
	var $embed = $.data(this, 'embed_obj') || null,
		params = { 
				allowScriptAccess: "always", 
				wmode :'opaque'
				},
		atts = { id: "cysplayer" },
		uri = 'http://www.youtube.com/e/' + $embed.attr('rev') + '?',
		uriParams = {
			enablejsapi : 1,
			playerapiid : 'ytplayer',
			version : 3,
			autohide : 1,
			showinfo :0
		};
	
	if (!$embed) {
		return;
	}
	//reset swf container
	swfobject.embedSWF(uri + $.param(uriParams),"embedfade_swf", /*560*/"720", /*"345"*/'444', "8", null, null, params, atts);
	
	$('#embedfade_container').show();	
}

$(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"]').live('click', function(e) {
		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);
		});
	});

	$('#header .menu').superfish();
	
	var textarea = $('.textarea');
	
	if(textarea && textarea.length > 0) {
		var settings = {
			autoReinitialise: !(/\bop_action_(ftp-area|contatti)\b/.test(document.body.className)) //!($('.op_action_contatti').length)
		};
		$('.textarea').jScrollPane(settings);
	}
	
	var menuFooter = $('.footerContent ul.menu');
	
	if(menuFooter && menuFooter.length > 0) {
		$('.footerContent ul.menu').css('height', $('.footerContent ul.menu').height());
	}
	
	//origH = $("#contentSpace").height();
	origH = 444;
	origW = 988;
	dxW	  = 394;
	ratio = $(".sx").height() / $(".sx").width();
	
	setLiquidSize();
	
	
	
	if ( $('.footerContent > .menu > li:last-child').offset()!= null ){
		var left = $('.footerContent > .menu > li:last-child').offset().left+$('.footerContent > .menu > li:last-child').width();
		$('.menuIII').css('left',left+'px');
	}
	
});

$(window).resize(function() {

	setLiquidSize();	

});

function setLiquidSize () {
	
	var w = 1200;

	var mrg = ($(window).height() - $(".page").height() - 30) / 2;
	
	mrg = (mrg > 0) ? mrg : 0;
	
	$(".page").css("margin-top", mrg + "px");

}
function initSlideThumb(){
	
	//funzione di slide al movimento sulle thumb
	if($('#slider').find('li').length > 4){
		
		sliderWidth = $('#slider').width();
		var totalContent=0;
		
		$('#slider').find('li').each(function () {
			totalContent+=$(this).innerWidth();
		});
		
		totalContent+=parseInt($('#slider').find('.cs_slider > ul').css('paddingRight').replace('px',''));
		totalContent+=parseInt($('#slider').find('.cs_slider > ul').css('paddingLeft').replace('px',''));
		
		$('#slider').find('.cs_slider').css('width',totalContent);
		
		$('#slider').mousemove(function(e){
			var mouseCoords=(e.pageX - $('#slider').offset().left);
			
	
			var mousePercentX=mouseCoords/sliderWidth;
	
			var destX=-(((totalContent-(sliderWidth))-sliderWidth)*(mousePercentX));
	
			var thePosA=mouseCoords-destX;
			
			var thePosB=destX-mouseCoords;
	
			var animSpeed=600; //ease amount
	
			var easeType='easeOutCirc';
	
			if(mouseCoords==destX){
	
				$('#slider').find('.cs_slider').stop();
	
			}
	
			else if(mouseCoords>destX){
	
				//$('#thumbScroller .container').css('left',-thePosA); //without easing
	
				$('#slider').find('.cs_slider').stop().animate({left: -thePosA}, animSpeed,easeType); //with easing
	
			}
	
			else if(mouseCoords<destX){
	
				//$('#thumbScroller .container').css('left',thePosB); //without easing
	
				$('#slider').find('.cs_slider').stop().animate({left: thePosB}, animSpeed,easeType); //with easing
	
			}
	
		})
	}
	
	
}
