
//Google Analytics
if (window.location.toString().indexOf('empirestaging') < 0) {
	try {
		var pageTracker = _gat._getTracker("UA-0000000-0");
		pageTracker._trackPageview();
	} 
	catch (err) {
	}
}

//Test to see if a selector exists
$.exists = function(selector){
	return ($(selector).length > 0);
}

// MotiveQuest Javascript Document

$(document).ready(function(){


	// Figure out what browser is being used
	var userAgent = navigator.userAgent.toLowerCase();
	
	// Figure out what browser is being used
	jQuery.browser = {
		version: (userAgent.match(/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/) || [])[1],
		chrome: /chrome/.test(userAgent),
		safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent),
		opera: /opera/.test(userAgent),
		msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
		mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
	};
	
	// Main nav adjustments for vertical alignment
		
	if ($.browser.mozilla || $.browser.msie) {
		$('#header-container-nav ul li a').css({
			'padding-top': '0px'
		})
	}
	
	if ({chrome: /chrome/.test(userAgent)} && navigator.platform === 'Win32') {
		$('#header-container-nav ul li a').css({
			'padding-top': '0px'
		})
	}
	
	if (($.browser.mozilla) && (navigator.platform === 'MacPPC')) {
		$('#header-container-nav ul li a').css({
			'padding-top': '2px'
		})
	}
	
	
	
	/* home */
	if ($('body').hasClass('home')) {
	
		// Note: Commented out per revision on 1/19/2011 but left in code for easy roll back
		/*var _xmlData;
		 var _id = 0, _fadeIn = 400, _fadeOut = 100;
		 if ($.browser.msie) {
		 // ie doesn't play well with the transparent png's
		 _fadeIn = 0; _fadeOut = 0;
		 }
		 $.ajax({
		 type: "GET",
		 url: "xml/mountaincoords.xml",
		 dataType: "xml",
		 success: function (xml) { _xmlData = xml; }
		 });
		 $('#main-mountains AREA').mouseover(function () {
		 var id = $(this).attr('id');
		 if (id !== _id) {
		 // reset old id
		 if (_id !== 0) {
		 // hide info
		 $('#info' + _id).fadeOut(_fadeOut);
		 }
		 // set new id
		 _id = id;
		 var i = $(_xmlData).find('#' + _id + ' info').text();
		 var w = $(_xmlData).find('#' + _id + ' width').text();
		 var l = $(_xmlData).find('#' + _id + ' left').text();
		 var t = $(_xmlData).find('#' + _id + ' top').text();
		 // hilight mountain
		 $('#main-mountains-hitarea span').css({ 'left': l + 'px', 'width': w + 'px', 'background-position': -l + 'px ' + t + 'px' });
		 // show info
		 $('#info' + _id).css({ 'left': i + 'px' }).fadeIn(_fadeIn);
		 }
		 }).click(function () { return false }); */
	};
	
	/* clients */
	if ($('body').hasClass('clients')) {
	
		// position in middle
		$('#main ul li').each(function(index){
			var containerHeight = $('a', this).height();
			var companyHeight = (containerHeight - $('a span.company', this).height()) / 2;
			var quoteHeight = (containerHeight - $('a span.quote', this).height()) / 2;
			$('a span.company', this).css({
				'top': companyHeight
			});
			$('a span.quote', this).css({
				'top': quoteHeight
			});
		});
		
		
		// client quote hover
		$('#main ul li').hover(function(){
			$('a span.quote', this).css({
				'left': '0px'
			});
			$('a span.company', this).css({
				'left': '-310px'
			});
		}, function(){
			$('a span.quote', this).css({
				'left': '-310px'
			});
			$('a span.company', this).css({
				'left': '0px'
			});
		});
		
	}
	
	/* careers */
	if ($('body').hasClass('careers')) {
		//set results height;
		var resultsHeight = $('#header').height() + $('#main').height() + -30; // compensate for nav absolute position;
		$('#results').css({
			'height': resultsHeight
		});
		
		var choicesMade = new Array(null, false, false, false);
		var repelFrom = new Array(130, 230, 375, 520);
		var repel = 0;
		
		//check to see if buttons are already checked
		$('form fieldset').each(function(index){
			var checked = $('input:checked', this).val();
			if (checked !== undefined) {
				choicesMade[index + 1] = true;
				repel++;
			}
		});
		// animate to positon
		$('#col1 .info span').animate({
			height: repelFrom[repel] + 'px'
		}, {
			duration: 1000,
			specialEasing: {
				height: 'easeOutBounce'
			}
		}, 200, 'linear');
		$('form fieldset').click(function(){
			choiceId = $('input', this).attr('name').replace('Choice', '');
			if (choicesMade[choiceId] == false) {
				choicesMade[choiceId] = true;
				repel++
				$('#col1 .info span').animate({
					height: repelFrom[repel] + 'px'
				}, {
					duration: 1000,
					specialEasing: {
						height: 'easeOutBounce'
					}
				}, 200, 'linear');
			}
		});
		
		// check form
		$('form').submit(function(){
			var complete = true, pass = 0, key = new Array('True', 'True', 'True');
			$('fieldset', this).each(function(index){
				var fieldset = $('input:checked', this).val();
				if (fieldset !== undefined) {
					if (fieldset == key[index]) {
						pass++;
					}
				}
				else {
					complete = false;
				}
			});
			if (complete) {
				$('#results').css({
					'display': 'block'
				});
				if (pass !== key.length) {
					$('#results-wrapper').removeClass('passed').addClass('failed');
					$('#results-wrapper h4').html('We&rsquo;re sorry');
					$('#results-wrapper h5').html('You currently lack the conviction we need <br />to serve our clients well.');
					$('#results-wrapper p').css({
						'display': 'none'
					});
				}
				$(window).scrollTop(76);
			}
			else {
				alert("All questions need a selection.");
			}
			return false;
		});
		
		// close pop-up
		$('#results .close-btn span').click(function(){
			$('#results').css({
				'display': 'none'
			});
		});
	}
	
	/* services */
	if ($('body').hasClass('services')) {
		//add class to col anchor related to current slide
		$('#three-column-wrapper .col ul li a[name="' + _selected + '"]').attr({
			'class': 'selected'
		});
		// add click capture to col links
		$('#three-column-wrapper .col').each(function(i){
			$('ul li', this).each(function(ii){
				$('a', this).click(function(){
					colClick($(this).attr('name'));
					return false;
				});
			});
		});
		
		// move content flow
		function colClick(index){
			newFlow.moveTo(index);
		}
		
		$('#more-info .close-btn span').click(function(){
			$('#more-info').css({
				'display': 'none'
			});
			$('.typekit').css({
				'display': 'block'
			});
			$('#header-container-nav a#logo').css({
				'height': '177px'
			});
			var carousel = $('#carousel').data('jcarousel');
			carousel.scroll(1); //reset;
		});
		
		// init jcarousel
		$('#carousel').jcarousel({
			scroll: 1,
			itemFirstInCallback: checkPosition
		
		});
		
		// hack to appropriate witdth
		var listWidth = 0;
		$('#carousel li').each(function(j){
			listWidth += parseFloat($(this).css('width').replace('px', ''));
			if (j == 0) {
				listWidth += listWidth
			}
		});
		$('.jcarousel-container .jcarousel-clip .jcarousel-list').css({
			'width': listWidth
		});
		
		//capture next & previous click
		$('.jcarousel-container .jcarousel-next').click(function(){
			if (String($(this).attr('disabled')) == 'false') {
				colClick(_selected + 1);
			}
		});
		$('.jcarousel-container .jcarousel-prev').click(function(){
			if (String($(this).attr('disabled')) == 'false') {
				colClick(_selected - 1);
			}
		});
		
	}
	
	/*about*/
	if ($('body').hasClass('about')) {
		//function to load twitter feed
		getTwitters('tweet', {
			id: 'MotiveQuest',
			count: 6,
			enableLinks: true,
			ignoreReplies: true,
			clearContents: true,
			template: '<span class="company"><a href="http://twitter.com/%user_screen_name%/statuses/%id_str%/">%user_screen_name%</a></span> <span class="subject">"%text%"</span> <span class="author">%source%</span> <span class="time">// %time%</span>'
		});
	}
	
	// Mail address replace
	jQuery.fn.nospam = function(settings){
		settings = jQuery.extend({
			replaceText: false, // optional, accepts true or false
			filterLevel: 'normal' // optional, accepts 'low' or 'normal'
		}, settings);
		
		return this.each(function(){
			e = null;
			if (settings.filterLevel == 'low') { // Can be a switch() if more levels added
				if ($(this).is('a[rel]')) {
					e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
				}
				else {
					e = $(this).text().replace('//', '@').replace(/\//g, '.');
				}
			}
			else { // 'normal'
				if ($(this).is('a[rel]')) {
					e = $(this).attr('rel').split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
				}
				else {
					e = $(this).text().split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
				}
			}
			if (e) {
				if ($(this).is('a[rel]')) {
					$(this).attr('href', 'mailto:' + e);
					if (settings.replaceText) {
						$(this).text(e);
					}
				}
				else {
					$(this).text(e);
				}
			}
		});
	};
	
	$('a.mail_link').nospam({
		replaceText: true,
		filterLevel: 'low'
	});
	
	$('#myFantasticFlow .item img').each(function(index){
		$(this).attr({
			'href': 'javascript:targetclick()'
		});
		
	});
	
	//Header handlers
	$("#header-container-nav .typekit li a").click(function(e){
		var link_key = $(e.target).attr("rel");
		$.ajax({
			url: '/tracking.asmx/track',
			async: false,
			type: 'POST',
			data: 'link=' + link_key,
			success: function(){
			},
			failure: function(){
			}
		});
		return true;
	});
	
});

$(window).load(function(){
	/* clients */
	if ($('body').hasClass('clients')) {
	
	
		// position in middle
		$('#main ul li').each(function(index){
			var containerHeight = $('a', this).height();
			var companyHeight = (containerHeight - $('a span.company', this).height()) / 2;
			var quoteHeight = (containerHeight - $('a span.quote', this).height()) / 2;
			$('a span.company', this).css({
				'top': companyHeight
			});
			$('a span.quote', this).css({
				'top': quoteHeight
			});
		});
		
	}
})



