/**
 * @depends jquery.min.js
 */


var isAnimating = false;
var isFlashReplaced = false;
var enlarge_interval_id;

function afterLoadFunctions() {

	$(".social-side-slidebox").each(function() {
		$(this).css("height", $(this).height() + "px");
		$(this).css("display", "none");
	});
	
	$(".social-side-header").click(function() { 
		var plustag = $(this).children().eq(0).children(".plus").eq(0);
		var plustagText = plustag.text();
		var toggleMe = $(this).next();
		if(plustagText == '+') {
			plustag.html('&ndash;');
			if(toggleMe.next()) { var toggleMeNext = toggleMe.next(); toggleMeNext.css("border-top", "1px solid #BCBAB9"); }
			toggleMe.show('blind', {}, 500, function(){
				if($(this).next()) {
				    $(this).next().css("border-top", "0px dashed #BCBAB9");
				} 
			});
		} else {
			plustag.html('+');
			if(toggleMe.next()) { var toggleMeNext = toggleMe.next(); toggleMeNext.css("border-top", "1px solid #BCBAB9"); }
			toggleMe.hide('blind', {}, 500, function(){
				if($(this).next()) {
				    $(this).next().css("border-top", "0px dashed #BCBAB9");
				} 
			});
		}
	});

	if($().tinymce) {
		$("#descr,#content").tinymce({
			  script_url : '/js/tinymce/tiny_mce.js',
			  theme : "advanced",
			  plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
			  theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect",
			  theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview",
			  theme_advanced_buttons3 : "hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,forecolor,backcolor",
	//		  theme_advanced_buttons4 : "moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,restoredraft,|,insertfile,insertimage",
			  theme_advanced_toolbar_location : "top",
			  theme_advanced_toolbar_align : "left"
		  });
	}
}

function afterLoad() {
	setTimeout(afterLoadFunctions, 1);
}

$(window).load(function(){
    $(".social-side-slidebox").css("display","none");
	
    if(!(jQuery.browser.msie) || parseInt(jQuery.browser.version) > 6) {

    if(jQuery.browser.mozilla) { $('body').css('overflow-y', 'scroll'); }
    if(jQuery.browser.safari) { 
      $('body').css('overflow-y', 'scroll'); 
/*      $(window).resize(function() {
        $(".fixed-nav").each(function() {
          // allow the nav bar to move while the window is being resized
          // but hide it so it doesn't display other elements
          $(this).css('position','static');
          $(this).hide();

          // fix the nav and reshow
          $(this).css('position','fixed');
          $(this).show();
        });
      });
*/
    }
    
    // prevent the home header from scrolling
    $('.home-content .header').addClass('fixed-header');

    assignHomeActions();

    // scroll to the correct home page if set
    if(typeof startAt != 'undefined' && startAt != '') {
      if(startAt == 'hb') {
        loadHomeAndBuilding();
      } else if(startAt == 'sp') {
        loadSports();
	  } else if(startAt == 'fs') {
        loadFinancialServices();
      } else if(startAt == 'hc') {
        loadHealthcare();
      }
    }
    
  } else {
    
    // start at the correct home page if set
    if(typeof startAt != 'undefined' && startAt != '') {
      if(startAt == 'hb') {
        document.location = '/homeAndBuilding';
      } else if(startAt == 'fs') {
        document.location = '/financialServices';
      } else if(startAt == 'hc') {
        document.location = '/healthcare';
      } else if(startAt == 'sp') {
        document.location = '/sports';
      }
    }
    
    assignImageRollovers();
  }
});

function assignCapabilitiesActions() {
  $('.cap-title').hover(function(){
    $(this).addClass('cap-title-hover');
    var src = $(this).children('.accordian').children('.ro').attr("src");
    $(this).children('.accordian').children('.ro').attr("src", src.replace(/base/, "hover"));
    },
    function(){
      if(!$(this).hasClass('open'))
      {
        $(this).removeClass('cap-title-hover');
        var src = $(this).children('.accordian').children('.ro').attr("src");
        $(this).children('.accordian').children('.ro').attr("src", src.replace(/hover/, "base"));
      }
    }
  );

  $('.cap-title').click(function(){
  if($(this).siblings('.cap-content').css('display') != 'block')
  { $('.cap-block .open').click();
    $(this).addClass('cap-title-hover open').siblings('.cap-content').slideDown();
    $(this).children('a').children('img.minimize').show();
  }
  else
  {  
    var src = $(this).children('.accordian').children('.ro').attr("src");
    $(this).children('.accordian').children('.ro').attr("src", src.replace(/hover/, "base"));
    
    $(this).siblings('.cap-content').slideUp(function(){
      $(this).siblings('.cap-title').removeClass('cap-title-hover open');
    });
    
    $(this).children('a').children('img.minimize').hide();
  }  
    return false;
  });
}

