﻿$(document).ready(function() {
    //When in the CMS not all javascript should run
    if (location.href.indexOf("cmspagemode=edit") > -1)
    {
        H1TagDisplay();
        
        return;
    }
    
    ExternalLinks();
    
    ApplyThickBox();
    
    IE6FlickerFix();
});

// XHTML valid way of adding target="_blank" to anchor links
function ExternalLinks() {
    $("a[rel]").each(function() {
        if (this.getAttribute("rel").indexOf("external") > -1) {
            this.target = "_blank";
        }
    });
}

// The H1 tag should not display, except in the CMS where it can be edited
function H1TagDisplay() {
    $('h1').each(function() {
        this.css({
            "line-height" : "20px",
            "font-size" : "20px"
        });
    });    
}

function ApplyThickBox() {
    // Check that the window is big enough to show the map in a thickbox
    var width = $(window).width();  
    var height = $(window).height();  
    
    if (width > 1010 && height > 572)
    {
        // Loop through the links from the map feature box
        $('.uklocationsmappanel .maplink').each(function() {
            // Add the hook that Thickbox script looks for
            $(this).addClass("thickbox");
            
            // Update the href to point to the iframe version rather than the full page version
            if ($.browser.className == 'msie6' || $.browser.className == 'msie7') {
                this.href = "/map.aspx?keepThis=true&TB_iframe=true&height=535&width=945";
            }
            else {
                this.href = "/map.aspx?keepThis=true&TB_iframe=true&height=525&width=935";
            }
        });
    }
}

//Prevents IE6 from flickering when links have images as backgrounds
function IE6FlickerFix() {
    try { 
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch(err) {}
}
