$(document).ready(function(){
	initSearchTeam();
	bgImage = 'team_1.gif';
	x_offset = 630;
  
	//bgImageCheck();
	
  $.ajaxHistory.initialize(function()
  {
	setSearchTeam();
	buildProfileMenu();
  });
});

function bgImageCheck()
{
  if($('#portfolio-menu').css("display") == "block")
  {
	setBgImage(bgImage, x_offset, true);
  }
  else
  {
    setBgImage("", 0);
  }
}

function initSearchTeam()
{
  initProfiles();
  //loading = 0;
  $('.categories/li/.industry, .categories/li/.profiletype').each(function(){
    $(this).click(function(){
    var parent_id = $(this).parent().attr("id");
    var this_id = $(this).attr("id");
    //$(this).history(function(){
      if($('#'+parent_id).attr("class") == "active"){
      //alert("this is active");
        $('#'+parent_id).removeClass("active");
        $('#'+this_id).removeClass("active");
        $('.categories/li, .categories/li/*/li, .categories//a').removeClass("active");
        $('.categories/*/ul').each(function(){
          $(this).hide();
        });
      }
      else
      {
        $('.categories/li, .categories/li/*/li, .categories//a').removeClass("active");
        $('.categories/li/ul').hide();

        $('#'+parent_id).addClass("active");
        $('#'+this_id).addClass("active");
        //alert("adding class active");
        $('#'+parent_id).find('ul').show();
      }  
      initParams();
      buildProfileMenu();
      return false;
    });
  });
  
  $('.categories/li/ul/li/.location').each(function(){
    $(this).click(function(){
      var this_id = $(this).attr("id");
      //$(this).history(function(){
      if($('#'+this_id).attr("class") == "active"){
      //alert("this is active");
        $('#'+this_id).removeClass("active");
        $('#'+this_id).parent().removeClass("active");
        $('.categories/li/ul/li[.location]').removeClass("active");
        $('.categories//a').removeClass("active");
      }
      else
      {
       $('.categories/li/ul/li/.location').removeClass("active");
       $('.categories/li/ul/li[.location]').removeClass("active");
       //$('.categories//a').removeClass("active");
       //$('.categories/li').toggleClass("active");
      $('#'+this_id).addClass("active");
      $('#'+this_id).parent().addClass("active");
      //alert("adding class active");
      }  
      initParams();
      buildProfileMenu();
      
      return false;
    });
  });
  
  
  $('.categories/li/ul/li/ul/li/.profile').each(function(i){
    //$(this).click(function(){

      var profile_id = $(this).parent().attr("id");
      profile_id = profile_id.split("_");
      profile_id = profile_id[1];
      $(this).history(function(){
        if(loading == 0)
        {
        $('.categories/li//.profile').removeClass("active");
        $('.categories/li/ul/li[a.location]').removeClass("active");
        $('.categories/li//.profile:eq('+i+')').addClass("active");
        $('.categories/li//.profile:eq('+i+')').parent().parent().parent().removeClass("active");
        $('.categories/li//.profile:eq('+i+')').parent().parent().parent().addClass("active");
        setSearchTeam(profile_id);
        loadProfile(profile_id);
        }
      //return false;
      });
      
    //});
  });
  /*
  $.ajaxHistory.initialize(function()
  {
    window.location = window.location.pathname;
  });*/
}

function setSearchTeam(profile_id)
{
//alert(profile_id);
/*set state of profile link*/
  $('.categories/li//.profile').removeClass("active");
  $('.categories/li/ul/li[a.location]').removeClass("active");
  $('.categories/li[a.industry]').removeClass("active");
  $('.categories/li[a.profiletype]').removeClass("active");
  $('.categories/li[a]').removeClass("active");
  $('.categories//li[@id = profile_'+profile_id+']/.profile').addClass("active");
  $('.categories//li[@id = profile_'+profile_id+']/.profile').parent().parent().parent().removeClass("active");
  $('.categories//li[@id = profile_'+profile_id+']/.profile').parent().parent().parent().addClass("active");
  
/* show the right part of the menu*/  

//  $('.categories/li/ul').hide();
  $('.categories//li[@id = profile_'+profile_id+']/.profile').parent().parent().parent().parent().parent().addClass("active");

  $('.categories//li[@id != profile_'+profile_id+']/.profile').parent().parent().parent().parent().parent().find('ul').hide();
  $('.categories//li[@id = profile_'+profile_id+']/.profile').parent().parent().parent().parent().parent().find('ul').show();
}

