var constants = {};
constants.defaultSection = 'capacity';

function log(value){
	if(typeof console == 'undefined'){
		return false;
	}
	console.log(value)	
}

function initTourOverlay() {
	// if the page has no tour overlay, load it via ajax
	if($('#tourOverlay').length === 0){		
		$.ajax({
			method: "get",url: "/assets/tour_skeleton.html",
			beforeSend: function(){						
				
				}, // end before send
			complete: function(){ 
				// log('Request Complete');
				}, //end complete
			success: function(result){ 												
				$('body').prepend(result);	
				initTourOverlay();
			}			
		}); //close $.ajax(		
	}
		else {
		
		$('#tourHeader .nav ol li a').click(function(e){
			e.preventDefault();
			if($(this).hasClass('current')){
				return false;
			}	
			$('a.current').removeClass('current');
			$(this).addClass('current');
			var section = $(this).attr('href').split('#')[1];	
			setTourContent(section);
		});		
		log('binding dt');
		$('#tourContainer #tourContentList dt').live('click', function(){
			
			if($(this).next('dd').hasClass('hidden')){
				$('#tourContentList dt.active').removeClass('active');
				$(this).addClass('active');
				// fade out the other overlays
				$('.overlay').fadeOut(function(){
					$(this).addClass('hidden');
				});			
				// fade in the associated overlay
				$('#' + $(this).data('overlayId')).fadeIn(function(){
					$(this).removeClass('hidden');
				});
				
				// slide up the other content
				$('#tourContentList dd.revealed').removeClass('revealed').slideUp(function(){
					$(this).addClass('hidden');
				});
				
				// slide down the associated content
				$(this).next('dd').slideDown(function(){
					$(this).removeClass('hidden').addClass('revealed');
				});
				
			}
		});		
		
		$('#tourCloser').click(function(){
			hideTourOverlay();
		})
		
		var containerTop = ($(window).height() - $('#tourContainer').height()) / 2;
		var containerLeft = (($(window).width()) - $('#tourContainer').width()) / 2;
		var containerCss = {
			'top' : containerTop,
			'left' : containerLeft
		}				
		var section = setTourNav();	
		setTourContent(section);	
		$('#tourContainer').css(containerCss);
		$('#tourOverlay').fadeIn('fast');
	}
}

function hideTourOverlay(){
	$('#tourOverlay').fadeOut('fast');
}


function setTourNav(){
	urlParts = window.location.href.split('#');	
	
	var section = false;	
	var sectionIndex = 0;
		
	if(urlParts.length > 1){		
		log(urlParts[urlParts.length-1]);
		section = 	urlParts[urlParts.length-1];		
	}
	else {
		section = constants.defaultSection;
	}
	$('.nav a').each(function(){
		if($(this).attr('href').split('#')[1] == section){				
			$(this).addClass('current');
			sectionIndex = $(this).index();
		}
	});	
	return section;
}


function getContentDictionary(section){	
	for(var x = 0, max = tourDictionary.length; x < max; x++){	
		if(tourDictionary[x].name == section){	
			return tourDictionary[x];
		}	
	}	

}


function setTourContent(section){		
	
	var contentDictionary = getContentDictionary(section);		
	if(typeof contentDictionary == 'object'){
	
		if(contentDictionary.type == 'slide'){
			//set the slide
			if($('#tourVideoContainer').length > 0){
				var anim = {height: 530,  top: parseInt($('#tourContainer').css('top')) + 100};				
				$('#tourContainer').animate(anim, 300);
				$('#tourVideoContainer').fadeOut('fast', function(){
					$(this).remove();
					$('#tourContentContainer').fadeIn(function(){
						$(this).removeClass('hidden');
						$('#tourVisualContainer').removeClass('hidden');
						$('#contactUsContainer').fadeIn().removeClass('hidden');
					})
				})
			}	
			var bg = 'url(' + contentDictionary.slide+ ') 0 0  no-repeat';				
			$('#tourVisualContainer').fadeOut('fast', function(){
				$(this).css('background', bg).fadeIn('slow')
			});
			$('#tourContentList dt:not(.template), #tourContentList dd:not(.template), #tourVisualContainer .overlay').remove();
			//set the overlays 
			for (var x = 0, max = contentDictionary.overlays.length; x < max; x++){	
				// set up the overlays
				var newOverlayCss = {
					height: parseInt(contentDictionary.overlays[x].height) + 15 + 'px',
					width: parseInt(contentDictionary.overlays[x].width) + 15 + 'px',
					top:  contentDictionary.overlays[x].top,
					left: parseInt(contentDictionary.overlays[x].left) - 15 + 'px'				
					}
					
				var newOutlineCss = {
					height: contentDictionary.overlays[x].height,
					width: contentDictionary.overlays[x].width				
					}
					
				var newOverlay = 
					$('#tourVisualContainer .overlayTemplate')
					.clone()		
					.attr('id', section + '_overlay_' + x)				
					.css(newOverlayCss)
					.removeClass('overlayTemplate')
					.addClass('overlay');
				
				$(newOverlay)
					.find('.number')
					.text(parseInt(x+1))
					.end()
					.find('.outline')
					.css(newOutlineCss);
				
				$('#tourVisualContainer').append(newOverlay);
			
				// set up the headings and content
				var newContentHeading = 
					$('#tourContentList dt.template')
					.clone()		
					.attr('id', section + '_heading_' + x)					
					.removeClass('template')
					.data('overlayId', section + '_overlay_' + x)
					.find('span')
					.text(parseInt(x+1))
					.end()
					.append(contentDictionary.overlays[x].heading);
					
				var newContentBody = 
					$('#tourContentList dd.template')
					.clone()
					.attr('id', section + '_body_' + x)
					.html(contentDictionary.overlays[x].content)
					.removeClass('template')
					.addClass('hidden');;			
					
				$('#tourContentList').append(newContentHeading).append(newContentBody);
					
			}
			$('#tourContentList dt:not(.template):first').addClass('active');
			$('#tourContentList dd:not(.template):first').slideDown().removeClass('hidden').addClass('revealed');				
			$('#tourVisualContainer .overlay:first').fadeIn(function(){
				$(this).removeClass('hidden');
			});
		}
		else {
		
			$(this).addClass('hidden');							
			$.ajax({
				method: "get",url: contentDictionary.url,
				beforeSend: function(){						
					$('#tourVisualContainer, #tourContentContainer, #contactUsContainer').fadeOut().addClass('hidden');	
					}, //show loading just when link is clicked
				complete: function(){ 
					log('Request Complete');
					}, //stop showing loading when the process is complete
				success: function(result){ 												
					var anim = {height: 685, top: parseInt($('#tourContainer').css('top')) - 100};
					$('#tourContainer').animate(anim, 300, function(){
						$('#tourVideoWrapper').html(result);							
					});
				}			
			}); //close $.ajax(	
			
			
			$('#tourVideoWrapper').load(contentDictionary.url, function(){
				$(this).fadeIn(function () {
					$(this).removeClass('hidden');
				});
			});		
			
		}
		
	}
	else {
		log('No content for this section');
	}
	
	
	
}


