var shouldStop = false;

$(document).ready(function() {
	scrollToContact();
	scrollToWork();
	scrollToTop();
	changeBannerPictures();
	arrowHeartBeat();
	toggleWorkButtons();
	showDownArrow();
	showOrHideArrowOnScroll();
});

function scrollToContact() {
	$('#contact-button').click(function() {
		scrollTo('#contact', 1500);
	});
}
function scrollToWork() {
	$('#work-link, #arrow-down').click(function() {
		scrollTo('#what_we_do', 1000);
	});
}

function scrollToTop() {
	$('#arrow').click(function() {
		scrollTo(null, 1500);
	});
}

function changeBannerPictures() {
	$('#banner-img').crossSlide({
		sleep : 3,
		fade : 1
	}, [ {
		src : 'img/pict1.jpg'
	}, {
		src : 'img/pict2.jpg'
	}, {
		src : 'img/pict3.jpg'
	}, {
		src : 'img/pict4.jpg'
	} ]);
}

function toggleWorkButtons() {
	
	$('#button-work').click(function() {
		$('#portfolio').hide();
		$('#work').show();			
		
		$('#button-portfolio').removeClass('gray-pill');
		$('#button-portfolio').addClass('blue-pill');
		$('#button-work').removeClass('blue-pill');
		$('#button-work').addClass('gray-pill');
		
		if (window.pageYOffset < 500)
			scrollTo('#what_we_do', 1000);
			
	});
	$('#button-portfolio').click(function() {
		$('#work').hide();
		$('#portfolio').show();
		
		$('#button-portfolio').removeClass('blue-pill');
		$('#button-portfolio').addClass('gray-pill');
		$('#button-work').removeClass('gray-pill');
		$('#button-work').addClass('blue-pill');
		
		if (window.pageYOffset < 500)
			scrollTo('#what_we_do', 1000);
	});
}

function arrowHeartBeat() {
	setInterval(function() {
		if ($('#arrow-img').queue('fx').length == 0) {
			$('#arrow-img').animate({
				width : 130
			}, {
				duration : 200,
				specialEasing : {
					width : 'linear',
					height : 'linear'
				},
				complete : function() {
					$('#arrow-img').animate({
						width : 120
					}, {
						duration : 500,
						specialEasing : {
							width : 'linear',
							height : 'linear'
						},
						complete : function() {
							$('#arrow-img').animate({
								width : 130
							}, {
								duration : 200,
								specialEasing : {
									width : 'linear',
									height : 'linear'
								},
								complete : function() {
									$('#arrow-img').animate({
										width : 120
									}, {
										duration : 500,
										specialEasing : {
											width : 'linear',
											height : 'linear'
										}
									});
								}
							})
						}
					});
				}
			})
		}
	}, 3000);
}

function arrowDownJump() {
	if (shouldStop) {
		$('#arrow-down').clearQueue();
		$('#arrow-down').fadeOut(1000);
		return;
	}

	var pos = 0;
	if ($('#what_we_do').offset().top > (window.innerHeight + window.pageYOffset)) {
		pos = window.innerHeight + window.pageYOffset - 70;
	} else {
		pos = $('#what_we_do').offset().top - 70;
	}
	$('#arrow-down').css('top', pos);
	setTimeout(function() {
		if ($('#arrow-down').queue('fx').length == 0) {
			$('#arrow-down').animate({
				top : (pos - 20)
			}, {
				duration : 500,
				specialEasing : {
					width : 'swing',
					height : 'swing'
				},
				complete : function() {
					$('#arrow-down').animate({
						top : pos
					}, {
						duration : 500,
						specialEasing : {
							width : 'swing',
							height : 'swing'
						},
						complete : function() {
							arrowDownJump()
						}
					});
				}
			})
		}
	}, 100);
}

function showOrHideArrowOnScroll() {
	$(window).scroll(function() {
		if (window.pageYOffset > 0) {
			hideDownArrow();
		}
	});
}

function showDownArrow() {
	$('#arrow-down').fadeIn(1000);
	arrowDownJump();
}

function hideDownArrow() {
	shouldStop = true;
}

function scrollTo(element, time) {
	var top
	if (element != null)
		top = $(element).offset().top;
	else 
		top = 0;
    $('html,body').animate({scrollTop: top}, time);
}
