/*--------------------------------------------------
S P A F O R M
www.spaform.it
by websolute
--------------------------------------------------*/

/*--------------------------------------------------
MENU PRINCIPALE
--------------------------------------------------*/
function AlignMenu() {
			
	/* Elementi da allineare */
	var menuItem = "#navTop li";	
	var menuSize = $(menuItem).size();
	var menuWidth = 960;
	var liWidth = 0;
	var totActualLiWidth = 0;
	var totSpaceToDivide = 0;
	var liSpaceToAdd = 0;
	var halfMargin = 0;
	var totActualLiOuterWidth = 0;
	
		
	$(menuItem).each(function(i){totActualLiWidth += $(this).width();});
	
	totSpaceToDivide = menuWidth - totActualLiWidth;
	liSpaceToAdd = Math.floor(totSpaceToDivide / (menuSize-1));	
	
	$(menuItem).each(function(i){
		if(i<(menuSize-1)) {
			$(this).css({'padding-right' : liSpaceToAdd-1 + 'px'});			
		}			
	});
	
	$('#navTop').css('visibility', 'visible');
}

/*--------------------------------------------------
Slideshow
--------------------------------------------------*/
function slideshow(){
	var increment = 0;
	var slideSize = $("#slideshow .item").size();
	var winW = $(window).width();
	var loop;
	
	// Inserisce le immagini sullo sfondo del div
	$("#slideshow .item").each(function(){
		img = $(this).find("img").attr("src");
		$(this).css({'background':'url(' + img + ') no-repeat center top', 'width': winW}).find('img').remove();
	});	
	
	$(window).resize(function(){
		$("#slideshow .item").css({"width" : $(window).width()});
		moveOn(increment);
	});	
		
	// balls
	for (i=0; i<slideSize; i++){
		$("#wrapBalls").append('<div class="ball"><a href="#" rel="' + i + '"></a></div>');
	}
	
	$("#wrapBalls a:first").addClass("on");
	
	function moveOn(parametro){
		$("#wrapBalls .ball a").removeClass("on").eq(parametro).addClass("on");
				
		$("#wrapSlideshow #slideshow").stop().animate({"left":-($(window).width()*parametro) + "px"},2000);
		clearTimeout(loop);
		loop = setTimeout(function(){			
			increment = increment + 1;
			if (increment >= slideSize){
				increment = 0;
			}
			moveOn(increment);
		},5000);
	}
	
	$("#wrapBalls .ball a").click(function(){return false;}).mouseover(function(){
		clearTimeout(loop);
		increment = $(this).attr('rel');
		moveOn(increment);
	});
	
	$(".ball").mouseover(function(){
		clearTimeout(loop);
	});
	
	moveOn(increment);
}


/*--------------------------------------------------
Accordion MENU DX
--------------------------------------------------*/
function menuDx(){	
	$('.accordion').each(function(e,f){			
		
		$('li>a', f).click(function(event){
			//var actualLev = $('li>a', f).parent().parent();
			//console.log(actualLev);
			if( $(this).parent().find('img').size() > 0){
				
				$(this).parent().siblings().not($(this).parent()).find('img').addClass('rotate0').removeClass('rotate180');
				$(this).parent().siblings().not($(this).parent()).find('>div').slideUp(500);
				$(this).parent().find('>img').toggleClass('rotate180'); //.toggleClass('rotate0');
				$(this).parent().find('>div').slideToggle(500);
				$('li>a', f).not(this).removeClass('open');
				//$('li>a', actualLev).not(this).removeClass('open');
				$(this).toggleClass('open');
				return false;
			} 		
		});
		function SlidePrefooter(){
			var diff = parseInt($(f).height() - $('#main').height());
			$('#main').animate({
				'height' : $('#main').height() + diff + 'px'
			});
			//trace("diff: " + diff + " Main: " + $('#main').height() + " f:" + $(f).height());
		}		
	});
}

/*--------------------------------------------------
Outline
--------------------------------------------------*/
function KillOutline(){
	$('a').focus(function(){
			 $(this).attr("hideFocus", "hidefocus");
	});
}

