"use strict";

var $,
	$$,
	player,
	curVideo,
	lightbox,
	timer,
	contactInfoIsSliding = false,
	Effect;

// browser alert
function closeBrowserAlert()
{
	$('browser-alert').fade({ duration: 0.25 });
	$('overlay').fade({ duration: 0.25 });
}

function showAnswer($answer)
{
	$$('.browser-alert-answer').each(function (e) { e.hide(); });
	$($answer).appear({duration: 0.25});	
}

/********** TESTIMONIALS **********/

// array that holds all testimonails to be shown
var testimonial_array = [];
testimonial_array[0] = "<p><span class='testimonial white-text serif'>We chose Delorum for their integrity. We've worked with a few different Magento developers, and have always found Delorum to be the most capable, easy to work with and responsive.</span><span class='testifier white-text'>-- Troy Brown, VP Web – Zumiez</span></p>";
testimonial_array[1] = "<p><span class='testimonial white-text serif'>We approached Delorum for a refreshed and relevant web presence. What they delivered was a first-class, customer-centered site… that we can customize as needed.</span><span class='testifier white-text'>–- Jared Miller, IT Director Idahoan</span></p>";
testimonial_array[2] = "<p><span class='testimonial white-text serif'>Professionalism, exceptional value, utility, and personal attention are qualities that sustain our partnership with Delorum… They have been one of the significant factors of our success.</span><span class='testifier white-text'>–- James V. Kennedy, VP IBEX</span></p>";
testimonial_array[3] = "<p><span class='testimonial white-text serif'>Thanks again for your help on this. We couldn't have gone live without your help!</span><span class='testifier white-text'>–- Keith Bristol – Wambo (regarding Rightstart's 2 day optimization)</span></p>";

// randomley display a testimonial
function loadTestimonial()
{
	var i = Math.floor(Math.random() * testimonial_array.length);
	document.write(testimonial_array[i]); 
}

/********** ABOUT-US **********/

// TEAM //

// show right side biographies
function showBio($who)
{
	$$('.team-bio').each(function (e) { e.hide(); });
	$($who).appear({duration: 0.25});
	
	if (window.pageYOffset > 50)
	{
		$($who).style.top = window.pageYOffset - 35 + 'px';
	} else {
		$($who).style.top = window.pageYOffset + 'px';
	}	
}

/********** DEMOS **********/

// show demo description
function showTool($toolDescription)
{
	$$('.tool-description').each(function (e) { e.hide(); });
	$($toolDescription).show();	
}

// VIDEO PLAYER

// get video player and add event listeners
function playerReady(obj)
{
	player = $('player-licensed');
	player.addModelListener('STATE', 'onStateChange');
	player.addViewListener('PLAY', 'onPlay');
}

function playVideo()
{
	player.sendEvent('PLAY', 'true');
	
	if ($('video-projector').className === 'large-video-player')
	{
		$('tools-view-larger').hide();
	} else {
		$('tools-view-larger').appear({ duration: 0.25 });
	}
}

function pauseVideo()
{
	player.sendEvent('PLAY', 'false');
	
	if ($('video-projector').className === 'large-video-player')
	{
		$('video-projector').className = $('video-projector').className.replace('large-video-player', 'small-video-player');
		$('tools-view-larger').show();
		
		player.width = 316;
		player.height = 260; 
	}
}

function stopVideo()
{
	player.sendEvent('STOP');
}

function loadVideo($video, $tool)
{
	stopVideo();
	
	curVideo = $video;
	
	player.sendEvent('LOAD', curVideo);	
	playVideo();	
	
	$$('.tool').each(function (e) { e.setStyle({textDecoration: 'none' }); });
	$($tool).setStyle({ textDecoration: 'underline' });
}

function viewLargerVideo()
{
	$('video-projector').className = $('video-projector').className.replace('small-video-player', 'large-video-player');
	$('tools-view-larger').hide();
	
	player.width = 785;
	player.height = 610;
	
	lightbox.start();
	
	playVideo();
}

