$(document).ready(function() {
	var h = $('#bio').height();
	$('#bio').css("height", h);
});

function setActiveCompany(companyIndex)
{
  $('#results li').attr("class", "");
  $('#company_'+companyIndex).addClass("active");
}

function loadCompany(companyIndex)
{
//alert("loading company");
    /*
    if(window.location.hash)
    {
    alert(window.location.hash);
    }*/
    if(loading == 0)
    {
      setActiveCompany(companyIndex);
      if($('#dyn-area').css("display") == "none")
      {
	    bgImageCheck();
        loading = 1;
        $.ajax({
          type: "POST",
          url: "/components/index/module_name/team/component/companyContent/company_id/" + companyIndex,
          beforeSend: function () { urchinTracker(  "/components/index/module_name/team/component/companyContent/company_id/" + companyIndex); }, 
          success: function(xmlData){
            //var content = company[companyIndex];
            $('#dyn-content').html(xmlData);
            //$('#dyn-area .dyn-header h2').html($('#company-logo').attr("title"));
            $('#dyn-area .dyn-header h2').html($('#companyTitle').html());
  			    //alert($('#dyn-height').height());
            companyToggle();
          }
        });
      }
      else
      {
        loading = 1;
        //$('#dyn-area #dyn-content').fadeOut("slow", function(){
        /*$('#dyn-area .portfolio').fadeOut("slow", function(){*/
        //removeLoader();
        /*
        $('.dyn-header').fadeOut("slow");
        $('#dyn-content').fadeOut("slow", function(){*/
        $('.dyn-header/h2').hide();
        $('#dyn-content').hide();
//          $('#dyn-area').append(loader);
          $.ajax({
            type: "POST",
            url: "/components/index/module_name/team/component/companyContent/company_id/" + companyIndex,
            beforeSend: function () { urchinTracker( "/components/index/module_name/team/component/companyContent/company_id/" + companyIndex); },          
            success: function(xmlData){
              //var content = company[companyIndex];
              $('#dyn-content').html(xmlData);
//              $('#dyn-area .dyn-header h2').html($('#company-logo').attr("title"));
			  $('#dyn-area .dyn-header h2').html($('#companyTitle').html());
              
              isReady= setInterval("readyToFade()", 100);
             // getContentHeight();
              /*
          		$('#dyn-area').animate(
          		{
          		height:contentHeight + 80
          		}
          		, 500, function(){
                  $('#dyn-area .portfolio').fadeIn("slow");
                }
              );
              */
            }
          });
        //});
        //$('#dyn-area #dyn-content').fadeIn("slow");
      }
    }
}

function readyToShowDynamicArea()
{
  if(document.images["company-logo"])
  {
    if(document.images["company-logo"].complete)
    {
      //alert("complete");
//      removeLoader();
      clearInterval(isReady);
      loading = 0;
      showDynArea();
    }
  }
  else
  {
    //alert("no company-logo found");
//    removeLoader();
    clearInterval(isReady);
    loading = 0;
    showDynArea();
  }
}

function readyToFade()
{
  if(document.images["company-logo"])
  {
    if(document.images["company-logo"].complete)
    {
      //alert("complete");
      clearInterval(isReady);
      loading = 0;
      getContentHeight();
      //adding on the 80 for the company logo manually
      //
	  /*
	  $('#content').animate(
        {
        height:contentHeight+110+miniBioHeight
        }
        , 500);*/
      $('#dyn-area').css('height', 'auto');
      $('#dyn-area').animate(
        {
        height:contentHeight + 110
        }
        , 500, function(){
              //removeLoader();
/*            $('#dyn-area .portfolio/*').fadeIn("slow");*/
//      $('.dyn-header').fadeIn("slow");
//      $('#dyn-content').fadeIn("slow");
	 $('.dyn-header/h2').css("display", "block");
     $('#dyn-content').css("display", "block");
//	 $('.dyn-header/h2').show();
//    $('#dyn-content').show();
          }
      );
      
    }
  }
  else
  {
    //alert("no company-logo found");
    clearInterval(isReady);
    loading = 0;
    getContentHeight();
     $('#dyn-area').css('height', 'auto'); 
    $('#dyn-area').animate(
      {
      height:contentHeight+110
      }
      , 500, function(){
//      removeLoader();
/*          $('#dyn-area .portfolio/*').fadeIn("slow");*/
      /*$('.dyn-header').fadeIn("slow");
      $('#dyn-content').fadeIn("slow");*/
	  $('.dyn-header/h2').css("display", "block");
	  $('#dyn-content').css("display", "block");
//        $('.dyn-header/h2').show();
//       $('#dyn-content').show();
        }
    );
  }
}

