var screenW 	= $(window).width();
var screenH 	= $(window).height();
var scrollTop	= null;
var offset		= null;		
var position	= null;
var isScroll	= false;
var timer		= null;
var homeH		= null;
var chisiamoO	= null;
var ilgruppoO	= null;
var contacH 	= null;
var contacO 	= null;
var homeO		= null;


function getWindowSize() {
	 screenW = $(window).width();
	 screenH = $(window).height();
}

function setContactBot() {
	if((contacH)<screenH) {
		boxH = $('#contatti').height()
		pad	 = screenH-boxH//-231
		$('#contatti').css('padding-bottom',pad)
	}	
}

function getScrollTop() {
	offset = $(window).scrollTop()
}

function showHead() {
	var opc = false
	if((offset>=homeO && offset<homeO+40) ||
	(offset>=chisiamoO-150 && offset<chisiamoO+40) ||
	(offset>=ilgruppoO-150 && offset<ilgruppoO+40) ||
	(offset>=contacO-150 && offset<contacO+40))
		opc = true

	if(opc)
		$('h1 a,span').stop().animate({'opacity':1},function(){ $('h1 a,span').removeAttr('style')})
	else
		$('h1 a,span').stop().animate({'opacity':0})
}

function detectPosition() {
	if(offset>=homeO-150 && offset<chisiamoO-150){
		return 'home'
	}else
	if(offset>=chisiamoO-150 && offset<ilgruppoO-150){
		return 'chisiamo'
	}else
	if(offset>=ilgruppoO-150 && offset<contacO-150){
		return 'ilgruppo'
	}else
	if(offset>=(contacO-150)){
		return 'contatti'
	}
}

function activeMn() {
	/*$('#debug').html(offset)*/
	//if(detectPosition()!=position || position=='') {
		position=detectPosition();
		$('nav li a').filter('.active').removeClass('active')
		$('nav li.li_'+position+' a').addClass('active')

		switch(position) {
			case 'home': $('title').html('Legrenzi fashion company'); break;
			case 'chisiamo': $('title').html('Chi siamo | Legrenzi fashion company'); break;
			case 'ilgruppo': $('title').html('Il gruppo | Legrenzi fashion company'); break;
			case 'contatti': $('title').html('Contatti | Legrenzi fashion company'); break;
		}
		
	//}
}

function init(){
	$('nav ul').hide()
	homeH		= $('#home').height();
	homeO		= 0;
	chisiamoO	= $('#chisiamo').offset();
	ilgruppoO	= $('#ilgruppo').offset();
	contacH 	= $('#contatti').height();
	contacO 	= $('#contatti').offset();
	
	chisiamoO	= chisiamoO.top;
	ilgruppoO	= ilgruppoO.top;
	contacO 	= contacO.top;
	
	setContactBot()
	activeMn()
	
}

jQuery(window).load(function() {
	init()
})

jQuery(document).ready(function($){
	offset = $(window).scrollTop()
	
	$('nav li a,#home ul a').each(function(){
		$(this).click(function(){
			isScroll = true
			$(document).scrollTo($(this).attr('href'),700,function(){
				isScroll = false
			})
			return false
		})
	})
	
	// -----------------
	// HOME

	$('#home .slide_cont').cycle({timeout:2000,speed:3500,prev:'#home #bt_prev',next:'#home #bt_next',pause:true})	
	$('#home ul li a').each(function(){
		$(this).hover(function(){
			$(this).stop().animate({'background-position':'147px bottom'},300,'easeOutQuint')
		},function(){
			$(this).stop().animate({'background-position':'130px bottom'},300,'easeOutQuint')
		})
	})
	$('.toTop').hover(function(){ $(this).stop().animate({'opacity':'0.7'},300,'easeOutQuint') },function(){ $(this).stop().animate({'opacity':'1'},300,'easeOutQuint') })
		.click(function(){ $(document).scrollTo(0,600,'easeInQuint'); return false} )
		
	$('#chisiamo #gal').slider({
				_panels 	: 'img',
				_container 	: '.scrollContainer',
				_mask 		: '.scroll',
				_prev 		: '.btPrev',
				_next 		: '.btNext',
				_horizontal	: true,
				_padding	: 8,
				_stepById	: true,
				_stepView	: 2,
				_stepScroll	: 1,
				_easing		: 'easeInOutQuint',
				_durata		: 430,						// durata animazione
				_circle		: false,						// animazione ciclica
				_tPlay 		: 2000,						// delay animazione autamtica
				_isPlay		: false						// player immagini
		})		
	$('#ilgruppo ul li').each(function(){
		$(this).hover(function(){
			$(this).find('a').stop().animate({'background-position':'11px 33px '},300,'easeOutQuint')
		},function(){
			$(this).find('a').stop().animate({'background-position':'0 33px '},300,'easeOutQuint')
		})
		$(this).click(function(){
			location.href = $(this).find('a').attr('href')
		})
	})
	$('#contatti input[type=submit]').each(function(){
		$(this).hover(function(){
			$(this).stop().animate({'background-position':'6px 10px'},300,'easeOutQuint')
		},function(){
			$(this).stop().animate({'background-position':'0 10px'},300,'easeOutQuint')
		})
	})


	$('form').formValidate({
		campi : new Array(
					new Array('#nome','Nome','Nome: campo obbligatorio','text',true), // tipi campi: text, email, privacy
					new Array('#cognome','Cognome','Cognome: campo obbligatorio','text',true), // tipi campi: text, email, privacy
					new Array('#mail','Mail','Mail: campo obbligatorio','email',true),
					new Array('#msg','Messaggio','Messaggio: campo obbligatorio','text',true),
					new Array('#privacy','Privacy','Autorizzare il trattamento dei dati','privacy',true)
				),
		textIn : true, // campi di testo con il nome dentro
		alertt: new Array(true,''), // new Array(false,'#idBoxPerTesto')
		ajax: true,
		ajaxURL :'send.php',
		ajaxResponse: '#response'
	})
	
	$('nav').hover(function(){
		$(this).find('ul').stop().show()
	},function(){
		$(this).find('ul').hide()
	})

})

function ancora(){
	if(isScroll) return false
	
	clearInterval( timer );
	timer = setTimeout( function(){
		if(offset>=homeO && offset<homeO+40)
			$(document).scrollTo('#home',100,'easeInOutQuint')
		if(offset>=chisiamoO-200 && offset<chisiamoO+40)
			$(document).scrollTo('#chisiamo',100,'easeInOutQuint')
		if(offset>=ilgruppoO-200 && offset<ilgruppoO+40)
			$(document).scrollTo('#ilgruppo',100,'easeInOutQuint')
		if(offset>=contacO-200 && offset<contacO+40)
			$(document).scrollTo('#contatti',100,'easeInOutQuint')
	} , 350 );
}

$(window).bind('resize', function(e) {
    getWindowSize()
	setContactBot()
})
$(window).bind('scroll', function() {
	offset = $(window).scrollTop()
	ancora()
	showHead()
	activeMn()
})
