/**
 * website.js
 */

var website = {
	
	 bannerInterval: false
	,bannerBaseLeft: 832
	
	,init: function() {
		
		website.init_navigation();
		website.init_header_banners();
		website.init_external_links();
		
		$('#contact-form').validationEngine();
		
	}
	
	,init_navigation: function() {
		$('.navigation li').hoverIntent(function() {
			$(this).find('ul:first').fadeIn('fast');
			
		}, function() {
			$(this).find('ul:first').fadeOut('fast');
			
		});
	}
	
	,init_external_links: function() {
		$("a[href^='http']").click(function(e) {
			var url = $(this).attr('href');
			if( url.match( window.location.hostname ) ) {
				return true;
			}
			window.open(url);
			return false;
		});
	}
	
	,init_header_banners: function() {
		
		var baseleft = 832;
		
		var total = $('.header-banner-panel').length;
		if( total < 2 ) return false;
		
		$('.header-banner-panel:first').addClass('visible');
		
		$('.header-banner-panels-container').width( total * baseleft );
		
		website.do_header_banners_cycle();
		
		$(window).blur(function() {
			clearInterval(website.bannerInterval);
		}).focus(function() {
			website.do_header_banners_cycle();
		});
		
	}
	
	,do_header_banners_cycle: function() {

		clearInterval(website.bannerInterval);
		
		website.bannerInterval = setInterval(function() {
			
			$('.header-banner-panel.visible').animate({
				marginLeft: '-=' + website.bannerBaseLeft
				,opacity: 0
			}, {
				 duration: 'slow'
				,queue: true
				,easing: 'easeInOutQuad'
				,complete: function() {
					$(this).removeClass('visible').appendTo( $('.header-banner-panels-container') ).css('margin-left', 0).css('opacity', 1);
					$('.header-banner-panel:first').addClass('visible');
				}
			});
			
		}, 10000);

	}
	
};

$(document).ready(website.init);
