/* bonk_ncs - Edited June, 2009 to add a title change when a menu opens/closes  - for accessibility */

function showHide(element_id) {
	if (document.getElementById && document.getElementById(element_id) && document.getElementById(element_id).style) {
		var menu  = document.getElementById(element_id);
		var arrow = document.getElementById(element_id + '_arrow');
		var title = document.getElementById(element_id + '_arrow');

		if (menu.style.display == "block") {
			menu.style.display = "none";

			if (arrow.src) { arrow.src = arrow.src.replace("080709","080769");}
			if (title.title) {
				title.title = title.title.replace(/Close/g,"Open");
				title.title = title.title.replace(/Fermer/g,"Ouvrez");
			}
		}
		else {
			menu.style.display = "block";
			if (arrow.src) { arrow.src = arrow.src.replace("080769","080709");}
			if (title.title) {
				title.title = title.title.replace(/Open/g,"Close");
				title.title = title.title.replace(/Ouvrez/g,"Fermer");
			}
		}
	}
}

/* Dennis :: moved menu closing from CSS file :: accessibility */
/*
#contacts { display: none; }
#topicmenu { display: none; }
#urgentbox { display: none; }
*/

/* if exists #InThisSection or #DansCetteSection or #INTHISSECTION, etc */
if( $('ul[id*="SECTION"]').length != 0 || $('ul[id*="ection"]').length != 0 ) {
	showHide('WhatWeDo'); // English
	showHide('Contacts'); // English
	showHide('urgentbox'); // Same in English and French
	showHide('Cequenousfaisons'); // French What We Do - Bruce, 03/18/11 */
	showHide('Pournousjoindre'); // French Contacts - Bruce, 03/18/11 */
}

/* Dennis :: track 404 Page Not Found */

var _gaq=_gaq||[];
if( document.title.indexOf( "Page Not Found" ) != -1 ) {
	_gaq.push( ['_m2._trackEvent', 'error_404', document.location.pathname + document.location.search, document.referrer] );
}

/* Dennis :: populate Explore Government menu */
/* setup ExploreGov variables */
var _exploreGovtContainerId = "exploreGovt";
var _exploreGovtULId = "explore";
var _lang = $("html").attr("lang");

(function() {
	/* include ExploreGov script */
	var eg = document.createElement('script'); eg.type = 'text/javascript';
	var _p = ("http:" == document.location.protocol ? "http" : "https");
	eg.src = _p + '://www.plugins.gov.on.ca/exploregovernment/scripts/exploregovernment.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(eg, s);
})();

function explore_click_handler() {

	/* test for explore list */
	if( $('#explore').length == 0 ) {

		/* not found, wait longer */
		window.setTimeout( explore_click_handler, 150 );

	} else {

		/* add click handler to menu using jQuery */
		$("a[name=exploremenulink]").click(function(){showHide('explore');return false;});

		/* remove href from menu header using jQuery */
		$("a[name=exploremenulink]").attr( "href", "javascript:void(0);" );

	}
}
window.setTimeout( explore_click_handler, 150 );

/* 4Q */

if("http:" == document.location.protocol) {
	var p=document.location.pathname;
	if( p.indexOf('Business/LetsFish') >= 0 || p.indexOf('fish_online/') >= 0) {
		document.write(unescape("%3Cscript src='http://4qinvite.4q.iperceptions.com/1.aspx%3Fsdfc%3D208b5005-14813-79394780-ae1b-4cdb-b5f9-c754730a9085%26lID%3D1%26loc%3D4Q-WEB2' type='text/javascript'%3E%3C/script%3E"));
	}
}


/* Track Google Analytics on Search Ontario templates :: Dennis :: June 28, 2011 */

function getHiddenValue(name) {
	var elems = document.getElementsByName(name);
	for(var i=0; i<elems.length; i++) {
		if( elems[i].type=='hidden' ) {
			return name + '=' + encodeURIComponent(elems[i].value).replace(/%20/g,'+');
		}
	}
}

