// JavaScript Document
;(function() {
	
var shwoTM, hideTM;
var expanded = false;
var browserName=navigator.appName;

function getScrollOffset() {
	var scrollOffset = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrollOffset = window.pageYOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrollOffset = document.body.scrollTop;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrollOffset = document.documentElement.scrollTop;
	}
	return scrollOffset;
}

function showDock() {
	var scrolled = getScrollOffset();
	if (scrolled > 146) {
		if (browserName == "Microsoft Internet Explorer") {
			$("#dock").animate ({
				top: "-327px"
			}, 500 );
		} else {
			$("#dock").animate ({
				opacity: 1,
				top: "-327px"
			}, 500 );
		}
		clearInterval (showTM);
		hideTM = setInterval(hideDock, 100);
	}
}
function hideDock() {
	var scrolled = getScrollOffset();
	if (scrolled <= 146) {
		if (browserName == "Microsoft Internet Explorer") {
			$("#dock").animate ({
				top: "-371px"
			}, 500 );
		} else {
			$("#dock").animate ({
				opacity: 0,
				top: "-371px"
			}, 500 );
		}
		$('#expandDock').removeClass('active');
		expanded = false;
		clearInterval(hideTM);
		showTM = setInterval(showDock, 100);
	}
}

function initDock() {
	//console.log(scrolled);
	showTM = setInterval(showDock, 100);
}

function expandDock() {
	$('#expandDock').click( function() { 
		if (!expanded) {
			$('#expandDock').addClass('active');
			$("#dock").animate ({
				top: "0px"
			}, 500 );
			expanded = true;
		} else {  
			$('#expandDock').removeClass('active');
			$("#dock").animate ({
				top: "-327px"
			}, 500 );
			expanded = false;
		}
	});
}


/* --- */

function randQuote() {
	var quotestring = $('div#quote span');
	var quoteperson = $('div#quote em');
	var quotes = [
		'LiveRail empowers its clients with the most advanced technology for online video advertising',
		'LiveRail technology sits at the heart of the multi-platform, realtime video advertising ecosystem',
		'Real-time bidding has fundamentally changed the paradigm for online video advertising',
		'Weʼre passionate about helping our customers succeed in the fast moving world of video advertising',
		'Weʼve built the best engineering team in the business. Weʼre passionate about building world class technology',
		'We love our customers. And know how to show it',
		'Our clients are more than just clients, theyʼre partners. They view LiveRail as an extension of their team'
	];
	var people = [
		'Tommy Schroder, Director Technology Sales',
		'Andrei Dunca, CTO and co-founder',
		'Mark Trefgarne, CEO and co-founder',
		'Jing Feng, VP Sales',
		'Antoniu-George Savu, Head of Engineering',
		'Vijay Balan, VP Client Services',
		'Punit Sarin, Product Manager'
	];
	var rand = Math.floor(Math.random()*7);

	if (quotestring) { quotestring.text('"' + quotes[rand] + '"'); }
	if (quoteperson) { quoteperson.text(people[rand]); }
}

function validateForm() {
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var firstName = document.getElementById('first_name').value;
	var lastName = document.getElementById('last_name').value;
	var company = document.getElementById('company').value;
	var email = document.getElementById('email').value;
	var badFormMessageDiv = document.getElementById('form_error');
	var badFormMessage = false;
	
	if ( firstName == "" || lastName == "" || company == "" || email == "" ) {
		badFormMessage = "All the fields are required.";
	} else if ( emailRegEx.test( email ) == false ) {
		badFormMessage = "Please enter a valid email address.";
	}
	if ( badFormMessage != false ) {
		badFormMessageDiv.innerHTML = badFormMessage;
		badFormMessageDiv.style.display = "block";
		return false;
	}
	
	badFormMessageDiv.style.display = "none";
	
	return true;
}



$(document).ready(function () {
	
	initDock();
	expandDock();
	randQuote();
	
	if ( $("#adweek_form").length ) {
		$("#adweek_form input[type='submit']").click( validateForm );
	}
	
})

})();