/*--------------------------------------------------
tooltip
--------------------------------------------------*/
	
var X = 0;
var Y = 0;

function tooltip(){
	$("body").append('<div id="tooltip"></div>');
	
	function moveTooltip(){		
	
		maxX = $(window).width() - $("#tooltip").outerWidth() - 40;
		maxY = $(window).height() + $(window).scrollTop() - $("#tooltip").outerHeight() - 40;

		if (X > maxX) {
			X = maxX;
		}
		
		if (Y > maxY) {
			Y = maxY;
		}		
		
		$("#tooltip").css({"top":Y+"px","left":X+"px"}).show();
		
	}
	
	$(".schedaIcone li").each(function(){
		$(this).attr("rel",$(this).attr("title")).removeAttr("title");
	
	
		$(this).mouseover(function(e){			
			X = e.pageX + 10;
			Y = e.pageY - 30;

			var testoTooltip = $(this).attr("rel");
			$("#tooltip").html(testoTooltip);
			
			moveTooltip();
		}).mouseout(function(){
			$("#tooltip").hide();
		}).mousemove(function(e){
			X = e.pageX + 10;
			Y = e.pageY - 30;

			moveTooltip();
		});
		
		$("#tooltip").mouseover(function(){
			$(this).show();
		}).mouseout(function(){
			$(this).hide();
		});
	});
}


/*--------------------------------------------------
Trace
--------------------------------------------------*/
function trace(myMessage){
	var printMessage = myMessage.toString();
	if( $('#trace').size() < 1 ) {
		$('body').append('<div id="trace"></div>');
		$('#trace').css({
			'position' : 'fixed',
			'color' : 'black',
			'bottom' : '0',
			'right' : '0',
			'background-color' : '#fff', 
			'width' : '200px',
			'text-align' : 'left',
			'font-size' : '12px',
			'font-family' : 'Courier New',
			'padding' : '5px',
			'margin' : '0 auto',
			'border' : '1px solid black'
		});
	}
	$('#trace').append('<p>' + printMessage + '</p>');
}

/*--------------------------------------------------
SHOW BUBBLE
--------------------------------------------------*/
function showBubble(){
	$(".cont_bubble .item .link a").mouseover(function(){
		$(this).css({background:"#fff",opacity:0}).parent().next().show();
		$(this).parents(".item").css({"z-index":10});
	}).mouseout(function(){
		$(this).parent().next().hide();
		$(this).parents(".item").css({"z-index":2});
	});
	$(".cont_bubble .item .link a").click(function(){
		return false
	});

	$(".cont_bubble_7 .item .link a").mouseover(function(){
		$(this).css({background:"#fff",opacity:0}).parent().next().show();
		$(this).parents(".item").css({"z-index":10});
	}).mouseout(function(){
		$(this).parent().next().hide();
		$(this).parents(".item").css({"z-index":2});
	});
	$(".cont_bubble_7 .item .link a").click(function(){
		return false
	});

	$(".cont_bubble_8 .item .link a").mouseover(function(){
		$(this).css({background:"#fff",opacity:0}).parent().next().show();
		$(this).parents(".item").css({"z-index":10});
	}).mouseout(function(){
		$(this).parent().next().hide();
		$(this).parents(".item").css({"z-index":2});
	});
	$(".cont_bubble_8 .item .link a").click(function(){
		return false
	});

	$(".cont_bubble_9 .item .link a").mouseover(function(){
		$(this).css({background:"#fff",opacity:0}).parent().next().show();
		$(this).parents(".item").css({"z-index":10});
	}).mouseout(function(){
		$(this).parent().next().hide();
		$(this).parents(".item").css({"z-index":2});
	});
	$(".cont_bubble_9 .item .link a").click(function(){
		return false
	});
}

