var num_colonne;
var num_righe;
var colonne_da_riempire;
var resize_timer;
var menu_footer_click = false;

$(document).ready(function(){
	
	$('#grid_container .grid_3').css('opacity','0');
	$('#menu_1 video').css('opacity','0');
    $('#img_logo').hide();
    $('#sotto_logo').css('opacity','0');
    $('#header #menu_top ul li').hide();
    $('#anim_logo').css('width','16px');
   
    $('#menu_top .menu_line:last').css('opacity','0');
    $('#menufooter .menu_line:last').css('opacity','0');
    $('#pulsanti_menu_portfolio .sez_portfolio').css('opacity','0');
    $('#sfondo_menu_portfolio').css('height','0px');
    
    $('#artwork .art_1').css('margin-left',-(1680-$(window).width())/2);
	$('#artwork .art_2').css('margin-left',-(1680-$(window).width())/2);
	$('#backgrounds').css('width',$(window).width())
   
    $('#artwork').css('width',$(window).width());
	
	
	$('#dope').hide();
    
    if(window.location.pathname == "/"){
    	$('#facebook').hide();
    	$('#tweeter').hide();
    	 $('#sotm_dope').hide();
    	$('#artwork .art_1').hide();
        $('#artwork .art_2').hide();
        $('#logo').css('margin-top',-$('#logo').height());
    } else {
    	$('#bk_left').css('width',($(window).width()-960)/2);
        $('#bk_left').css('left', '0');
        
        $('#bk_right').css('width',$(window).width()-(($(window).width()-960)/2+185));
        $('#bk_right').css('right','0');
        
        $('#logo').css('margin-top','0');
        $('#img_logo').show();
        $('#anim_logo').css('width','170px');
        $('#header #menu_top ul li').show();
        
        $('#menu_1').css('background','#282828');
        $('#artwork .art_1').show();
        
        if(corrente == "index"){
        $('#artwork .art_2').show();
        $('#artwork .art_2').delay(400).fadeOut(600, 'easeOutQuart');
        }
    }
	Cufon.replace('#header #menu_top ul li, #menufooter li, #menu_portfolio .sez_portfolio',{ fontFamily: 'Helvetica67' ,  hover: true });

    $('#wrap').show();
 
   
    
});
$(window).resize(function(){
	$('#artwork .art_1').css('margin-left',-(1680-$(window).width())/2);
	$('#artwork .art_2').css('margin-left',-(1680-$(window).width())/2);
	  $('#backgrounds').css('width',$(window).width())
	   
	    $('#artwork').css('width',$(window).width());
	  $('#bk_left').css('width',($(window).width()-960)/2);
	    $('#bk_left').css('left', '0');
	    
	    $('#bk_right').css('width',$(window).width()-(($(window).width()-960)/2+185));
        $('#bk_right').css('right','0');
	    
	    $('#logo').css('margin-top','0');
	    $('#footerPortfolio').css('width',$(window).width()-(($(window).width()-960)/2)-185);
	 	spostamento_max_x = -($('#grid_portfolio').width()-$('#footerPortfolio').width())-2;
	 	  
	    if($(window).width() < 960) {
		    $('#bk_right').css('width',775);
		    $('#artwork').css('width','960px');
		    $('#artwork .art_1').css('margin-left',-(1680-960)/2);
			$('#artwork .art_2').css('margin-left',-(1680-960)/2);
			  $('#backgrounds').css('width','960px');
			
			 


	    }

	    clearTimeout(resize_timer);
	    resize_timer = setTimeout(resize_footer, 300);

	    });
	


