// constants

var TOTAL_TIPS = 10;

// vars

var currentTip = 0;
var rolloverActive = false;
var emitter;

var aboutActive = false;
var solutionsActive = false;
var creativeActive = false;
var insightsActive = false;

var homeVideoLoaded = false;
var aboutVideoLoaded = false;
var solutionsVideoLoaded = false;
var creativeVideoLoaded = false;
var insightsVideoLoaded = false;
var contactVideoLoaded = false;

var resetFlow = false;

/**
 * Document ready
 */
$(document).ready(function(){
	
	////////////////////////////////////////////////////////////
	// global rollovers
	$('ul.globals li').mouseover(function(){
		$(this).siblings('li').find('ul').hide();
		$(this).find('ul').show();
	});
	
	$('ul.globals li ul').mouseout(function(){
		$(this).hide();
	});
	
	////////////////////////////////////////////////////////////
	// home
	
	var homeVideo = $('.rollover-home video').get(0);
	if (homeVideo) homeVideo.addEventListener('loadedmetadata', function(){
		homeVideoLoaded = true;
  	}, false);
	var aboutVideo = $('.rollover-about video').get(0);
	if (aboutVideo) aboutVideo.addEventListener('loadedmetadata', function(){
		aboutVideoLoaded = true;
  	}, false);
	var solutionsVideo = $('.rollover-solutions video').get(0);
	if (solutionsVideo) solutionsVideo.addEventListener('loadedmetadata', function(){
		solutionsVideoLoaded = true;
  	}, false);
	var creativeVideo = $('.rollover-creative video').get(0);
	if (creativeVideo) creativeVideo.addEventListener('loadedmetadata', function(){
		creativeVideoLoaded = true;
  	}, false);
	var insightsVideo = $('.rollover-insights video').get(0);
	if (insightsVideo) insightsVideo.addEventListener('loadedmetadata', function(){
		insightsVideoLoaded = true;
  	}, false);
	var contactVideo = $('.rollover-contact video').get(0);
	if (contactVideo) contactVideo.addEventListener('loadedmetadata', function(){
		contactVideoLoaded = true;
  	}, false);

	/*
	$('.rollover-home video').bind('loadedmetadata', function(){
		homeVideoLoaded = true;
	});
	*/

	$('.home-roll').hide();
	$('.rollout').hide().mouseover(function(){
		hideHomeRolls();
	});
	
	// home rollover
	$('ul.nav a.home').mouseover(function(){
		hideHomeRolls();
	});
	
	$('ul.nav a.home').click(function(){
		//if (homeVideoLoaded) {
		if (true) {
			rolloverActive = true;
			$('html').css('background-image', 'none');
			$('.rollover-home').fadeIn();
			var video = $('.rollover-home video').get(0);
			if (video) {
				video.currentTime = 0;
				video.play();
			}
			$('ul.nav').fadeOut();
			
			if (resetFlow) $('.rollover-home a').flowplayer(0).resume();
			
		}
		return false;
	});
	$('.rollover-home video').bind('ended', function(){
		hideHomeRolls();
		$('ul.nav').fadeIn();
	});
	$('.rollover-home').click(function(){
		hideHomeRolls();
		$('ul.nav').fadeIn();
		return false;
	});

	// about rollover
	$('ul.nav a.about').mouseover(function(){
		//if (aboutVideoLoaded) {
		if (true) {
			if (!aboutActive) {
				hideHomeRolls();
				aboutActive = true;
				rolloverActive = true;
				$('html').css('background-image', 'none');
				$('.rollover-about').fadeIn();
				var video = $('.rollover-about video').get(0);
				if (video) {
					video.currentTime = 0;
					video.play();
				}
				$('.rollout').show();
				
				if (resetFlow) $('.rollover-about a').flowplayer(0).resume();
				
			}
		}
	});
	$('.rollout-about').mouseover(function(){
		hideHomeRolls();
	});


	// solutions rollover
	// hack to loop solutions
	$('.rollover-solutions-final video').bind('ended', function(){ this.play(); });	
	$('.rollover-solutions-start').hide();
	$('.rollover-solutions-final').hide();
	$('ul.nav a.solutions').mouseover(function(){
		//if (solutionsVideoLoaded) {
		if (true) {
			if (!solutionsActive) {
				hideHomeRolls();
				solutionsActive = true;
				rolloverActive = true;
				$('html').css('background-image', 'none');
				$('.rollover-solutions-start').show();
				$('.rollover-solutions-final').hide();
				$('.rollover-solutions').fadeIn();
				var video = $('.rollover-solutions-start video').get(0);
				if (video) {
					video.currentTime = 0;
					video.play();
				}
				$('.rollout').show();
				
				if (resetFlow) $('.rollover-solutions-start a').flowplayer(0).resume();

			}
		}
	});
	$('.rollover-solutions-start video').bind('ended', function(){
		$('.rollover-solutions-start').hide();
		$('.rollover-solutions-final').show();
		var video = $('.rollover-solutions-final video').get(0);
		video.muted = true;
		video.currentTime = 0;
		video.play();
	});
	$('.rollout-solutions').mouseover(function(){
		hideHomeRolls();
	});

	// creative rollover
	$('ul.nav a.creative').mouseover(function(){
		//if (creativeVideoLoaded) {
		if (true) {
			if (!creativeActive) {
				hideHomeRolls();
				creativeActive = true;
				rolloverActive = true;
				$('html').css('background-image', 'none');
				$('.rollover-creative').fadeIn();
				var video = $('.rollover-creative video').get(0);
				if (video) {
					video.currentTime = 0;
					video.play();
				}
				$('.rollout').show();
				
				if (resetFlow) $('.rollover-creative a').flowplayer(0).resume();

			}
		}
	});
	$('.rollout-creative').mouseover(function(){
		hideHomeRolls();
	});

	// insights rollover
	$('ul.nav a.insights').mouseover(function(){
		//if (insightsVideoLoaded) {
		if (true) {
			if (!insightsActive) {
				hideHomeRolls();
				insightsActive = true;
				rolloverActive = true;
				$('html').css('background-image', 'none');
				$(this).css('color', '#000000');
				$('.rollover-insights').fadeIn();
				var video = $('.rollover-insights video').get(0);
				if (video) {
					video.currentTime = 0;
					video.play();
				}
				$('.rollout').show();
				
				if (resetFlow) $('.rollover-insights a').flowplayer(0).resume();

			}
		}
	});
	$('.rollover-insights video').bind('ended', function(){
		var video = $('.rollover-insights video').get(0);
		video.currentTime = 1.2;
	});
	$('.rollout-insights').mouseover(function(){
		hideHomeRolls();
	});

	// contact rollover
	$('ul.nav a.contact').mouseover(function(){
		//if (contactVideoLoaded) {
		if (true) {
			hideHomeRolls();
			rolloverActive = true;
			$('html').css('background-image', 'none');
			$('.rollover-contact').fadeIn();
			var video = $('.rollover-contact video').get(0);
			if (video) {
				video.currentTime = 0;
				video.play();
			}
			
			if (resetFlow) $('.rollover-contact a').flowplayer(0).resume();
			
			$('.flowplayer-contact-roll').show();
			
		}
	});
	$('.rollover-contact').mouseout(function(){
		hideHomeRolls();
		var video = $('.rollover-contact video').get(0);
		if (video) video.pause();
		
		$('.flowplayer-contact-roll').hide();
		
	}).click(function(){
		document.location = $('ul.nav a.contact').attr('href');
		return false;
	});
	
	$('.flowplayer-contact-roll').click(function(){
		document.location = $('ul.nav a.contact').attr('href');
	});
	

	////////////////////////////////////////////////////////////
	// guiding principles
	
	setupComet();

	////////////////////////////////////////////////////////////
	// leadership
	
	$('.helen').hide();
	$('.helen-img').hide();
	
	$('.scott-btn').click(function(){
		$('.scott').fadeIn();
		$('.helen').hide();
		$('.helen-img').hide();
		$('.scott-img').show();
		return false;
	}).hover(
		function(){
			$('.helen-img').get(0).src = '../../images/about/scott-over.jpg';
		},
		function(){
			$('.helen-img').get(0).src = '../../images/about/helen-selected.jpg';
		}	
	);

	$('.helen-btn').click(function(){
		$('.scott').hide();
		$('.helen').fadeIn();
		$('.scott-img').hide();
		$('.helen-img').show();
		return false;
	}).hover(
		function(){
			$('.scott-img').get(0).src = '../../images/about/helen-over.jpg';
		}, 
		function(){
			$('.scott-img').get(0).src = '../../images/about/scott-selected.jpg';
		}
	);

	////////////////////////////////////////////////////////////
	// solutions
	
	$('img.ipresent-rollover').hide();
	$('area.ipresent-btn').mouseover(function(){
		$('img.ipresent-rollover').show();
	});
	$('img.ipresent-rollover').mouseout(function(){
		$('img.ipresent-rollover').hide();
	});
	

	$('img.istory-rollover').hide();
	$('area.istory-btn').mouseover(function(){
		$('img.istory-rollover').show();
	});
	$('img.istory-rollover').mouseout(function(){
		$('img.istory-rollover').hide();
	});
	
	////////////////////////////////////////////////////////////
	// creative
	
	$('img.creative-rollover').hide();
	$('area.creative-btn').mouseover(function(){
		var id = $(this).attr('data-roll');
		$('img.'+id).show();
		var title = $(this).attr('alt');
		var desc = $(this).attr('data-desc');
		$('.creative-teaser').html('<span><h3>'+title+'</h3>'+'<p>'+desc+'</p></span>');
	});
	$('area.creative-btn').mouseout(function(){
		var id = $(this).attr('data-roll');
		$('img.'+id).hide();
		$('.creative-teaser').html('');
	});
	
	$('.overlay-btn').click(function(){
		var overlayId = $(this).attr('href');
		
		var video = $(overlayId + ' video').get(0);
		if (video) video.play();
				
		var myFlow = $(overlayId + ' a.flowplayer-video').flowplayer(0);
		if (myFlow) myFlow.play();
				
		return false;
	});
	
	$('.overlay a.close-btn').click(function(){
		
		var video = $(this).parents('.overlay').find('video').get(0);
		if (video) {
			if (video.pause) video.pause();
		}
		
		var myFlow = $(this).parents('.overlay').find('.flowplayer-video').flowplayer(0);
		if (myFlow) myFlow.stop();
		
		return false;
	});

	////////////////////////////////////////////////////////////
	// iq
	
	// setup tip buttons
	for (var i = 0; i < TOTAL_TIPS; i++) {
		$('ul.iq-nav li:nth-child('+(i+1)+') a').click(function(){
			var tipIndex = $(this).parent().index();
			goToTip(tipIndex);
			return false;
		});
	}
	
	// setup next tip button
	$('a.next-tip').click(function(){
		goToNextTip();
		return false;
	});

	// go to first tip
	hideAllTips();
	goToTip(0);
	
	
	////////////////////////////////////////////////////////////
	// contact
	
	var contactVideo = $('.contact-video video').get(0);
	if (contactVideo) contactVideo.muted = true;
	
	
	////////////////////////////////////////////////////////////
	// overlay

	$('.overlay').hide();
	
	$('.overlay a.close-btn').click(function(){
		$(this).parents('.overlay').fadeOut();
		return false;
	});
	
	$('.overlay-btn').click(function(){
		var overlayId = $(this).attr('href');
		$(overlayId).fadeIn();
		return false;
	});



});

