$(document).ready(function() {

    /**** Make the skip nav links transparent in IE 8 ****/
    if (/MSIE (8)/.test(navigator.userAgent)) {
        var placeHolder = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        $('div.navaid a:eq(1)').each(function() {
            var n1 = jQuery(this).text();
            jQuery(this).html('<span class="navaid">' + n1 + '</span>' + placeHolder);
            jQuery(this).css('text-decoration', 'none');
            jQuery(this).focus(function() {
                jQuery(this).html(n1);
            });
            jQuery(this).mouseover(function() {
                jQuery(this).html(n1);
            });
            jQuery(this).blur(function() {
                jQuery(this).html('<span class="navaid">' + n1 + '</span>' + placeHolder);
            });
            jQuery(this).mouseout(function() {
                jQuery(this).html('<span class="navaid">' + n1 + '</span>' + placeHolder);
            });
        });
        $('div.navaid a:eq(2)').each(function() {
            var n2 = jQuery(this).text();
            jQuery(this).html('<span class="navaid">' + n2 + '</span>' + placeHolder);
            jQuery(this).css('text-decoration', 'none');
            jQuery(this).focus(function() {
                jQuery(this).html(n2);
            });
            jQuery(this).mouseover(function() {
                jQuery(this).html(n2);
            });
            jQuery(this).blur(function() {
                jQuery(this).html('<span class="navaid">' + n2 + '</span>' + placeHolder);
            });
            jQuery(this).mouseout(function() {
                jQuery(this).html('<span class="navaid">' + n2 + '</span>' + placeHolder);
            });
        });
    }

    /**** Add 'Share this page' feature ****/
    if ($('div.bookmarkContainer').length > 0) {
        // English version
        if ($('html').attr('lang') == "en") {
            // Add container for top widget
            //$('div.center h1:first-child').before('<div class="bookmarkContainer bookmarkTop"><div class="bookmarkTitle">Share this page</div></div>');

            // Configure options
            $('div.bookmark').bookmark({
                favoriteText: 'Favourites',
                hint: 'Send to {s} (opens in a new window)',  // Popup hint for links, {s} is replaced by display name
                emailSubject: 'An interesting page on the Prime Minister of Canada\'s Web site',  // The subject for the e-mail 
                emailBody: 'I thought you might find this page interesting:\n{t} ({u})',
                sites: ['digg', 'facebook', 'friendfeed'],
                iconSize: 16, addEmail: true, addFavorite: false, compact: true, popup: false, icons: '/images/bookmarks.gif'
            });

            // Add Youtube option
            //$('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            //$('div.bookmark li:last-child span').each(function() {
            //    jQuery(this).parent().attr('href', 'http://www.youtube.com/user/pmocpm'); // + ' - ' + escape(window.location.href));
            //    jQuery(this).attr('title', 'Send to Youtube (opens in a new window)');
            //    jQuery(this).css('background', 'transparent url(/grfx/icons/YouTube16x16.png) no-repeat');
            //});

            // Add Twitter option
            $('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            $('div.bookmark li:last-child span').each(function() {
                jQuery(this).parent().attr('href', 'http://twitter.com/home?status=' + jQuery("title").html() + ' - ' + escape(window.location.href));
                jQuery(this).attr('title', 'Send to Twitter (opens in a new window)');
                jQuery(this).css('background', 'transparent url(/images/twitter16x16.gif) no-repeat');
            });

          

            // Add Podcast option
            //$('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            //$('div.bookmark li:last-child span').each(function() {
            //	jQuery(this).parent().attr('href', '/Podcast');
            //	jQuery(this).parent().attr('target', '');
            //	jQuery(this).attr('title','Podcast');
            //	jQuery(this).css('background','transparent url(/grfx/icons/icon_podcast_16x16.jpg) no-repeat');
            //});

            // Add Print option
            $('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            $('div.bookmark li:last-child span').each(function() {
                jQuery(this).parent().attr('href', 'Javascript:window.print();');
                jQuery(this).parent().attr('target', '');
                jQuery(this).attr('title', 'Send to: Printer');
                jQuery(this).css('background', 'transparent url(/grfx/icons/icon_printer.jpg) no-repeat');
            });
        }
        // French version
        else {
            // Add container for top widget
            //$('div.center h1:first-child').before('<div class="bookmarkContainer bookmarkTop"><div class="bookmarkTitle">Partagez cette page</div></div>');

            // Configure options
            $('div.bookmark').bookmark({
                favoriteText: 'Favoris',
                hint: 'Envoyer à : {s} (ouvre dans une nouvelle fenêtre)',  // Popup hint for links, {s} is replaced by display name
                emailSubject: 'Une page du site Web du Premier ministre du Canada', //'Une page intéressante du site Web du Premier ministre du Canada.',  // The subject for the e-mail
                emailBody: 'Je veux partager cela avec vous :\n ({u})', //'J\'ai pensé que cette page vous intéresserait :\n{t} ({u})',
                sites: ['digg', 'facebook', 'friendfeed'],
                iconSize: 16, addEmail: true, addFavorite: false, compact: true, popup: false, icons: '/images/bookmarks.gif'
            });
            
             // Add Youtube option
            $('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            $('div.bookmark li:last-child span').each(function() {
            jQuery(this).parent().attr('href', 'http://www.youtube.com/user/pmocpm'); // + ' - ' + escape(window.location.href));
                jQuery(this).attr('title', 'Envoyer à : Youtube (ouvre dans une nouvelle fenêtre)');
                jQuery(this).css('background', 'transparent url(/images/YouTube16x16.png) no-repeat');
            });

            // Add Twitter option
            $('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            $('div.bookmark li:last-child span').each(function() {
                jQuery(this).parent().attr('href', 'http://twitter.com/home?status=' + encodeURI(jQuery("title").html()) + ' - ' + escape(window.location.href));
                jQuery(this).attr('title', 'Envoyer à : Twitter (ouvre dans une nouvelle fenêtre)');
                jQuery(this).css('background', 'transparent url(/images/twitter16x16.gif) no-repeat');
            });

            // Add Podcast option
            //$('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            //$('div.bookmark li:last-child span').each(function() {
            //	jQuery(this).parent().attr('href', '/Balados');
            //	jQuery(this).parent().attr('target', '');
            //	jQuery(this).attr('title','Balados');
            //	jQuery(this).css('background','transparent url(/grfx/icons/icon_podcast_16x16.jpg) no-repeat');
            //});

            // Add Print option
            $('div.bookmark ul').append($('div.bookmark li:eq(1)').clone());
            $('div.bookmark li:last-child span').each(function() {
                jQuery(this).parent().attr('href', 'Javascript:window.print();');
                jQuery(this).parent().attr('target', '');
                jQuery(this).attr('title', 'Envoyer à : Imprimer');
                jQuery(this).css('background', 'transparent url(/grfx/icons/icon_printer.jpg) no-repeat');
            });
        }

        // Create duplicate at the top of the page
        //$('div.bookmarkTop').append($('div.bookmark').clone());

        // Enable keyboard focus tooltips for each option
        $('div.bookmark a').each(function() {
            jQuery(this).attr('title', jQuery(this).children('span').attr('title'));
        });
        //if (!/MSIE (8)/.test(navigator.userAgent) || !/Windows NT 5.1/.test(navigator.userAgent))
        //    $.readyOrDone( Tltps.execute );

        // Make the share widget visible
        $('div.bookmarkContainer').css('display', 'block');
    }

    /**** Round menu bar and banner corners (except for IE, Chrome, and Opera) ****/
    //if (!(/MSIE (5\.5|6|7|8)/.test(navigator.userAgent) || /Chrome/.test(navigator.userAgent) || /Opera/.test(navigator.userAgent))) {
    /****** Disable non-script solution ******/
    //	if ($('html').attr('lang') == "en") {
    //		$('div.banner-eng').each(function() {
    //			jQuery(this).css('background', 'url(/images/clouds-nuages.jpg) #004F82 no-repeat center center');
    //			jQuery(this).css('border-bottom', '1px solid #FFF');
    //			jQuery(this).css('border-right', '2px solid #4894b9');
    //			jQuery(this).css('border-left', '2px solid #4894b9');
    //			jQuery(this).css('width', '977px');
    //		});
    //	} else {
    //		$('div.banner-fra').each(function() {
    //			jQuery(this).css('background', 'url(/images/clouds-nuages.jpg) #004F82 no-repeat center center');
    //			jQuery(this).css('border-bottom', '1px solid #FFF');
    //			jQuery(this).css('border-right', '2px solid #4894b9');
    //			jQuery(this).css('border-left', '2px solid #4894b9');
    //			jQuery(this).css('width', '977px');
    //		});
    //	}
    //	$('div.fp').each(function() {
    //		jQuery(this).css('background','none');
    //		jQuery(this).css('background-color','#79ad36');
    //		jQuery(this).css('border-bottom','2px solid #4894b9');
    //		jQuery(this).css('border-right','2px solid #4894b9');
    //		jQuery(this).css('border-left','1px solid #4894b9');
    //		jQuery(this).css('width', '977px');
    //	});
    //	$('ul.commonbar').each(function() {
    //		jQuery(this).css('background','none');
    //		jQuery(this).css('background-color','#79ad36');
    //		jQuery(this).children('li').each(function() {
    //			jQuery(this).css('background-color','#79ad36');
    //		});
    //	});

    // Round common menu bar corners
    //	$('div.fp').corners("20px bottom-left bottom-right transparent");
    //	$('.roundedbottomleft').corners("20px bottom-left");

    // Round banner corners
    //	if ($('html').attr('lang') == "en") {
    //		$('div.banner-eng').corners("20px top-left top-right transparent");
    //	} else {
    //		$('div.banner-fra').corners("20px top-left top-right transparent");
    //	}
    //}
    $(document).bind('click', function(e) {
        var host = window.location.host.toLowerCase().replace("www.", "");
        var nodeNm = '';
        var hrefTtl = '';
        var linkURL;
        var compareLinkURL;
        var classNms = '';

        $parentEvnt = e.target.parentNode;
        if (!$parentEvnt) { return; }
        if ($parentEvnt.nodeName == 'A' && e.target.nodeName == 'IMG') {
            nodeNm = 'A';
            linkURL = $parentEvnt.href;
            hrefTtl = $parentEvnt.href;
            classNms = $parentEvnt.className;
        }
        else {
            nodeNm = e.target.nodeName;
            linkURL = e.target.href;
            hrefTtl = e.target.text;
            classNms = e.target.className;
        }
        $blnNewWindowFlg = false;

        if (classNms.indexOf("openWindow") > -1)
            $blnNewWindowFlg = true;
        if (nodeNm == 'A') {
            compareLinkURL = linkURL.replace("www.", "");
            compareLinkURL = compareLinkURL.replace("WWW.", "");
            if (!compareLinkURL.match('//' + host) && (compareLinkURL.match('http:') || compareLinkURL.match('https:'))) {
                var txt = '<div id="leavingSite">';

                $cancelRedirect = false;

                if ($('html').attr('lang') == "en") {
                    txt += '<h2>You are leaving the Prime Minister of Canada\'s web site.</h2>';
                    txt += '<div id="newSite">';
                    txt += '    <p>You will now access:<br /><br />';
                    txt += '    ' + linkURL + '</p>';
                    txt += '</div>';
                } else {
                    txt += '<h2>Vous sortez du site Web du Premier ministre du Canada.</h2>';
                    txt += '<div id="newSite">';
                    txt += '    <p>Vous accédez maintenant à :<br /><br />';
                    txt += '    ' + linkURL + '</p>';
                    txt += '</div>';
                }
                txt += '</div>';
                $.fn.colorbox({
                    width: "50%",
                    open: true,
                    html: txt,
                    onClosed: function() { $cancelRedirect = true; }
                });
                setTimeout("redirect('" + linkURL + "', $blnNewWindowFlg)", 3000);
                e.preventDefault();
            }
            else {
                return true;
            }
        }
    });
});

function redirect(pStrURL, pBlnNewWindowFlg) {
    if (pStrURL != '' && !$cancelRedirect) {
        if (pBlnNewWindowFlg == true) {
            var objWindow = window.open(pStrURL, 'window', '');
            
            //  Safari bug.  window may not open with a URL directly in the window.open call.
	        if(!objWindow)
            {
                objWindow = window.open('', 'window', '');
                if (!objWindow)
                    window.location = pStrURL;
                else
                    objWindow.location.href = pStrURL;
            }
            $.fn.colorbox.close();
        } else {
            window.location = pStrURL;
        }
    }
    $blnNewWindowFlg = false;
    $cancelRedirect = false;
    return false;
}
