
function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}



// Initialization
$(document).ready(function(){
						   				   
	// fix: prevent scrolling when visiting page with anchor, directly. 
	// Tab IDs are "#tab-blah", "#tab-blah2", etc. Checks that the start of the hash is "tab".
	// if (window.location.hash) { $('html, body').animate({scrollTop: '0px'}, 0); } 
	
	// Main menu hover
	$('#menu > ul > li').append('<div class="hovereffect"></div>');
	$('#menu > ul > li').hover(function () {
		$('.hovereffect',this).show();
	},function () {
		$('.hovereffect',this).hide();
	});

	// CLEAR ON FOCUS
	// set all default value according to title attribute. 
	$(".txt").each(function(){
	  $(this).val($(this).attr("title"));
	});
	// clear input.text on focus, if still in default
	$(".txt").focus(function() {
	  if($(this).val()==$(this).attr("title")) {
		$(this).val("");
	  }
	});
	// if field is empty afterward, add text again
	$(".txt").blur(function() {
	  if($(this).val()=="") {
		$(this).val($(this).attr("title"));
	  }
	});
	
	// Remove aside if empty
	if ( $.trim( $('.subnav > .wrap').text() ) == ""){
		$('.subnav').remove();
	}
	// Remove tabs if empty
	if ( $.trim( $('#tab-bio').text() ) == ""){ $('#tab-bio').remove(); $('#attorney-tabs-ui a[href="#tab-bio"]').parent().remove(); }
	if ( $.trim( $('#tab-news').text() ) == ""){ $('#tab-news').remove(); $('#attorney-tabs-ui a[href="#tab-news"]').parent().remove(); }
	if ( $.trim( $('#tab-speaking').text() ) == ""){ $('#tab-speaking').remove(); $('#attorney-tabs-ui a[href="#tab-speaking"]').parent().remove(); }
	if ( $.trim( $('#tab-pubs').text() ) == ""){ $('#tab-pubs').remove(); $('#attorney-tabs-ui a[href="#tab-pubs"]').parent().remove(); }
	if ( $.trim( $('#tab-matters').text() ) == ""){ $('#tab-matters').remove(); $('#attorney-tabs-ui a[href="#tab-matters"]').parent().remove(); }
	if ( $.trim( $('#tab-overview').text() ) == ""){ $('#tab-overview').remove(); $('#attorney-tabs-ui a[href="#tab-overview"]').parent().remove(); }
	
	// Active Menu Item
	$(function() {
		var pathname = location.pathname;
		var highlight;
		//highlight home
		if(pathname == "/")
			highlight = $('#menu > ul > li:first');
		else {
			var path = pathname.substring(1);
			if (path)
				highlight = $('#menu a[href$="' + path + '"]').parent();
		}
		highlight.attr('class', 'active');
	});
	
	// Print Page
	$("div.printarea li.print a").click(function(e){
		e.preventDefault();
		window.print();
	});
	// Email page
	$("div.printarea li.emailthis a").click(function(e){
		var text = "mailto:?subject=Information from Willmont.com&body=For your consideration, following is a link to content from www.willmont.com:" + escape("\n") + window.location;
		window.location.href = text;
		e.preventDefault(); 
	});

	// First and last classes for ye old browser
	$("#footer ul li:last").addClass('last');
	$("#attorney-tabs-ui li:last").addClass('last');
	$("#attorney-tabs-ui li:last").css( {border:"none"} );
	$("body#home #aside ul li:last").addClass('last');
	$("#wrapper div.subnav ul li:last").addClass('last');
	
	// Fix IE6 Hover
	$("html.ie6 #menu > ul > li").hover(function() {
	  $("html.ie6 #menu ul li").removeClass("hover");
	  $(this).addClass("hover");
	}, function() {
	  $(this).removeClass("hover");
	});
	
	// Columnizer
	$('.columnized').easyListSplitter({ colNumber: 2 });
	
	// Expand/Collapse
	$("#read-collapse").hide();
	$("#read-expand-link").click( function(e){ $("#read-collapse").slideToggle(); e.preventDefault();  });
	
});


