BrowserDetect.init();

// Mac styles
if (BrowserDetect.OS == "Mac" && BrowserDetect.browser == "Firefox" && BrowserDetect.version == "2" || BrowserDetect.browser == "Safari") {
	document.write("<link type='text/css' rel='stylesheet' href='css/ff2-safari-mac.css' />");
}
if (BrowserDetect.OS == "Mac" && BrowserDetect.browser == "Firefox" && BrowserDetect.version >= "3") {	
	document.write("<link type='text/css' rel='stylesheet' href='css/ff3-mac.css' />");
}

$(document).ready(function(){	
						   
	/************************************************************** 
	* Init 
	**************************************************************/
	$('ul#navigation').superfish();
	
	$("#sub-nav ul").treeview({
		animated: "normal",
		collapsed: true,
		unique: true,
		persist: "location"	// opens active section by default
	});

	// highlight table rows, set product table width
	$('.product table tr:even, .design-solutions table tr:even').addClass('even');
	$('.product table tr td:even').css('width','176px');
	
	// remove padding from last link in downloads
	//$('ul#downloads li:last').css('padding-bottom','0px');
	
	
	/************************************************************** 
	* Navigation 
	**************************************************************/
	
	// page url taken from the last '/ '
	var path = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
	
	if (path == ""){		
		// highlight home button
		$('ul#navigation li a[href$="index.html"]').addClass('active');	
	} else {	
		// adds active class to current page link
		var currLink = $('ul#navigation li a[href$="' + path + '"]');
		$(currLink).addClass('active');
	}	
	
	// if a dropdown link is the currLink, add active class to parent header
	if ($('ul#navigation li ul li a').hasClass('active')){
		var dropdown = $('ul#navigation li ul li a.active');
		var parent = $(dropdown).parents('li').children('a.header');		
		
		$(currLink).removeClass('active');
		$(parent).addClass('active');				
	} 
	
	// when you rollout of a dropdown menu, the header is set to inactive
	// set header rollover so the css is applied each time
	$('ul#navigation li a.header').each(function(){			
		$(this).mouseover(function(){
			$(this).css('border-top','5px solid #ed5748');
			$(this).css('color','#333333');
		}).mouseout(function(){
			$(this).css('border-top','5px solid #ffffff');
			$(this).css('color','#666666');
		});
	});
	
	// when rolling over a dropdown menu, header maintains red border
	$('ul#navigation li ul li').each(function(){
		$(this).mouseover(function(){
			// add red border to parent
			var header = $(this).parents('li').children('a.header');
			$(header).css('border-top','5px solid #ed5748');
			$(header).css('color','#333333');
		}).mouseout(function(){
			// add white border to parent
			var header = $(this).parents('li').children('a.header');	
			$(header).css('border-top','5px solid #ffffff');
			
			if ($(header).hasClass('active')) {
				$(header).css('color','#333333');	
			}else{
				$(header).css('color','#666666');
			}
		});
	});
	
	
	/************************************************************** 
	* Sub-Navigation 
	**************************************************************/
	
	// if currLink is a sub-nav dropdown menu item, add selected class to parent sub-header
	if ($('#sub-nav ul li ul li a').hasClass('selected')){
		var currSection = $('#sub-nav ul li ul li a.selected').parents('li').children('h4.sub-header');
		$(currSection).addClass('selected');
	}
	
	// slide down sub-nav sections onclick of breadcrumbs
	$('a.comm-breadcrumb').click(function(){
		$('#comm-drop-down').slideDown("normal");
		$('#res-drop-down').slideUp("normal");
	});	
	$('a.res-breadcrumb').click(function(){
		$('#res-drop-down').slideDown("normal");
		$('#comm-drop-down').slideUp("normal");
	});
	
	// highlight comm or res section
	if ($('ul#comm-drop-down li ul li a').hasClass('selected')){
		$('h4#commercial').addClass('selected');
	}
	if ($('ul#res-drop-down li ul li a').hasClass('selected')){
		$('h4#residential').addClass('selected');
	}
	
	// manually dropdown nav when a link is inside a header
	$('.heatflow-fundamentals #sub-nav ul li.expandable ul.treeview').css('display','block');
	$('.heatflow-fundamentals #sub-nav ul li h4 a').addClass('selected');
	$('.eco #sub-nav ul li.expandable ul.treeview').css('display','block');
	// highlighting
	$('.eco #sub-nav ul li h4.section a').addClass('selected');
	$('.gallery #sub-nav ul li h4.section').eq(1).addClass('selected');
	// highlight in-use dropdown nav (different styling from building systems)
	$('.installation #sub-nav ul li h4.section').eq(0).addClass('selected');
	
	
	/************************************************************** 
	* Gallery 
	**************************************************************/
	if ($('body').hasClass('gallery')){
		
		$('#thumbnails').galleriffic({
			delay: 				2500,
			numThumbs:          8,
			preloadAhead:       -1,	// preload all images
			enableTopPager:     false,
			enableBottomPager:	true,
			maxPagesToShow:     6,
			onPageTransitionOut: function(callback) {
				this.fadeTo('fast', 1, callback);
			},
			onPageTransitionIn: function() {
				this.fadeTo('fast', 1);
				$('#thumbnails ul li.selected').fadeTo('fast', 1);
				// get the selected image link
				var selectedImage = $('#thumbnails ul li.selected').children('a').attr('href');
				// remove # from link and set link ad current image text
				$('#current_image').text(selectedImage.substring(1));
				
			}
		});
		
		// set "image x of x"
		var totalImages = $('#thumbnails ul li').length;
		$('#total_images').replaceWith(totalImages);
		
		// set opacity of thumbs
		$('#thumbnails ul li').each(function() {
			// click
			$(this).click(function(){
				$(this).fadeTo('fast', 1);
				$(this).siblings().fadeTo('fast', .5);
				
				// get index of list item relative to parent ul
				var selectedIndex = $('li','#thumbnails ul').index(this) + 1;	// so it doesn't start at 0
				$('#current_image').text(selectedIndex);
			
			});
			// rollover
			$(this).hoverIntent(rollOver, rollOut);
			function rollOver() {
				$(this).not('.selected').fadeTo('fast', 1);
			}		
			function rollOut() {
				$(this).not('.selected').fadeTo('fast', .5);
			}
		});
	}
	
	
		
	
	/************************************************************** 
	* Rollovers 
	**************************************************************/
	
	// change jpg on rollover - homepage thumbs
	$('ul#features li a img').hover(function(){
		$(this).css('cursor','pointer');
		var src = $(this).attr("src");
		$(this).attr('src', src.replace( /\.jpg$/, '_over.jpg'));	
	}, 
	function(){
		var src = $(this).attr("src");			
		$(this).attr('src', src.replace( /\_over.jpg$/, '.jpg'));
	});
	
	// change gif on rollover - homepage building systems images
	$('ul#building-systems-links li img').hover(function(){
		$(this).css('cursor','pointer');
		var src = $(this).attr("src");
		$(this).attr('src', src.replace( /\.gif$/, '_over.gif'));	
	}, 
	function(){
		var src = $(this).attr("src");			
		$(this).attr('src', src.replace( /\_over.gif$/, '.gif'));
	});
	
});