function hideHomeRolls()
{
	$('.home-roll').hide();
	$('.rollout').hide();
	rolloverActive = false;
	aboutActive = false;
	solutionsActive = false;
	creativeActive = false;
	insightsActive = false;
	$('html').css('background-image', 'url(images/marble.jpg)');

	var video1 = $('.rollover-solutions-start video').get(0);
	if (video1) video1.pause();
	var video2 = $('.rollover-solutions-final video').get(0);
	if (video2) video2.pause();

	$('ul.nav a.insights').css('color', '#ffffff');
	
	/*
	$('.rollover-home a').flowplayer(0).pause().seek(0);
	$('.rollover-about a').flowplayer(0).pause().seek(0);
	$('.rollover-solutions-start a').flowplayer(0).pause().seek(0);
	$('.rollover-solutions-final a').flowplayer(0).pause().seek(0);
	$('.rollover-creative a').flowplayer(0).pause().seek(0);
	$('.rollover-insights a').flowplayer(0).pause().seek(0);
	$('.rollover-contact a').flowplayer(0).pause().seek(0);
	*/
	
}


function setupComet()
{
	var canvas = $('canvas#comet').get(0);
	
	if (canvas) {
		canvas.width = $('canvas#comet').width();
		canvas.height = $('canvas#comet').height();
		
		if (canvas.getContext) {
			var ctx = canvas.getContext('2d');
		}
		
		// create the emitter
		emitter = new Emitter();
		emitter.canvas = canvas;
		emitter.context = ctx;
		
		// setup initial values
		emitter.position = new Vector(canvas.width / 2, canvas.height / 2);
		emitter.positionRandom = new Vector(20, 20);
		
		emitter.spawnRate = 5;
		emitter.spawnRateRandom = 0;
		
		emitter.lifetime = 60;
		emitter.lifetimeRandom = 60;
		
		emitter.color = new Color(192, 128, 255, 1);
		emitter.colorRandom = new Color(64, 64, 64, 0);
			
		//emitter.endColor = new Color(0, 0, 32, 1);
		emitter.endColor = new Color(128, 128, 255, 1);
		emitter.endColorRandom = new Color(64, 64, 64, 0);
		
		emitter.velocity = new Vector(0, 0);
		emitter.velocityRandom = new Vector(1, 1);
		
		emitter.size = 15;
		emitter.sizeRandom = 15;
		
		emitter.wind = new Vector(-0.5, 0.125);
		
		if (canvas.addEventListener) {
			canvas.addEventListener('mousemove', onMouseMove, false);
			canvas.addEventListener('touchmove', onTouchMove, false);
		}
		
		$('#comet-start').mouseover(function(){
			emitter.start();
			$(this).hide();
		});
		
	}
}