function onPlay()
{
	if (curVideo === undefined)
	{
		$('select-video-warning').appear({ duration: 0.25 });
		player.removeViewListener('PLAY', 'onPlay');
	}
}

function onStateChange(obj)
{
	switch (obj.newstate)
	{
	case "PLAYING":
		lightbox.start();
		$('select-video-warning').hide();
		break;
		
	case "PAUSED":
		lightbox.end();
			
		if ($('video-projector').className === 'large-video-player')
		{
			$('video-projector').className = $('video-projector').className.replace('large-video-player', 'small-video-player');
			$('tools-view-larger').show();

			player.width = 316;
			player.height = 260; 
		}
		break;
		
	case "COMPLETED":
		lightbox.end();
		$('tools-view-larger').show();
			
		if ($('video-projector').className === 'large-video-player')
		{
			$('video-projector').className = $('video-projector').className.replace('large-video-player', 'small-video-player');

			player.width = 316;
			player.height = 260; 
		}
		break;
	}
}

/********** SERVICES **********/

// HOME //

// toggle drop down services
function toggleOverviewService($header, $icon, $content)
{
	if ($($content).style.display === 'none')
	{
		$$('.overview-service-header').each(function (e) { e.className = e.className.replace(' orange-text', ''); });
		$($header).className += ' orange-text';
		
		$$('.overview-service-icon').each(function (e) { e.className = e.className.replace(' active', ''); });
		$($icon).className += ' active';
		
		$$('.overview-service-content').each(function (e) { e.hide(); });
		$($content).appear({duration: 0.25});
		
		// BEGIN GOOGLE EVENT TRACKING
		pageTracker._trackEvent("Services Page: Index", "Toggle: Service Overview", ($content).replace( /(^.+\D)(\d+)(\D.+$)/i, '$2'));
		// END GOOGLE EVENT TRACKING
		
	} else {
		$($header).className = $($header).className.replace(' orange-text', '');
		$($icon).className = $($icon).className.replace(' active', '');
		$($content).hide();
	}
}

// show processes
function showProcess($process)
{	
	$$('.services-process').each(function (e) { e.hide(); });
	$($process).appear({ duration: 0.25 });
	
	$$('.services-process-link').each(function (e) { e.className = e.className.replace(' orange-text', ''); });
	$($process + '-link').className += ' orange-text';
}

/********** CONTACT INFORMATION **********/

// create an 'hit box' for mouse-over detection
function createShield()
{
	var div = document.createElement('div'),
		width = $('contact-info-wrapper').getWidth() + 10,
		height = $('contact-info-wrapper').getHeight() + 10;

    div.style.width = width + 'px';
	div.style.height = height + 'px';

	div.style.margin = '0px 0px 0px -5px';

	//div.style.background = "#FF0000";
	//div.style.MozOpacity = .5;
	
	$('contact-info-wrapper').appendChild(div);		
}

// drop down contact information
function showContactInfo()
{
	if (!contactInfoIsSliding)
	{	
		contactInfoIsSliding = true;
		var effect = new Effect.SlideDown('contact-info-wrapper', { duration: 0.25, afterFinish: function () { createShield(); }, queue: { position: 'front', scope: 'contact-info', limit: 1 } });
	}
}

// hide contact information if drop up timer times-out
function hideContactInfo()
{
	if (contactInfoIsSliding)
	{
		$('contact-info-wrapper').removeChild($('contact-info-wrapper').lastChild);
		var effect = new Effect.SlideUp('contact-info-wrapper', { duration: 0.25, afterFinish: function () { contactInfoIsSliding = false; }, queue: { position: 'end', scope: 'contact-info', limit: 1 } });
	}
}

// drop up timer
function startHideTimer()
{
	timer = setTimeout(hideContactInfo, 5000);
}

// stop timer if over 'hit box'
function stopHideTimer()
{
	clearTimeout(timer);
}
