$(document).ready(function() {
	on = false;
	stop = false;
	slideOn = true;
//	slideShow();
	if ($('#mapWrap').length > 0) initMap();
	
	
	if($('#slidesWrapText').length > 0) {
		SlideShow.init();
	}

	
	// Fancy Box
	
	//$(".sideBar").children('a').fancybox();
	Nav();
});

config = {
	slideshow : {
		rotateSpeed : 4000,
		fadeSpeed : 300
	}
};

SlideShow = function() {
	var $slideshow, $nav, $images, $text;
	var slidesNo = 0, useSlideshow = true;	
	
	function init() {
		// init vars bind DOM elements
		$slideshow = $('#slider');
		$nav = $slideshow.children('ul');
		$images = $('#slidesWrapImages');
		$text = $('#slidesWrapText');
		slidesNo = $images.children(':last').index() + 1;
					
		$nav.children(':first').addClass('on');
		$images.children().not(':first').hide();
		$text.children().not(':first').hide();
		rotateSlides();
		bindEvents();
	}
	
	function rotateSlides() {
		var slideShowing = 1;
		
		function nextSlide() {
			
			setTimeout(function() {
				if(useSlideshow) {
					fadeSlideUp();
				}
			}, config.slideshow.rotateSpeed);
			
		}
		
		function resetSlides() {
			setTimeout(function() {
				if(useSlideshow) {
				
					$nav.children().removeClass('on').eq(0).addClass('on');

					$images.children('img:first').show();
					$images.children('img:last').fadeOut(config.slideshow.fadeSpeed);

					$text.children('div:first').fadeIn(config.slideshow.fadeSpeed);
					$text.children('div:last').fadeOut(config.slideshow.fadeSpeed);

					slideShowing = 1;
					nextSlide();
				}
			}, config.slideshow.rotateSpeed);
		}

		
		function fadeSlideUp() {
			slideShowing++;
			
			// add on state to nav item
			$nav.children().removeClass('on').eq(slideShowing-1).addClass('on');

			// fade in slide image
			$images.children('img').eq(slideShowing-1).fadeIn(config.slideshow.fadeSpeed, function() {
				//hide the previous slide after this one has faded in
				$images.children('img').eq(slideShowing-2).hide();
			});

			//fade in slide text, fade out previous text
			$text.children('div').eq(slideShowing-1).fadeIn(config.slideshow.fadeSpeed);
			$text.children('div').eq(slideShowing-2).fadeOut(config.slideshow.fadeSpeed);
			
			// if it's not the last slide...
			if(slideShowing != slidesNo) {
				nextSlide();
			} else {
				resetSlides();
			}
			
			
		}
		
		nextSlide();
	}
	
	function showSlide($navItem) {
		useSlideshow = false;
		var thisSlide = $navItem.parent().index();
		$nav.children().removeClass('on');
		$navItem.parent().addClass('on');
		$images.children('img').hide().eq(thisSlide).show();
		$text.children('div').hide().eq(thisSlide).show();		
	}
	
	function bindEvents() {
		$nav.children('li').children('a').click(function() {
			showSlide($(this));
			
			return false;
		});
	}
	
	
	
	return {
		init: init
	}
}();



function initMap() {
	var mapWrap = $('#mapWrap');
	var centerCoords = String(mapWrap.attr('data-center')).split(', ');
	var latlng = new google.maps.LatLng(Number(mapWrap.attr('data-lat')), Number(mapWrap.attr('data-lng')));
	var center = new google.maps.LatLng(Number(centerCoords[0]), Number(centerCoords[1]));
	var options = {
		zoom: Number(mapWrap.attr('data-zoom')),
		center: center,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById('mapWrap'), options);
}

Nav = function() {

	function bindEvents() {
		$('#topNav ul li').hover(function() {
			$(this).children('.secondary').addClass('secondaryHover');
		}, function() {
			$(this).children('.secondary').removeClass('secondaryHover');
		});
		
		$('#topNav ul.secondary li').hover(function() {
			$(this).children('ul.tertiary').addClass('tertiaryHover');
		}, function() {
			$(this).children('ul.tertiary').removeClass('tertiaryHover');
		});
		
	}
	
	bindEvents();
};

