/*
	functions.site.js
	DOM Interactivity Functions
	Created: July 15, 2008
	Creator: Matt Kircher, Mainline Media LLC
*/

/* GENERAL */
function setupPage(){
	
	//IE6 BG flicker issue
	try { document.execCommand('BackgroundImageCache', false, true); } catch(e){}
	
	BrowserDetect.init();	//start browser detection object
	translateEmails();		//changes unlinked email address to usable ones (spam protection)
	setupNewletterForm();	//initialize Newsletter form on all the pages
	animateHome();		//if on homepage, start to animate the masthead
	
	//* BASIC
		$('a').bind('focus', function(){ $(this).blur(); });
		
		$('img[align="left"]').css({ margin:'10px 15px 10px 0px' });
		$('img[align="right"]').css({ margin:'10px 0px 10px 15px' });
		
		$('table.rate_table tbody tr').each(function(){ $(this).find(':first-child').css({ fontWeight:'bold' }); });
		$('table.rate_table tbody')
		.find('tr:even td').css({ backgroundColor:'#EBEFF5' }).end()
		.find(':first-child td').css({ paddingTop:'15px' }).end()
		.find(':last-child td').css({ paddingBottom:'15px' });
	
	//* NAVIGATION
		$('#functional-nav, #footer-functional-nav, #footer-main-nav').find('ul').each(function(){
			$(this).find(':first-child').addClass('nobullet').css({ paddingLeft:'0px' });
		});
		
		$('#sub-nav ul li:last').addClass('end_nav');
		
		$('.index_page')
		.find('#footer-main-nav ul:eq(0)').css({ marginLeft:'5px' }).end()
		.find('#footer-functional-nav ul:eq(0)').css({ marginLeft:'35px' });
				
		//set nav bg's
		$('.english #main-nav')
		.find('> ul > li:eq(0)')
		.find('a').css({ width:"19px" }).parent().next()
		.find('a').css({ width:"30px", backgroundPosition:"-19px 0px" }).parent().next()
		.find('a').css({ width:"37px", backgroundPosition:"-49px 0px" }).parent().next()
		.find('a').css({ width:"28px", backgroundPosition:"-86px 0px" }).parent().next()
		.find('a').css({ width:"75px", backgroundPosition:"-114px 0px" }).parent().next()
		.find('a').css({ width:"57px", backgroundPosition:"-189px 0px" });
		
		$('.french #main-nav')
		.find('> ul > li:eq(0)')
		.find('a').css({ width:"18px" }).parent().next()
		.find('a').css({ width:"56px", backgroundPosition:"-18px 0px" }).parent().next()
		.find('a').css({ width:"36px", backgroundPosition:"-74px 0px" }).parent().next()
		.find('a').css({ width:"52px", backgroundPosition:"-110px 0px" }).parent().next()
		.find('a').css({ width:"75px", backgroundPosition:"-162px 0px" }).parent().next()
		.find('a').css({ width:"22px", backgroundPosition:"-237px 0px" });
		
		//photo gallery bg		
		$('.drop_down_nav').find('li:last').css({ border:'none' });
		
		//set dropdown hover events
		$('#main-nav')
		.find('> ul > li > a').bind('mouseover', function(event){ $('.drop_down_nav').hide(); }).end()
		.find('> ul > li > a[href="ski.php"]').bind(  'mouseover', function(event){ $('.dd_ski').css({   opacity:0.95, height:eval($('.dd_ski').height())+"px",   marginBottom:"-"+eval($('.dd_ski').height()+5)+"px" }).show(); }).end()
		.find('> ul > li > a[href="stay.php"]').bind( 'mouseover', function(event){ $('.dd_stay').css({  opacity:0.95, height:eval($('.dd_stay').height())+"px",  marginBottom:"-"+eval($('.dd_stay').height()+5)+"px" }).show(); }).end()
		.find('> ul > li > a[href="rates.php"]').bind('mouseover', function(event){ $('.dd_rates').css({ opacity:0.95, height:eval($('.dd_rates').height())+"px", marginBottom:"-"+eval($('.dd_rates').height()+5)+"px" }).show(); }).end()
		.find('> ul > li > a[href="kids.php"]').bind( 'mouseover', function(event){ $('.dd_kids').css({  opacity:0.95, height:eval($('.dd_kids').height())+"px",  marginBottom:"-"+eval($('.dd_kids').height()+5)+"px" }).show(); })
		
		//set dropdown removal event
		$('#masthead').each(function(){
			$(this).bind('mouseover', function(event){
				$('.drop_down_nav').hide();
			});
		});
		
		$('#footer-main-nav')
		.find('li:first').css({ paddingLeft:"0px" }).end()
		.find('.drop_down_nav').remove();
		
		$('#quick-links')
		.find('li:last').addClass('noborder');
		
	//* STRUCTURE
		$('#content')
		.append('<div></div>').find('div:last').addClass('clear_block');
		
		//grids
		$('.grid2, .grid2_offset_left, .grid2_offset_right, .grid3').addClass('clearfix');
		$('.grid3').find('div:eq(2)').addClass('end_col');
		$('.grid2, .grid2_offset_left, .grid2_offset_right, .grid3').equalHeights();
		
			//IE6 Grid CSS (lack of child syntax support)
			if($.browser.msie && $.browser.version == 6){
				$('.grid2 > .left').css({ float:'left', width:'50%' });
				$('.grid2 > .right').css({ float:'right', width:'49%' });
				
				$('.grid2_offset_left > .left').css({ float:'left', width:'33%' });
				$('.grid2_offset_left > .right').css({ float:'right', width:'65%' });
				
				$('.grid2_offset_right > .left').css({ float:'left', width:'66%' });
				$('.grid2_offset_right > .right').css({ float:'right', width:'33%' });
				
				$('.grid3 > div').css({ float:'left', width:'33%' });

				$('.display_grid .grid2 > .left').css({ width:'48%', paddingRight:'2%' });
				$('.display_grid .grid3 > div').css({ width:'31%', paddingRight:'2%' });
				$('.display_grid .grid3 > div.end_col').css({ width:'33%', paddingRight:'0%' });
			}
		
		//headers
		$('#main-content :header').each(function(){
			//add top margin
			$(this).css({ marginTop:"12px" })
			
			//add bottom margin
			if(!$(this).next().is(':header, p, ul, ol, blockquote')){
				$(this).css({ marginBottom:"8px" });
			}
		});
		
		//IE6 issues
		if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6"){			
			$('#footer').prepend('<div></div>').find('div:first').addClass('ie6_footer_edge');
		}
		
		//icon / image application
		$('#ski-conditions-module').prepend('<div></div>').find('div:first').addClass('ski_conditions_icon');
		$('#news-module').prepend('<div></div>').find('div:first').addClass('news_icon');
		$('#newsletter-module').prepend('<div></div>').find('div:first').addClass('newsletter_icon');
		$('#quick-links').prepend('<div></div>').find('div:first').addClass('quick_link_icon');
		
		$('#vmb-callout').prepend('<div></div>').find('div:first').addClass('vmb_callout_image');
		
		$('._home_grid')
		.find(' > div:eq(0)').css({ background:'url(../images/home_image1.png) top left no-repeat' }).end()
		.find(' > div:eq(1)').css({ background:'url(../images/home_image2.png) 0px 10px no-repeat' }).end()
		.find(' > div:eq(3)').css({ background:'url(../images/home_image3.png) top left no-repeat' }).end()
		.find(' > div:eq(4)').css({ background:'url(../images/home_image4.png) 10px 0px no-repeat' }).end();
}