function assignExpertsActions() {

  $('.experts-gallery a').attr('href', function(){
    var href = $(this).attr('href');
    if(href.indexOf('expert') != -1) {
      var arr = href.split('/');
      var name = arr[arr.length - 1];
      href = '/experts/modal/' + name;
    }
    return href;
  });

  $('.portrait').hover(function(){
    $('.portrait .info').stop().removeAttr('style').slideDown();
    //$('.experts-gallery .prev, .experts-gallery .next').stop().removeAttr('style').fadeIn();
  },
  function(){
    //$('.portrait .info').stop().slideUp();
    //$('.experts-gallery .prev, .experts-gallery .next').stop().fadeOut();
  });

  $('.experts-gallery .down').click(function(){  
    if($('.bio-list li:last-child').hasClass('show'))
    {
      $('.bio-list li:first-child').clone(true).insertAfter('.bio-list li:last-child');
      $('.bio-list li:first-child').remove();
    }

     $('.bio-list .show').eq(0).removeClass('show');
     $('.bio-list .show').eq(1).next().addClass('show');

    return false;
  });

  $('.experts-gallery .up').click(function(){
  
    if($('.bio-list li:first-child').hasClass('show'))
    {
      $('.bio-list li:last-child').clone(true).insertBefore('.bio-list li:first-child');
      $('.bio-list li:last-child').remove();
    }

    $('.bio-list .show').eq(2).removeClass('show');
    $('.bio-list .show').eq(0).prev().addClass('show');

    return false;
  });

  $('.bio-list li').click(function(){
    if($(this).hasClass('selected')){return false;}
    if($('.portrait .portrait-link img').size() != 1){return false;} //Still animating

    var start_new_left;
    var end_old_left;

    //Slide from left or right ?
    if($(this).nextAll().is('.selected'))
    {
      start_new_left = -750;
      end_old_left = 750;
    }
    else
    {
      start_new_left = 750;
      end_old_left = -750;
    }

    var cur_src = $('.portrait .portrait-link img').attr('src');
    var cur_img = cur_src.substring(cur_src.indexOf('portraits/') + 10);

    var next_img = $(this).children('a').children('img').attr('src');
    next_img = next_img.substring(next_img.indexOf('thumbs/') + 7);
        
    $('.bio-list li.selected').removeClass('selected');
    $(this).addClass('selected');

    $('.portrait a.portrait-link').attr('href', $(this).children('a').attr('href')+'?keepThis=true&TB_iframe=true&width=835&height='+$(this).children('.info').children('.height').text());    
    $('.portrait .portrait-link img').clone(true).attr('src', cur_src.replace(cur_img, next_img)).appendTo('.portrait .portrait-link').css('left', start_new_left);
    $('.portrait .portrait-link img:first').animate({left: end_old_left}, 750, 'swing', function(){$(this).remove()});
    $('.portrait .portrait-link img:last').animate({left: 0}, 750, 'swing', function(){});
    $('.portrait .info').html($(this).children('.info').html());
    $('.portrait .info a.thickbox').attr('href', $('.portrait a.portrait-link').attr('href'));
    tb_init('.portrait .info .meet a.thickbox');
   /* $('.portrait .info .meet a.thickbox').click(function(){
      return false;
    });*/
    return false;
  });

  $('.experts-gallery .prev').click(function(){
    previousPortrait();
    return false;
  });
 
  $('.experts-gallery .next').click(function(){
    nextPortrait();
    return false;
  });
}