function getContentHeight()
{
/*  contentHeight = $('.portfolio').height();*/
  //$('#dyn-area').css ('height', 'auto');
  $('.dyn-header').css("display", "block");
  $('#dyn-content').css("display", "block");
  
  contentHeight = parseInt($('.dyn-header').height()) + parseInt($('#dyn-content').height());// + parseInt($('#company-logo').height());
  //alert(contentHeight);
  minContentHeight = parseInt($('#content').css("min-height"));
  miniBioHeight = $('#bio').height();
  minHeight = minContentHeight - miniBioHeight;
  
  /*
  alert("miniBioHeight = " + miniBioHeight);
  alert("minHeight - miniBioHeight = " + minHeight - miniBioHeight);
  alert("contentHeight = " + contentHeight);
  */
  if(contentHeight > minHeight)
  {
    contentHeight = contentHeight;
  }
  else
  {
    contentHeight = minHeight;
  }
   /*
  $('.dyn-header').css("display", "none");
  $('#dyn-content').css("display", "none");
     */
  //alert("contentHeight = " + contentHeight);
  return contentHeight;
}

function companyToggle(){
	if($('#dyn-area').css("display") == "block")
	{
		hideDynArea();
	}
	else
	{
/*
    $('#bio').css("borderBottom", "4px solid #E9E5DE");
    $('#bio').css("borderBottom", "4px solid #fff");
    $('.six-col').css("background", "#E9E5DE");
    $('#bio').css("background", "#fff");
    $('.six-col').find("h2").css("background", "#fff");*/
  $('#bio').css("marginBottom", "12px");

  $('#bio').after('<div id="false-border"></div>');
  /*$('#dyn-area').css("height", "auto");
  $('#dyn-area').css("borderTop", "4px solid #7A84A2");
  $('#dyn-area').css("borderBottom", "4px solid #7A84A2");*/
  $('#dyn-area').addClass("display");

  /*
    will need to set this each time a biography is loaded
    */
    
  bioPic_height = $('#bio-pic').height();
  bio_height = $('#bio').height();
  contentWithBio = $('#content').height();
  
  /*
  $('#bio-details').fadeOut("fast");
  $('.bio-text').fadeOut("fast");
  */

  $('#bio/*[@id!=mini-bio]').fadeOut("slow");
  /*
  $('#bio-pic').animate(
    {
    height:112
    }
    , 500);
    */
    
  getContentHeight();

  $('#bio').animate(
    {
    height:$('#mini-bio').height()
    }
    , 500, function(){
      loading = 1;
//      $('#false-border').after(loader);
      isReady = setInterval("readyToShowDynamicArea()", 100);
      
    });
  }
};

function showDynArea() {
  getDynAreaOffset();
  $('#mini-bio').fadeIn("slow");
  
  $('#content').css("height", "auto");
  //$('#dyn-area').css('height', 'auto');
  $('#dyn-area').fadeIn("fast");
  
};

function hideDynArea(){
//alert(window['bio_height']);
//  var contentWithBio = $('#content').height();
  //alert(contentWithBio);
   $('#dyn-area').fadeOut("fast",
   $('#content').animate(
  {
  height:contentWithBio
  }
  ,500, function(){
  //getContentHeight(); 

    /*
		$('#bio-pic').animate(
		{
		height:window['bioPic_height']
		}
		, 500);
    */
    $('#mini-bio').fadeOut("slow", function(){
  		$('#bio').animate(
  		{
  		height:window['bio_height']
  		}
  		, 500, function(){
          //$('#bio/*[@id != mini-bio]').fadeIn("slow");
          $('#full-bio').fadeIn("slow");
              $('#bio').css("border-bottom", "none");
              $('#false-border').remove();
              $('#bio').css("marginBottom", "0px");
        });
      });
	})
  );

}

function getDynAreaOffset(){

  if(window['topAdj'] == undefined)
  {
  /* 
  	set the dynamic area display to block so it's height and offset attributes are accessible
  	*/
  	$('#dyn-area').css("display", "block");
    $('#dyn-area').css("height", "auto");
   //alert($('#dyn-area .portfolio').height());
  	/*
  	Get the footer offset values
  	*/
  	footerOffset = {};
  	$('#footer').offset({scroll:false}, footerOffset);

    //alert(footerOffset.top);
    
  	/*
  	Get the dynamic area offset values
  	*/
  	offset ={};
  	$('#dyn-area').offset({scroll:false}, offset);
    
    footerTop = footerOffset.top;
  	targetHeight = footerOffset.top - (offset.top + 4);
    //alert(targetHeight);
    //alert(offset.top);
    
    /*
  	Adjust height to stretch to the bottom of the page
  	*/
  	$('#dyn-area').css({height: targetHeight});
  	//$('#dyn-area').find('.portfolio').css({height: targetHeight-8});
  	
  	targetY = footerOffset.top - $('#dyn-area').height();
  	topAdj = targetY - (offset.top + 4); /*+4 to account for the border added to the biography area*/
  	
  	 	
  	/*
  	Adjust to stay aligned to the bottom of the page
  	*/
    //alert(topAdj);
  	$('#dyn-area').css({position:"relative", top: topAdj});

  	/*
  	hide dynamic area again...
  	*/
  	$('#dyn-area').css("display", "none");

  	return offset;
  }
}
