﻿function SetupLanguageSwitcher() {
	$("a.lang-switch").each(function() {
		var Current_URL = window.location.href;
		var Target_URL = $(this).attr('href');
		var Target_URL_Parts = new Array();
		
		if (Current_URL.indexOf('/fr/') >= 0) {
			Target_URL_Parts = Current_URL.split('/fr/');
		}
		else {
			Target_URL_Parts = Current_URL.split('/en/');
		}
		Target_URL += Target_URL_Parts[1];

		this.href = Target_URL;
	});
}

var Tab_Next_Timeout;
var Current_Tab;

function SetupHomepageTabs() {
	$('#callout3 h4 a').click( function() {
		var Target_ID = $(this).attr('rel');
		Tab_Click( Target_ID );
		
		return false;
	});
	
	Tab_Click( '01', true );
}

function Tab_Click( Target_ID, Is_Automatic ) {
	clearTimeout( Tab_Next_Timeout );
	Current_Tab = Target_ID;
	
	// hide all tabs
	$('#callout3 div.tab_content').hide();
	
	// show the right tab
	var Target_Div = '#cslide_' + Target_ID;
	$( Target_Div ).show();
	
	// deactivate all nav buttons
	$('#callout3 h4 a').removeClass('active');
	
	// activate the right nav button
	var Target_Button = '#href_' + Target_ID;
	$( Target_Button ).addClass('active');
	
	if( Is_Automatic == true ) {
		Tab_Next_Timeout = setTimeout( 'Tab_Next()', 8000 );
	}
}

function Tab_Next() {
	// not a nice way to do this, let's fix this up later...
	if( Current_Tab == '01' ) {
		Next_Tab = '02';
	} else if( Current_Tab == '02' ) {
		Next_Tab = '03';
	} else if( Current_Tab == '03' ) {
		Next_Tab = '04';
	} else if( Current_Tab == '04' ) {
		Next_Tab = '05';	
	} else if( Current_Tab == '05' ) {
		Next_Tab = '01';
	}
	Tab_Click( Next_Tab, true );
}

function SetupAlternateTableRows() {
	$('table tr:even').addClass('even');
	$('table tr:odd').addClass('odd');
}

function SetupIEButtons() {
	$('ul.buttons li').hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
}

function SetupLinks() {
	// external links
	$('a[href^="http://"]').addClass('external').attr('target', '_blank');
	$('a[href^="https://"]').addClass('external').attr('target', '_blank');
	
	$('ul.pdf li a').attr('target', '_blank');
}


function SetupFormPopups() {
	$('a.form-popup').click(function() {
		//	Init default vals
		var w = 625;
		var h = 500;
		
		//	Extract the URL from which we will attempt to pull the width and height
		var url = $(this).attr('href');

		//	Resolve the width
		var wRegex = /width=(\d+)/;
		var wArray = wRegex.exec(url);
		if (wArray.length >= 1)
		{
			w = wArray[1];
		}

		//	Resolve the height
		var hRegex = /height=(\d+)/;
		var hArray = hRegex.exec(url);
		if (hArray.length >= 1) {
			h = wArray[1];
		}
		
		//alert('width: ' + w + '\nheight: ' + h);

		//	Assemble the params
		var params = 'width=' + w + ', menubar=0, height=' + h + ', location=0, status=0, scrollbars=1, resizable=1,toolbar=0,location=0,directories=0';

		window.open($(this).attr('href'), 'BayerForm', params);

		return false;
	});
	$('a.cabinet-popup').click(function() {
		window.open($(this).attr('href'), 'BayerForm', 'width=780, menubar=0, height=540, location=0, status=0, scrollbars=1');
		return false;
	});
}

function SetupInPageNavigation() {
	if( $("*").index( $('#pagemenu')[0] ) > -1 ) {
		
		// internal anchors get back-to-top links
		var English_Top_Link = '<div class="top"><a href="#container"><span>Top of Page</span></a></div>';
		var French_Top_Link = '<div class="top"><a href="#container"><span>Haut de la page</span></a></div>';
		
		$('.lang-en #primary-content h2').before( English_Top_Link );		
		$('.lang-fr #primary-content h2').before( French_Top_Link );
		
		var Table_Of_Contents = '<ul>';
		
		$('#primary-content h2').each( function( i ) {
		  // add the H2 content to the ToC
		  Table_Of_Contents += '<li><a href="#a' + i + '">' + $(this).html() + '</a></li>';
		  
		  // add the ID
		 $(this).attr('id', 'a' + i);
		});
		
		Table_Of_Contents += '</ul>';
		
		// add the table of contents after the H1
		$('#primary-content div#pagemenu').html( Table_Of_Contents );
	}
}


function DEBUG_BLANK_LINKS() {
	$('a[href="#"]').addClass('DEBUG');
}

$(document).ready(function() {
	SetupLanguageSwitcher();
	SetupAlternateTableRows();
	SetupIEButtons();
	SetupLinks();
	SetupFormPopups();
	SetupInPageNavigation();

	DEBUG_BLANK_LINKS();
	
	$('div#container').pngFix();
	SetupHomepageTabs();
});



// Homepage show/hide div function
function hidediv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('readdiv').style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.readdiv.visibility = 'hidden';
}
else { // IE 4
document.all.readdiv.style.visibility = 'hidden';
}
}
}
function showdiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('readdiv').style.visibility = 'visible';
}
else {
if (document.layers) { // Netscape 4
document.readdiv.visibility = 'visible';
}
else { // IE 4
document.all.readdiv.style.visibility = 'visible';
}
}
}