function assignWorkActions() {
  var cur_index = 0;
  var row_width = $('.case-studies-gallery .row').width();
  var client_width = 361;
  var client_height = 250;
  var col_width = 379;
  var num_cols = $('.case-studies-gallery .row .col').size();
  var num_cols_shown = 2;
  var minimized_description_height = 12;
  var maximized_description_height = 150;
  var browser_is_ie = $.browser.msie;
  var minimize_timeout_id;

  $('.case-studies-gallery .row .col:odd').addClass('right');
  $('.case-studies-gallery .row .col:even').addClass('left');
  $('.case-studies-gallery .row .client:odd').addClass('down');
  $('.case-studies-gallery .row .client:even').addClass('up');
  
  $('.case-studies-gallery .client .expanded .enlarge').each(function(){
    var href = $(this).attr('href');
    var orig_href = href;
    if(orig_href != href.replace('page', 'modal'))
    {
      href = href.replace('page', 'modal')
    
      var dimensions_index = href.indexOf('height');
      var dimensions_string = href.substr(dimensions_index);
      
      var dimensions = dimensions_string.split('&');
      var modal_height = parseInt(dimensions[0].split('=')[1]);
      var modal_width = parseInt(dimensions[1].split('=')[1]);
      
      if(browser_is_ie)
      {
        modal_height+=20;
        modal_width-=9;
      }
      else
      {
        modal_height+=5;
        modal_width-=14;
      }
      
      href = href.replace(dimensions_string, 'height='+modal_height+'&width='+modal_width);
      
      $(this).attr('href', href);
    }
  });
  
  $('#more-and-prev a.more').click(function(){
     cur_index++;
     adjustMoreAndPrevLinks();
     $('.case-studies-gallery .row').animate({left: "-="+col_width}, 250, 'linear', function(){
          
      });
      return false;
    });
  
  
  $('#more-and-prev a.prev').click(function(){
     cur_index--;
     adjustMoreAndPrevLinks();
     $('.case-studies-gallery .row').animate({left: "+="+col_width}, 250, 'linear', function(){
      });
      return false;
    });
  
  function adjustMoreAndPrevLinks() {
    $('#more-and-prev').fadeIn();
    
    if(cur_index == 0) {
      $('#more-and-prev a.prev').hide();
      $('#more-and-prev span.prev').show();
    }
    else {
      $('#more-and-prev span.prev').hide();
      $('#more-and-prev a.prev').show();
    }
    
    if(cur_index == num_cols-num_cols_shown){
      $('#more-and-prev a.more').hide();
      $('#more-and-prev span.more').show();
    }
    else {
      $('#more-and-prev span.more').hide();
      $('#more-and-prev a.more').show();
    }
  }
  
  function disableMoreAndPrevLinks() {
    $('#more-and-prev').fadeOut();
  }

  
  $(".case-studies-gallery .client").click(function(){
    if($('#cur').length){return false;}
    $(this).attr('id', 'cur');
  
    if(minimize_timeout_id){clearTimeout(minimize_timeout_id);}
  
    var right_pos = row_width - ((2 + cur_index)*col_width);
    var left_pos = cur_index * col_width;
    var top_pos = 0;
    var bottom_pos = -15;
    
    disableMoreAndPrevLinks();
    
    $(this).parent().clone(true).css('position', 'absolute').css('z-index', '99').attr('id', 'cur_parent').hide().insertAfter('.row .col:last');
    $('#cur_orig').removeAttr('id');
    $(this).attr('id', 'cur_orig'); //#cur got copied in the cloning
    $('#cur').siblings().css('visibility','hidden');

    if($('#cur').hasClass('up')){$('#cur_parent').css('top', top_pos);}
    if($('#cur').hasClass('down')){$('#cur_parent').css('bottom', bottom_pos+'px');}
    
    
    if(cur_index%2 == 0)
    {
      if($('#cur_parent').hasClass('right')){$('#cur_parent').css('right', right_pos);}
      if($('#cur_parent').hasClass('left')){$('#cur_parent').css('left', left_pos);}    
    }
    else
    {
      if($('#cur_parent').hasClass('right')){$('#cur_parent').css('left', left_pos);}
      if($('#cur_parent').hasClass('left')){$('#cur_parent').css('right', right_pos);}      
    }
    
    $('#cur_parent').show();

    var done_once = false;
        
    $('#cur').children(":not('.expanded')").fadeOut(450, function(){
      if(!done_once){
          done_once = true;
          $('#cur_orig').children(":not('.expanded')").hide();
          $('#cur').animate({height: "519px", width: "740px"}, 450, 'linear', function(){
          $('#cur').children('.expanded').fadeIn(450, function(){
          minimize_timeout_id = setTimeout("$('.case-studies-gallery .client .expanded .description .minimize_description img').click();minimize_timeout_id=false;", 10000);
          });
          maximized_description_height = $('.case-studies-gallery #cur .expanded .description .text').height();
        });
      }
    });
    return false;
  });
  
  
  $(".case-studies-gallery .expanded .nav span a").click(function(){
    if($(this).hasClass('viewing')){return false;}
    
    $('.case-studies-gallery #cur .expanded .nav span a').removeClass('viewing');
    $(this).addClass('viewing');
    var url = $(this).attr('href');
    var name = url.substring(url.indexOf('#') +1);
    
    $('#cur .expanded .pages .viewing').fadeOut(450, function(){
      $(this).removeClass('viewing');
      $('#cur .expanded .pages .'+name).fadeIn(450, function(){
        $(this).addClass('viewing');
        
        if($(this).hasClass('has_pdf'))
        {
          $('.case-studies-gallery #cur .expanded .nav .download_pdf').removeClass('hidden').attr('href', $('#cur .expanded .pages .viewing a img').attr('src').replace(/jpg|gif/, 'pdf'));
        }else
        {
          $('.case-studies-gallery #cur .expanded .nav .download_pdf').addClass('hidden');
        }
      });
    });
    
  });
  
  
  function closeExpandedClient(expand_prev_client, expand_next_client)
  {
    if(minimize_timeout_id){clearTimeout(minimize_timeout_id);}
    
    $('#cur .expanded').fadeOut(450, function(){
      $('#cur').animate({height: client_height+"px", width: client_width+"px"}, 450, 'linear', function(){
         $(this).children(":not('.expanded')").fadeIn();
         $(this).removeAttr('id').parent().removeAttr('style');
         $('#cur_parent').remove();
         $('#cur_orig').children(":not('.expanded')").fadeIn(450, function(){
         });
         
         adjustMoreAndPrevLinks();
         if(expand_prev_client || expand_next_client) {
            expandPrevOrNextClient($('#cur_orig'), expand_prev_client, expand_next_client);
          }        
      });
    });  
  }
  
  $(".case-studies-gallery .client:first .expanded .nav a.prev_client").hide();
  $(".case-studies-gallery .client:first .expanded .nav span.prev_client").show();  
  
  $(".case-studies-gallery .client:last .expanded .nav a.next_client").hide();
  $(".case-studies-gallery .client:last .expanded .nav span.next_client").show();
  
  function expandPrevOrNextClient(cur_client, expand_prev_client, expand_next_client)
  {
    var side;
    $('#cur_orig').removeAttr('id');          
    
    if(cur_index%2 == 0)
    {
      if($(cur_client).parent().hasClass('right'))
      {
        side = 'right';
      }
      else
      {
        side = 'left';
      }
    }
    else
    {
      if($(cur_client).parent().hasClass('right'))
      {
        side = 'left';
      }
      else
      {
        side = 'right';
      }
    }
    

    if($(cur_client).hasClass('up'))
    {
      if(expand_next_client)
      {
        $(cur_client).siblings('.down').click();
      }
      else if(expand_prev_client)
      {
        if(side == 'right')
        {
          $(cur_client).parent().prev('.col').children('.down').click();
        }
        else
        {
         cur_index--;
         adjustMoreAndPrevLinks();        
         $('.case-studies-gallery .row').animate({left: "+="+col_width}, 450, 'linear', function(){
            $(cur_client).parent().prev('.col').children('.down').click();
          });
        }
      }
    }
    else if($(cur_client).hasClass('down'))
    {
      if(expand_prev_client)
      {
        $(cur_client).siblings('.up').click();
      }
      else if(expand_next_client)
      {
        if(side == 'right')
        {
         cur_index++;
         adjustMoreAndPrevLinks();        
         $('.case-studies-gallery .row').animate({left: "-="+col_width}, 450, 'linear', function(){
              $(cur_client).parent().next('.col').children('.up').click();  
          });
        }
        else
        {
         $(cur_client).parent().next('.col').children('.up').click();
        }
      }
    }
  }
  
  
  $('.case-studies-gallery .client .expanded .nav a.prev_client').click(function(){
    closeExpandedClient(true,false);
    return false;
  });
  
    
  $('.case-studies-gallery .client .expanded .nav a.next_client').click(function(){
    closeExpandedClient(false,true);
    return false;
  });
  
  
  $('.case-studies-gallery .client .expanded .nav .close').click(function(){
    closeExpandedClient(false,false);
    return false;
  });

  $('.case-studies-gallery .client .expanded .description .expand_description img').click(function(){
    $('.case-studies-gallery #cur .expanded .minimize_description img').show();
    $('.case-studies-gallery #cur .expanded .description').animate({height: maximized_description_height+"px"});
    
    return false;
  });
  
  $('.case-studies-gallery .client .expanded .description .minimize_description img').click(function(){
    if(minimize_timeout_id){clearTimeout(minimize_timeout_id);}
    $('.case-studies-gallery #cur .expanded .minimize_description img').hide();
    $('.case-studies-gallery #cur .expanded .description').animate({height: minimized_description_height+"px"});
    
    return false;  
  });


  $(".case-studies-gallery .client").hover(
      function() {
        var src = $(this).find(".top img").attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/active/, "base");      
          $(this).find(".top img").attr("src", src.replace(/base/, "hover"));
        }
      },
      function() {
        var src = $(this).find(".top img").attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/active/, "hover");
          $(this).find(".top img").attr("src", src.replace(/hover/, "base"));
        }
      }
    );

 $('.case-studies-gallery .client .expanded .nav .click_enlarge').click(function(){
  $('.case-studies-gallery #cur .expanded .viewing .enlarge').click();
  return false;
 });
 
  $('.case-studies-gallery .client .expanded .external_link').click(function(){
    window.open($(this).attr("href"));
  });
 
  $('.case-studies-gallery .client .expanded .nav .download_pdf').click(function(){
    window.open($(this).attr("href")); //Have to do this because all clicks are intercepted and false is returned.
  });
 

 $('.case-studies-gallery .client .expanded .enlarge').click(function(){
  enlarge_interval_id = setInterval("checkModal()", 50); //Busy waiting. Kind of bad/inefficient
 });
}