function onMouseMove(event)
{
	var mouseX = event.pageX - $('canvas#comet').offset().left;
	var mouseY = event.pageY - $('canvas#comet').offset().top;
	if (mouseX < 600) mouseX = 600;
	if (mouseY > 300) mouseY = 300;
	emitter.position = new Vector(mouseX, mouseY);
}

function onTouchMove(event) {
	if (event.touches.length == 1) {
		event.preventDefault();
		var mouseX = event.touches[0].pageX - $('canvas#comet').offset().left;
		var mouseY = event.touches[0].pageY - $('canvas#comet').offset().top;
		if (mouseX < 600) mouseX = 600;
		if (mouseY > 300) mouseY = 300;
		emitter.position = new Vector(mouseX, mouseY);
	}
}

/**
 * Hide all iQ tips
 */
function hideAllTips()
{
	for (var i = 0; i < TOTAL_TIPS; i++) {
		$('.tips .tip'+(i+1)).hide();
		$('ul.iq-nav li:nth-child('+(i+1)+') a').removeClass('selected');
	}
}

/**
 * Show the next tip
 */
function goToNextTip()
{
	var nextTip = currentTip + 1;
	if (nextTip >= TOTAL_TIPS) nextTip = 0;
	goToTip(nextTip);
}

/**
 * Go to specified tip
 */
function goToTip(tipIndex)
{
	currentTip = tipIndex;
	hideAllTips();
	$('.tips .tip'+(tipIndex+1)).fadeIn();
	$('ul.iq-nav li:nth-child('+(tipIndex+1)+') a').addClass('selected');
}