//makes email tags invisible to spiders / spammers
function translateEmails(){
	$('span.email, address.email').each(function(){
		
		//example: <span class="email" title="[title] | [addr at domain dot com] | [subject of email]"> [link text] </span>
							   
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		var inner_content = $(spt).html();						//inner HTML of span tag
		var t = $(spt).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
		
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@").replace(dot,".");				//replace words with chars
		
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed
		
		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

//home animation functions
function animateHome(){
	
	if($('.home').length){
		
		//append leadin to masthead-content
		$('#leadin').appendTo('#masthead-content').fadeIn(700);
	
		//create / format masthead-content
		if(BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6"){}
		else {
			$('#masthead-gallery').each(function(){	
				var count = 1;
				var max_count = 4;
				
				//show initial content
				$('#masthead-content-blocks li:eq(0)')
				.css({ opacity:0, marginBottom:'-150px' }).show()
				.animate({ opacity:1 }, 1000);
								
				var i = setInterval(function(){
								 
					var count_prev = count;
					count = (count >= max_count)?1:(count+1);
					$('#masthead-content-blocks').find('li:eq('+eval(count_prev-1)+')').animate({ opacity:0 }, 1000, function(){
						$('#masthead-gallery').animate({ opacity:0 }, 1000, function(){
													
							//show masthead					
							$('#masthead-gallery')
							.removeClass('feature'+count_prev)
							.addClass('feature'+count)
							.animate({ opacity:1 }, 1000, function(){
								
								//show content
								$('#masthead-content-blocks')
								.find('li').hide().end()
								.find('li:eq('+eval(count-1)+')')
								.css({ opacity:0, marginBottom:'-100px' }).show()
								.animate({ opacity:1 }, 1000);					   
							});
						});
					});
				}, 7000);
			});
		}
	}
}

//coupon printer
function printCoupon(){
	if($('#coupon').length){
		
		var lang = ($('body > div.english').length == 1)?"english":"french";
		
		var win1 = window.open('', 'Coupon');
		win1.document.open();
		win1.document.write('<head><title>Mont Blanc Coupon</title>');
		win1.document.write('<link type="text/css" rel="stylesheet" media="all" href="../stylesheets/main.css" /></head>');
		win1.document.write('<body><div class="'+lang+'"><div id="coupon_frame"><div id="coupon"><img src="../images/coupon_template.gif" style="margin-bottom:-215px;" align="middle" />'+$('#coupon').html()+'</div></div></div></body>');
		win1.print();
		win1.document.close();
	}
}

//interactive accordian
function initInfoAccordian(){
		
	$('.info_accordian').each(function(index){
		
		//no IE support for corners, or thmbnail shadow right now...
		if($.support.leadingWhitespace){
			$('.info_accordian dt').corners('15px'); //make corners if not IE
		} else {
			$('.mini_gallery .photos li').css('background', 'none'); //remove shadows if IE
		}
		
		var page_language = ($('body > div.english').length == 1)?"EN":"FR";
				
		var iaNavIndex = index;
		$(this).find('dt').each(function(index){
			
			var view_txt = (page_language == 'EN')?'Click to open':'clicquez ici';
			$(this).wrapInner('<a href="#" title="'+view_txt+'..."></a>');
			
			//interaction
			$(this)
			.attr('id', 'ia-'+iaNavIndex+'-'+index)
			.hover(
				function(){ $(this).addClass('hover'); $(this).siblings().removeClass('hover') },
				function(){ $(this).removeClass('hover'); }
			)
			.click(function(){
				if(!$(this).hasClass('open')){
					
					var view_txt = (page_language == 'EN')?'Click to close':'fermer';
					
					$(this).find('a > span.desc').text(view_txt+' [x]');
					$(this).addClass('open').next('dd').slideDown(700, 'easeOutCubic');
				} else {
					$(this).find('a > span.desc').text($(this).find('a').attr('title'));
					$(this).removeClass('open').next('dd').slideUp(700, 'easeOutCubic');
				}
				
				$(this).trigger('blur');
				
				return false;
			})
			.find('a')
			.addClass('clearfix').wrapInner('<span class="title"></span>');
			
			//add description if available
			if($.trim($(this).find('a').attr('title')).length < 1){
				$(this).find('span.title').css({ width:'100%' });
			} else {
				$(this).find('a').append('<span class="desc">'+$(this).find('a').attr('title')+'</span>');
			}
		});
		
	});
	
	if(window.location.hash){
		var sections = window.location.hash.substr(1).split(",");
		for(x=0; x<sections.length; x++){
			var section = '#'+sections[x];
			if($(section).length){
				$(section).prev().find('> a').trigger('click');
			}
		}
	}
}

function announceOpeningDays(){
	var lang = ($('body > div.english').length == 1)?"EN":"FR";
	var c = (lang=="EN")?"Close":"fermer";
	$('#announcementContentWrap').append('<p><a href="#">'+c+'</a></p>');
	$('#announcementContentWrap p:last a').bind('click', function(){ tb_remove(); return false; });
	$('#openingDayAnnouncement p a.thickbox').trigger('click');
}

function setupTrailMap(){
	if($('#trail-map').length){
		$('.overlay').css({ top:'10px', opacity:0 }).show();
		$('#easy-trails').animate({ top:'0px', opacity:1 }, 400, function(){
			$('#difficult-trails').animate({ top:'0px', opacity:1 }, 400, function(){
				$('#very-difficult-trails').animate({ top:'0px', opacity:1 }, 400, function(){
					$('#expert-trails').animate({ top:'0px', opacity:1 }, 400, function(){
						$('#trail-lifts').animate({ top:'0px', opacity:1 }, 400, function(){
							$('#trail-services').animate({ top:'0px', opacity:1 }, 400);
						});
					});
				});
			});
		});
		
		$('#trail-map-controls input').bind('change', function(){
			switch($(this).attr('id')){
				case "toggleEasyTrails": 		$overlay = $('#easy-trails'); break;
				case "toggleDifficultTrails": 	$overlay = $('#difficult-trails'); break;
				case "toggleVeryDifficultTrails": 	$overlay = $('#very-difficult-trails'); break;
				case "toggleExpertTrails": 		$overlay = $('#expert-trails'); break;
			}
			if(!$(this).is(':checked')){
				$overlay.animate({ top:'10px', opacity:0 }, 400);
			} else {
				$overlay.animate({ top:'0px', opacity:1 }, 400);
			}
		});
	}
}

/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});
