//funzione per il cambio del tipo di input text => password
function password_change(){
html = '<input type="password" name="password0" id="password" class="user" value="" />';
	$('#password').after(html).remove();
	$('#password').focus();
	$('#password').blur(function(){
		if(this.value==''){
			html = '<input type="text" name="password0" id="password" class="user" value="Password" />';
			$(this).after(html).remove();
			$('#password').focus(function(){ 
				password_change();
			});
		}
	});
}

//gallery3 
function makeScrollable($outer, $inner){
	var extra = 100;
	//Get menu width
	var divWidth = $outer.width();

	//Find last image in container
	var lastElem = $inner.find('img:last');
	$outer.scrollLeft(0);
	//When user move mouse over menu
	$outer.unbind('mousemove').bind('mousemove',function(e){
		var containerWidth = lastElem[0].offsetLeft + lastElem.outerWidth() + 2*extra;
		var left = (e.pageX - $outer.offset().left) * (containerWidth-divWidth) / divWidth - extra;
		$outer.scrollLeft(left);
	});

}
function gallery3scorri(){
	if ($('.jackgallery3 .tbcont .actual').next().length==0){
		$questo=$('.jackgallery3 .tbcont img').eq(0);
	} else {
		$questo=$('.jackgallery3 .tbcont .actual').next();
	}
		$('.jackgallery3 .main').fadeOut(500, function(){
			$('.jackgallery3 .main').attr('src', '').attr('alt', '');
			$('.jackgallery3 .main').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).load(function(){
				$('.jackgallery3 .main').show();
				$('.jackgallery3 .fade').hide();
			});
		});
		$('.jackgallery3 .fade').fadeIn(300).attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt'));
		$('.jackgallery3 .tbcont img').removeClass('actual');
		$questo.addClass('actual');
		$('.jackgallery3 .dett_foto').html("<h1>"+$questo.attr('alt').split("=")[0]+"</h1><p>Foto di <br /><strong>"+$questo.attr('alt').split("=")[1]+"</strong></p>");
}

$(document).ready(function(){

//link esterni
	$('.ExternalLink').click(function(){
		window.open($(this).attr('href'));
		return false;
	});

//ricerca
	$('#Search').focus(function(){
			this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Cerca sul sito...';
		});
	$('#Search_footer').focus(function(){
			this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Cerca sul sito...';
		});
		
	//loginUtenti
	$('#username').focus(function(){
			if ($(this).val()=='Username')
				this.value='';
		}).blur(function(){
			if(this.value=='')
				this.value='Username';
	});

	$('#password').focus(function(){ 
		password_change();
	});
	
	//login si apre in shadowbox
	$('.loginform').submit(function(){
		
			Shadowbox.open({
				type:'iframe',
				content:$('base').href+'user.jsp?act=0&username0='+$('#username').val()+'&password0='+$('#password').val()+'&article='+$('#article').val(),
				height:410,
				width:520
			});
		
		return false;
	});
	
	
	//gallery semplice gallery 3
	$('.jackgallery3').mouseenter(function (){
		if ($('.jackgallery3 .tbcont .slider img').length>1){
			$('.jackgallery3 .tbcont').stop(true, true).fadeIn();
		}
	}).mouseleave(function (){
		$('.jackgallery3 .tbcont').stop(true, true).fadeOut();
	});
	$('.jackgallery3 .tbcont img').mouseenter(function(){
		$(this).stop().animate({'opacity':'1'});
	}).mouseleave(function(){
		$(this).stop().animate({'opacity':'0.7'});
	}).click(function(){
		clearInterval(gallery3interval);
		$questo=$(this);
		$('.jackgallery3 .main').fadeOut(500, function(){
			$('.jackgallery3 .main').attr('src', '').attr('alt', '');
			$('.jackgallery3 .main').attr('src', $questo.attr('title')).attr('alt', $questo.attr('alt')).load(function(){
				$('.jackgallery3 .main').show();
				$('.jackgallery3 .fade').hide();
			});
		});
		$('.jackgallery3 .fade').fadeIn(300).attr('src', $(this).attr('title')).attr('alt', $(this).attr('alt'));
		$('.jackgallery3 .tbcont img').removeClass('actual');
		$(this).addClass('actual');
		$('.jackgallery3 .dett_foto').html("<h1>"+$questo.attr('alt').split("=")[0]+"</h1><p>Foto di <br /><strong>"+$questo.attr('alt').split("=")[1]+"</strong></p>");
		gallery3interval=setInterval('gallery3scorri()', 6000);
		return false;
	});
	if ($('.jackgallery3').length>0){
		gallery3interval=setInterval('gallery3scorri()', 6000);
	}
	var widthsliderjackgallery3 = ($('.jackgallery3 .tbcont .slider img').length)*132;
	$('.jackgallery3 .tbcont .slider').css('width', widthsliderjackgallery3+'px')
	var sinistramassima = -(parseInt($('.jackgallery3 .tbcont .slider').css('width'))-(parseInt($('.jackgallery3 .tbcont').css('width'))));
	
	makeScrollable($('.tbcont'), $('.slider'));
	
});
