//highlight current link on left side menu
$(function() {
    var path = location.pathname;
    $("div .nav li a[href $='" + path + "']").parents("li").each(function() {
        $(this).addClass("navCurrent");
        $("div .navOn").removeClass("navOn").addClass("navOff");
    });
    $("div .nav li a[href$='.htm']").addClass("navSingle");
});

var imgLinkNewWindow = '<img class="link-image-right" alt="" src="/CMSWebsite/images/icon_new_window.gif" width="10" height="10" />';
var imgLinkPdf = '<img class="link-image" alt="" src="/CMSWebsite/images/pdficon_small14.gif" width="14" height="14" />'
var imgLinkDoc = '<img class="link-image" alt="" src="/CMSWebsite/images/DOC14.GIF" width="14" height="14" />'
var imgLinkXls = '<img class="link-image" alt="" src="/CMSWebsite/images/XLS14.GIF" width="14" height="14" />'
var imgLinkPpt = '<img class="link-image" alt="" src="/CMSWebsite/images/PPT14.GIF" width="14" height="14" />'
var imgLinkOth = '<img class="link-image" alt="" src="/CMSWebsite/images/Document14.gif" width="14" height="14" />'

jQuery(function() {

    // Make promo areas more clickable by making the image clickable and setting the hover state over the whole promo
    jQuery("div.Promo").hover(function() { this.className = this.className + " hover" }, function() { this.className = this.className.replace(/ hover/, "") });
    jQuery("div.Promo img").css("cursor", "pointer");
    jQuery("div.Promo img").click(function() { window.location = this.parentNode.getElementsByTagName("A")[0].href; });

    // Add blank query text to search textbox and process the enter key. This ensures the "keyword" text is only added if JS is on and it will be cleared when the user enters it
    jQuery("input.search").focus(function() { if (this.value == "Search our website...") { this.value = ""; this.className = "search"; } }).blur(function() { if (this.value == "") { this.value = "Search our website..."; this.className = "search unfocus"; } }).trigger("blur");
    jQuery(".miniform").keydown(function(e) { if (e.which == 13) { jQuery(this).find("input[type='submit']").click(); if (window.event) { event.returnValue = false; event.cancel = true; } if (e.preventDefault) { e.preventDefault(); } return false; } })
    jQuery(".miniform").keypress(function(e) { if (e.which == 13) { return false; } });

    // Configure the text size links. These will only display if JS is on and they will work
    jQuery("#textSizeControls").css("display", "inline");
    jQuery("#smallTextLink").click(function() { txtSize("100%"); return false; });
    jQuery("#largeTextLink").click(function() { txtSize("140%"); return false; });

    // Configure jump to keyboard commands to only change the page if the "enter" key is pressed
    jQuery("select.jump-to").keydown(function(e) {
        try {
            var key = e.which;
            jQuery(this).attr("rel", "disable");
            /*if (jQuery.browser.opera) {
            if (key == 40) { this.selectedIndex++; return false; }
            else if (key == 38) { this.selectedIndex--; return false; }
            }*/
            /*else*/if (key == 13 && this.selectedIndex != 0) { jQuery(this).attr("rel", "enable"); jQuery(this).change(); return false; }

            return true;
        }
        catch (ex) { }
    });

    // Configure jump to mouse select to change the page when a new item has been clicked
    jQuery("select.jump-to").change(function(e) { if (this.selectedIndex != 0 && jQuery(this).attr("rel") == "enable") document.location.href = this.options[this.selectedIndex].value; });
    jQuery("select.jump-to").mousedown(function(e) { jQuery(this).attr("rel", "enable"); });

    // run through every link on the page and make each one more accessible
    jQuery("a").each(function() {
    
    if(this.className != "addthis_button_compact"){
        makeLinkMoreAccessible(this);}
    }); 

/*
    // make call to action buttion rounded
    jQuery(".call-to-action").append("<span></span>");

    jQuery(".call-to-action a").each(function() {

        jQuery(this).appendTo(jQuery(this).next("span"));

    });
*/
   
    
    
});


// Function that given a link will make it more accessible by adding screen reader markup and icons for documents
function makeLinkMoreAccessible(link) {
    var isImageLink = (link.innerHTML.toUpperCase().indexOf("<IMG") > -1);

    if (!link.href)
        return;
    if (link.href.toLowerCase().indexOf("mailto:") > -1)
        return;
    if (link.href.toLowerCase().lastIndexOf(".pdf") == link.href.length - 4) {
        link.target = "_blank";
        link.innerHTML = (isImageLink ? "" : imgLinkPdf) + link.innerHTML + '<span class="screen-reader"> (PDF document - opens in new window)</span>';
        jQuery("#adobeLink").show();
    }
    else if (link.href.toLowerCase().lastIndexOf(".doc") == link.href.length - 4) {
        link.target = "_blank";
        link.innerHTML = (isImageLink ? "" : imgLinkDoc) + link.innerHTML + '<span class="screen-reader"> (Word document - opens in new window)</span>';
    }
    else if (link.href.toLowerCase().lastIndexOf(".xls") == link.href.length - 4) {
        link.target = "_blank";
        link.innerHTML = (isImageLink ? "" : imgLinkXls) + link.innerHTML + '<span class="screen-reader"> (Excel spreadsheet - opens in new window)</span>';
    }
    else if (link.href.toLowerCase().lastIndexOf(".ppt") == link.href.length - 4) {
        link.target = "_blank";
        link.innerHTML = (isImageLink ? "" : imgLinkPpt) + link.innerHTML + '<span class="screen-reader"> (Powerpoint presentation - opens in new window)</span>';
    }
    else if (link.href.toLowerCase().lastIndexOf(".htm") != link.href.length - 4 && link.href.toLowerCase().indexOf("/nr/rdonlyres/") > 0) {
        link.target = "_blank";
        link.innerHTML = (isImageLink ? "" : imgLinkOth) + link.innerHTML + '<span class="screen-reader"> (' + link.href.substring(link.href.lastIndexOf(".") + 1, link.href.length).toUpperCase() + ' file)</span>';
    }
    else if (link.href.toLowerCase().indexOf("cambridgeshire.gov.uk") == -1 && link.href.toLowerCase().indexOf("cambridgeshire.net") == -1 && link.href.toLowerCase().indexOf("search3.openobjects.com") == -1 && link.href.toLowerCase().indexOf("javascript") == -1) {
        link.target = "_blank";
        if (link.innerHTML.indexOf("window") < 0) {
            link.innerHTML += '<span class="screen-reader"> (opens in new window)</span>';
        }
        if (!isImageLink) {
            link.innerHTML += imgLinkNewWindow;
        }
    }
    else if (link.target == "_blank") {
        if (link.innerHTML.indexOf("window") < 0) {
            link.innerHTML += '<span class="screen-reader"> (opens in new window)</span>';
        }
        if (!isImageLink) {
            link.innerHTML += imgLinkNewWindow;
        }
    }
}


// Sets a cookie value for text size changes
function setTSCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}


// Gets the text size from a cookie
function getTSCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

// Sets a new text size based on 100% being standard and stores in a cookie
function txtSize(sz) {
    if (sz) {
        setTSCookie('TextSize', sz, '', '/');
    } else {
        sz = getTSCookie('TextSize');
    }
    if (sz) {
        sz = parseInt(sz);
        sz *= 0.67;
        document.body.style.fontSize = sz + "%";
    }
    return false;
}

// set the text size as soon as practical on page load
if (document.body.style) {
    txtSize();
}