function assignClientActions(){
	var offsetX = $('#clients').offset().left;
	var offsetY = $('#clients').offset().top;
	
	//console.log(offsetX+', '+offsetY);

	$('#clients').mousemove(function(e){
		var x = e.pageX - offsetX;
		var y = e.pageY - offsetY;
		drawTooltip(x,y);
	   });

	$('#clients').hover(function(){},function(){$('#clients .tooltip').hide();})
	
	$('#clients .show-tooltip').hover(function(e){
		var x = e.pageX - offsetX;
		var y = e.pageY - offsetY;
		var description_class = $(this).attr('class').replace('show-tooltip', '').replace('ro ', '').replace(/\s/, '');
		
		var description_div = $('#clients .client-descriptions .'+description_class);
		
		$('#clients .tooltip').html(description_div.html());
		//$('#clients .tooltip').css('height', description_div.height());
		
		drawTooltip(x,y);
		$('#clients .tooltip').show();
	},
	function(e){
		$('#clients .tooltip').hide();
	});
	
	function drawTooltip(x, y){
		//console.log( x+'px'+','+ y+'px');
		$('#clients .tooltip').css('left', 10+x+'px').css('top', 10+y+'px');
	}
}

function checkModal(){
  if($('#TB_window') == null){

  }
  else{
   clearInterval(enlarge_interval_id);
   var practice = $('#page .content').attr('class').replace('-content content', '');
   var company_name = $('.case-studies-gallery #cur .expanded .company-name').text();
   var cat = $('.case-studies-gallery #cur .expanded .nav .viewing').parent().attr('class');
   $('#TB_window').prepend('<div class="csg-nav '+ practice +'">'+company_name+' | '+cat+' <a href="#" class="close">Close</a></div>');
   $('#TB_window  .close', top.document).click(function(){
    closeModal();
    return false;
  });
  }
}

function meetExpert() {
  $('.portrait-link').click();
}

function closeModal() {
  $('#TB_overlay', top.document).remove();
  $('#TB_window', top.document).remove();
}

function previousPortrait() {
  $('.experts-gallery .up').click();
  $('.bio-list li.selected').prev().click();
}

function nextPortrait() {
  $('.experts-gallery .down').click();
  $('.bio-list li.selected').next().click();
}

function assignHomeActions() {
  $('.nav-hb').click(function(){
    closeCareersContentNews();
    loadHomeAndBuilding();
    return false;
  });

  $('.nav-sp').click(function(){
    closeCareersContentNews();
    loadSports();
    return false;
  });

  $('.nav-hc').click(function() { 
    closeCareersContentNews();
    loadHealthcare();
    return false;
  });

  $('.nav-fs').click(function() {
    closeCareersContentNews();
    loadFinancialServices();
    return false;
  });
  
  assignImageRollovers();
  assignImreLinks();
}

function removeFlash() {
//  if(!isFlashReplaced) {
    $('.home-block').empty();
    $('.home-block').append('<img src="/images/homepage-screenshot.png" alt="IMRE" />');
    
    $('.home-content .header .logo-and-title .title').empty();
    $('.home-content .header .logo-and-title .title').append('<img src="/images/home-header-flash.png" alt="Digital. Advertising. Marketing. Public Relations" />');     
//    isFlashRemoved = true;
//  }
}

