/* Author: Michael Bradley

*/

$(document).ready(function() {
	
	$('a[href*="facebook"], a[href*="twitter"]').click(function(eve){
		eve.preventDefault();
		eve.stopPropagation();
		window.open(this.href, '_blank');
	});
	
		
	if ($('body').attr('class') == 'home') {
		
		function pagerFactory(idx, slide) {
			var s = idx > 2 ? ' style="display:none"' : '';
		    return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
		};
		
		function eventPrevNext(isNext, zeroBasedSlideIndex, slideElement) {
			var current_slide = zeroBasedSlideIndex + 1;
			$('ul#ss_nav li:nth-child('+current_slide+')').addClass('activeSlide');
		}
		
		$('#slideshow_holder').cycle({
			fx: 'fade',
			timeout: 0,
			prev:    'a#ss_prev',
			next:    'a#ss_next',
			pager:   '#ss_nav',
			pagerAnchorBuilder: pagerFactory,
			onPrevNextEvent: eventPrevNext
		});
	}
	
	/* Change Region Modal */
	
	$("a#change_region").fancybox({
		'titlePosition'		: 'inside',
		'overlayColor' 		: '#000',
		'overlayOpacity' 	:  0.5,
		'padding' : 0,
		'width'	: 600,
		'height' : 400
	});
	
	
	
	// set page height
	
	function position()
	{
		var main_height = $('#main').height();
		var timeline_height = $('#timeline').height();
		var masthead_height = $('#masthead').height();

		var total_height = main_height + timeline_height + masthead_height;
		var window_height = $(window).height();
		var total_combined_height = 195 + total_height + 110;

		if (total_combined_height < window_height) {
			var buffer = 20;
			var height_increase = window_height - total_combined_height - buffer;
			$('#main').css('height', main_height + height_increase);
		}
	}
	
	$(window).resize(function () {
		position();
	});
	
	position();
	
	
	// create duplicated nav labels for IE < 9 to create shadow effect...
	var browser_version = parseInt($.browser.version);
	
	if ($.browser.msie == true && browser_version < 9) {
		$('div#nav ul li a').each(function(){
			var item = $(this).html();
			$(this).html(item+"<span>"+item+"</span>");
		});	
	}
	
	$('ul#menu li').first().addClass('first');
	$('ul#nav-sub li').first().addClass('first');
	
	$('a#print_page').click(function(eve){
		eve.preventDefault();
		window.print();
		return false;
	});
	
	
	/* Region selection */
	
	
	$('#region_selector a').click(function(eve){
		eve.preventDefault();
		var id = $(this).attr('id');
		var href = $(this).attr('href');
		window.location = href;
		
	});
	
	if($('body').attr('class') == 'products') {
		
		
		
		
		$("body.products div.component a").fancybox({
			
			'onStart' : function() {
				$('div.component a').each(function(){
					$(this).attr('href', $(this).attr('href') + 'modal');
				});
			},
			'onClosed' : function() {
				$('div.component a').each(function(){
					var link = $(this).attr('href');
					$(this).attr('href', link.substr(0, link.length - 5));
				});
			},
			'onCancel' : function() {
				$('div.component a').each(function(){
					var link = $(this).attr('href');
					$(this).attr('href', link.substr(0, link.length - 5));
				});
			},
			
			'titlePosition'		: 'inside',
			'overlayColor' 		: '#000',
			'overlayOpacity' 	:  0.5,
			'padding' : 20,
			'titleShow' : false
		});
		
		
		
	}
	
	
	
});
