// Email window popup with disclaimer static text.
// use: when user clicks and attorney email link
//
function emailWindow(email)
{
	window.open("email-popup.html?email=" + email, "EmailNotice", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,menubar=no,resizable=yes,width=500,height=380");
}

// Bookmark page link, IE only.
// all other browsers displays an alert box.  not sure how detailed we
// want to make this, eg NS, Firefox 'press ctrl+d' message, Mozilla ??
//
function addToFavorites()
{
	var ua = navigator.userAgent.toLowerCase(); 

	// browser engine name
	this.isGecko	   = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
	this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

	// browser name
	this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
	this.isSafari	   = (ua.indexOf('safari') != - 1);
	this.isOmniweb	   = (ua.indexOf('omniweb') != - 1);
	this.isOpera	   = (ua.indexOf('opera') != -1); 
	this.isIcab	   = (ua.indexOf('icab') != -1); 
	this.isAol	   = (ua.indexOf('aol') != -1); 
	this.isIE	   = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
	this.isMozilla	   = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
	this.isFirebird    = (ua.indexOf('firebird/') != -1);
	this.isFirefox	  = (ua.indexOf('firefox/') != -1);
	this.isNS	   = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );

	this.versionMajor = parseInt(this.versionMinor);
	this.isMac    = (ua.indexOf('mac') != -1);

	this.isIE5x = (this.isIE && this.versionMajor == 5);

	if (window.external && !this.isMac && !this.isFirefox)
	{
		var url=window.location.href;
		var title=document.title;
		window.external.AddFavorite(url,title)
	} else if (!this.isMac && (this.isNS || this.isFirefox || this.isMozilla))
	{
		alert("Press Ctrl+D to bookmark this page."); 
	} else if (this.isMac && (this.isSafari || this.ie5x || this.isMozilla))
	{
		alert("Press Apple/Squiggle+D to bookmark this page."); 
	} else {
		alert("Sorry! Your browser doesn't support this function.\nUse your browser's bookmark feature to add a bookmark."); 
	}
}

// Place holder for the 'print this page' link
// s/b replaced when we do the html templates
//
function printPage()
{
	window.print();  
}

/* added for atty name hover popups */
/* db 03/01/11 - bug 17008: added for atty name hover popups */
function attyPopups()
{
		
     	$('div.attyListName').hover(
      		function (e) {	
      			
      			$attyPopup = $(this).parent().prev('div');
      			
      			$attyPopup_location = $(this).offset();
      			
      			//$attyPopup_x = $attyPopup_location.left;
      			$attyPopup_y = $attyPopup_location.top;
      			
      			$attyPopup_height = $attyPopup.height();
      			$window_height = $(window).height();
      			$window_bottom = $window_height + $(window).scrollTop();
      			$attyPopup_plus_y = $attyPopup_height + $attyPopup_y;
      			
      			if($attyPopup_plus_y > $window_bottom)
      			{
      				$move_diff = $attyPopup_plus_y - $window_bottom - 32;
      				$attyPopup_y = $attyPopup_y - $move_diff;
      			}
      			
      			//$attyPopup_new_x = $attyPopup_x;
      			$attyPopup_new_y = $attyPopup_y - 32;
      			
      			$attyPopup.css({'top' : $attyPopup_new_y});
      			
      			$attyPopup.css({'display' : 'none'});
      			$attyPopup.show();
      			$attyPopup.css({'z-index' : '10000'});
      			
      			$(this).addClass('attyListNameBG');
      			
      			if($(this).is(':visible'))
      			{
      				$('div.popupAtty').mouseenter(
      					function () {		
      						$(this).stop().css({'display' : 'block'});
      						$(this).next('div').children('div').stop().addClass('attyListNameBG');					
      					}).mouseleave(
      					function(){	
      						$(this).hide();
      						$(this).next('div').children('div').stop().removeClass('attyListNameBG');
      					}
    				);
    				
    				$('div.popupAtty').click(
      					function(){	
      						$(this).hide();
      						$(this).next('div').children('div').stop().removeClass('attyListNameBG');
      					}
    				);
    			}
      		}, 
      		function () {
         		$(this).parent().prev('div').hide();
         		$(this).removeClass('attyListNameBG');
      		}
    	)
    	.click(function(){
    		$('div.popupAtty').hide();	
    	});
}
 
