$(document).ready(function () {

    /*the below script constructs the base web root for use when referncing images from any level */
    var url = location.href.split("/");
    var webrootString = url[0] + "//" + url[2] + "//";
    /*alert(webrootString);*/

    //A note on how this works by Ben Hartigan
    // this javascript essentially creates the resize and re-contrast images and creates
    //event handlers for them. Using the jquery .attr functionality it looks for an element 
    //of a certain type i.e <Link> with a certain id i.e "stylesize" and modifies the specified
    //attribute i.e "href" on the fly. In this case it is modifying the href of the css file that
    //is used for the text sizing

    //Caution. When the template is run through the CMS, Alterian seems to append Template_ to the 
    //element id, this took me ages to debug so be careful to point at what the element id will
    //be at runtime (use firebug to determine :-)

    //create image html and apply to the accessibilty div
    $("#accessibility").html('<p id="text-size"><span>Text size:</span> <img id="small-text" src="' + webrootString + 'images/stw/text-size-small.gif" alt="Small text" /> <img id="medium-text" src="' + webrootString + 'images/stw/text-size-medium.gif" alt="Medium text" /> <img id="large-text" src="' + webrootString + 'images/stw/text-size-large.gif" alt="Large text" /></p><p id="contrast"><span>Contrast:</span> <img id="highcontrast" src="' + webrootString + 'images/stw/contrast-high.gif" alt="High contrast" /> <img id="normalcontrast" src="' + webrootString + 'images/stw/contrast-normal.gif" alt="Normal contrast" /></p>');

    //interrogate cookie to see if any preferences exist
    $("link#Template_stylesize").attr('href', webrootString + "templates/default/css/stw/" + $.cookie('text_size') + ".css");
    $("link#Template_stylecontrast").attr('href', webrootString + "templates/default/css/stw/" + $.cookie('contrast') + ".css");

    //react to events on text size buttons (Template_stylesize) and set cookies accordingly
    // (bhyr2190) ensure that / path is set for cookie to stop different cookies being set for different page levels
	//this causes all kinds of fun if not done :-)	
	
    $("img#small-text").click(function () {
        $("link#Template_stylesize").attr('href', webrootString + "templates/default/css/stw/small.css");
		$.cookie('text_size', 'small', { expires: 365 , path: '/'});
    });
    $("img#medium-text").click(function () {
        $("link#Template_stylesize").attr('href', webrootString + "templates/default/css/stw/medium.css");
        $.cookie('text_size', 'medium', { expires: 365, path: '/' });

    });
    $("img#large-text").click(function () {
        $("link#Template_stylesize").attr('href', webrootString + "templates/default/css/stw/large.css");
        $.cookie('text_size', 'large', { expires: 365, path: '/' });

    });

    //react to events on contrast buttons (Template_stylecontrast)
    $("img#highcontrast").click(function () {
        $("link#Template_stylecontrast").attr('href', webrootString + "templates/default/css/stw/highcontrast.css");
        $.cookie('contrast', 'highcontrast', { expires: 365 , path: '/'});

    });
    $("img#normalcontrast").click(function () {
        $("link#Template_stylecontrast").attr('href', webrootString + "templates/default/css/stw/normalcontrast.css");
        $.cookie('contrast', 'normalcontrast', { expires: 365 , path: '/'});

    });


});

