function navSpace(selector, align){
	$(selector).children().children('li:last').css('border-right', 'none');
	totalWidth = $(selector).width();
	total = $(selector).children().children().size();
	$(selector).children().children().find('a:first').css('padding-left', 0).css('padding-right', 0);
	currentWidth=0;
	$(selector).children().children().each(function(){
		currentWidth+=$(this).width();
	});
	if(currentWidth < totalWidth){
		newWidth = Math.floor((totalWidth-currentWidth)/total);
		remainder = (totalWidth-currentWidth-(newWidth*total));
		$(selector).children().children().find('a:first').css('padding-left', Math.floor(newWidth/2)+'px').css('padding-right', Math.ceil(newWidth/2)+'px');
		$(selector).children().children('li:last').find('a:first').css('padding-right', Math.ceil(newWidth/2)+remainder+'px');
		if(align == true){
			$(selector).children().children().find('a').css('padding-left', Math.floor(newWidth/2)+'px');
		}
	}
	else{
		$(selector).children().children().find('a:first').css('padding-left', '7px').css('padding-right', '7px');
	}
	$(selector).children().children().hover(function(){
		$(this).find('ul:first').show();
		$(this).addClass('navon');
	},function(){
		$(this).find('ul:first').hide();
		$(this).removeClass('navon');
	});
}


/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        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();
        }
        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 {
        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]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(document).ready(function(){
	navSpace('#mainnav', true);
	$('#mainnav ul ul').each(function(){
		$(this).find('li:first').remove();
	});
	if($('#barristerul').children().size() < 1 && $('#qcul').children().size() < 1){
    	$('#barrister').remove();
	}
	else{
		var liNo = $('#barristerul li').size();
		var liNo1 = $('#qcul li').size();
		$('#barristerul').after('<ul id="barristerul2" class="barristerul"></ul>');
		$('#qcul').after('<ul id="qcul2" class="qcul"></ul>');
		if($('#barristerul li').size() > 1){
			$('#barristerul li').each(function(e){
				if(e >= Math.floor(liNo/2)){
					$(this).appendTo($('#barristerul2'));
				}
			});
		}
		if($('#qcul li').size() > 1){
			$('#qcul li').each(function(e){
				if(e >= Math.floor(liNo1/2)){
					$(this).appendTo($('#qcul2'));
				}
			});
		}
	}
	$('#rightcol h2').each(function(){
		if($(this).next().not('h2').size() < 1){
			$(this).remove();
		}
	});
		$('#rightcol h3').each(function(){
		if($(this).next().not('h3').size() < 1){
			$(this).remove();
		}
	});
	$('#mainnav ul li').not($('#mainnav ul ul li')).hover(function(){
		ddWidth = $('#mainnav').children(':last').children(':last').width();
		if(ddWidth > 210){
			$('#mainnav ul ul').width(ddWidth + 'px');
		}
	});
	
	
	///// TEXT RESIZER /////
	
	//testURL = document.location.href;
	//testURL = testURL.split('#');
	//testURL = testURL[testURL.length-1]
	//if(testURL == 'tester'){
		$('#search').css('width' ,'255px');
		$('<div id="textResizer"><a href="Reset" id="resizeA">A</a><a href="Medium" id="resizeAA">A</a><a href="Large" id="resizeAAA">A</a></div>').prependTo('#search');
	//}
	$('#resizeA').click(function(){
		$('#textResizer a').removeClass('on');
		$('#midcol, #midcol2, #midcolsite, #rightcol, .homequote').removeClass('mediumText').removeClass('largeText');
		$(this).addClass('on');
		$.cookie('textSize', '0', {expires:30, path: '/', domain: 'hardwicke.co.uk'});
		return false;
	});
	$('#resizeAA').click(function(){
		$('#textResizer a').removeClass('on');
		$('#midcol, #midcol2, #midcolsite, #rightcol, .homequote').addClass('mediumText').removeClass('largeText');
		$(this).addClass('on');
		$.cookie('textSize', '1', {expires:30, path: '/', domain: 'hardwicke.co.uk'});
		return false;
	});
	$('#resizeAAA').click(function(){
		$('#textResizer a').removeClass('on');
		$('#midcol, #midcol2, #midcolsite, #rightcol, .homequote').removeClass('mediumText').addClass('largeText');
		$(this).addClass('on');
		$.cookie('textSize', '2', {expires:30, path: '/', domain: 'hardwicke.co.uk'});
		return false;
	});
	if($.cookie('textSize') == '1'){
		$('#resizeAA').click();
	}
	else if($.cookie('textSize') == '2'){
		$('#resizeAAA').click();
	}
	else{
		$('#resizeA').click();
	}
});