/* db 03/01/11 - bug 17008: for addThis.com hovers */
function addThis()
{
	var delay = 180;
    
    function hideMenu() {
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden')) {
            $('.hover_menu').fadeOut('fast');
            $('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    }
    
    $('a.custom_button, div.hover_menu').mouseenter(function() {
        $('div.hover_menu').fadeIn('fast');
        $('div.custom_button').addClass('active');
        $(this).data('in', true);
        $('div.hover_menu').data('hidden', false);
    }).mouseleave(function() {
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });    
}

/* db 03/01/11 - bug 17008: accept disclaimer */
function acceptDisclaimer()
{
	if(!$('#formDisclaimer').length)
		$('#formDisclaimerAccepted').toggle();
	
	$('#linkButtonAccept').click(function() {
        $('#formDisclaimer').toggle();
        $('#formDisclaimerAccepted').fadeIn();
    }) 
}

function mobileMenu()
{
	// first, add an id to the current button
	$currNav = $('.navButton.udmY').parent().attr('id','currNav');
	
	var $menuToggle = $("<a id='menuToggle'>Show Menu</a>");
	var altText = "Hide Menu";
	
	var $subToggle = $("<span id='subToggle'>+</span>");
	
	$menuToggle.appendTo('#menuWrapper')
	.click(function(){
		
		var currText = $menuToggle.text();
		$menuToggle.text(altText);
		altText = currText;
		
		$('#currNav > ul').show();
		$('#udm > li').slideToggle(200);
	});
}

function setUpMobile()
{
	var isMobile = ( screen.width < 600 );
	
	// isMobile = ($(window).width() < 600 ); 	// for testing
	
	if( !isMobile )
		return false;
		
	$('#mobileSwitch').click(function(){
		
		$.cookie('fullsize', $.cookie('fullsize') ? null : 1);
		
		var bareURL = document.location.href.replace(/fullsize=\d/, '').replace(/\?$/,'');
		document.location.href = bareURL;
		
		return false;
	});
	
	// force fullsize with a query string
	if( document.location.search.indexOf('fullsize=1') >= 0 )
	{
		$.cookie('fullsize', 1);
	}
	// undo force fullsize
	else if(document.location.search.indexOf('fullsize=0') >= 0 )
	{
		$.cookie('fullsize', 0);
	}
	
	if( isMobile && $.cookie('fullsize') )
	{
		$('link[media*=max-device-width],link[media*=max-width]').remove();
		$('link[media*=min-device-width],link[media*=min-width]').removeAttr('media');
		
		$('#mobileSwitch').css('display', 'inline');
		$('#footerSearch').remove()
		
		return false;
	}
	
	$(mobileMenu);
	
	return isMobile;
}


function homeGlobal()
{
	$('#global').hover(function() {
		$('#reach').toggle();
		$(this).addClass('globalON');
	}, function() {
		$(this).removeClass('globalON');
		$('#reach').toggle();
	});

}

// end functions --------------------------------------------

jQuery(function(){

	var basetag = document.body.id.split('-').shift();
	
	// test for mobile by screen width
	var isMobile = setUpMobile();
	
	if( !isMobile )
	{
		// basically a caching version of $.getScript
		$.ajax({
			type: "GET",
			url: 'lib/udm/udm-dom.js',
			dataType: "script",
			cache: true
		})
	}
	
	// udm's youAreHere is too slow, get that class on there instantly
	$('.navButton[href="'+basetag+'.html"]')
	.addClass('udmY')
	.siblings('ul')
	.find('a')
	.filter(function(){
		return this.href === document.location.href
	})
	.addClass('udmY');
	
	// db 03/01/11 - bug 17008
	if($('body.attorneys').length && !isMobile)
		$(attyPopups);
	
	// for addThis.com hovers
	if( !isMobile )
	{
		$(addThis);
		$(homeGlobal);
	}
	
	// mobile stuff
	
	// contact-question disclaimer
	if($('#contact-question').length)
		$(acceptDisclaimer);

});

$(window).load(function(){
	$('body').addClass('loaded');
});
