$(document).ready(function(){
	//right col
	$('.collapsed,.expanded').children('h2')
	.click(function(){
		if($(this).parent().hasClass('expanded'))
		{
			$(this).parent().find('.content').slideUp("fast");
			$(this).parent().removeClass('expanded');
			$(this).parent().addClass('collapsed');
		}
		else
		{
			$(this).parent().find('.content').slideDown("fast");
			$(this).parent().removeClass('collapsed');
			$(this).parent().addClass('expanded');
		}
	}).css('cursor', 'pointer');
	//dont add dropdown stuff to labels in tab section
	$('.relInfoEC h2').css('cursor','').unbind('click');
	
	//services listing page
	$('.serviceslisting .list .expanded .serviceNameLinkArrow > a, .serviceslisting .list .collapsed .serviceNameLinkArrow > a')
	.click(function(){
		if($(this).closest("li").hasClass('expanded'))
		{
			$(this).closest("li").children('ul').slideUp("fast");
			$(this).closest('li').removeClass('expanded');
			$(this).closest('li').addClass('collapsed');
		}
		else
		{
			$(this).closest('li').children('ul').slideDown("fast");
			$(this).closest('li').removeClass('collapsed');
			$(this).closest('li').addClass('expanded');
		}
	}).css('cursor', 'pointer');
	
	//offices
	$('.officeslisting .offices .region').find('.groupStart')
	.click(function(){
		if($(this).parent().hasClass('expanded'))
		{
			$(this).parent().find('ul').slideUp("fast");
			$(this).parent().removeClass('expanded');
			$(this).parent().addClass('collapsed');
		}
		else
		{
			$(this).parent().find('ul').slideDown("fast");
			$(this).parent().removeClass('collapsed');
			$(this).parent().addClass('expanded');
		}
	}).css('cursor', 'pointer');
	
	//////////////////////////////////////////////////////////////////////////////////////////////
	//home
	//////////////////////////////////////////////////////////////////////////////////////////////
	$('.home .ec').find('h2').unbind('click');
	$('.home .ec').children('h2')
	.click(function(){
		if($(this).parent().hasClass('expanded'))
		{
			$(this).parent().children('.content').slideUp("fast", up);
		}
		else
		{
			$(this).parent().children('.content').slideDown("fast", down);
			//hide others
			$('.home .expanded').children('h2').click();
			//hide welcome
			$('.language').filter(".active").click();
			
		}
	}).css('cursor', 'pointer');
	
	function up(){
		$(this).parent().removeClass('expanded');
		$(this).parent().addClass('collapsed');
		$(this).parent().children('.content').css("height", "auto");
	}
	function down(){
		$(this).parent().removeClass('collapsed');
		$(this).parent().addClass('expanded');
		$(this).parent().children('.content').css("height", "auto");
	}
	//////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////
	
	//not() is bugged
	//$('.collapsed,.expanded').find('h2').not('.listing h2').css('border', '3px solid pink');
	
	//tabs
	$('.relatedlinks li').click(function(){
		//collapse all tabs
		$('.relInfoEC .expanded').removeClass('expanded').addClass('collapsed');
		$(this).parent().find('.active').removeClass('active');
		
		//make tab active, expand content
		$(this).addClass('active');
		var name = '.' + $(this).attr('name');
		//if tab is expandAll, expand all content
		if(name == ".expandAll")
		{
			$('.relInfoEC .collapsed').removeClass('collapsed').addClass('expanded');
			$('.relInfoEC').find('.more').find('.more:visible').click();
		}
		else
		{
			$(name).removeClass('collapsed').addClass('expanded');
		}
	}).css('cursor', 'pointer');
});

//find, a:services, label:offices, h2:everything else
function expandAll(obj){
	//.not('.relatedresources'), because when in edit content mode, expandall would 'click' on a ConnectLink
	obj.closest('.expandCollapse').parent().find('.collapsed').not('.relatedresources').find('a:first,h2:first,label:first').click();
	//related resources page data exception
	obj.closest('.expandCollapse').parent().find('.collapsed.relatedresources').find('h2:first').click();
	//for related resources 'more' link
	obj.closest('.expandCollapse').parent().find('.more').find('.more:visible').click();

	if(obj.attr('tagName') == "BODY"){ //profDet popup expandAll only (i think)
		obj.find('.collapsed').find('a:first,h2:first,label:first').click();
		obj.find('.relInfoEC li:last').click(); //prof relinfo tabs
		obj.find('.more').find('.more:visible').click();
	}
}
function collapseAll(obj){
	obj.closest('.expandCollapse').parent().find('.expanded').find('a:first,h2:first,label:first').click();
	//for related resources 'more' link
	obj.closest('.expandCollapse').parent().find('.more').find('.less:visible').click();
}

//expand all print popup
//A shorthand for $(document).ready()
$(function () {
	$('.popupWrapper').each(function () {
		// options
		var distance = 10;
		var time = 100;
		var hideDelay = 150;

		var hideDelayTimer = null;

		// tracker
		var beingShown = false;
		var shown = false;

		var trigger = $('.trigger', this);
		var popup = $('.expandPopup', this).css('opacity', 0);

		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			// stops the hide event if we move from the trigger to the popup element
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
				beingShown = true;
				
				// reset position of popup box
				popup.css({
					top: 70,
					right: 0,
					'z-index': 9999,
					display: 'block' // brings the popup back in to view
				})
				// (we're using chaining on the popup) now animate it's opacity and position
				.animate({
					right: '+=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					// once the animation is complete, set the tracker variables
					beingShown = false;
					shown = true;
				});
			}
		}).mouseout(function () {
			// reset the timer if we get fired again - avoids double animations
			if (hideDelayTimer) clearTimeout(hideDelayTimer);

			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					right: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					// once the animate is complete, set the tracker variables
					shown = false;
					// hide the popup entirely after the effect (opacity alone doesn't do the job)
					popup.css('display', 'none');
				});
			}, hideDelay);
		});
	}).click(function(){
		var trigger = $('.trigger', this);
		var popup = $('.expandPopup', this).css('opacity', 0);
		$([trigger.get(0), popup.get(0)]).mouseout();
	});
});
