// onchange event handler for dropdown list navigation
function OnListChange() { document.location.href = this.options[this.selectedIndex].value; }

// Format advert
if (typeof (jQuery) != 'undefined') {
    $(function () { if (document.getElementById('tAd') && document.getElementById('tAd').getElementsByTagName('img').length > 0) { document.getElementById('tAd').className = 'tAd'; document.getElementById('tAd').innerHTML += '<div class="policy"><a href="http://www.eastsussex.gov.uk/business/doingbusiness/advertising/">Advertising on this website</a></div>'; } });

     // not on public library computer
    if (navigator.userAgent.indexOf("ESCC Libraries") == -1)
    {
        // Work with download links
        $(function ()
        {
            // new window for these file extensions
            var popupExts = new Array(".pdf", ".rtf", ".doc", ".docx", ".dot", ".dotx", ".xls", ".xlsx", ".xlt", ".xltx", ".csv", ".ppt", ".pptx", ".pps", ".ppsx", ".pot", ".potx");

            // Google Analytics for these file extensions (should include everything in popupExts)
            var trackExts = new Array(".pdf", ".rtf", ".doc", ".docx", ".dot", ".dotx", ".xls", ".xlsx", ".xlt", ".xltx", ".csv", ".mp3", ".wmv", ".mov", ".xml", ".ashx", ".rss", ".ical", ".hcard", ".ppt", ".pptx", ".pps", ".ppsx", ".pot", ".potx");

            // IE <=6 uses plugins for all the formats; others only for PDF
            var iePlugins = ($.browser.msie && parseInt($.browser.version, 10) <= 6);

            // IE <=7 puts the icon at the right edge of the block box, not the inline box, which is wrong when a link wraps
            var ieWrapBug = ($.browser.msie && parseInt($.browser.version, 10) <= 7);

            // Loop through all links on the page
            var links = document.getElementsByTagName("a");
            if (!links) return;

            var href, leni, lenj, link;
            leni = links.length;
            for (var i = 0; i < leni; i++)
            {
                href = links[i].getAttribute('href').toLowerCase();
                if (!href) continue;

                // Loop through all file extensions to see if this link matches
                lenj = trackExts.length;
                for (var j = 0; j < lenj; j++)
                {
                    if (href.substring(href.length - trackExts[j].length, href.length) == trackExts[j])
                    {
                        link = $(links[i]);

                        // If the download should open in a new window...
                        if ($.inArray(trackExts[j], popupExts))
                        {
                            //$(links[i]).addClass('window').append('<span class="aural"> (opens new window)</span>');
                            if (!ieWrapBug)
                            {
                                // Add a span *around* the link so that the new window icon isn't underlined
                                link.wrap('<span class="new-window">');
                            }
                            else
                            {
                                // For IE 6 and 7 add the span around the last word so that the icon wraps with that word
                                var trimmed = $.trim(link.html());
                                var lastWord = trimmed.split(/[ -]/);
                                lastWord = lastWord[lastWord.length - 1];
                                link.html(trimmed.substr(0, trimmed.length - lastWord.length) + '<span class="new-window">' + trimmed.substr(trimmed.length - lastWord.length) + '</span>');
                            }

                            // ...and add an audible warning of the change of context
                            link.append('<span class="aural"> (opens new window)</span>');

                            // ...and make it open a new window if it wouldn't already do that
                            if (iePlugins || trackExts[j] == ".pdf")
                            {
                                link.click(function (e) { e.preventDefault(); window.open(this.getAttribute('href'), 'doc', 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=no,resizable=yes'); });
                            }
                        }

                        // Track download in Google Analytics
                        link.click(function ()
                        {
                            if (typeof (_gaq) != 'undefined') 
                            {
                                // Get file extension
                                var href = this.getAttribute('href').toLowerCase();
                                var pos = href.lastIndexOf(".");
                                if (pos == -1) return;
                                
                                var action = href.substr(pos);
                                if (action == ".docx" || action == ".dot" || action == ".dotx") action = ".doc";
                                if (action == ".xlsx" || action == ".xlt" || action == ".xltx") action = ".xls";
                                if (action == ".pptx" || action == ".pps" || action == ".ppsx" || action == ".pot" || action == ".potx") action = ".ppt";
                                _gaq.push(['_trackEvent', 'download', action.substring(1).toUpperCase(), href]); 
                            }
                        });
                    }
                }
            }
        });

        // Open Twitter/Facebook links in new window
        $(function () { $(".pageTweet a, .pageFB a").click(function (e) { e.preventDefault(); var share = window.open(this.getAttribute('href'), 'doc', 'toolbar=no,directories=no,menubar=no,resizable=yes,width=500,height=400,left=200,top=200'); }) });

        // Act on all external links 
        $("a[href^='http']").not('[href*="eastsussex.gov.uk"]').not('[href*="eastsussexcc.gov.uk"]').click(function ()
        {
            var link = $(this);
            var href = link.attr("href").toLowerCase();

            // Add rel="external"
            var rel = link.attr("rel");
            if (rel.length > 0) rel += " ";
            rel += "external";
            link.attr("rel", rel);

            // Track in Google Analytics
            if (typeof (_gaq) != 'undefined') _gaq.push(['_trackEvent', 'links', 'external', href, 0]);
        });
    }
}
