/**
 * @author jmorga
 */
 
 
jQuery().ready(function(){

	$("#primary_nav li").hover(
		function(){
			$(this).children('ul').show().siblings('a').css('backgroundPosition', '0 0');;
		},
		function(){
			$(this).children('ul').hide().siblings('a').attr('style', '');
		}
	)
	
	/*
	$('ul.sm_menu a:not(.selected) + ul.sm_sub_menu').hide(); 
	$('ul.sm_sub_menu').prev('a').click(
		function(){
			if(!$(this).next('ul').hasClass('openDrawer')){
				$('ul.sm_menu a:not(.selected) + ul.sm_sub_menu').slideUp().removeClass('openDrawer'); 
				$(this).next('ul').slideDown().addClass('openDrawer');
				return false;
			}
		}
	);
	*/
	$("input#reset").click(function(){
		$("#profilesearch_form select").each(function(){
			this.selectedIndex = 0;
			window.location = $("#profilesearch_form").attr('action')
		})
		return false;
	})
		
/* For accordion */	
	$('ul.accordion li:not(:first) h4~div').hide();
	$('ul.accordion li:not(:first)').addClass('closed');
	$('ul.accordion li:first').addClass('open');
	$('ul.accordion li.closed').click(
		function(){
			useAccordion(this);
		}
	);
	$('ul.accordion li li').unbind().removeClass('open').removeClass('closed');
	
	var useAccordion = function(elem){
		$('ul.accordion li:not(.closed) h4~div').slideUp();
		$('ul.accordion li:not(.closed)').addClass('closed').removeClass('open');	
		$(elem).removeClass('closed').addClass('open');
		$(elem).children('h4~div').slideDown();
		$('ul.accordion li:not(.closed)').unbind();
		$('ul.accordion li.closed').click(
			function(){
				useAccordion(this);
			}
		);
		$('ul.accordion li li').unbind().removeClass('open').removeClass('closed');
	}
	
	
	$('.expandableItem div').hide();

	$('.expandableItem h4').click(function(){
		if ($(this).hasClass('expand')){
			$(this).removeClass('expand')
			.children('span').html('+').end()
			.siblings('div').removeClass('expand');
		} else {
			$(this).addClass('expand')
			.children('span').html('- ').end()
			.siblings('div').addClass('expand');
		}
	});
	
/* Search Text Replace on Home Page */

	$('.searchText').one("click", function(){
		$(this).attr('value', '');
	});	
	
/* Drop-Down Menu z-index priority over "select" elements */	
	
	$("#primary_nav li ul").bgIframe();
	
/* Print Function */

	$('.print_icon').click(function(){
		window.print();
	});	
	
/* Hint Pop Up Menu */
	
	$('.hint').click(function(){
		$(this).children('span').addClass('show');
	});
	
	$('.hint a.close').click(
		function(e){
  			e.stopPropagation();
			$(this).parent('span').removeClass();
	});


/* Offices City View Page Tooltip function */
	$('ul.employee_profiles li img').hover(
		function(){
			$(this).parents().siblings('div.profileOverlay').show().children('div').show();
		},
		function(){
			$(this).parents().siblings('div.profileOverlay').hide();
		}
	);

/* To open the links in the same new window*/

	$('.newLink div a').click(function(){
		var location = $(this).attr('href');
		window.open(location, "window_same"); 
		return false;
	})
	
/* Success Stories SupCol Popup links */

	$('.graphLink div a').click(function(){
		var location = $(this).attr('href');
		window.open(location, "_blank", "width=600, height=300, location=no, titlebar=no, toolbar=no,  menubar=no, status=no, scrollbars=no, resizable=no"); 
		return false;
	})

	
		$("#smallAccordion li div:not(#firstCityView)").each(function(){
        if($(this).height() > 400) {
            $(this).css("max-height", "400px");
        }
    });
    

    
    /* City View Page (first office drawer) Functions */
	    if ($("#cityViewWrapper").attr("id") == "cityViewWrapper"){
	// Declare some variables to work with
		var firstSec = $("#cityViewWrapper .accordion :first div");
		var firstSecPs = $(firstSec).children("p");
		var charCount = 0;
		var pIndex = 0;
		
	/* 
	 * Set max number of characters to allow before beginning to hide paragraphs	
	 * I chose this method rather than just hiding all but the first paragraph because
	 * this way, a few small paragraphs can display prior to the "Read more" link, 
	 * or just one large one.
	 */
		while(charCount < 10){
		
		// Get the html content of a paragraph, trim excess whitespace, note length
			var pChars = $(firstSecPs[pIndex]).html();
			pChars = pChars.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
			pChars = pChars.length;
			
		// Add paragraph length to character count	
			charCount += pChars;
			
		// While max character count hasn't been reached yet, set "shownP" as class on paragraph	
			$(firstSecPs[pIndex]).addClass("shownP");
			pIndex++;
		}
		var shownPSet = $(firstSec).children("p.shownP");
		
	// Hide all paragraphs in the first section that don't have the "shownP" class	
		$(firstSec).children("p:not(.shownP)").hide();
		
	// Add a "Read more" link after the last shown paragraph	
		$(shownPSet[shownPSet.length-1]).after('<a href="#" id="showMoreLink">Read more</a>');
	
	// Add a click function to the "Read more" link to show all hidden paragraphs and remove itself
		$("#showMoreLink").click(
			function(){
				$(firstSec).children("p:not(.shownP)").slideDown("fast");
				$(this).remove();
				return false;
			}
		);
		}
/* END City View Page (first office drawer) Functions */
    
    
});