function resize_footer(){
	

	$('#grid_container .grid_3').each(function(i){
    	$(this).stop().clearQueue().delay(5*i).animate({
	      opacity: 0
	    }, 400, 'easeOutQuart');
	});
    	
    
    	$('#logo').stop().clearQueue().delay(1200).animate({    // anim pezzotta per temporizzare il footer
            opacity: 1
        }, 1, 'easeOutExpo',function(){
        	
    	$('#grid_container .appended').remove();
    	
        num_colonne = Math.ceil($(window).width()/185)+1;
        num_righe = Math.ceil($('#grid_container .grid_3').length/num_colonne);
        colonne_da_riempire = (num_colonne*num_righe)-$('#grid_container .grid_3').length
      
        var random_num = new Array();
        for (i=1; i<=num_colonne*(num_righe-1); i++) {
        random_num.push(i);
        }
        shuffle(random_num);
       
       
        $('#grid_container').css('width',185*num_colonne);
    	$('#footerImg').css('width','100%');
        if($(window).width() < 960) {
        		$('#footerImg').css('width','960px');
        	   $('#grid_container').css('width','1110px');
        }
        $('#grid_container').css('margin-left', $('#menu_1').offset().left-Math.ceil($('#menu_1').offset().left/185)*185);
        
        
        
        for (i=0; i<colonne_da_riempire; i++) { 
        $('#grid_container').append('<div class="grid_3 q_185_185 appended">'+$('#grid_container .grid_3:nth-child('+random_num[i]+')').html()+'</div>')
        }
        
        $('#grid_container .grid_3').css('opacity','0');
        $(' #grid_container .fondo_small').css('opacity','0.5');
        
        $('#backgrounds').css('height',1295+185*num_righe);	
        
        mostra_footer();
        
        
        })
}
$(window).load(function(){
	
	$().UItoTop({ easingType: 'easeOutQuart' });
    
	 if(window.location.pathname == "/"){
    	time_delay = 0;
    
    $('#bk_left').css('width',0);
    $('#bk_left').css('left', $('#main').offset().left+185);
    
    $('#bk_right').css('width',$(window).width()-($('#main').offset().left+185));
    $('#bk_right').css('right', -$('#bk_right').width());
    
    $('#bk_left').delay(1000).animate({
        left: 0,
        width:$('#main').offset().left+185
    }, 800, 'easeInExpo');
    
    $('#bk_right').delay(600).animate({
        right: 0
    }, 1400, 'easeOutExpo');
    
    $('#facebook').delay(900).fadeTo(600,1,'easeOutQuart');
    $('#tweeter').delay(950).fadeTo(600,1,'easeOutQuart');
    
    
    $('#logo').delay(600).animate({
        marginTop: 0
    }, 800, 'easeOutExpo',function(){$(window).bind('scroll',gestione_scroll_footer)});
    
    $('#img_logo').delay(200).fadeIn(400, 'easeOutQuart');
    
    $('#menu_1 video').delay(3500).animate({
       opacity: 1
     }, 900, 'easeOutQuart',function(){$('#menu_1').css('background','#282828');});
    
   $('#anim_logo').delay(1000).animate({
        width: 170
    }, 600, 'easeInOutExpo');
   
   $('#artwork .art_1').delay(2600).fadeIn(600, 'easeOutQuart');
   
   $('#header #menu_top ul li').each(function(i){
   
   	
   $('#header #menu_top ul li:nth-child('+(i+1)+')').delay(1000+(20*i)).fadeIn(800, 'easeOutQuart');
 	
 
   $('#sotm_dope').delay(1200).fadeIn(600,'easeOutQuart');
   
   $('#bk_left').delay(100).animate({
 	       
         width:$('#main').offset().left
     }, 1200, 'easeOutExpo');
  });
} else {
	time_delay = 1000;
	$(window).bind('scroll',gestione_scroll_footer);
	
    $('#menu_1 video').delay(1000).animate({
        opacity: 1
      }, 900, 'easeOutQuart');
    
    
    $('#links ul li a').click(function(e){
    	e.preventDefault();
    
    	destinazione = $(this).attr('href').replace('/','')
    	identifica_corrente();
    	
    });
	
}
    
    $('#anim_logo').click(function(){
    	if(corrente != "index"){
			destinazione = "index" // invio della prossima pagina al gestore del menu
			identifica_corrente();
		}
    });
    
    // gestione footer
    
    num_colonne = Math.ceil($(window).width()/185)+1;
    num_righe = Math.ceil($('#grid_container .grid_3').length/num_colonne);
    colonne_da_riempire = (num_colonne*num_righe)-$('#grid_container .grid_3').length
  
    var random_num = new Array();
    for (i=1; i<=num_colonne*(num_righe-1); i++) {
    random_num.push(i);
    }
    shuffle(random_num);
   
   
    $('#grid_container').css('width',185*num_colonne);
    if($(window).width() < 960) {
		$('#footerImg').css('width','960px');
	   $('#grid_container').css('width','1110px');
}
    $('#grid_container').css('margin-left', $('#menu_1').offset().left-Math.ceil($('#menu_1').offset().left/185)*185);
    
    
    
    for (i=0; i<colonne_da_riempire; i++) { 
    $('#grid_container').append('<div class="grid_3 q_185_185 appended">'+$('#grid_container .grid_3:nth-child('+random_num[i]+')').html()+'</div>')
    }
    
    $('#grid_container .grid_3').css('opacity','0');
    $(' #grid_container .fondo_small').css('opacity','0.5');
    
    if($(window).height()>1295+30 || $(window).height()+$(window).scrollTop()>1295+30) {  	
    	$('#logo').delay(2600).animate({    // anim pezzotta per temporizzare il footer
            opacity: 1
        }, 1, 'easeOutExpo',function(){mostra_footer()});
    }
    
    $('#grid_container .grid_3').css('cursor','pointer');
    
    $('#grid_container .grid_3').mouseenter(function(){
    	 $(this).append("<div class=\"plus_footer\"><img src=\"../../style/img/piu.png\"/></div>");
    	$(this).find('.fondo_small').stop().animate({
    	       opacity: 0
        }, 400, 'easeOutQuart');
    	$(this).find('.plus_footer').animate({
    	       opacity: 1,
    	       marginTop:-107
        }, 250, 'easeOutQuart');
    	
    })
     $('#grid_container .grid_3').mouseleave(function(){
    	 $(this).find('.plus_footer').stop().animate({
  	       opacity: 0
  	     
      }, 200, 'easeOutCubic');
    	$(this).find('.plus_footer').remove();
    	
    	 $(this).find('.fondo_small').stop().animate({
  	       opacity: 0.5
      }, 400, 'easeOutQuart');
    	
    })
    
    $('#grid_container .grid_3').click(function(){
    	footer_click = true;
    	window.location = BASE_URL+$(this).find('a').attr('href');
    
    	
    })
    
    
      $('#backgrounds').css('height',1295+185*num_righe)
    
    $('#header #menu_top ul li a').bind('mouseover',seleziona_menu_sup);
      
	
    $('#header #menu_top ul li a').bind('mouseout',deseleziona_menu_sup);
    
    $('#header #menu_top ul li a').bind('click',vai_selezione);
    
    $('#menufooter li a').bind('click',vai_selezione);
    
    $('#menufooter li a').bind('mouseover',seleziona_menu_port);
    $('#menufooter li a').bind('mouseout',deseleziona_menu_port);
    
    
    	$('#facebook').mouseover(function(){
    		$('#facebook .facebook_logo').stop().animate({
    	  	       marginTop: -23,
    	    	     
    	      }, 300, 'easeOutExpo');
    	})
    	$('#facebook').mouseout(function(){
    		$('#facebook .facebook_logo').stop().animate({
    	  	       marginTop: 0,
    	    	     
    	      }, 600, 'easeInOutExpo');
    	})
    	
    	$('#tweeter').mouseover(function(){
    		$('#tweeter .tweeter_logo').stop().animate({
    	  	       marginTop: -23,
    	    	     
    	      }, 300, 'easeOutExpo');
    	})
    	$('#tweeter').mouseout(function(){
    		$('#tweeter .tweeter_logo').stop().animate({
    	  	       marginTop: 0,
    	    	     
    	      }, 600, 'easeInOutExpo');
    	})
    	$('#facebook').click(function(){
    		window.open('http://it-it.facebook.com/mediasoulelastic');

    	})
    	$('#tweeter').click(function(){
    		
    	})
    	
    
}); // fine window.load


