var bHasInteracted = false;

$(window).resize( function() {
	if ($(window).width() < 1000) {
		if ($('a.a_show_nav_secondary').next().children('ul').children('li').length < 1) {
			$('a.a_show_nav_secondary').next().hide();
		}
		if ($('a.a_show_nav_secondary').next().find('li').length == 0) {
			$('a.a_show_nav_secondary').css({'height' : '0px', 'padding' : '0px'}).off('click');
		}
	}
});

/*

$(window).scroll( function() {

	if ($(window).scrollTop() < 420) {

		// $('body.header_overlap header#header_main').css({'z-index' : '999'});

	}
	
	if ($(window).scrollTop() < 400) {
		
		// $('body.header_overlap div.div_prefs_form').css({'margin-top' : ($(window).scrollTop() / 2) - 240 + 'px'});

	} else if ($(window).scrollTop() < 420) {

		// $('body.header_overlap header#header_main').css({'z-index' : '999'});
		// $('body.header_overlap div.div_prefs_form').css({'margin-top' : '-28px'});

	} else {

        // $('body.header_overlap header#header_main').css({'z-index' : '9999999999'});

	}

});

*/

$(document).ready( function() {
	
	
	// BW DETECT THREE COLUMNS IN DROP DOWN MENU IN ORDER TO RESIZE INTRO QUOTE

	
	$('li.li_intro').each( function() {
		if ($(this).siblings().length == 3 ) {
			$(this).css('width', '20%').addClass('li_intro_3col');
			$(this).siblings().addClass('li_no_pad');
		}
	});
	
	// ================================================================================
    // DJN: Function to create jump menu for office directory
    // ================================================================================

    $('select#select_directory').change(function(e) {

        if ($(this).val() != 'all') {
			window.location = $(this).val();
        }

	});

	//alert('change');
	
	$('.form_filter_search select, #form_careers select, form.form_careers_block select').change(function(e) {
		truncateMenu($(this));
	});
	
	 
	//alert('included');
	
	if ($(window).width() < 850) {
		if ($('video#video_main').length > 0 ) {
			//alert('yes');
			var posterSRC = $('video#video_main').attr('poster');
			$('video#video_main').before('<img id="img_main" src="' + posterSRC + '" width="1300" height="420" alt="" />');
			$('video#video_main').remove();
		}
	}
	
	if ($(window).width() < 1000) {
		$('a.a_show_nav_secondary').next().hide();
		$('a.a_show_nav_secondary').click( function() {
			$(this).next().slideToggle();
		});
		if ($('a.a_show_nav_secondary').next().find('li').length == 0) {
			$('a.a_show_nav_secondary').css({'height' : '0px', 'padding' : '0px'}).off('click');
		}
	}
	
	
	//alert('ready');
	/*if ($(window).width() > 850) {
		$('header#header_main nav.nav_primary li').hover( function() {
			if ($(this).children('ul').children('li').length > 0) {
				$(this).children('ul').stop().slideDown(250);
				$(this).addClass('selectedHover');
			}
		}, function() {
			$(this).children('ul').stop().slideUp(250);
			$(this).removeClass('selectedHover');
		});
	}*/
	
			function megaHoverOver() {
				//alert('working');
				bHasInteracted = true;
				if ($(this).children('ul').children('li').length > 0) {
					$(this).children('ul').stop().slideDown(250);
                   
					$('html, body.header_overlap').stop().animate({scrollTop: 0}, 100, function() {
						$('body.header_overlap div.div_prefs_form').stop().animate({'margin-top' : '-28px'}, 100);
					});
					$(this).addClass('selectedHover');
				}
			}
			//On Hover Out
			function megaHoverOut() {
				$(this).children('ul').stop().slideUp(250);
				$(this).removeClass('selectedHover');
				if ($('.selectedHover').length == 0) {
					$('body.header_overlap header#header_main').css({'z-index' : '999'});
                    $('body.header_overlap div.div_prefs_form').stop().animate({'margin-top' : '-220px'}, 100);
					// $('body.header_overlap div.div_prefs_form').stop().animate({'margin-top' : '-220px'}, 250);
					// $('body.header_overlap header#header_main').css({'z-index' : '999'});
					$('html, body.header_overlap').stop().animate({scrollTop: 0}, 100);
				}
				
			}
				
			//Set custom configurations
			var config = {
				sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
				interval: 50, // number = milliseconds for onMouseOver polling interval
				over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
				timeout: 800, // number = milliseconds delay before onMouseOut
				out: megaHoverOut // function = onMouseOut callback (REQUIRED)
			};

			
	if ($(window).width() > 999) {
		$('header#header_main nav.nav_primary > ul > li').hoverIntent(config); //Trigger Hover intent with custom configurations
	}
	
	
	/*$('li#nav_supplemental_li_2 > a').click( function() {
		$(this).next().slideToggle();
		return false;
	});*/
	

	/*

	$('a.a_show_search').click( function() {
		
		if ($(window).width() > 999) {
				$('form.form_search').slideToggle();
				$('section#section_tkosp').animate({'top': '-200px'});
				$('header#header_main').animate({'margin-top': '0px'});
				$('a#a_tkosp_button').animate({'background-position-y': '5px'});
		} else {
			if ($(this).hasClass('selected')) {
				$(this).removeClass('selected');
				//alert('yes');
				$('form.form_search').slideUp();
				$('section#section_tkosp').animate({'top': '-200px'});
				$('header#header_main').animate({'margin-top': '0px'});
				$('a#a_tkosp_button').animate({'background-position-y': '5px'});
			} else {
				$(this).addClass('selected');
				//alert('no');
				$('form.form_search').slideDown();
				$('section#section_tkosp').animate({'top': '-300px'});
				$('header#header_main').animate({'margin-top': '0px'});
				$('a#a_tkosp_button').animate({'background-position-y': '5px'});
			}
		}
		return false;
	});

    */
	

	
	// BW CLICK FUNCTTION FOR NAV DROP DOWN INNER LISTS -------------------------------
    if ($(window).width() > 999) {
        
        
        $('ul.nav_primary_ul_level_2 > li').each( function() {
            if ($(this).children('ul').children('li').length > 0) {
                $(this).addClass('hasChildren');
            }
        });
        
        $('ul.nav_primary_ul_level_2 > li.hasChildren > a').not('li.hasChildren.li_show_children > a').on('click', function() {
            //alert('Has children');
            $('ul.nav_primary_ul_level_2 > li.hasChildren > a').removeClass('selected');
            $(this).addClass('selected');
            $('ul.nav_primary_ul_level_2 > li.hasChildren > ul').animate({'width' : '0px'});
            $(this).next('ul').animate({'width' : '500px'});
            return false;
        });
        
        $('ul.nav_primary_ul_level_3').each( function() {
            if ($(this).parent().parent().prev().text().trim() === "Expertise") {
                $(this).css({'height' : '430px'})
                $(this).parent().parent().css({'height' : '480px'})
                if ($(this).children().length > 15) {
                     $(this).children().each( function(i) {
                        if (i == 16) {
                            $(this).css({'margin-top' : '-336px'})
                            $(this).css({'line-height' : '3px'});
                        }
                        if (i > 15) {
                            $(this).css({'margin-left' : '135px'});
                        }
                        if (i == 31) {
                            $(this).css({'margin-top' : '-336px'})
                             $(this).css({'line-height' : '3px'});
                        }
                        if (i > 30) {
                            $(this).css({'margin-left' : '270px'});
                        }
                    });
                }
            } else {
                if ($(this).children().length > 12) {
                //alert('yes');
                     $(this).children().each( function(i) {
                        if (i == 13) {
                            $(this).css({'margin-top' : '-336px'})
                            $(this).css({'line-height' : '3px'});
                        }
                        if (i > 12) {
                            $(this).css({'margin-left' : '135px'});
                        }
                        if (i == 25) {
                            $(this).css({'margin-top' : '-336px'})
                             $(this).css({'line-height' : '3px'});
                        }
                        if (i > 24) {
                            $(this).css({'margin-left' : '270px'});
                        }
                    });
                }
            }
        });
        
    } 
	
	$('a.a_show_nav').on('click', function() {
		if ($(window).width() < 1000) {
			$('nav.nav_primary').slideToggle();
			return false;
		}
	});
	
	$('nav.nav_primary ul li a').not('nav.nav_primary ul li#li_search_2020 a').click( function() {
		if ($(window).width() < 1000) {
			if ($(this).parent().children('ul').children('li').children('a').length > 0) {
				$(this).next('ul').slideToggle();
                $(this).toggleClass('active');          
               // alert("Class Added");
                return false;
			}
		}
	});

    $('nav.nav_primary').on('click', 'li#li_search_2020 a', function(e) {
        $('div.findPeople').slideToggle();
		window.scrollTo(0, 0);
        e.preventDefault();
    });

    $('a.a_show_search').click( function(e) {
        $('div.findPeople').slideToggle();
        $('nav.nav_primary').slideUp();
		window.scrollTo(0, 0);
        e.preventDefault();
    });

	// ------------------------ END OF BW CLICK FUNCTTION FOR NAV DROP DOWN INNER LISTS

	// ================================================================================
    // DJN: Function to evaluate briefings search before submission
    // ================================================================================

    $('form.form_filter_search, form.form_search, div.div_dropdown_find_lawyer form').submit(function(e) {

		$(this).find('select').each( function() {
			if ($(this).val() == '') {
				$(this).attr('disabled', 'true');
			}
		});
		
		$(this).find('input').each( function() {
			if ($(this).val() == 'Find a briefing' || $(this).val() == 'Find a lawyer' || $(this).val() == 'Type to search') {
				$(this).val('');
			}
		});
			
		e.preventDefault();
		$(this).unbind();
		$(this).submit();

	});

    // ================================================================================
    // Control nav tertiary 2020 visibility 
    // ================================================================================

    if ($('nav#nav_tertiary_2020 ul li ul').length == 1) {
        $('nav#nav_tertiary_2020').css('height', '120px');
		$('nav#nav_tertiary_2020 ul li ul').slideDown();
    }
	
});

function truncateMenu(thisElement) {


	thisElement.children('option').each( function() {
		$(this).text($(this).data('original'));
	});

	var nCharLength = 15;
	if (thisElement.width() > 200) {
		nCharLength = 25;
	}

	thisElement.children('option').each( function() {
    		if(($(this).attr('selected') == 'selected') && ($(this).text().trim().length > nCharLength)){
    		$(this).text($(this).text().trim().slice(0, nCharLength) + '...')
    		}
    });

}
