window.addEvent('domready', function() {
if($('userpref')){



	var countername = document.location.pathname.substring(1) // strip of first slash
 .replace(/\//g,".") // replace slashes with dots
 .replace(/\.html?/i,"") // replace html or htm extension 
 .replace(/\.asp/i,"") // replace html or htm extension 
 .replace(/(\s|%20|\+)/g,"_"); // replace spaces and plus signs with underscores
 
var  strLocation
strLocation = document.location.href
strLocation = strLocation.toLowerCase()
		
var counternameprefix=""

// if the url in format blah.ltscotland.org.uk rather than www.ltscotland.org.uk then add a prefix to counter name
if  (strLocation.search("://www.") == -1) 
	{
		counternameprefix = strLocation.replace("http://", "")
		counternameprefix = counternameprefix.replace("https://", "")
		
		var iPos = counternameprefix.indexOf(".", 0)
		counternameprefix=counternameprefix.slice(0, iPos)
		
		if (counternameprefix.length > 0)
			{
				counternameprefix = counternameprefix + "." 
			}
	}
	
// if the final counter name ends with a . then add index to the end	
var finalcountername = counternameprefix + countername

var  finalcounternamelength = finalcountername.length

var lastcharacter = finalcountername.slice(finalcounternamelength-1, finalcounternamelength)

if (lastcharacter == ".")
	{
		finalcountername = finalcountername + "index"
	}
	
// if this is the homepage then replace .index with .home.index

// a homepage is recognised if there is only one . in the counter name and also ends in .index

var ifirstposition = finalcountername.lastIndexOf(".")
var tempstring = finalcountername.slice(0, ifirstposition)
var isecondposition = tempstring.indexOf(".")

// if only one . then replace .index with .home.index

if (isecondposition == -1) {
	finalcountername=finalcountername.replace(".index", ".home.index")
}

// if final countername blank then must be homepage
if (finalcountername.length == 0) {
	finalcountername=finalcountername + "home.index"
}




    var mySavedColourPreference = Cookie.read('colour-preference');
    if (mySavedColourPreference == 'colourhighvisibility') {
        $$('body').removeClass('high');
        $$('body').addClass('highvisibility');
    }
    else if (mySavedColourPreference == 'colourhighcontrast') {
        $$('body').removeClass('highvisibility');
        $$('body').addClass('high');
    }
    else {
        $$('body').removeClass('high');
        $$('body').removeClass('highvisibility');
    }

    var mySavedTextPreference = Cookie.read('text-preference');
    if (mySavedTextPreference == 'textxlarge') {
        $$('body').removeClass('large');
        $$('body').addClass('xlarge');
    }
    else if (mySavedTextPreference == 'textlarge') {
        $$('body').removeClass('xlarge');
        $$('body').addClass('large');
    }
    else {
        $$('body').removeClass('large');
        $$('body').removeClass('xlarge');
    }

    //inject textSwitcher buttons
    new Element('p', { id: 'accessibility', html: 'Text size:' }).inject($('textSwitcher'));
    new Element('a', { href: '#', id: 'textnormal', title: 'Adjust text size: normal', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.textNormal\',\'onclick\',\'\');return false' }).inject($('textSwitcher'));
    new Element('a', { href: '#', id: 'textbig', title: 'Adjust text size: big', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.textBig\',\'onclick\',\'\');return false' }).inject($('textSwitcher'));
    new Element('a', { href: '#', id: 'textbigger', title: 'Adjust text size: bigger', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.textBigger\',\'onclick\',\'\');return false' }).inject($('textSwitcher'));

    //inject colourSwitcher buttons
    new Element('p', { id: 'accessibilitycolour', html: 'Contrast options:' }).inject($('colourSwitcher'));
    new Element('a', { href: '#', id: 'colournormal', title: 'Adjust colour contrast: normal', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.colourNormal\',\'onclick\',\'\');return false' }).inject($('colourSwitcher'));
    new Element('a', { href: '#', id: 'colourhighcontrast', title: 'Adjust colour contrast: high contrast', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.colourHighContrast\',\'onclick\',\'\');return false' }).inject($('colourSwitcher'));
    new Element('a', { href: '#', id: 'colourhighvisibility', title: 'Adjust colour contrast: high visibility', onclick: 'ns_onclick(this,\'\',\'' + countername + '.onclick.www.ltscotland.org.uk.accessibility.colourHighVisibility\',\'onclick\',\'\');return false' }).inject($('colourSwitcher'));

    $$('a#colournormal').addEvent('click', function() {
        colourSwitch('colournormal');
    });

    $$('a#colourhighcontrast').addEvent('click', function() {
        colourSwitch('colourhighcontrast');
    });

    $$('a#colourhighvisibility').addEvent('click', function() {
        colourSwitch('colourhighvisibility');
    });

    $$('a#textnormal').addEvent('click', function() {
        textSwitch('textnormal');
    });

    $$('a#textbig').addEvent('click', function() {
        textSwitch('textlarge');
    });

    $$('a#textbigger').addEvent('click', function() {
        textSwitch('textxlarge');
    });
}


    function colourSwitch(myColourPreference) {
        if (myColourPreference == 'colournormal') {
            $$('body').removeClass('high');
            $$('body').removeClass('highvisibility');
        }
        else if (myColourPreference == 'colourhighcontrast') {
            $$('body').removeClass('highvisibility');
            $$('body').addClass('high');
        }
        else {
            $$('body').removeClass('high');
            $$('body').addClass('highvisibility');
        }

        Cookie.write('colour-preference', myColourPreference, {
            path: '/',
            duration: 1
        });
    };

    function textSwitch(myTextPreference) {
        if (myTextPreference == 'textnormal') {
            $$('body').removeClass('large');
            $$('body').removeClass('xlarge');
        }
        else if (myTextPreference == 'textlarge') {
            $$('body').removeClass('xlarge');
            $$('body').addClass('large');
        }
        else {
            $$('body').removeClass('large');
            $$('body').addClass('xlarge');
        }

        Cookie.write('text-preference', myTextPreference, {
            path: '/',
            duration: 1
        });
    };

    //once js has loaded #pre.js changes to #post.js. Used with our css rules  
    document.documentElement.id = 'post';

/* start nar banner */ 
if ($('narAdvert')) {
                var elsNarAdvert = $$('#narAdvert ul, #narAdvert li'),
                    elNarAdvert1 = elsNarAdvert[1].set('tween', { duration: 1500 }),
                    elNarAdvert2 = elsNarAdvert[2],
                    elNarAdvert3 = elsNarAdvert[3],
                    iMargin = -380,
                    iMaxMargin = 0,
                    iTallest = 150;

                function sizeAdverts() {
                    //reset all advert heights and see if their scroll height > default height (190)
                    elsNarAdvert.each(function(el, i) {
                        el.setStyle('height', i == 0 ? 150 : 130);
                    });

                    //ensure all adverts are of the same height
                    //get the tallest advert
                    elsNarAdvert.each(function(el, i) {
                        if (i > 0 && el.getScrollSize().y > iTallest) {
                            iTallest = el.getScrollSize().y;
                        }
                    });

                    //set all adverts to the tallest height
                    elsNarAdvert.each(function(el, i) {
                        el.setStyle('height', i == 0 ? iTallest : iTallest - 20);
                    });

                    //reset the margins for tween
                    iMargin = -(iTallest * 2);
                    elNarAdvert1.setStyle('marginTop', iMargin);
                }

                function roll() {
                    if ((iMargin += iTallest) > iMaxMargin) {
                        iMargin = -(iTallest * 2);
                    }
                    elNarAdvert1.tween('marginTop', iMargin);
                }

                sizeAdverts();

                window.addEvent('resize', function() {
                    sizeAdverts();
                    iMargin = -(iTallest * 2);
                    elNarAdvert1.tween('marginTop', iMargin);
                });

                roll.periodical(6000);
            }
/* end nar banner */


/* promo */
    if ($('promo')) {
        function startGalleryRotation(selector, delay) {
            var i = 0;
            var layers = $$(selector).set('tween', { duration: 2000 });
            delay = (delay == undefined) ? 6000 : delay;
            setInterval(function() {
                layers[i].fade('out');
                i = (i == layers.length - 1) ? 0 : i + 1;
                layers[i].fade('in');
            }, delay);
        }

        startGalleryRotation('#promo a', 6000);
    }
    /* end promo */





	/* rounded corners for legacy sites */
	            String.implement({
                toElement: (function() {
                    var div = document.createElement('div');
                    return function() {
                        div.innerHTML = this;
                        return div.firstChild;
                    }
                })()
            });
	
	            var corners = (function() {
                //create our corners and box
                var eCorners = '<span class="cnrs"><b class="cnr top tl"></b><b class="cnr top tr"></b><b class="cnr btm bl"></b><b class="cnr btm br"></b></span>'.toElement(),
                    //if IE6 apply fix for 1px error
                    bIE6 = (Browser.Engine.trident && Browser.Engine.version == 4) ? true : false,
                    eCornersNew,
                    eLast;

                return function(sSelector) {
                    //add corners to each element in collection
                    $$(sSelector).each(function(e) {
                        eCornersNew = eCorners.cloneNode(true);

                        //if IE 6, fix 1px bug for heights with odd number
                        if (bIE6) {
                            if (e.getSize().y % 2) {
                                (eLast = eCornersNew.lastChild).style.bottom = eLast.previousSibling.style.bottom = '-1px';
                            }
                        }

                        //setup element for corners by applying our class
                        e.addClass('cnrBox').appendChild(eCornersNew);
                    });
                }
            })();

            corners(".m,#header,#HeaderImage,#searchToggle li a,#footer");
	
/* end rounded corners for legacy sites */





});
