(function($){
$(document).ready(function() {

	
	/* PAGELOAD POSITION FOOTER */
	if ($(window).height()<740) 
			$('#footer').css({'position':'relative'});
		else
			$('#footer').css({'position':'fixed'});	

			
	/* MENIU */
	$('#menu').show();
	$('#menu li:not(.off)').hover(
		function(){
			if ($(this).hasClass('sub')){
				$(this).css({'background':'#acacac','color':'#000'});
				height=$('ul li.l1',this).size()*24+5;
				$('ul',this).stop().animate({height:height+'px'},300);
			} else {
				$(this).css({'background':'#acacac','color':'#000'});
			}
			
		},
		function(){
			if ($(this).hasClass('sub')){
				$('ul',this).stop().animate({height:'0px'},200);
				$(this).css({'background':'','color':'#fff'});
				$('li.l2').slideUp();
			} else {
				$(this).css({'background':'','color':'#fff'});
			}
			
		}
	);
	
	$('#menu li.gal li.l1').click(
		function(){	
			cat = $('span',this).html();
			$('li.l2').hide();
			$('#menu li.gal li.l2[rel="'+cat+'"]').slideDown();
			height=$('#menu li.gal li.l2[rel="'+cat+'"]').size()*24;
			heighta=$('#menu li.gal li.l1').size()*24+5;
			height=height+heighta;			
			if ($('#menu li.gal ul').height()>height){
				cl=this;
				setTimeout("$(cl).parent().stop().animate({height:height+'px'},300);",10);
			}
			else {
				$(this).parent().stop().animate({height:height+'px'},200);
			}
			
		}

	);
	
	/* GALERIE */
	$('.thumbs img:not(.active)').live('click',function(){		
			$('.object .desc').html('loading...');
			$('.object .desc').fadeIn();
			element=$(this);
			$('.thumbs img.active').removeClass('active');
			$(element).addClass('active');
			$('img.mainpic').stop().animate({opacity:0},100,function(){
				$(this).attr('src',$(element).parent().attr('rel'));
				$('a.mainpic').attr('href',$(element).parent().attr('rel'));
				$(this).load(function(){
					$(this).animate({opacity:1},100);					
					if ($(element).attr('alt')=='')
					$('.object .desc').fadeOut();
					$('.object .desc').html($(element).attr('alt'));
				});
			});
		
	});
	
	/* ADD BLITZ */
	function objectBlitz(element,maxW,maxH,elW,elH,nro,min){
		var o=['<div class="blitz" style="top:','px;left:','px;" /></div>'];
		var text=$('#'+element).html();;
		for (var i=1;i<=nro;i++){
			text=text+o[0]+Math.floor(Math.random()*(maxH-elH))+o[1]+(Math.floor(Math.random()*(maxW-elW))+min)+o[2];
		}
		$('#'+element).html(text);
	}
	
	function addBlitz(){
		$('#blacktop,#blackbottom').html('');
		objectBlitz('blacktop',960,90,40,40,5,0);
		objectBlitz('blackbottom',350,110,40,40,3,0);
		objectBlitz('blackbottom',350,110,40,40,2,610);
		$('#blacktop,#blackbottom').animate({opacity:1});		
	}
	addBlitz();

	
	/* FANCYBOX */
	$("#map a").fancybox({
		'titleShow' : false,
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'easingIn' : 'easeOutBack',
		'easingOut' : 'easeInBack' ,
		'overlayShow' :false
	});	
	$("a.mainpic").fancybox({
		'titleShow' : false,
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'easingIn' : 'easeOutBack',
		'easingOut' : 'easeInBack' ,
		'overlayShow' :false
	});	
	
	//$('#pane').jScrollPane({showArrows:true});
	
	
	/* SIGLA FOOTER */
	$('#footer a.on').css('opacity','0');
	$('#footer .middle').mouseenter(
		function(){
			$("#footer a.off").stop().animate({opacity:'0'},800);
			$('#footer a.on').stop().animate({opacity:'1'},200);
		});
	$('#footer .middle').mouseleave(
		function(){
			$("#footer a.off").stop().animate({opacity:'1'},90);
			$('#footer a.on').stop().animate({opacity:'0'}),800;
		}
	);
	
	
	/* FOOTER POSITION ON RESIZE */
	$(window).resize(function() {
		if ($(this).height()<740) 
			$('#footer').css({'position':'relative'});
		else
			$('#footer').css({'position':'fixed'});	
	});

	
	/* CLEAR INPUT */
	$('input:text').each(function(){
		var default_value = $(this).val();
		$(this).focus(function(){
			if ($(this).val() == default_value) $(this).val("");
		});
		$(this).blur(function(){
			if ($(this).val() == "") $(this).val(default_value);
		});
	});
	
	
	/* INPUT HOVER */
	$('input,textarea').live('mouseover',function(){$(this).addClass('over');});
	$('input,textarea').live('mouseout',function(){$(this).removeClass('over');});
	
	
	/* LINK TARGET */
	$('a.external').attr("target", "_blank");
	$('#wall a').attr("target", "_blank");
	
	
	/* TANIA */
	mlt = -Math.floor(Math.random()*3+1)*220;
	$('#tania div').stop().animate({marginLeft:mlt},10,function(){
		$('#tania').slideDown(1000);
	});
	
	
	
	/* HOMEPAGE SLIDESHOW */
	function onAfter() { 
		if ($('#control').css('margin-bottom')!='0px')
		$('#jcbar div').stop().animate({marginLeft:'0px'},4500);
	}
	function onBefore() { 
		$('#jcbar div').stop().animate({marginLeft:'-84px'},600);
	}
	
	if ( $('#slideshow').width()>0 ) {
	$('#slideshow').cycle({ 
		fx:     'fade', 
		speed:  1500, 
		timeout: 6000,
		pause: true,
		prev:   '#prev', 
		next:   '#next',
		after: onAfter,
		before: onBefore 
	});
	}
	
	$('.object.homepage ').hover(function(){
		$('#control').stop().animate({marginBottom:'0px'},400);
		$('#jcbar div').stop().animate({marginLeft:$('#jcbar div').css('margin-left')},0);
	},function(){
		$('#control').stop().animate({marginBottom:'-50px'},400);
		$('#jcbar div').stop().animate({marginLeft:'0px'},4500);
	});
	
	$('#play').toggle(function(){
		$('#slideshow').cycle('pause');
		$('img',this).animate({marginTop:'-22px'});
	},function(){
		$('#slideshow').cycle('resume',true); 
		$('img',this).animate({marginTop:'0px'});
	});
	
});

})(jQuery);;