/* get search keywords from Search Ontario script text :: Dennis :: Jan 12, 2009 */
function getUpdateQueryValue() {
	var elems = document.getElementsByTagName('script');
	for(var i=elems.length-1; i>=0; i--) {
		var str=elems[i].text;
		if( str.indexOf('updateQueryValue') >= 0 ) {
			str=str.replace(/^.*updateQueryValue\('/,'');
			str=str.replace(/\'\).*$/,'');
		return 'query=' + encodeURIComponent(str).replace(/%20/g,'+');
		}
	}
}

/* add .ready() :: Dennis :: Sep 8, 2011 */
$(document).ready(
	function(){

		hostname = document.location.hostname.toLowerCase();

		/* Search Ontario :: add query parameter to GA path :: Dennis */
		if( hostname.indexOf( "search.gov.on.ca" ) != -1 ) {

			params = [];
			var langval = getHiddenValue('language');
			if(langval){params.push(langval);}
			/* remove searchType parameter :: Dennis :: Jan 12, 2009 */
			/* get query from script text :: Dennis :: Jan 12, 2009 */
			query = getUpdateQueryValue();
			if(query){
				/* check for 'no search results'  :: Dennis :: Jan 5, 2010 */
				var content = document.body;
				if( content.innerHTML.search('No documents found that match your query :')>-1 || content.innerHTML.search('n&#146;a donn&#233; aucun r&#233;sultat.')>-1 ) {
					params.push('cat=no-results');
					query = query.replace('query=','query=no-results:');
					document.title = 'Search Term Not Found - ' + document.title;
				}
				params.push(query);
			}

			/* switch href to pathname :: Dennis :: Jan 12, 2009 */
			searchpath = window.location.pathname;
			if( searchpath.indexOf('?') == searchpath.length-1 ) {
				searchpath += params.join( '&' );
			} else if( searchpath.indexOf('?') >= 0 ) {
				searchpath += '&' + params.join( '&' );
			} else {
				searchpath += '?' + params.join( '&' );
			}

			/* new style Google Analytics format :: Dennis :: June 28, 2011 */
			_gaq.push( ['_m2._trackPageview', searchpath], ['_m1._trackPageview', searchpath] );
		}

		/* Dennis :: Cab.Office Google Analytics Code :: Mar 23, 2011 */
		if( hostname.indexOf( "stage." ) == -1 ) { 		/* skip stage server :: Dennis :: Oct 12, 2011 */
			try{
				(function(){
					var _p = ("http:" == document.location.protocol ? "http" : "https");
					var _h = document.getElementsByTagName("head")[0];
					var _s = document.createElement("script");
					_s.src = _p + "://www.webservices.gov.on.ca/metrics.php?h="
					+ escape('www.mnr.gov.on.ca');
					_h.appendChild(_s);
				})();
			} catch(e){}
		}

		/* Dennis :: Cab.Office External/Download Tracking Code :: Oct 20, 2011 */
		/* if( hostname.indexOf( "stage." ) == -1 ) {
			try{
				(function(){
					var _p = ("http:" == document.location.protocol ? "http" : "https");
					var _h = document.getElementsByTagName("head")[0];
					var _s = document.createElement("script");
					_s.src = _p + "://s3.amazonaws.com/ontarioassets/jquery.custom_analytics.js";
					_h.appendChild(_s);
				})();
			} catch(e){}
		}
		*/

}); /* END $(document).ready */


/* Download tracking for Google Analytics :: added by Dennis C :: Nov, 2008 */
/* Outbound links and links to non-html files will be automatically tracked. This script has been provided by Goodwebpractices.com Thanks to ShoreTel, MerryMan and Colm McBarron */

//	This javascript tags file downloads and external links in Google Analytics.
//	You need to be using the Google Analytics New Tracking Code (ga.js)
//	for this script to work.
//	To use, place this file on all pages just above the Google Analytics tracking code.
//	All outbound links and links to non-html files should now be automatically tracked.
//
//	This script has been provided by Goodwebpractices.com
//	Thanks to ShoreTel, MerryMan and Colm McBarron
//
//	www.goodwebpractices.com
//	VKI has made changes as indicated below.

if (document.getElementsByTagName) {
        // Initialize external link handlers
        var hrefs = document.getElementsByTagName("a");
        for (var l = 0; l < hrefs.length; l++) {
				// try {} catch{} block added by erikvold VKI
			try{
	                //protocol, host, hostname, port, pathname, search, hash
	                if (hrefs[l].protocol == "mailto:") {
	                        startListening(hrefs[l],"click",trackMailto);
	                } else if (hrefs[l].hostname == location.host) { // same domain
	                        var path = hrefs[l].pathname + hrefs[l].search;
							var isDoc = path.match(/\.(?:doc|eps|jpg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3)($|\&|\?)/);
	                        if (isDoc) {
	                                startListening(hrefs[l],"click",trackExternalLinks);
	                        }
	                } else {
	                        startListening(hrefs[l],"click",trackExternalLinks);
	                }
			}
			catch(e){
					continue;
			}
        }
}

function startListening (obj,evnt,func) {
        if (obj.addEventListener) {
                obj.addEventListener(evnt,func,false);
        } else if (obj.attachEvent) {
                obj.attachEvent("on" + evnt,func);
        }
}

function trackMailto (evnt) {
        var href = (evnt.srcElement) ? evnt.srcElement.href : this.href;
        var mailto = "/mailto/" + href.substring(7);
        /* adjust Google Analytics call method :: Dennis :: June 29, 2011 */
        _gaq.push( ['_m2._trackPageview', mailto], ['_m1._trackPageview', mailto] );
}

function trackExternalLinks (evnt) {
        var e = (evnt.srcElement) ? evnt.srcElement : this;
        while (e.tagName != "A") {
                e = e.parentNode;
        }
        var lnk = (e.pathname.charAt(0) == "/") ? e.pathname : "/" + e.pathname;
        if (e.search && e.pathname.indexOf(e.search) == -1) lnk += e.search;
        if (e.hostname != location.host) {	// probably external link (might be cross-domain)
			//lnk = "/external/" + e.hostname + lnk;
			lnk = e.hostname + lnk;

			// TODO: add whitelist (don't trigger event if destination page is GA tracked by us)

			/* use trackEvent for external links:: Dennis :: Oct 21, 2011 */
			_gaq.push( ['_m2._trackEvent', 'external', lnk] );
		}
		else {
			/* adjust Google Analytics call method :: Dennis :: June 29, 2011 */
			_gaq.push( ['_m2._trackPageview', lnk], ['_m1._trackPageview', lnk] );
		}
}

