//--------------------------------------------------
// COOKIES
//--------------------------------------------------
function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

function setCountry() {
	$('ul#footer-countries ul.address').hide();
	
	if ( getCookie('exp_country') ) {
		var curCountry = getCookie('exp_country');
		$('ul#footer-countries li.' + curCountry + ' ul.address').addClass('active').show();
	} else {
		$('ul#footer-countries li:first ul.address').addClass('active').show();
	}
	
	$('ul#splash-countries li a, ul#footer-countries li a').click( function() {
		var country = $(this).attr('id');
		setCookie('exp_country', country, '7')
	});
}

function footerAddress() {
	var countryFade = 300;
	
	
	/* Disabled so we can do a hard link to the new country code URL
	$('ul#footer-countries a.country').click( function(e) {
		$(this).blur();
		e.preventDefault();
	});
	*/
	
	$('ul#footer-countries a.country').hover( function() {
		if ( !$(this).siblings('ul.address').hasClass('active') ) {
			
			$('ul#footer-countries ul.address').removeClass('active').hide();
			$(this).siblings('ul.address').addClass('active').show();
		}
	});
}


//--------------------------------------------------
// MOBILE
//--------------------------------------------------
function mobile() {
	if ( ( navigator.userAgent.match(/iPhone/i) ) || ( navigator.userAgent.match(/iPod/i) ) || ( navigator.userAgent.match(/iPad/i) ) || ( navigator.userAgent.match(/blackberry/gi) ) || ( navigator.userAgent.match(/android/gi ) ) ) {
		return true;
	}
}


//--------------------------------------------------
// PARALLAX
//--------------------------------------------------
function parallax() {
	if ( !mobile() ) {
		var windowScrollTop = $(window).scrollTop();
		var windowHeight = $(window).height();
		var headerHeight = $('div#header').height();
		var footerHeight = $('div#footer').height();
		var windowTop = windowScrollTop + headerHeight;
		var windowBottom = windowScrollTop + windowHeight - footerHeight;
		var viewHeight = windowHeight - headerHeight - footerHeight;
		
		$('div.parallax:not(.last)').each( function() {
			var height = $(this).height();
			var marginTop = parseInt( $(this).css('margin-top') );
			var heightDifference = height - viewHeight;
			var top = $(this).parent('div.parallax-section').offset().top;
			var bottom = top + height;
			
			if ( heightDifference < 0 ) {
				var heightDifference = 0;
			}
			
			var newMarginTop = ( windowTop - top - heightDifference ) * 0.4;
			
			if ( newMarginTop >= 0 && windowBottom >= bottom ) {
				$(this).css('margin-top', newMarginTop + 'px');
			} else if ( marginTop > 0 ) {
				if ( newMarginTop < 0 ) {
					var newMarginTop = 0;
				}
				
				$(this).css('margin-top', newMarginTop + 'px');
			}
		});
	}
}

function parallaxLast() {
	var windowHeight = $(window).height();
	var headerHeight = $('div#page-top').height();
	var footerHeight = $('div#page-bottom').height();
	var viewHeight = windowHeight - headerHeight - footerHeight;
	
	$('div.parallax.last').css('min-height', viewHeight);
}

function parallaxStart() {
	if ( !mobile() ) {
		$('div.parallax:last').addClass('last');
		
		$('div.parallax:not(.last)').each( function() {
			var fixHeight = $(this).parent('div.parallax-section').height();
			
			$(this).parent('div.parallax-section').css({ 'height': fixHeight, 'overflow': 'hidden' });
		});
		
		parallaxLast();
		
		$(window).scroll(function() {
			parallax();
		});
		
		$(window).bind('resize', function() { 
			parallax();
			parallaxLast();
		});
	}
}


//--------------------------------------------------
// AUTOSCROLL
//--------------------------------------------------
function autoScroll() {
	var scrollTime = 400;
	
	$('ul.side-menu a').click( function(e) {
		$(this).blur();
		e.preventDefault();
		
		var section = $(this).attr('href');
		var sectionOffset = $('div' + section).parent('div.parallax').parent('div.parallax-section').offset().top;
		var headerHeight = $('div#header').height();
		var sectionTop = sectionOffset - headerHeight;
		var windowTop = $(window).scrollTop();
		var scrollSpeed = ( ( windowTop - sectionTop ) / scrollTime ) * scrollTime;
		
		if ( scrollSpeed < 0 ) {
			var scrollSpeed = scrollSpeed * -1;
		}
		
		$('html, body').animate( { scrollTop: sectionTop + 'px' }, scrollSpeed);
	});
}


//--------------------------------------------------
// INFOGRAPHIC BARS (Currently Disabled)
//--------------------------------------------------
function infoGraphic() {
	$('.infographic').each(function() {
		var dynWidth = $(this).attr('rel');
		dynWidth = parseInt(dynWidth);
		dynWidth = dynWidth/3 + 40
		$(this).width(dynWidth);
	});
}


//--------------------------------------------------
// MIN HEIGHT
//--------------------------------------------------
function minHeight() {
	var windowHeight = $(window).height();
	var headerHeight = $('div#page-top').height();
	var footerHeight = $('div#page-bottom').height();
	var viewHeight = windowHeight - headerHeight - footerHeight;
	
	$('div#error-404').css('min-height', viewHeight);
	$('div#our-team-single').css('min-height', viewHeight);
}


//--------------------------------------------------
// START
//--------------------------------------------------
$(document).ready( function() {
	setCountry();
	footerAddress();
	parallaxStart();
	parallax();
	autoScroll();
	minHeight();
	
	$(window).bind('resize', function() { 
		minHeight();
	});
});


