var closewin=false;

$(document).ready(function() { 
	//Funzione per aggiungere l'effetto Onclick ai prodotti
	$('.top-main').click(function(){closePopup();});
	$('#prodotti').click(function() {
  		
		if(!$('#prodotti').hasClass('active'))
			$('#prodotti').addClass('active');
		if($('#collezioni').hasClass('active'))
			$('#collezioni').removeClass('active');
		if($('#azienda').hasClass('active'))
			$('#azienda').removeClass('active');
		/*	
		if ($('.box-main-menu3').position().top == 708) {
			px = '-=64px';
			$('.box-main-menu3').animate({
				top: px
			}, 500, function() {
   				showMenu(0,1,0);
 		 });
		}
		else
		*/
			showMenu(0,1,0);
	});
	
	$('#collezioni').click(function() {
  		
		if(!$('#collezioni').hasClass('active'))
			$('#collezioni').addClass('active');
		if($('#prodotti').hasClass('active'))
			$('#prodotti').removeClass('active');
		if($('#azienda').hasClass('active'))
			$('#azienda').removeClass('active');
		if ($('.box-main-menu3').position().top == 708) {
			px = '-=64px';
			$('.box-main-menu3').animate({
				top: px
			}, 500, function() {
   			showMenu(0,1,1,false);
 		 });
		}
		else
			showMenu(0,1,1,false);
	});
	
	$('#azienda').click(function() {
  		
		if(!$('#azienda').hasClass('active'))
			$('#azienda').addClass('active');
		if($('#prodotti').hasClass('active'))
			$('#prodotti').removeClass('active');
		if($('#collezioni').hasClass('active'))
			$('#collezioni').removeClass('active');
		if ($('.box-main-menu3').position().top == 708) {
			px = '-=64px';
			$('.box-main-menu3').animate({
				top: px
			}, 500, function() {
    		showMenu(0,1,2,false);
  		});
		}
		else
			showMenu(0,1,2,false);
	});
	
	
	$('#languages').click(function() {
  			$('#setLanguages').fadeToggle();	
  	});
  	
  	 
	 
  	
  	
});

function showPage(page) {
	closewin=false;
	load_page=page;
	$.post(load_page, function(responseText){
		if (responseText != '-1') {
			//Mostro la sottocategoria
			$('.top-main').html(responseText);
			$('.top-main').fadeIn('slow', function(){
				
				
			});
		}
	}, "html");
}

function showMenu(root,step,tipologia,endChild) {
	$('#' + root+step+tipologia).parent().parent().children("li").each(function(i,e){
		$(e).children("a").each(function(i2,e2){

				$(e2).removeClass();
		});
	});
	
	checkMenuOpen(step);
	if(!endChild) {
		$('#' + root+step+tipologia).addClass('active2');
		$.post('menu.php', {root: root,step: step,tipologia:tipologia}, function(responseText){
			if (responseText != '-1') {
				//Mostro la sottocategoria
				$('#step' + step).html(responseText);
				$('#step' + step).fadeIn('slow', function(){});
				//$('#step' + step).show();
			}
			else {
				$('#' + root+step+tipologia).removeClass();
				$('#' + root+step+tipologia).addClass('active');
				if($('.top-main').css("display") == "block") {
					$('.top-main').hide();
					$(".top-main div").remove();
				}
				if(tipologia==0) {
					load_page='collezioni.php';
					$('.box-main-menu').fadeOut(1000);	
				}
				else if(tipologia==1)
					load_page='collezioni_topic.php';
				else
					load_page='company.php';
				$.post(load_page, {root: root,step: step,tipologia: tipologia}, function(responseText){
					if (responseText != '-1') {
						//Mostro la sottocategoria
						
						$('.top-main').html(responseText);
						$('.top-main').fadeIn('slow', function(){
							/*
							$('.top-main').click(function(){closePopup();});
							$('.box-main-menu').click(function(){closePopup();});
							$('#box-trasp').click(function(){doNothing();});
							*/
						});
						
					}
				}, "html");
			}
			
		}, "html");
	}
	else {
		$('#' + root+step+tipologia).addClass('active');
		if($('.top-main').css("display") == "block") {
			$('.top-main').hide();
			$(".top-main div").remove();
		}
		if(tipologia==0) {
			load_page='collezioni.php';
			$('.box-main-menu').fadeOut(1000);
		}
		else if(tipologia==1)
			load_page='collezioni_topic.php';
		else
			load_page='company.php';
		$.post(load_page, {root: root,step: step,tipologia: tipologia}, function(responseText){
			if (responseText != '-1') {
				//Mostro la sottocategoria
				$('.top-main').html(responseText);
				$('.top-main').fadeIn('slow', function(){
					
				});
				
			}
		}, "html");
	}
	
}

function checkMenuOpen(step) {
	var max_menu=4;
	for(i=max_menu;i>=step;i--) {
		if($("#step"+i).css("display") == "block") {
			 $("#step"+i).hide();
			 $("#step"+i+" ul").remove();
		}

	}
	
}

function closePopup() {
	if(closewin) {
	if ($('#box_tutti').length > 0 || $('.box-contents-top').length > 0 || $('#box-trasp').length > 0){
		$('.top-main').fadeOut('slow', function() {
			$('.top-main div').remove();
		});
	}
	}
	else
		closewin=true;
}

function doNothing() {
	closewin=false;
}



function showVideo(link) {
	showPage('youtube.php?link='+link);
}

function fullshowproduct() {
	$('#dettScheda').fadeToggle(1000, function () {
    	if($('#dettScheda').css("display") == "block") {
    		$('.top-claim-page-coll').addClass('top-claim-page-coll_bg');
    		$("#imgfullscreen").attr("src","./static-content/img/arrowProdDown.png");
    	}
    	else {
    		$('.top-claim-page-coll').removeClass('top-claim-page-coll_bg');
    		$("#imgfullscreen").attr("src","./static-content/img/arrowProdRight.png");
    	}

  	});
}