function seleziona_menu_sup(){

	 $(this).css('color','#A6EE00');
	 
 	
}

function deseleziona_menu_sup(){
	
 	
 	if($(this).parent().parent().attr('id') == "menufooter"){
 		$(this).css('color','#FFFFFF');
 	} else {
 		 $(this).css('color','#666666');
 	}
 	
 	 
}

function seleziona_menu_port(){

	 $(this).css('color','#A6EE00');
	
	 
	
}

function deseleziona_menu_port(){
	 $(this).css('color','#FFFFFF');
	 
	
	 
}


// click dei pulsanti del menu (sia superiore che inferiore)

function vai_selezione(e){
	e.preventDefault();
$('#header #menu_top ul li a').unbind('mouseover',seleziona_menu_sup);
$('#header #menu_top ul li a').unbind('mouseout',deseleziona_menu_sup);
$('#header #menu_top ul li a').unbind('click',vai_selezione);


$(this).parent().siblings().find('a').bind('mouseover',seleziona_menu_sup);
$(this).parent().siblings().find('a').bind('mouseout',deseleziona_menu_sup);
$(this).parent().siblings().find('a').bind('click',vai_selezione);

$(this).parent().siblings().find('a').css('color','#666666');

if($(this).parent().parent().attr('id') == "menufooter"){
	
	$(this).parent().siblings().find('a').css('color','#FFFFFF');
}

Cufon.refresh();

	if($(this).text() == "Portfolio" && portfolio_open == false){
		mostra_menu_portfolio();
	} else if(portfolio_open == true){
	
		togli_menu_portfolio();
	}
	
	// verifica del pulsante premuto
	if($(this).text() == "Profilo"){
		
		destinazione = "profilo" // invio della prossima pagina al gestore del menu
			identifica_corrente();
	}
if($(this).text() == "Awards"){
		
		destinazione = "award" // invio della prossima pagina al gestore del menu
			identifica_corrente();
	}
	
	if($(this).text() == "Home"){
		
		if(corrente != "index"){
			destinazione = "index" // invio della prossima pagina al gestore del menu
			identifica_corrente();
		} else {
			$('#header #menu_top ul li a').bind('mouseover',seleziona_menu_sup);
			$('#header #menu_top ul li a').bind('mouseout',deseleziona_menu_sup);
			$('#header #menu_top ul li a').bind('click',vai_selezione);
			$('#header #menu_top ul').find('.segna_puls').remove();
			$('#header #menu_top ul li a').css('color','#666666');
   			$('body,html').stop(false,false).animate({scrollTop:0}, 1200, 'easeInOutExpo');  
   			
   			Cufon.refresh();
			
		}
	}
	if($(this).text() == "News"){
		
		if($(this).parent().parent().attr('id') == "menufooter"){
			$('#header #menu_top ul li a').bind('mouseover',seleziona_menu_sup);
			$('#header #menu_top ul li a').bind('mouseout',deseleziona_menu_sup);
			$('#header #menu_top ul li a').bind('click',vai_selezione);
			$('#header #menu_top ul').find('.segna_puls').remove();
			$('#header #menu_top ul li a').css('color','#666666');
		
			Cufon.refresh();
		}
   	 	if(corrente != "index"){
		destinazione = "index/?news=true" // invio della prossima pagina al gestore del menu
		identifica_corrente();
   	 	} else {
   			$('body,html').stop(false,false).animate({scrollTop:185}, 1200, 'easeInOutExpo');  

   	 	}
}
	if($(this).text() == "Contatti"){
		
	   	 
		$('body,html').stop(false,false).animate({scrollTop:1110}, 1200, 'easeInOutExpo');  

}
	
if($(this).parent().parent().parent().attr('id') == "menu_top"){
$(this).parent().siblings().find('.segna_puls').remove();
$(this).append('<img class="segna_puls" src="../../../style/img/segna_puls.png"/>');
$(this).find('.segna_puls').css('margin-left',-($(this).parent().width()+6)/2)
$(this).find('.segna_puls').css('margin-top','26px');
$(this).find('.segna_puls').delay(20).animate({
     marginTop: 16,
}, 200, 'easeOutExpo');
}
}

