jQuery.fn.regionswitcher = function(settings) {
    var options = jQuery.extend({
        loadRegion: null,
        initialText: 'Audiences',
        width: 209,
        height: 100,
        buttonPreText: '',
        closeOnSelect: true,
        buttonHeight: 21,
        cookieName: 'fsbp-region',
        onOpen: function() { },
        onClose: function() { },
        onSelect: function() { }
    }, settings);

    //markup 
    var button = jQuery('<a href="#" class=""><span class="jquery-ui-regionswitcher-icon"></span><span class="jquery-ui-regionswitcher-title">' + options.initialText + '</span></a>');
    var switcherpane = jQuery('<div class="regionswitcher-panel"><div><ul><li><a href="/contractors/"><span class="regionName">Contractors</span></a></li><li><a href="/architects/"><span class="regionName">Architects</span></a></li><li><a href="/owners/"><span class="regionName">Building Owners</span></a></li><li><a href="/consultants/"><span class="regionName">Consultants</span></a></li></ul></div></div>').find('div').removeAttr('id');

    //button events
    button.click(
		function() {
		    if (switcherpane.is(':visible')) { switcherpane.spHide(); }
		    else { switcherpane.spShow(); }
		    return false;
		}
	);

    //menu events (mouseout didn't work...)
    switcherpane.hover(
		function() { },
		function() { if (switcherpane.is(':visible')) { jQuery(this).spHide(); } }
	);

    //show/hide panel functions
    jQuery.fn.spShow = function() { jQuery(this).css({ top: button.offset().top + options.buttonHeight - 21, left: button.offset().left }).slideDown(50); button.css(button_active); options.onOpen(); }
    jQuery.fn.spHide = function() { jQuery(this).slideUp(50, function() { options.onClose(); }); button.css(button_default); }


    /* Region Loading
    ---------------------------------------------------------------------*/
    switcherpane.find('a').click(function() {
        var regionName = jQuery(this).find('span').text();
        button.find('.jquery-ui-regionswitcher-title').text(options.buttonPreText + regionName);
        jQuery.cookie(options.cookieName, regionName);
        options.onSelect();
        if (options.closeOnSelect && switcherpane.is(':visible')) { switcherpane.spHide(); }
        var href = jQuery(this).attr("href");
        if (href.length > 0) location.replace(href);
        return false;
    });


    /* Inline CSS 
    ---------------------------------------------------------------------*/
    var button_default = {
        textDecoration: 'none',
        padding: '1px 3px 3px 8px',
        width: options.width - 11, //minus must match left and right padding 
        display: 'block',
        height: options.buttonHeight,
        lineHeight: options.buttonHeight + 'px',
        outline: '0',
        background: 'url(/site_assets/images/common/regions_bg.png) no-repeat'
    };
    var button_hover = {
        cursor: 'pointer'
    };
    var button_active = {
        borderBottom: 0,
        outline: '0'
    };
    //button css
    button.css(button_default)
	.hover(
		function() {
		    jQuery(this).css(button_hover);
		},
		function() {
		    if (!switcherpane.is(':animated') && switcherpane.is(':hidden')) { jQuery(this).css(button_default); }
		}
	)
	.find('.jquery-ui-regionswitcher-icon').css({
	    float: 'right',
	    width: '16px',
	    height: '16px'
	});
    //pane css
    switcherpane.css({
        position: 'absolute',
        float: 'left',
        fontSize: '12px',
        lineHeight: '12px',
        color: '#fff',
        padding: '0px 3px 3px 8px',
        background: 'url(/site_assets/images/common/regions_panel_home_bg.png) no-repeat',
        borderTop: 0,
        zIndex: 999999,
        width: options.width - 6//minus must match left and right padding
    })
	.find('ul').css({
	    listStyle: 'none',
	    margin: '2px 0 0',
	    padding: '4px 0 0',
	    overflow: 'auto',
	    height: options.height
	}).end()
	.find('li').hover(
		function() {
		    jQuery(this).css({
		        cursor: 'pointer'
		    });
		},
		function() {
		    jQuery(this).css({
		        cursor: 'auto'
		    });
		}
	).css({
	    width: options.width - 30,
	    height: '',
	    padding: '0px',
	    margin: '0px',
	    clear: 'left',
	    float: 'left'
	}).end()
	.find('a').addClass('regionLink').css({
	    textDecoration: 'none',
	    float: 'left',
	    width: '100%',
	    outline: '0',
	    cursor: 'pointer'
	}).end()
	.find('img').css({
	    float: 'left',
	    margin: '0 2px'
	}).end()
	.find('.regionName').css({
	    float: 'left',
	    padding: '3px 0',
	    '_display': 'inline-block'
	}).end();



    jQuery(this).append(button);
    jQuery('body').append(switcherpane);
    switcherpane.hide();
    //if (jQuery.cookie(options.cookieName) || options.loadRegion) {
    //    var regionName = jQuery.cookie(options.cookieName) || options.loadRegion;
    //    switcherpane.find('a:contains(' + regionName + ')').trigger('click');
    //}

    return this;
};




/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

