var inAnim = false
var explode = false
var $window = $(window)

$('#elenco > div').hide()
$('.people img').css('opacity',0)
//$('.scheda .reset').hide()

jQuery(document).ready(function(e) {
	reset_section()
	$(document).mousemove(function(e){
		if(e.pageX<$(window).width())
			$('footer .shooter').css('left',e.pageX-37)
		else
			$('footer .shooter').css('left',$(window).width()-75)
	})
	$(document).click(function(e){
		$('#s1 .score .num').html(parseInt($('#s1 .score .num').html())+rnd(0,300))
	})
	
	$(document).click(function(e){
		shoot(e,false)
	})
	
	
	
	
	
	$('section').each(function() {
		var $self = $(this),
			offsetCoords = $self.offset(),
			topOffset = offsetCoords.top;
			$(window).scroll(function() {
				if ( ($window.scrollTop() + $window.height()/2) > (topOffset) &&
					 ( (topOffset + $self.height()) > ($window.scrollTop()) ) ) {
							open_section($self.attr('id'))
				}
			})
	});
	
	$('.people a').each(function(index,item){
		$(this).click(function(e){
			if(!inAnim) {
				shoot(e,true,index)
				return false
			}
		})
	})
	
	$('.space').each(function(index,item) {
		$(this).click(function(e){
			space(e,$(this))
			return false
		})
	});

	$('ul.social a').each(function(index,item) {
		$(this).hover(function(e){
			$(this).stop().animate({'opacity':0})
		},function(e){
			$(this).stop().animate({'opacity':1})
		})
	});
	
	$('.scheda .reset').click(function(){
		reset_scheda()
	})
	
	$("#twitt").tweet({
		count: 1,
		username:"Urbangap",
		loading_text: "caricamento...",
		template:'"{join}{text}"'
	});


	$('#form').formValidate({
		campi : new Array(
					new Array('#form_nome','Nome','Nome: campo obbligatorio','text',true), // tipi campi: text, email, privacy
					new Array('#form_email','Email','Email: campo obbligatorio','email',true),
					new Array('#form_msg','Messaggio','Messaggio: campo obbligatorio','text',true),
					new Array('#form_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 :'/urban12/send.php',
		ajaxResponse: '#response'
	})
	
	$('#form_box a').click(function(){
		$('.form').scrollTo('#privacy_box',400)
		return false
	})
	$('#privacy_box a').click(function(){
		$('.form').scrollTo('#form_box',400)
		return false
	})
});

jQuery(window).load(function(){
	$('#wrap').css('width',$(window).width())	
	open_section('s2')
})

jQuery(window).resize(function(e) {
	$('#wrap').css('width',$(window).width())	
});


function shoot(e,explode,elem) { 
	$('#s1 .score .num').html(parseInt($('#s1 .score .num').html())+rnd(0,300))
	l = $('footer .shooter').offset()
	l = l.left+35
	
	to = $(window).height() - (e.pageY - $(window).scrollTop()) - 30


	$('footer .shoot').css({'left':l,'top':-39})
	$('footer .shoot').show()
	$('footer .shoot').stop().animate({'top':-(to)},500,function(){
		$('footer .shoot').hide()
		if(explode) { 
				inAnim = true
				scheda(elem)
		}
	})
}

function space(e,elem) { 
	
	$('#s1 .score .num').html(parseInt($('#s1 .score .num').html())+rnd(0,300))
	l = $('footer .shooter').offset()
	l = l.left+35
	
	to = $(window).height() - (e.pageY - $(window).scrollTop()) - 30
	
	$('footer .shoot').css({'left':l,'top':-39})
	$('footer .shoot').show()
	$('footer .shoot').stop().animate({'top':-(to)},500,function(){
		$('footer .shoot').hide()
		elem.attr('src',elem.attr('src').replace('png','gif'))
		setTimeout(function(){
			elem.fadeOut(function(){
				elem.attr('src',elem.attr('src').replace('gif','png'))
				elem.fadeIn()
			})
			
		},1900)
		
	})
	
}


function scheda(index) { 
	inv = $('.people div').eq(index)
	team = inv.find('a').attr('id')
	
	if(!inv.hasClass('opened')) {
		inv.addClass('explosion')
		
		$('#elenco > div').fadeOut(250)
		$('#elenco #'+team).delay(1000).fadeIn(250,function(){
			inv.css('background','none')
			inv.addClass('opened')
			inv.find('img').animate({'opacity':1})
			
			$('.score_txt .num').html(parseInt($('.score_txt .num').html())+1)
			if(parseInt($('.score_txt .num').html())==15) { 
				$('.score_txt').html('YOU WIN!')
			}
			noAnim()	
		})
	}else{
		$('#elenco > div').fadeOut(250)
		$('#elenco #'+team).delay(1000).fadeIn(250,function(){
			inv.css('background','none')
			inv.addClass('opened')
			inv.find('img').animate({'opacity':1})
			
			noAnim()	
		})
	}
	
}

function reset_scheda(){
	//$('.scheda .reset').hide()
	$('.people div').removeAttr('style').removeClass('explosion').removeClass('opened')
	$('.people div img').animate({'opacity':0})
	$('.score_txt .num').html(0)
	$('.score_txt').html('Score <span class="num">0</span>/15 ')
	$('#elenco > div').fadeOut()
}

function rnd(min,max) {
    //return (Math.floor(Math.random() * (max - min + 1)) + min);
	return (min + parseInt((Math.random() * ((max - min) + 1))))
}

function noAnim() {
	inAnim = false
}

function open_section(s) {
	$('#'+s+' .motion').each(function(index, element) {
		if($(this).css('opacity')==0)
				$(this).animate({'top':'+=20px','opacity':1},rnd(100,600))
	});
}

function reset_section() {
	$('.motion').each(function(index, element) {
			$(this).css({'top':"-=20px",'opacity':0})
	});
}