function assignImreLinks() {  
  $('.contact-link').click(function(){
    loadContact();
    return false;
  });

  $('.terms-link').click(function(){
    loadTerms();
    return false;
  });
  
  $('.clients-link').click(function(){
    loadClients();
    return false;
  });  
  
  $('.careers-link').click(function(){
    loadCareers();
    return false;
  });
  
  $('.news-link').click(function(){
    loadNews();
    return false;
  });

  $('#open-print').click(function() {
  if(jQuery.browser.safari) { 
      $('body').css('overflow-y', 'scroll'); 
/*        $(".fixed-nav").each(function() {
          // allow the nav bar to move while the window is being resized
          // but hide it so it doesn't display other elements
          $(this).css('position','static');
          $(this).hide();

          // fix the nav and reshow
          $(this).css('position','fixed');
          $(this).show();
        });*/
    }
    $("#print-menu").show();
    $("#open-print").hide();
    return false;
  });
   
  $('#print-experts').click(function() {
    $('#experts').removeClass('hide');
    $('#work').addClass('hide');
    $('#clients').addClass('hide');
    $('#capabilities').addClass('hide');
    $('#expertise').addClass('hide'); 
  });
  
  $('#print-work').click(function() {
    $('#work').removeClass('hide');   
    $('#experts').addClass('hide');
    $('#clients').addClass('hide');
    $('#capabilities').addClass('hide');
    $('#expertise').addClass('hide');     
  });
  
  $('#print-clients').click(function() {
    $('#clients').removeClass('hide');   
    $('#experts').addClass('hide');
    $('#work').addClass('hide');
    $('#capabilities').addClass('hide');
    $('#expertise').addClass('hide');     
  });
    
  $('#print-capabilities').click(function() {
    $('#capabilities').removeClass('hide');   
    $('#experts').addClass('hide');
    $('#work').addClass('hide');
    $('#clients').addClass('hide');
    $('#expertise').addClass('hide');     
  });      
   
  $('#print-expertise').click(function() {
    $('#expertise').removeClass('hide');   
    $('#experts').addClass('hide');
    $('#work').addClass('hide');
    $('#clients').addClass('hide');
    $('#capabilities').addClass('hide');     
  });
  
  $('#print-all').click(function() {
    $('#experts').removeClass('hide');
    $('#work').removeClass('hide');
    $('#clients').removeClass('hide');
    $('#capabilities').removeClass('hide');
    $('#expertise').removeClass('hide');
  });
  
  $('#close-print').click(function() {
    $("#print-menu").hide();
    $("#open-print").show();
    return false;
  });
}

function assignNavScrollLinks() {
  
  $('.scroll-link a').click(function() {
      
    var pageName = $(this).attr('alt');
    var pageDiv = $('#page-'+ pageName);
    var offset = pageDiv.position().top;
    var outerPageOffset = (offset) - 105;

    $('html,body').animate({scrollTop: outerPageOffset}, 1000, 'swing');
      
    return false;
  });
      
  $('.top-link-btn').click(function() {
      
    $('html,body').animate({scrollTop: 0}, 1000, 'swing');
    return false;
  });
}

function assignHomeAndBuildingActions() {

  $('.home-and-building-content a.header-logo').click(function() {
    if(!isAnimating) {
      closeCareersContentNews();
      loadHomeFromHomeAndBuilding();
    }
    return false;
  });
  
  assignNavScrollLinks();
  assignImageRollovers();
  assignImreLinks();
  assignCapabilitiesActions();
  assignExpertsActions();
  assignWorkActions();
  assignClientActions();
  assignNewsActions('/homeAndBuilding/news');
  initThickbox();
}


function assignSportsActions() {

  $('.sports-content a.header-logo').click(function() {
    if(!isAnimating) {
      closeCareersContentNews();
      loadHomeFromSports();
    }
    return false;
  });
  
  assignNavScrollLinks();
  assignImageRollovers();
  assignImreLinks();
  assignCapabilitiesActions();
  assignExpertsActions();
  assignWorkActions();
  assignClientActions();
  assignNewsActions('/sports/news');
  initThickbox();
}




function assignHealthcareActions() {
  $('.healthcare-content a.header-logo').click(function() {
  
    if(!isAnimating) {
      closeCareersContentNews();    
      loadHomeFromHealthcare();
    }
    return false;
  });
  
  assignNavScrollLinks();
  assignImageRollovers();
  assignImreLinks();
  assignCapabilitiesActions()
  assignExpertsActions();
  assignWorkActions();
  assignClientActions();
  assignNewsActions('/healthcare/news');
  initThickbox();
}

function assignFinancialServicesActions() {

  $('.financial-services-content a.header-logo').click(function() {
    if(!isAnimating) {
      closeCareersContentNews();
      loadHomeFromFinancialServices();
    }
    return false;
  });
  
  assignNavScrollLinks();
  assignImageRollovers();
  assignImreLinks();
  assignCapabilitiesActions()
  assignExpertsActions();
  assignWorkActions();
  assignClientActions();
  assignNewsActions('/financialServices/news');
  initThickbox();  
}

function assignImageRollovers() {

    //Preload hover images  
/*    $("img").each(function() {
    
      var src = $(this).attr("src");
      if(src && src.indexOf("base") != -1) {
      
        var img = new Image();
        img.src = src.replace(/base/, "hover");
      }
    });*/
  
    // assign the rollover code
    $(".ro").hover(
      function() {
       
        var src = $(this).attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/active/, "base");      
          $(this).attr("src", src.replace(/base/, "hover"));
        }
      },
      function() {
       
        var src = $(this).attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/active/, "hover");
          $(this).attr("src", src.replace(/hover/, "base"));
        }
      }
    ).mousedown(function() {

        var src = $(this).attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/base/, "hover");
          $(this).attr("src", src.replace(/hover/, "active"));   
        }
        
    }).mouseup(function() {
        
        var src = $(this).attr("src");
        if(src.indexOf('/inactive') == -1) {
          src = src.replace(/base/, "active");
          $(this).attr("src", src.replace(/active/, "hover")); 
        }
    });  
}