function initParams()
{
  params = "";
  param_industry = "";
  param_profiletype = "";
  param_location = "";
  param_sector = "";
  param_profile = "";
}
function getParams()
{
  $(".profile-sector-active").each(function(){
    var sector_id = $(this).attr("id");
    sector_id = sector_id.split("_");
    sector_id = sector_id[1];
    param_sector = "/sector/" + sector_id;
  });
  
  $(".categories//a.active").each(function(){
    if( $(this).attr("class") == "industry active" )
    {
      var id = $(this).parent().attr("id");
      id = id.split("_");
      id = id[1];
      param_industry += id + ',';
    }
    if( $(this).attr("class") == "profiletype active" )
    {
      var id = $(this).parent().attr("id");
      id = id.split("_");
      id = id[1];
      param_profiletype += id + ',';
    }
    if( $(this).attr("class") == "location active" )
    {
      var id = $(this).parent().attr("id");
      id = id.split("_");
      id = id[1];
      param_location += id + ',';
      
      var id = $(this).parent().children('ul').children('li').attr("id");
      id = id.split("_");
      id = id[1];
      param_profile += id + ',';
    }
  });
  
  // designed to override the ealier one from the left hand menu...
  //
  $(".profile-location-active").each(function(){
      var location_id = $(this).attr("id");
      location_id = location_id.split("_");
      location_id = location_id[1];
      param_location = location_id;
  });
  
  if(param_industry != "")
  {
    param_industry = param_industry.substring(0, param_industry.length -1);
    param_industry = '/industry/' + param_industry;
  }

  if(param_profiletype != "")
  {
    param_profiletype = param_profiletype.substring(0, param_profiletype.length -1);
    param_profiletype = '/profiletype/' + param_profiletype;
  }
  
  if(param_location != "")
  {
    if(param_location.substring(param_location.length -1) == ",")
    {
      param_location = param_location.substring(0, param_location.length -1);
    }
    param_location = '/location/' + param_location;
  }

  if(param_profile != "")
  {
    param_profile = param_profile.substring(0, param_profile.length -1);
    param_profile = '/profile/' + param_profile;
  }
}

function buildProfileMenu()
{
  //alert("building profile menu");
  if(loading == 0)
  {
  loading = 1;
  initParams();
  getParams();
  params = param_industry + param_profiletype + param_location + param_sector;
  //alert(params);

  //$('#portfolio-menu li').attr("class", "");
  
  $.ajax({
    type: "POST",
    url: "/components/index/module_name/team/component/getProfiles" + params,
    beforeSend: function () { urchinTracker( "/components/index/module_name/team/component/getProfiles" + params); },        
    success: function(xmlData){
      // alert(xmlData);
      if($('#portfolio-menu').css("display") != "none")
      {
	    bgImageCheck();
        var currentProfiles = $('#portfolio-menu//li');
        //alert(currentProfiles.length);
        /*if(currentProfiles.length == $(xmlData).find('li').length)
        {
          $(xmlData).find('li').each(function(){
            var profile_id = $(this).find('a').attr("id");
            //alert($('#portfolio-menu//#'+profile_id).attr("class"));
            $('#portfolio-menu//#'+profile_id).removeClass("active");
            if($(this).find('a').attr("class"))
            {
            $('#portfolio-menu//#'+profile_id).addClass($(this).find('a').attr("class"));
            }
          });
        }
        else
        {*/
        $('#portfolio-menu').fadeOut("fast", function(){
          $('#portfolio-menu').html(xmlData);
          isReady = setInterval("readyToShowProfileMenu()", 100);
        });
          //initProfiles();
        //}
      }
      else
      {
      //alert("here");
        $('.four-col .content-wrapper/*').fadeOut("fast");          
        $('.two-col:eq(0)/.content-wrapper/*').fadeOut("fast");
        $('.two-col:eq(0)/.content-wrapper').html("");
        $('.four-col .content-wrapper').html('<ul id="portfolio-menu"></ul><div id="display-profile"></div>');
        $('#portfolio-menu').fadeOut("fast", function(){
          $('#portfolio-menu').html(xmlData);
          isReady = setInterval("readyToShowProfileMenu()", 100);
        });
        $('#display-profile').hide();
        
        //$('#portfolio-menu').fadeIn("fast", initProfiles);
      }
    }
  });
  }
}

function readyToShowProfileMenu()
{
  // return variable
  var imagesloaded = 1;

  // All images are saved in an array called document.images. Very usefull
  var images = document.images;
  // Loop through all the images
  for (var i = 0;i<images.length;i++)
  {
          // If the image isnt loaded we set the return varible to 0
          if(images[i].complete == false) {
                  imagesloaded = 0;
          }
  }

  // This will return 0 if one or more images are not loaded and 1 if all images are loaded.
  if(imagesloaded == 1)
  {
  loading = 0;
  clearInterval(isReady);
//  $('#portfolio-menu').fadeIn("fast", initProfiles);
  $('#portfolio-menu').css("display", "block");
  bgImageCheck();
  initProfiles();
  }
  return imagesloaded;
}

function initProfileLinks()
{
  $('.profile-sector').each(function(){
  $(this).unbind("click");
    $(this).click(function(){
      if(loading==0)
      {
      $(this).removeClass("profile-sector");
      $(this).addClass("profile-sector-active");
      //alert(param_sector);      
      buildProfileMenu();
      }
      return false;
      
    });
  });
  
  $('.profile-location').each(function(){
  $(this).unbind("click");
    $(this).click(function(){
      if(loading==0)
      {
      $(this).removeClass("profile-location");
      $(this).addClass("profile-location-active");
      //alert(param_sector);      
      buildProfileMenu();
      }
      return false;
      
    });
  });
}