portfolio_open = false;

function mostra_menu_portfolio(){
	
	portfolio_open = true;
  	$('#sfondo_menu_portfolio').animate({
	       height:185,
 }, 800, 'easeInOutExpo');
  	
  	$('#pulsanti_menu_portfolio .sez_portfolio').each(function(e){
  		
  		$(this).delay(300+(30*e)).fadeTo(600,1,'easeOutQuart');
  		$(this).find('a').bind('mouseover',seleziona_menu_port);
  		$(this).find('a').bind('mouseout',deseleziona_menu_port);
  		$(this).find('a').bind('click',gestione_link_sottomenu);
  	})
  	
  	
	
}

function togli_menu_portfolio(){
	portfolio_open = false;
  	$('#sfondo_menu_portfolio').delay(0).animate({
	       height:0,
 }, 800, 'easeInOutExpo');
  	
  	$('#pulsanti_menu_portfolio .sez_portfolio').each(function(e){
  		
  		$(this).delay((30*(5-e))).fadeTo(600,0,'easeOutQuart');
  		$(this).find('a').unbind('mouseover',seleziona_menu_port);
  		$(this).find('a').unbind('mouseout',deseleziona_menu_port);
  	})
  	
  	
	
}

function mostra_footer(){
	
	if($(window).height()+$(window).scrollTop()>1295 + 30) {
		var random_num_1 = new Array();
		
		for (i=1; i<=num_colonne; i++) {
			random_num_1.push(i);
		}
		shuffle(random_num_1);
		
		
		for (i=0; i<num_colonne; i++) {
		$('#grid_container .grid_3:nth-child('+random_num_1[i]+')').stop().clearQueue().delay(100*i).animate({
	        opacity: 1
	    }, 800, 'easeOutQuart');
		}
	}
	if($(window).height()+$(window).scrollTop()>1295+185+30) {
			var random_num_2 = new Array();
		
		for (i=num_colonne+1; i<=num_colonne*2; i++) {
			random_num_2.push(i);
		}
		shuffle(random_num_2);
		
		
		for (i=0; i<num_colonne; i++) {
			
		$('#grid_container .grid_3:nth-child('+random_num_2[i]+')').stop().clearQueue().delay(100*i).animate({
	      opacity: 1
	    }, 800, 'easeOutQuart');
		}
		
	}
	if($(window).height()+$(window).scrollTop()>1295+(185*2)+30) {
		var random_num_3 = new Array();
		
		for (i=num_colonne*2+1; i<=num_colonne*3; i++) {
			random_num_3.push(i);
		}
		shuffle(random_num_3);
		
		
		for (i=0; i<num_colonne; i++) {
			
		$('#grid_container .grid_3:nth-child('+random_num_3[i]+')').stop().clearQueue().delay(100*i).animate({
	      opacity: 1
	    }, 800, 'easeOutQuart');
		}
		
	}
	if($(window).height()+$(window).scrollTop()>1295+(185*3)+30) {
var random_num_4 = new Array();
		
		for (i=num_colonne*3+1; i<=num_colonne*4; i++) {
			random_num_4.push(i);
		}
		shuffle(random_num_4);
		
		
		for (i=0; i<num_colonne; i++) {
			
		$('#grid_container .grid_3:nth-child('+random_num_4[i]+')').stop().clearQueue().delay(100*i).animate({
	      opacity: 1
	    }, 800, 'easeOutQuart');
		}
		
	}
	if($(window).height()+$(window).scrollTop()>1295+(185*4)+30) {
var random_num_5 = new Array();
		
		for (i=num_colonne*4+1; i<=num_colonne*5; i++) {
			random_num_5.push(i);
		}
		shuffle(random_num_5);
		
		
		for (i=0; i<num_colonne; i++) {
			
		$('#grid_container .grid_3:nth-child('+random_num_5[i]+')').stop().clearQueue().delay(100*i).animate({
	      opacity: 1
	    }, 800, 'easeOutQuart');
		}	
	}
	if($(window).height()+$(window).scrollTop()>1295+(185*5)+30) {
var random_num_6 = new Array();
		
		for (i=num_colonne*5+1; i<=num_colonne*6; i++) {
			random_num_6.push(i);
		}
		shuffle(random_num_6);
		
		
		for (i=0; i<num_colonne; i++) {
			
		$('#grid_container .grid_3:nth-child('+random_num_6[i]+')').stop().clearQueue().delay(100*i).animate({
	      opacity: 1
	    }, 800, 'easeOutQuart');
		}	
	}
	
};