function assignNewsActions(newsUrl) {
  // each .article-more link under .article should load content
  $('.article-block .article .article-more').click(function() {
   
    // load the article content 
    var href = $(this).attr('href');
    $('.article-block').empty();
    $('.article-block').addClass('article-ajax');
    $('.article-block').load(href +' .article-block div', null, function() {
      // create the back link
      $(this).append('<div class="article-back"><a class="article-more" href="#">&laquo; Back</a></div>');
      $('.article-back a', this).click(function() {      
      
        // load the list content; note: different selector required.
        $('.article-block').empty();
        $('.article-block').removeClass('article-ajax');
        $('.article-block').load(newsUrl +' .article-block .article', null, function() {
          
          // assign the actions from this function
          assignNewsActions(newsUrl);
        });
        
        return false;
      });
      
    });
    
    return false;
  });
}

function enablePageScroll() {
    $('body').css('overflow-x', 'auto');
}

function disablePageScroll() {
  $('body').css('overflow-x', 'hidden');
}

function assignCCNCloseLink(){
  $('.cc .close a').click(function(){
        closeCareersContentNews();
        return false;
      });
}

function assignCareersAndNewsActions() {

  // each <a> under .career should load content
  $('.cc .career-block .career a').click(function() {
    
    // load the career content
    var href = $(this).attr('href');
    $('.cc .career-block').empty().hide();
    $('.cc .career-block').load(href +' .career-block div', null, function() {
      
      // create the back link
      $(this).append('<div class="career-back"><a class="article-more" href="#">&laquo; Back</a></div>');
      $('.career-back a', this).click(function() {
        $('.cc .career-block').slideUp(750, function(){
          // load the list content; note: different selector required.
          $('.cc .career-block').empty();
          $('.cc .career-block').load('/home/careers .career-block .career', null, function() {
            // assign the actions from this function
            assignCareersAndNewsActions();
            $('.cc .career-block').slideDown(750);
          });
        });
        return false;
      });
      $('.cc .career-block').slideDown(750);
    });
    
    return false;
  });
  
  // each .article-more link under .article should load content
  $('.article-block .article .article-more').click(function() {
   
    // load the article content 
    var href = $(this).attr('href');
    $('.article-block').empty();
    $('.article-block').load(href +' .article-block div', null, function() {
      
      // create the back link
      $(this).append('<div class="article-back"><a class="article-more" href="#">&laquo; Back</a></div>');
      $('.article-back a', this).click(function() {      
      
        // load the list content; note: different selector required.
        $('.article-block').empty();
        $('.article-block').load('/home/news .article-block .article', null, function() {
          
          // assign the actions from this function
          assignCareersAndNewsActions();
        });
        
        return false;
      });
      
    });
    
    return false;
  });
}

function loadCareers() {

  removeFlash();
  
  // scroll to top
  $('html,body').animate({scrollTop: 0}, 1000, 'swing');

  if($('.cc').length > 0) {

    // keep the same content from opening twice
    var onlyClose = false;
    if($('.careers-content').length > 0) { onlyClose = true; }

    // if cc already exists; slide it up and then load with new content
    $('.cc').slideUp(500, function() {
      
      $('.cc').empty();
      if(!onlyClose) {
        $('.cc').load('/home/careers .careers-content', null,  function() {
        
          assignCCNCloseLink();
          assignCareersAndNewsActions();
          $(this).slideDown();
        });
      }
    });
    
  } else {
    
    // cc doesn't exist yet
    $('.page-center').prepend('<div id="careers" class="cc"></div>');
    
    $('.cc').load( '/home/careers .careers-content', null,  function() {
      
      $('.content .header').removeClass('fixed-header');
      $('.content .left-nav').removeClass('fixed-nav');
      
      assignCCNCloseLink();
      assignCareersAndNewsActions();
      $(this).slideDown();
    });
  }
}


function loadContact() {
  
  removeFlash();
  
  // scroll to top
  $('html,body').animate({scrollTop: 0}, 1000, 'swing');

  if($('.cc').length > 0) {

    // keep the same content from opening twice
    var onlyClose = false;
    if($('.contact-content').length > 0) { onlyClose = true; }

    // if cc already exists; slide it up and then load with new content
    $('.cc').slideUp(500, function() {
      
      $('.cc').empty();
      if(!onlyClose) {
        $('.cc').load('/home/contact .contact-content', null,  function() {
        
          assignCCNCloseLink();
          assignCareersAndNewsActions();
          $(this).slideDown();
        });
      }
    });
    
  } else {
    
    // cc doesn't exist yet
    $('.page-center').prepend('<div id="careers" class="cc"></div>');
    
    $('.cc').load( '/home/contact .contact-content', null,  function() {
      
      $('.content .header').removeClass('fixed-header');
      $('.content .left-nav').removeClass('fixed-nav');
      
      assignCCNCloseLink();
      assignCareersAndNewsActions();
      $(this).slideDown();
    });
  }
}