function initProfiles()
{
  loading = 0;
  $("#portfolio-menu li a").each(function(){
    /*$(this).click(function(){

    });*/
    if(loading == 0)
    {
    var profile_id = $(this).attr("id");
    //var profile_id = $("#portfolio-menu li a:eq("+i+")").attr("id");
    //alert(profile_id);
    profile_id = profile_id.split("_");
    profile_id = profile_id[1];
    $(this).history(function(){
      setSearchTeam(profile_id);
      loadProfile(profile_id);
    //return false;
    });
    }
  });
}

function loadProfile(profile_id)
{
  if(loading == 0)
  {
    loading = 1;
    if($('#portfolio-menu').css("display") == "block")
    {
     
      $('.two-col:eq(0) .content-wrapper').hide();
//      $('#display-profile').fadeOut("fast", function(){
          //$('.two-col:eq(1) .content-wrapper').before(loader);
          //$('.four-col .content-wrapper').before(loader);
//      });
      $('#display-profile').css("display", "none");
//      $('#portfolio-menu').fadeOut("fast", function(){
	    $('#portfolio-menu').css("display", "none");
		bgImageCheck();
		if(!$('.four-col//.loader').length)
		{
	        $('.four-col').append(loader);
		}
        $.ajax({
          type: "POST",
          url: "/components/index/module_name/team/component/companyList/profile_id/" + profile_id,
          beforeSend: function () { urchinTracker("/components/index/module_name/team/component/companyList/profile_id/" + profile_id); },    
          success: function(xmlData){
            $('.two-col:eq(0) .content-wrapper').html(xmlData);
//    $('.two-col:eq(0) .content-wrapper').show();
            initProfileLinks();
            initCompanyList();
          }
        });
        
        $.ajax({
          type: "POST",
          url: "/components/index/module_name/team/component/profile/profile_id/" + profile_id,
          success: function(xmlData){
            //$('#content').css("height", $('.four-col .content-wrapper').height());
            
            //$('.four-col .content-wrapper').css("border", "solid 1px #aaa");
            
            $('#display-profile').html(xmlData);

            removeLoader();
            $('#content').css("height", "auto");
            initProfileLinks();
            isReady= setInterval("readyToFadeProfile()", 100);
//            $('.two-col:eq(1) .content-wrapper').fadeIn("fast");
            $('.two-col:eq(0) .content-wrapper').css("display", "block");
            initCompanyHide();
          }
        });
//      });
    }
    else
    {
//      $('.two-col:eq(1) .content-wrapper').fadeOut("fast");
      $('.two-col:eq(0) .content-wrapper').css("display", "none");
//      $('#display-profile').fadeOut("fast", function(){
      $('#display-profile').css("display", "none");
		if(!$('.four-col//.loader').length)
		{
	        $('.four-col').append(loader);
		}

        $.ajax({
          type: "POST",
          url: "/components/index/module_name/team/component/companyList/profile_id/" + profile_id,
          beforeSend: function () { urchinTracker("/components/index/module_name/team/component/companyList/profile_id/" + profile_id); },  
          success: function(xmlData){
            $('.two-col:eq(0) .content-wrapper').html(xmlData);
//  $('.two-col:eq(0) .content-wrapper').show();
            initProfileLinks();
            initCompanyList();
          }
        });
        
        $.ajax({
          type: "POST",
          url: "/components/index/module_name/team/component/profile/profile_id/" + profile_id,
          success: function(xmlData){
            $('#display-profile').html(xmlData);
            initCompanyHide();
            initProfileLinks();
			$('#content').css("height", "auto");
			bgImageCheck();
            isReady= setInterval("readyToFadeProfile()", 100);
            //$('#display-profile').fadeIn("slow");
//            $('.two-col:eq(1) .content-wrapper').fadeIn("fast");
            $('.two-col:eq(0) .content-wrapper').css("display", "block");
          }
        });
//      });
    }
  }
}

function readyToFadeProfile()
{
  if(document.images["bio-pic"])
  {
   // if(document.images["bio-pic"].complete == true)
   // {
      //alert("image complete");
      //loading = 0;
      clearInterval(isReady);
      removeLoader();
//      $('#display-profile').fadeIn("fast");
       $('#display-profile').css("display", "block");
    //}
  }
  else
  {
      //loading = 0;
      clearInterval(isReady);
      removeLoader();
//      $('#display-profile').fadeIn("fast");
      $('#display-profile').css("display", "block");
  }
}

function initCompanyHide()
{
	$('.hide-dyn-area').click(function() {
    $('#results/li/a').removeClass("active");
		companyToggle('this');
    setActiveCompany(null);
	});
}

function initCompanyList()
{
  loading = 0;
  $('#results li a').each(function(){
      var company_id = $(this).attr("id");
      company_id = company_id.split("_");
      company_id = company_id[1];
      var this_id = $(this).attr("id");
    /*if(loading == 0)
    {*/
  //  $(this).history(function(){
  $(this).click(function(){
      if(loading == 0)
      {
      $('#results li a').removeClass("active");
      $('#'+this_id).addClass("active");

      
      loadCompany(company_id);
      }
      return false;
    //});
    });
  //}
  });
}

