function init() {
    document.body.className = "hasJS loading";
    window.addEvent('domready', function () {
        new plinth();
        new tables();
        var myUrl = document.location.href;
        if (location.href.indexOf("bac_co_uk") > -1 ) {
           window.env = "STAGING"
        } else if(location.href.indexOf("bac") > -1) {
		       window.env = "DEV"
		}else{
			 window.env = "PRODUCTION"
        }
		
        new confirm();
        Cufon.replace('h1, h2, .plinth h3, .existingcustomers h3, table.vertical th, td.lrg, caption, a.tabLinkTitle');
        document.body.className = "hasJS domReady";
        var FAQs = $$('a[rel=faqs]');
        FAQs.each(function (item) {
            new popup(item, {
                'name': 'eKnowledge'
            });
            item.appendText(" ");
            if (window.env == "STAGING") {
                slnk = "/bac_co_uk/lib-ui/img/pages/new-window.gif"
            } else if(window.env == "DEV"){
			slnk = "/bac/lib-ui/img/pages/new-window.gif"
		}else{
                slnk = "/lib-ui/img/pages/new-window.gif"
            }
            item.adopt(new Element('img', {
                'src': slnk,
                'alt': 'opens a new window'
            }))
        }, this);
        var blankLinks = $$('a[rel=blank]');
        blankLinks.set('target', '_blank');
        blankLinks.each(function (item) {
            item.appendText(" ");
            if (window.env == "STAGING") {
                slnk = "/bac_co_uk/lib-ui/img/pages/new-window.gif"
            }else if(window.env == "DEV"){
		     	slnk = "/bac/lib-ui/img/pages/new-window.gif"
		   }else{
                slnk = "/lib-ui/img/pages/new-window.gif"
            }
			
            item.adopt(new Element('img', {
                'src': slnk,
                'alt': 'opens a new window'
            }))
        }, this);
        var autoOff = $$('input.nocomplete');
        autoOff.set('autocomplete', 'off');
        var alertRole = $$('.error');
        alertRole.set('role', 'alert');
        $$('ul li:first-child').addClass('first');
        $$('ul li:last-child').addClass('last');
        var navSecondary = $$('.navsecondary');
        var navSecondarySub = navSecondary.getElement('ul ul');
        if (navSecondarySub[0] != null) {
            var selected = navSecondary.getElement('li.selected');
            selected.addClass('expanded')
        }
        if (!CheckApp()) {
            var myUrl = document.location.href;
            myUrl = myUrl.replace(new RegExp(/^https\:\/\//i), "http://");
            if (myUrl != document.location.href) {
                window.location = myUrl
            }
        }
    })
}
function $getUrlKey(key, url) {
    if (arguments.length < 2) url = location.href;
    if (arguments.length > 0 && key != "") {
        if (key == "#") {
            var regex = new RegExp("[#]([^$]*)")
        } else if (key == "?") {
            var regex = new RegExp("[?]([^#$]*)")
        } else {
            var regex = new RegExp("[?&]" + key + "=([^&#]*)")
        }
        var results = regex.exec(url);
        return (results == null) ? "" : results[1]
    } else {
        url = url.split("?");
        var results = {};
        if (url.length > 1) {
            url = url[1].split("#");
            if (url.length > 1) results["hash"] = url[1];
            url[0].split("&").each(function (item, index) {
                item = item.split("=");
                results[item[0]] = item[1]
            })
        }
        return results
    }
};

function CheckApp() {
    if ($('FDC_APPLICATION_NAME')) {
        return true
    } else {
        return false
    }
};

function popitup(url) {
    var newwindow = window.open(url, 'name', 'status=yes,width=790,height=577');
    if (window.focus) {
        newwindow.focus()
    }
    return false
};
var msgCheck = new Class({
    Implements: [Options, Events],
    options: {
        target: 'alert',
        folderDepth: '',
        xml: 'lib-ui/mbnauto/xml/alert.xml'
    },
    initialize: function (options) {
        this.setOptions(options);
        try {
            var myRequest = new Request({
                url: this.options.folderDepth + this.options.xml,
                onSuccess: function (txt) {
                    this.data = xml2json.parser(txt);
                    this.build();
                }.bind(this)
            }).send();
        } catch (e) {}
    },
    build: function () {
        if (this.data.message.image != 'none') {
            m = new Element('img', {
                'src': this.options.folderDepth + this.data.message.image,
                'alt': this.data.message.alt
            });
            $(this.options.target).adopt(m);
            if (this.data.message.href != 'none') {
                new Element('a', {
                    'href': this.data.message.href
                }).wraps(m);
            }
            $(this.options.target).setStyle('margin', '0 0 5px 0');
        }
    }
});
