/******************************************************************************************************************/
//  SCROLL FUNCTIONS
/******************************************************************************************************************/

var ww = 0;
var wh = 0;
var cw = 0;
var ch = 0;

function adjustSize(){
	ww = $(window).width();
	wh = $(window).height();
	
	cw = $('#container').width();
	ch = $('#container').height();
	
	ml = (cw - ww) / 2;
	mt = 0;
	
	if (ch > wh) mt = (ch - wh) / 2;
	
	$('#container').css({ marginLeft: -ml, marginTop: -mt });
	$('#main').show();
}

function handleScroll(){
	$('.scrollto').click(function(){
		mytarget = $(this).attr('href');
		
		if (mytarget == '#paste') {
			if (ww <= 1024) ml = - (2600 - (( ww - 930) / 2));
			else ml = - (cw - ww) + 70;
			$('#container').animate({ marginLeft: ml }, 1000);
			$('#main').fadeOut(950);
		}
		
		if (mytarget == '#pastificio') {
			ml = -100;
			$('#container').animate({ marginLeft: ml }, 1000);
			$('#main').fadeOut(950);
		}
		
		if (mytarget == '#main') {
			$('#main').show();
			ml =  - ((cw - ww) / 2);
			$('#container').animate({ marginLeft: ml }, 1000);
		}
		
		return false;
	});
}

function handleArrows(){
	$('.scrollto').hover(function(){
		if ($(this).hasClass('left')) $(this).animate({ marginLeft: -10 }, 100, function(){
			$(this).animate({ marginLeft: 0 }, 100);
		});
		if ($(this).hasClass('right')) $(this).animate({ marginRight: -10 }, 100, function(){
			$(this).animate({ marginRight: 0 }, 100);
		});
		
		if ($(this).hasClass('back')) $(this).animate({ marginRight: -10 }, 100, function(){
			$(this).animate({ marginRight: 0 }, 100);
		});
	}, function(){
	
	});
}

/******************************************************************************************************************/
// SLIDES
/******************************************************************************************************************/

var curr = 0;
var num = 0;

function showSlide(){
	$('#packs li').hide();
	$('#packs li:eq('+ curr +')').fadeIn(800);
	
	curr++;
	
	if (curr >= num) curr = 0;
	
	setTimeout('showSlide('+ curr +')', 5500);
}

function handleSlides(){
	num = $('#packs li').size();
	
	showSlide();	
}

/******************************************************************************************************************/
// PRODUCTS NAV
/******************************************************************************************************************/

function handleNav(){
	$('.prod').hide();
	myid = $('.nav .sel a').attr('href');
	$(myid).show();
	
	$('.nav a').css('opacity', 0.4);
	$('.nav .sel a').css('opacity', 1);
	
	$('.nav li').hover(function(){
		if (!$(this).hasClass('sel')) $(this).find('a').animate({ opacity: 1 }, 200);
	}, function(){
		if (!$(this).hasClass('sel')) $(this).find('a').animate({ opacity: 0.4 }, 300);
	});
	
	$('.nav a').click(function(){
		$('.nav li').removeClass('sel');
		$(this).parents('li').addClass('sel');
		
		$('.nav a').css('opacity', 0.4);
		$(this).css('opacity', 1);
		
		$('.prod').hide();
		myid = $(this).attr('href');
		$(myid).fadeIn();
		
		return false;
	});
}

/******************************************************************************************************************/
// TABS HANDLE
/******************************************************************************************************************/

function handleTabs(){
	$('.tabs .tab').hide();
	
	$('.tabs .sel').each(function(){
		initid = $(this).find('A').attr('href');
		$(initid).show();
	});
	
	$('.tabs .labels A').click(function(){
		$(this).parents('.labels').find('li').removeClass('sel');
		$(this).parent().addClass('sel');
		
		myid = $(this).attr('href');
		
		$(this).parents('.tabs').find('.tab').hide();
		$(myid).show();
		
		return false;
	});
}

/******************************************************************************************************************/
// PRELOADER
/******************************************************************************************************************/

var preload = true;

function pagePreload(){
	if (preload){
		$('body').prepend('<div id="loader"><h1>loading...</h1></div>');
		
		preload = false;
		setTimeout('pagePreload()', 6000);
	}	
	else {
		$('#packs li').hide();
		$('#loader').fadeOut(1000, function(){ 
			$(this).remove(); 
			
			// start slides
			handleSlides();
		});
	}
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){
	pagePreload();

	adjustSize();
	$(window).resize(function(){ adjustSize(); });
	
	handleScroll();
	handleArrows();
	
	handleTabs();
	
	handleNav();
	
	$('#credits').css('opacity',0.9);
});