// Attorney tabs bbq + uitabs, with history
$(document).ready(function(){
  
  // The "tab widgets" to handle.
  var tabs = $('.all-tabs-ui'),
    
  // This selector will be reused when selecting actual tab widget A elements.
  tab_a_selector = '#attorney-tabs-ui a';
  
  // Enable tabs on all tab widgets. The `event` property must be overridden so
  // that the tabs aren't changed on click, and any custom event name can be
  // specified. Note that if you define a callback for the 'select' event, it
  // will be executed for the selected tab whenever the hash changes.
  tabs.tabs({ event: 'change' });
  
  // Define our own click handler for the tabs, overriding the default.
  tabs.find( tab_a_selector ).click(function(){
		var state = {};

		// Get the id of this tab widget.
		id = $(this).closest( '.all-tabs-ui' ).attr( 'id' );
		
		// Get the index of this tab.
		idx = $(this).parent().prevAll().length;
		
		// CHANGE PRINT TO PDF LINK TO INCLUDE TAB VARIABLE
		var topdf = $("#attorney-side .to-pdf");
		if (topdf.length) {
			var thehash = this.href.split('#')[1];
			topdf.attr('href', replaceQueryString( topdf.attr('href'), 'tab', thehash ) );
		}
		var topdf = $("body#practices-page .to-pdf");
		if (topdf.length) {
			var thehash = this.href.split('#')[1];
			topdf.attr('href', replaceQueryString( topdf.attr('href'), 'tab', thehash ) );
		}
		
		// Set the state!
		state[ id ] = idx;
		$.bbq.pushState( state );
  });
  
  // Bind an event to window.onhashchange that, when the history state changes,
  // iterates over all tab widgets, changing the current tab as necessary.
  $(window).bind( 'hashchange', function(e) {
    
    // Iterate over all tab widgets.
    tabs.each(function(){
      
      // Get the index for this tab widget from the hash, based on the
      // appropriate id property. In jQuery 1.4, you should use e.getState()
      // instead of $.bbq.getState(). The second, 'true' argument coerces the
      // string value to a number.
      var idx = $.bbq.getState( this.id, true ) || 0;
      
      // Select the appropriate tab for this tab widget by triggering the custom
      // event specified in the .tabs() init above (you could keep track of what
      // tab each widget is on using .data, and only select a tab if it has
      // changed).
      $(this).find( tab_a_selector ).eq( idx ).triggerHandler( 'change' );
	  

		// CHANGE PRINT TO PDF LINK TO INCLUDE TAB VARIABLE
		var topdf = $("#attorney-side .to-pdf");
		if (topdf.length) {
			var thehash = $(this).find( tab_a_selector ).eq( idx ).attr('href').split('#')[1];
			topdf.attr('href', replaceQueryString( topdf.attr('href'), 'tab', thehash ) );
		}
		var topdf = $("body#practices-page .to-pdf");
		if (topdf.length) {
			var thehash = $(this).find( tab_a_selector ).eq( idx ).attr('href').split('#')[1];
			topdf.attr('href', replaceQueryString( topdf.attr('href'), 'tab', thehash ) );
		}

	  
    });
  })
  
  // Since the event is only triggered when the hash changes, we need to trigger
  // the event now, to handle the hash the page may have loaded with.
  $(window).trigger( 'hashchange' );
  
});



// BOOKMARK SCRIPT
$(document).ready(function(){
    // add a "rel" attrib if Opera 7+
    if(window.opera) {
        if ($("div.printarea li.bookmark a").attr("rel") != ""){
            $("div.printarea li.bookmark a").attr("rel","sidebar");
        }
    }
    $("div.printarea li.bookmark a").click(function(event){
        event.preventDefault();
        var url = this.href;
        var title = this.title;
 
        if ($.browser.mozilla ) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url,"");
        } else if( $.browser.msie ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing
        } else {
             alert('Unfortunately, this browser does not support the requested action,'
             + ' please bookmark this page manually.');
        }
    });
});