// gestione scroll footer //

function gestione_scroll_footer(){

if($(window).height()+$(window).scrollTop()>1295+30) {
	  mostra_footer();
	}
if($(window).height()+$(window).scrollTop()<1295) {
	//  togli_footer();
	}
}


// array shuffle

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

var cof_altezza = 120;
var cof_altezza_2 = 120;


// GESTIONE LINK

function gestione_link_sottomenu(){
	switch($(this).text()) {
	
	case "CASES HISTORY":
	 destinazione = "cases-history";	
	break;
	case "SOUND DESIGN":
		 destinazione = "sound";	
		break;
	case "BRANDING / IDENTITY / PRINT":
		 destinazione = "print";	
		break;
	
	case "WEB DESIGN / WEB MARKETING":
		 destinazione = "web";	
		break;
	case "SOFTWARE WEB BASED":
		 destinazione = "software";	
		break;

}
	
	
	identifica_corrente();
	
};

var footer_click = false;
function identifica_corrente(){
	if(!footer_click){
	 $('body,html').stop(false,false).animate({scrollTop:0}, 1200, 'easeInOutExpo');  
	}
	 switch(corrente){
	case "index":
	rimuovi_blocchi_index(destinazione);
	break;
	case "sound":
		rimuovi_sound(destinazione);
		break;
	case "cases-history":
		rimuovi_case(destinazione);
		break;
	case "profilo":
		rimuovi_profilo(destinazione);
		break;
	case "print":
		rimuovi_portfolio(destinazione);
		break;
	case "web":
		rimuovi_portfolio(destinazione);
		break;
	case "software":
		rimuovi_portfolio(destinazione);
		break;
	case "award":
		rimuovi_award(destinazione);
		break;
	}
};