function loadClients() {
  
  removeFlash();
  
  // scroll to top
  $('html,body').animate({scrollTop: 0}, 1000, 'swing');

  if($('.cc').length > 0) {

    // keep the same content from opening twice
    var onlyClose = false;
    if($('.clients-content').length > 0) { onlyClose = true; }

    // if cc already exists; slide it up and then load with new content
    $('.cc').slideUp(500, function() {
      
      $('.cc').empty();
      if(!onlyClose) {
        $('.cc').load('/home/clients .clients-content', null,  function() {
        
          assignCCNCloseLink();
          assignCareersAndNewsActions();
          $(this).slideDown();
        });
      }
    });
    
  } else {
    
    // cc doesn't exist yet
    $('.page-center').prepend('<div id="careers" class="cc"></div>');
    
    $('.cc').load( '/home/clients .clients-content', null,  function() {
      
      $('.content .header').removeClass('fixed-header');
      $('.content .left-nav').removeClass('fixed-nav');
      
      assignCCNCloseLink();
      assignCareersAndNewsActions();
      $(this).slideDown();
    });
  }
}


function loadTerms() {
  
  removeFlash();
  
  // scroll to top
  $('html,body').animate({scrollTop: 0}, 1000, 'swing');

  if($('.cc').length > 0) {

    // keep the same content from opening twice
    var onlyClose = false;
    if($('.terms-content').length > 0) { onlyClose = true; }

    // if cc already exists; slide it up and then load with new content
    $('.cc').slideUp(500, function() {
      
      $('.cc').empty();
      if(!onlyClose) {
        $('.cc').load('/home/terms .terms-content', null,  function() {
        
          assignCCNCloseLink();
          assignCareersAndNewsActions();
          $(this).slideDown();
        });
      }
    });
    
  } else {
    
    // cc doesn't exist yet
    $('.page-center').prepend('<div id="careers" class="cc"></div>');
    
    $('.cc').load( '/home/terms .terms-content', null,  function() {
      
      $('.content .header').removeClass('fixed-header');
      $('.content .left-nav').removeClass('fixed-nav');
      
      assignCCNCloseLink();
      assignCareersAndNewsActions();
      $(this).slideDown();
    });
  }
}

function loadNews() {

  // scroll to top
  $('html,body').animate({scrollTop: 0}, 1000, 'swing');

  if($('.cc').length > 0) {

    // keep the same content from opening twice
    var onlyClose = false;
    if($('.news-content').length > 0) { onlyClose = true; }

    // if cc already exists; slide it up and then load with new content
    $('.cc').slideUp(500, function() {
      
      $('.cc').empty();

      if(!onlyClose) {
        $('.cc').load('/home/news .news-content', null,  function() {

          assignCCNCloseLink();
          assignCareersAndNewsActions();
          $(this).slideDown();
        });
      }
    });
    
  } else {
    
    // cc doesn't exist yet
    $('.page-center').prepend('<div id="careers" class="cc"></div>');
    
    $('.cc').load( '/home/news .news-content', null,  function() {
      
      $('.content .header').removeClass('fixed-header');
      $('.content .left-nav').removeClass('fixed-nav');
      
      assignCCNCloseLink();
      assignCareersAndNewsActions();
      $(this).slideDown();
    });
  }
}

function closeCareersContentNews() {
  $('.cc').slideUp(500,  function(){
    $(this).remove();
    $('.content .header').addClass('fixed-header');
    $('.content .left-nav').addClass('fixed-nav');    
  });
}

function initThickbox(){
  tb_init('a.thickbox, a.open-modal');//pass where to apply thickbox
  imgLoader = new Image();// preload image
  imgLoader.src = tb_pathToImage;
}

function loadHomeFromHealthcare() {
  isAnimating = true;
  disablePageScroll();

  $('#page').append('<div class="page-top"></div>');

  // load home content into new div
  $('#page .page-top').load(
      '/home/index .content',
      null,
      function() {

        removeFlash();
        
        $('.page-top').show();
        
        $('html,body').animate({scrollTop: 0}, 1000, 'swing', function() {
          
          // unfix the position of the header and navbar
          $('.content .header').removeClass('fixed-header');
          $('.content .left-nav').removeClass('fixed-nav');
        
        $('.page-center').animate({top: 1440}, 1500, 'swing', function() {
            $(this).remove();
          });
  
        $('.page-top').animate({top: 0}, 1500, 'swing', function() {
  
            $(this).removeClass('page-top');
            $(this).addClass('page-center');
            
            // prevent the home header from scrolling
            $('.home-content .header').addClass('fixed-header');
            
            assignHomeActions();
            enablePageScroll();
            
            isAnimating = false;
          });
        
        });

      });
}

function loadHomeFromHomeAndBuilding() {
  
  isAnimating = true;
  
  $('#page').append('<div class="page-top"></div>');

  $('#page .page-top').load(
      '/home/index .content',
      null,
      function() {

        removeFlash();
        
        $('.page-top').show();

        $('.content .header').removeClass('fixed-header');
        $('.content .left-nav').removeClass('fixed-nav');      
        
        // start to reset the scroll, but don't wait (unlike on the left/right scrolls)
        $('html,body').animate({scrollTop: 0}, 1000, 'swing');
        
        $('.page-center').animate({top: 1440}, 1500, 'swing', function() {
          $(this).remove();
        });

        $('.page-top').animate({top: 0}, 1500, 'swing', function() {

          $(this).addClass('page-center').removeClass('page-top');
          
          // prevent the home header from scrolling
          $('.home-content .header').addClass('fixed-header');          
          
          assignHomeActions();
          isAnimating = false;
        });
      
      });  
}


