﻿$(function() {
    var topOff = $(".intOfficesTop"),
		botOff = $(".intOffices");
    if (topOff.length && botOff.length) {
        var img = '<img src="/GFA/Sites/MainSite/Theme/Images/flag_icons/gb.png" alt="UK flag" />';
        topOff.html('<span id="topOffText">' + img + ' UK</span>');
        topOff.append('<div id="topOffList">' + botOff.html() + '</div>');
        var topOffList = $("#topOffList"),
            offButton = $("#topOffText");
        topOffList.hide();
        offButton.bind('click.intOffices', toggleIntOffice);
    }
});
function toggleIntOffice() {
    var topOffList = $("#topOffList"),
        offButton = $("#topOffText"),
        body = $('body');
    offButton.toggleClass('intOfficesOpen');
    topOffList.toggle();
    topOffList.css('left', offButton.position().left);
    topOffList.css('top', offButton.position().top + 24);
    window.setTimeout(function() {
        offButton.unbind('click.intOffices');
        body.bind('click.intOffices', function() {
            offButton.toggleClass('intOfficesOpen');
            topOffList.toggle();
            body.unbind('click.intOffices');
            offButton.bind('click.intOffices', toggleIntOffice);
        });
    }, 300);
}
jQuery.fn.outerHTML = function(s) {
    return (s) ? this.before(s).remove() : jQuery("<span>").append(this.eq(0).clone()).html();
}