function two_cipher_num(arg){
	if(arg.toString().length < 2) {
		
		return "0"+arg
	}	else {
		return arg		
	}
	
}

(function($){
	$.fn.UItoTop = function(options) {

 		var defaults = {
			text: '',
			min: 200,
			inDelay:600,
			outDelay:400,
  			containerID: 'toTop',
			containerHoverID: 'toTopHover',
			scrollSpeed: 1200,
			easingType: 'linear'
 		};

 		var settings = $.extend(defaults, options);
		var containerIDhash = '#' + settings.containerID;
		var containerHoverIDHash = '#'+settings.containerHoverID;
		
		$('body').append('<a href="#" id="'+settings.containerID+'">'+settings.text+'</a>');
		$(containerIDhash).hide().click(function(){
			$('html, body').animate({scrollTop:0}, settings.scrollSpeed, settings.easingType);
			$('#'+settings.containerHoverID, this).stop().animate({'opacity': 0 }, settings.inDelay, settings.easingType);
			return false;
		})
		.prepend('<span id="'+settings.containerHoverID+'"></span>')
		.hover(function() {
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 1
				}, 600, 'linear');
			}, function() { 
				$(containerHoverIDHash, this).stop().animate({
					'opacity': 0
				}, 700, 'linear');
			});
					
		$(window).scroll(function() {
			var sd = $(window).scrollTop();
			if(typeof document.body.style.maxHeight === "undefined") {
				$(containerIDhash).css({
					'position': 'absolute',
					'top': $(window).scrollTop() + $(window).height() - 50
				});
			}
			if ( sd > settings.min ) 
				$(containerIDhash).fadeIn(settings.inDelay);
			else 
				$(containerIDhash).fadeOut(settings.Outdelay);
		});

};
})(jQuery);