function loadHomeFromSports() {
  
  isAnimating = true;
  
  $('#page').append('<div class="page-top"></div>');

  $('#page .page-top').load(
      '/home/index .content',
      null,
      function() {

        removeFlash();
        
        $('.page-top').show();

        $('.content .header').removeClass('fixed-header');
        $('.content .left-nav').removeClass('fixed-nav');      
        
        // start to reset the scroll, but don't wait (unlike on the left/right scrolls)
        $('html,body').animate({scrollTop: 0}, 1000, 'swing');
        
        $('.page-center').animate({top: 1440}, 1500, 'swing', function() {
          $(this).remove();
        });

        $('.page-top').animate({top: 0}, 1500, 'swing', function() {

          $(this).addClass('page-center').removeClass('page-top');
          
          // prevent the home header from scrolling
          $('.home-content .header').addClass('fixed-header');          
          
          assignHomeActions();
          isAnimating = false;
        });
      
      });  
}


function loadHomeFromFinancialServices() {

  isAnimating = true;
  disablePageScroll();
  
  $('#page').append('<div class="page-top"></div>');

  $('#page .page-top').load(
      '/home/index .content',
      null,
      function() {

        removeFlash();
        
        $('.page-top').show();

        $('html,body').animate({scrollTop: 0}, 1000, 'swing', function() {
        
          $('.content .header').removeClass('fixed-header');
          $('.content .left-nav').removeClass('fixed-nav');      
                
        $('.page-center').animate({top: 1440}, 1500, 'swing', function() {
  
            $(this).remove();
          });
  
        $('.page-top').animate({top: 0}, 1500, 'swing', function() {
  
            $(this).addClass('page-center').removeClass('page-top');
            
            // prevent the home header from scrolling
            $('.home-content .header').addClass('fixed-header');            
            
            assignHomeActions();
            enablePageScroll();
            
            isAnimating = false;
          });
        
        });

      });
}

function loadFinancialServices() {

  if(!isAnimating) {
    isAnimating = true;
    disablePageScroll();
    removeFlash();
    
    $('#page').append('<div class="page-bottom"></div>');
  
    $('#page .page-bottom').load(
      '/financialServices/oneColumn .content',
      null,
      function() {
          
        // allow the home header to scroll
        $('.home-content .header').removeClass('fixed-header');        
        
        $('.page-bottom').show();
  
        afterLoad();

          $('.page-center').animate({top: -1440}, 1500, 'swing', function() {
  
          $(this).remove();
        });
  
          $('.page-bottom').animate({top: 0}, 1500, 'swing', function() {
  
          $(this).addClass('page-center').removeClass('page-bottom');
          $('.financial-services-content .header').addClass('fixed-header');
          $('.financial-services-content .left-nav').addClass('fixed-nav');
          
          assignFinancialServicesActions();
          
          enablePageScroll();
          isAnimating = false;
        });
      });
  }
}

function loadHealthcare() {
  
  if(!isAnimating) {
    isAnimating = true;
    disablePageScroll();
    removeFlash();
    
    $('#page').append('<div class="page-bottom"></div>');
    
    $('#page .page-bottom').load(
      '/healthcare/oneColumn .content',
      null,
      function() {
        
        // allow the home header to scroll
        $('.home-content .header').removeClass('fixed-header');        
        
        $('.page-bottom').show();

        afterLoad();

          $('.page-center').animate({top: -1440}, 1500, 'swing', function() {
          
          $(this).remove();
        });
      
          $('.page-bottom').animate({top: 0}, 1500, 'swing', function() {
          
            $(this).addClass('page-center').removeClass('page-bottom');
            $('.healthcare-content .header').addClass('fixed-header');
            $('.healthcare-content .left-nav').addClass('fixed-nav');
                                           
            assignHealthcareActions();
            enablePageScroll();
            isAnimating = false;
        })
      });
  }
}

function loadHomeAndBuilding() {

  if(!isAnimating) {
    isAnimating = true;
    removeFlash();
    
    $('#page').append('<div class="page-bottom"></div>');
  
    // load content into new div
    $('#page .page-bottom').load(
        '/homeAndBuilding/oneColumn .content',
        null,
        function() {
  
          // allow the home header to scroll
          $('.home-content .header').removeClass('fixed-header');
          
         // content is loaded; set visible
          $('.page-bottom').show();
  
          afterLoad();
          
          // animate the center block
          $('.page-center').animate({top: -1440}, 1500, 'swing', function() {

             // animation is complete; remove the old center block
             $(this).remove();
          });
  
          // content is visible; begin animation
          $('.page-bottom').animate({top: 0}, 1500, 'swing', function() {
  
              // animation is complete; set this block as the center block
              $(this).addClass('page-center').removeClass('page-bottom');
  
              // fix the position of the header and navbar
              $('.home-and-building-content .header').addClass('fixed-header');
              $('.home-and-building-content .left-nav').addClass('fixed-nav');           
  
              assignHomeAndBuildingActions();

              isAnimating = false;
          });
        });
  }
}


function loadSports() {

  if(!isAnimating) {
    isAnimating = true;
    removeFlash();
    
    $('#page').append('<div class="page-bottom"></div>');
  
    // load content into new div
    $('#page .page-bottom').load(
        '/sports/oneColumn .content',
        null,
        function() {
  
          // allow the home header to scroll
          $('.home-content .header').removeClass('fixed-header');
          
         // content is loaded; set visible
          $('.page-bottom').show();
  
          afterLoad();
          
          // animate the center block
          $('.page-center').animate({top: -1440}, 1500, 'swing', function() {

             // animation is complete; remove the old center block
             $(this).remove();
          });
  
          // content is visible; begin animation
          $('.page-bottom').animate({top: 0}, 1500, 'swing', function() {
  
              // animation is complete; set this block as the center block
              $(this).addClass('page-center').removeClass('page-bottom');
  
              // fix the position of the header and navbar
              $('.sports-content .header').addClass('fixed-header');
              $('.sports-content .left-nav').addClass('fixed-nav');           
  
              assignSportsActions();

              isAnimating = false;
          });
        });
  }
}
