/* jQuery code - executes when document finishes loading */

$(document).ready(function() {
	var char_limit = 20;					   
	var top_nav = $('#nav > li a');													// get top level nav anchors
	top_nav.each( function() {
		var el = $(this);															// for each anchor...
		var txt = el.html();														// get anchor text
		var txt = txt.replace(/&amp;/g, '&');										// replace &amp; with &, to get correct character count
		if (txt.length > char_limit) {
			var mid = Math.round(txt.length / 2);									// find middle of text
			var before = txt.lastIndexOf(' ', mid);									// find space before middle
			var after = txt.indexOf(' ', mid);										// find space after middle
			if (Math.abs(mid - before) < Math.abs(mid - after)) {					// if before is closer to middle than after...
				txt = txt.slice(0, before) + '<br />' + txt.slice(before + 1);		// replace space before middle with <br /> 
			} else {																// otherwise...
				txt = txt.slice(0, after) + '<br />' + txt.slice(after + 1);		// replace space after middle with <br /> 
			}
			txt = txt.replace(/&/g, '&amp;');										// replace & with &amp;
			el.html(txt);															// replace element text
			el.addClass('stacked');													// set element class for 2 line style 
		}
    });


    // PageAllLocations.aspx
    $("select[id$=_CountryDD]").change(function () {
        filter($(this).val());
    });
    filter($("select[id$=CountryDD]").val());

});

function filter(id) {
    if (id == "0") {
        $("#offices td, #offices td div").show();
    }
    else {
        $("#" + id).show();
        $("#" + id).siblings().hide();
        $("#" + id).parent().show();
        $("#" + id).parent().siblings().hide();
    }
} 

