// JavaScript Document

/***
	WORLD MAP
*/

(function( $ ) {
					
	function changeMap (newMap) {
		var currentMap = $('img.active');
		
		if (!newMap.hasClass('active')) {
			newMap.addClass('activate').animate({opacity: 1.0}, 700, "easeOutQuart", function () {
				currentMap.css({opacity: 0.0}).removeClass('active');
				newMap.addClass('active').removeClass('activate');
			});
		}
	};
					
	$.fn.worldMap = function () {
		$this = $(this);
		$nav = $('> ul > li', this);
		$subNav = $('> ul > li > ul > li', this);
		$imgs = $('> div.container > img', this);
		$areas = $('> div.container > map > area', this);
		$boxes = $('.box', this);
		
		$imgs.css({opacity: 0.0}).first().addClass('active').css({opacity: 1.0});
		$boxes.hide();

		$('#back-lnk', $this).click(function() {
			$defaultImg = $imgs.first();
			$nav.removeClass('active');
			$boxes.hide();
			changeMap($defaultImg);
		});

		$nav.each(function() {
			var $linkWrapper = $(this);
			var rel = $('a', this).attr('rel');
			var delay;
			
			$linkWrapper.hover(function () {
					$linkWrapper.addClass('hover');
				}, function () {
					$linkWrapper.removeClass('hover');
			});

			if (rel.length > 1) {
				var $img = $(rel);
				
				$(this).click(function () {
					$boxes.hide();
					if (!$linkWrapper.hasClass('active')) {
						$nav.removeClass('active');
						$linkWrapper.addClass('active');
						changeMap($img);
						return false;
					}
				});
				
			}
			
		});
		
		$subNav.each(function() {
			var $linkWrapper = $(this);
			var rel = $('a', this).attr('rel');

			if (rel.length > 1) {
				var $img = $(rel);
				var country = rel.split('-').pop();
				var $box = $('#box-' + country);
				
				$linkWrapper.click(function () {
					$nav.removeClass('active').removeClass('hover');
					$linkWrapper.parents('li').first().addClass('active');
					changeMap($img);
					$boxes.hide();
					$box.show();

					return false;
				});
			}
			
		});
		
		$areas.each(function () {
			var $area = $(this);
			var rel = $area.attr('rel');
			var href = $area.get(0).hash;
			
			if (rel.length > 1) {
				var $img = $(rel);
				
				$area.hover(function () {
						if($.browser.msie) {
							$img.addClass('activate').css({opacity: 1.0});
						} else {
							$img.addClass('activate').animate({opacity: 1.0}, 700, "easeOutQuart");
						}
						/*
						*/
					}, function () {
						if($.browser.msie) {
							$img.css({opacity: 0.0}).removeClass('activate');
						} else {
							$img.animate({opacity: 0}, 50, "easeOutQuart", function () {
								$img.removeClass('activate');
							});
						}
				});
			}

			if (href.length > 1) {
				var $clickImg = $(href);

				$area.click(function () {
					if ($clickImg.length) {
						
						changeMap($clickImg);
						
						if ($area.hasClass('nav-na')) {
							$('#nav-na').addClass('active');
						}
						if ($area.hasClass('nav-eu')) {
							$('#nav-eu').addClass('active');
						}
						if ($area.hasClass('nav-me')) {
							$('#nav-me').addClass('active');
						}
						if ($area.hasClass('nav-ap')) {
							$('#nav-ap').addClass('active');
						}
							
						if ($area.hasClass('show-box')) {
							var country = rel.split('-').pop();
							var $box = $('#box-' + country);
							$boxes.hide();
							$box.show();
						}
					}
					return false;
				});
			}			
			
		});

	};
	
})( jQuery );

/***
	INFINITE CAROUSEL
*/

