/* 
	Mont Blanc Summer
	Site Functions
	functions.site.js
	
	Creator: Matt Kircher
	Created: 3/24/10

*/

function initHomeMasthead(){
						
	//initial background
	$('#home-masthead').attr('class','');
	
	//feature blocks
	$('#home-masthead > div').each(function(index){
				
		//add "next" functionality
		$(this).append('<p class="next_button"><a href="'+$(this).find('p.link a').attr('href')+'" onclick="return false;">More Information...</a></p>');
		$(this).find('.next_button a').css({ opacity:0.75 }).bind('click', function(){
			
			clearInterval(timer);
			mastheadIndex++;
			mastheadIndex = (mastheadIndex >= count)?0:mastheadIndex;
			
			setHomeMastheadTimer();
			changeHomeMasthead(mastheadIndex);
			return false;
		});
	});
	
	count = $('#home-masthead > div').length;
	
	//add feature 'nav' blocks
	$('#home-masthead').prepend('<ul id="home-feature-nav"></ul>')
	
	$('#home-masthead > div').each(function(index){
		$('#home-feature-nav').append('<li></li>');
		$('.next_button:eq('+index+') a').clone().appendTo($('#home-feature-nav li:eq('+index+')'));
		$('#home-feature-nav a:eq('+index+')').bind('click', { i:index }, function(event){
			
			clearInterval(timer);
			mastheadIndex = event.data.i;
			
			setHomeMastheadTimer();
			changeHomeMasthead(mastheadIndex);
			return false;
		});
	});
	
	setHomeMastheadTimer();
	changeHomeMasthead(mastheadIndex);
}

function setHomeMastheadTimer(){
	timer = setInterval(function(){
		mastheadIndex++;
		mastheadIndex = (mastheadIndex >= count)?0:mastheadIndex;
		changeHomeMasthead(mastheadIndex);
		
	}, 8000);	
}

function changeHomeMasthead(f){
	
	//hide all divs
	$('#home-masthead div').stop().animate({ opacity:0, top:'300px' }, 300, function(){
		
		//hide bg
		$('#home-masthead').stop().animate({ opacity:0 }, 400, function(){
			
			//change 'feature' nav
			$('#home-feature-nav li:eq('+f+')').css({ opacity:0.33 }).siblings().css({ opacity:1 });
			
			//change bg class
			$(this).attr('class', 'feature'+f);
			
			//animate bg
			$(this).animate({ opacity:1 }, 500, function(){
				
				//show div
				$('#home-masthead div:eq('+f+')').animate({ opacity:1, top:'200px' }, 700);
			});
		});										    
	});
}

function initAreaMap(language){
	if($('#area-map').length){
		
		var $map = $('#area-map');
		var en_labels = Array("Open Legend", "Close Legend", "Click to show / hide these points of interest", "Show All", "Hide All");
		var fr_labels = Array("Ouvrez la légende", "Légende étroite", "Cliquez sur pour montrer ou cacher cet intérêt", "Tout", "Rein");
		var labels = (language=="FR")?fr_labels:en_labels;
		
		//create legend frame
		var legend_html = '<div id="legend">';
		    legend_html +='<ul class="clearfix"></ul>';
		    legend_html +='<p id="open-legend"><a href="#">'+labels[0]+'</a></p>';
		    legend_html +='</div>';
		    
		$map.prepend(legend_html);
		
		var $legend = $map.find('#legend');
		
		//go through markers
		$map.find('> ul').each(function(index){
			
			var $class = $(this).attr('class');
			
			//add ids
			$(this).attr('id', $class.replace('_', '-')+'-section');
			
			//add marker classes
			$(this).find('li').each(function(index){
				$(this).addClass('m'+index);
				if($(this).html() == ''){
					$(this).html($class+' marker '+index);
				}
			});
			
			if(language == "FR"){
				
			}
			
			//add legend entry
			$legend.find('ul').append('<li id="legend'+index+'"><a href="#'+$(this).attr('id')+'" title="'+labels[2]+'">'+$(this).prev(':header').text()+'</a></li>');
		});
		
		//setup legend triggers
		$legend.find('ul li a').click(function(){
			$(this).trigger('blur');
			$(this).parent().toggleClass('inactive');
			if($(this).parent().hasClass('inactive')){
				//hide markers
				if($.support.leadingWhitespace){
					$($(this).attr('href')).animate({ opacity:0 }, 300);
				} else { 
					$($(this).attr('href')).hide();
				}
			} else {
				//show markers
				if($.support.leadingWhitespace){
					$($(this).attr('href')).animate({ opacity:1 }, 300);
				} else { 
					$($(this).attr('href')).show();
				}
			}
			return false;
		});
		
		//setup show hide all
		$legend.find('ul')
			.append('<li id="show_all"><a href="#">'+labels[3]+'</a></li>')
			.append('<li id="hide_all"><a href="#">'+labels[4]+'</a></li>');
		
		//show all
		$legend.find('#show_all a').click(function(){
			$(this).trigger('blur');
			
			//hide markers
			$legend.find('li:not(#show_all, #hide_all)').removeClass('inactive');
			if($.support.leadingWhitespace){
				$map.find('> ul').animate({ opacity:1 }, 300);
			} else { 
				$map.find('> ul').show();
			}
			
			return false;
		});
		
		//hide all
		$legend.find('#hide_all a').click(function(){
			$(this).trigger('blur');
			
			//hide markers
			$legend.find('li:not(#show_all, #hide_all)').addClass('inactive');
			if($.support.leadingWhitespace){
				$map.find('> ul').animate({ opacity:0 }, 300);
			} else { 
				$map.find('> ul').hide();
			}
			
			return false;
		});
		
		
		//setup legend open / close
		$legend.find('#open-legend a').click(function(){
			$(this).trigger('blur');
			$legend.toggleClass('expanded');
			if($legend.hasClass('expanded')){
				//open legend
				$legend.animate({ top:'0px', opacity:0.95 }, 300);
				$map.animate({ height:'750px'}, 300);
				$map.find('> ul').animate({ top:'220px' }, 300);
				$(this).text(labels[1]);
			} else {
				//close legend
				$legend.animate({ top:'-220px' }, 300);
				$map.animate({ height:'530px' }, 300);
				$map.find('> ul').animate({ top:'0px' }, 300);
				$(this).text(labels[0]);
			}
			return false;
		});
		
		//display marker sets
		idx = 0;
		$('#area-map > ul').each(function(index){
			setTimeout(function(){ $('#area-map > ul:eq('+idx+')').fadeIn(300); idx++; }, index*150);
		});
	}
}