/*--------------------------------------------------
SCROLL DETTAGLI
--------------------------------------------------*/
function scrollDettagli(){
    $(".numerazione a.avanti").click(function() {
        //alert ("avanti")
        $("#dettagli_scroll").animate({ left: "-729px" }, 500);
        //$(this).addClass("on",'color:#0088CC; background-position: left top;');
        //$(this).css('on');
        $(this).css('background-position', 'left top')
        $('.indietro').css('background-position', 'left bottom');
        //$(this).prev().removeClass("on");
        return false
    });
	
	$(".numerazione a.indietro").click(function(){
		$("#dettagli_scroll").animate({left:"0"},500);
		//$(this).addClass("on", 'color:#0088CC; background-position: left top;');
		$(this).css('background-position', 'left top')
		$('.avanti').css('background-position', 'left bottom');
		//$(this).next().removeClass("on");
		return false
	});
}

/*--------------------------------------------------
SHOW LINEA
--------------------------------------------------*/
function showLinea(){
	i = 0
	$(".linea").each(function(){
		altezza = $(this).height();
		if (altezza >= i) {
			i = altezza
		}
		
	});
	$(".linea").css({height:i});
	
	$(".linea").mouseover(function(){
		$(this).css({background:"#fff"}).children(".hover_sfondo").show();
	}).mouseout(function(){
		$(this).css({background:"none"}).children(".hover_sfondo").hide();
	});
}

/*--------------------------------------------------
SHOW JET
--------------------------------------------------*/
function showJet(){
	$("#jet li").mouseover(function(){
		$(this).children(".hover_jet").show();
		$(this).children(".hover_jet_ultimo").show();
	}).mouseout(function(){
		$(this).children(".hover_jet").hide();
		$(this).children(".hover_jet_ultimo").hide();
	});
}

/*--------------------------------------------------
LANCIA FANCY
--------------------------------------------------*/
function lanciaFancybox(){
	$(".fancy").fancybox();
	$(".fancyiframe").fancybox({type:"iframe",width:"90%",height:"90%"});
	$(".fancyiframeprodotto").fancybox({type:"iframe",width:650,height:470,scrolling:"no"});
}

/*--------------------------------------------------
Select Trasform
--------------------------------------------------*/
function beautifySelect() {
    $("div.sel_modify").each(function() {
        $(this).append('<span class="select close"><span style="width: 550px; overflow: hidden">' + $(this).find("option:selected").html() + '</span></span>').find("select").css({ opacity: 0 }).change(function() {
        $(this).parent().find("span.select span").html($(this).find(':selected').html());
        });
    })
};

/*--------------------------------------------------
add Clear to li
--------------------------------------------------*/
function addClear(){
	$("form ul li").not($("form ul li.doppia_colonna")).addClass("clear");
}

/*--------------------------------------------------
hideMenuTitle
--------------------------------------------------*/
function hideMenuTitle(){
	$('#navTop a').each(function(i,e){
		var tit = $(e).attr('title');
		$(e).hover(
			function(){
				$(e).attr('title', '');
			},
			function(){
				$(e).attr('title', tit);
			}
		);
	});
}

/*--------------------------------------------------
INIT
--------------------------------------------------*/
$(document).ready(function() {
    slideshow();
    menuDx();
    tooltip();
    showBubble();
    scrollDettagli();
    showLinea();
    showJet();
    lanciaFancybox();
    beautifySelect();
    addClear();
	hideMenuTitle();
	KillOutline();
		
    //alert("WARNING: Everything's fine.");

    /*--------------------------------------------------
    MENU LINGUA
    --------------------------------------------------*/
    $("#lgSelect a").click(function() {
        var url;
        var lg = $(this).text().toLowerCase();
        url = 'http://' + window.location.host;
        if (url.indexOf('wslabs') == -1) {
            if (lg != 'it') {
                url = url.replace('.it', '.com')
                url += '/' + lg;
            }
            else {
                url = url.replace('.com', '.it')
            }
        }
        else {
            if (lg != 'it') {
                url += '/' + lg;
            }
        }
        url += '/' + 'viewdoc.aspx';
        url += window.location.search;
        $(window.location).attr('href', url);
        return false;
    });

    $('.numerazione .indietro').css('background-position', 'left top')
});

/*--------------------------------------------------
WIN LOAD
--------------------------------------------------*/
$(window).load(function(){
	AlignMenu();
});