(function( $ ) {

	$.fn.infiniteCarousel = function (options) {
	
		options = $.extend($.fn.infiniteCarousel.defaults, options);
	
		function repeat(str, num) {
			return new Array( num + 1 ).join( str );
		}
		
		return this.each(function () {
			var $wrapper = $('> div', this).css('overflow', 'hidden'),
				$slider = $wrapper.find('> ul'),
				$items = $slider.find('> li'),
				$single = $items.filter(':first'),
				
				singleWidth = $single.outerWidth(true), 
				visible = Math.ceil($wrapper.innerWidth() / singleWidth),
				currentPage = 1,
				pages = Math.ceil($items.length / visible),
				autoAdvance = null;        
					
			// 1. Pad so that 'visible' number will always be seen, otherwise create empty items
			if (($items.length % visible) != 0) {
				$slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> li');
			}
		
			// 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
			$items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> li'); // reselect
			
			// 3. Set the left position to the first 'real' item
			$wrapper.scrollLeft(singleWidth * visible);
			
			// 4. paging function
			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
					n = Math.abs(currentPage - page),
					left = singleWidth * dir * visible * n;
				
				$wrapper.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, 500, function () {
					if (page == 0) {
						$wrapper.scrollLeft(singleWidth * visible * pages);
						page = pages;
					} else if (page > pages) {
						$wrapper.scrollLeft(singleWidth * visible);
						// reset back to start position
						page = 1;
					} 
					
					$('a:nth-child(' + currentPage + ')', pagination).removeClass('active');
					$('a:nth-child(' + page + ')', pagination).addClass('active');
					currentPage = page;
				});                
				
				return false;
			}
			
			var pagination = $('<div class="pagination"></div>').insertBefore($wrapper);
			for (var i = 1; i <= pages; i++) {
				$('<a href="#">' + i + '</a>').click(function() {
					gotoPage(parseInt($(this).html()));
					return false;
				}).appendTo(pagination);
			}
			$('a:first', pagination).addClass('active');
			
			$wrapper.after('<a class="arrow back">Back</a><a class="arrow forward">Next</a>');
			
			// 5. Bind to the forward and back buttons
			$('a.back', this).click(function () {
				return gotoPage(currentPage - 1);                
			});
			
			$('a.forward', this).click(function () {
				return gotoPage(currentPage + 1);
			});
			
			// create a public interface to move to a specific page
			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
			
			
			// Initiate auto advance
			
			function initAutoAdvance () {
				return setInterval (function () {
					gotoPage(currentPage + 1);
				}, options.speed);
			};
			
			if (options.speed > 0) {
				autoAdvance = initAutoAdvance ();
				$wrapper.hover(function () {
						clearInterval(autoAdvance);
					}, function () {
						autoAdvance = initAutoAdvance ();
				});
			};
			
		});  
	};
	
	$.fn.infiniteCarousel.defaults = {
		speed : 5000
	};
	
})( jQuery );

$(function () {
						
	// Initialize gallery components
	$('#gallery-overlay').overlay('init');
	$('.infinite-carousel').infiniteCarousel({ speed : 0 });
	$('#carousel .wrapper a').each(function (i) {
		$(this).click(function () {
			$('#gallery-overlay').overlay('open');
			$('#overlay-carousel').trigger('goto', i + 1);
			return false;
		});
	});
	
	// Initialize overlays
	$('.overlay-trigger').overlay();
	
	// Initialize world map
	$('#world-map').worldMap();

	// Initialize HQ Map
	$('#map-highlight').hide();
	$('#learning-center').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-learning-center.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#solution-place').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-solution-place.jpg';
		$('#map-highlight').fadeIn('medium');
	});	
	$('#service-center').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-service-center.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#transformation-place').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-transformation-place.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#associate-center').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-associate-center.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#innovation-place').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-innovation-place.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#delivery-center').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-delivery-center.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#innovation-campus').mouseover(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-innovation-campus.jpg';
		$('#map-highlight').fadeIn('medium');
	});
	$('#learning-center').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-learning-center.jpg';
		$('#map-highlight').hide();
	});
	$('#solution-place').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-solution-place.jpg';
		$('#map-highlight').hide();
	});
	$('#service-center').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-service-center.jpg';
		$('#map-highlight').hide();
	});
	$('#transformation-place').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-transformation-place.jpg';
		$('#map-highlight').hide();
	});
	$('#associate-center').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-associate-center.jpg';
		$('#map-highlight').hide();
	});
	$('#innovation-place').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-innovation-place.jpg';
		$('#map-highlight').hide();
	});
	$('#delivery-center').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-delivery-center.jpg';
		$('#map-highlight').hide();
	});
	$('#innovation-campus').mouseout(function() {
		document.getElementById('map-highlight').src='http://cerner.levelfivesolutions.com/assets/img/careers/career-worldhq-innovation-campus.jpg';
		$('#map-highlight').hide();
	});

});

