
// Author: Alex Littlejohn
// Collaborators: 
///////////////////////////////////////////

// constructor
function init() {
    $(".comparison-chart").width(($(".comparison-chart tr.name th").length * 193) + 178);

  contextInit(document);
  
  // tab change inits
  tabChange();
  $(window).bind("hashchange", function (e) {
    e.preventDefault();
      tabChange();
      return false;
  });
    function tabChange() {
        
        try {
            if (location.hash.length < 1 || $(location.hash).length < 1) {
                $(".tab-content-container").children().first().show().siblings().hide();
                $(".tabs li:first").addClass("selected").siblings().removeClass("selected");
        } else {
          $(location.hash).show().siblings().hide();
          $(".tabs li a[href*="+location.hash+"]").parent().addClass("selected").siblings().removeClass("selected");
        }
        } catch (e) {

        }
        standardHeightAdjust();
  }

  /*$("#nav ul li a").click(function () {
      if ($(this).siblings("ul").length > 0) {
          $(this).siblings("ul").slideToggle();
          return false;
      }
  });*/
  
  $("#indicators a.more, #indicators .close-button").click(function(){
    $(".hidden-indicators").slideToggle();
  });
  
  standardHeightAdjust();
  heightAdjust(".home-footer-column-one, .home-footer-column-two, .home-footer-column-three", 0);
  
  $(".search-results li").each(function(){
    var ph = 0;
    var t = $(this).children()
    t.each(function () {
        var h = $(this).height();
        h > ph ? ph = h : ph = ph;
    });
    t.css("height", ph);
  })
  
  $(".unit-column, .staff-column").hover(function(){
    $(this).find(".expanded-content, .staff-expanded-content")
      .css({"display":"block","opacity":0})
      .animate({"margin-top":0, "opacity":1},300)
  },function(){
    $(this).find(".expanded-content, .staff-expanded-content")
      .animate({"margin-top":"112px", "opacity":0},300,function(){
        $(this).css("display","none")
      });
  })
  
  $(".back-button").click(function() {
    history.go(-1);
    return false;
  });
  
  
  $(".asset-management .column ul li a").click(function(){
    $(".asset-management .popup").show().css({"left":$(this).offset().left-8, "top":$(this).offset().top-$(".asset-management .popup").outerHeight(true)+10})
    return false;
  })
  $(".asset-management .popup .close").live("click", function(){
    $(".asset-management .popup").hide();
    return false;
  })
  
  $(".error").live("focus", function(){
    $(this).removeClass("error").val("");
  });
  
  $(".modal .close-button").click(function(){
    $(".modal").hide();
    return false;
  })
    
    initCustomLinkLists();
    
  $(".emailtofriend").click(function(){
    $("#emailform").slideDown();
    return false;
  });
  $("#emailform a.close-button").click(function(){
    $("#emailform").slideUp();
    return false;
  });

}

function initCustomLinkLists() {
  //old type
  jQuery.each($("div.link-list ul a"), function(index, value) {
    while($(value).parent("div.link-list ul").length == 0) {
      $(value).unwrap();
    }
    $(value).wrap("<li />");
  });
  
  //new type
  jQuery.each($(".link-list-data"), function(index, element) {
    var links = $(element).find("a");
    links.wrap("<li />");
    $(element).next("ul").append(links.parent());
  });
}







function newsTicker() {
    var animSpeed = 24;
    //var sp = $("#news-ticker li:first-child").outerWidth(true) * animSpeed;
    var tw = 0;
    $("#news-ticker").children().each(function () {
        tw += $(this).outerWidth(true);
    })

    $("#news-ticker")
    .width(tw)
    .animate({ "marginLeft": 0 - $("#news-ticker li:first-child").outerWidth(true) }, $("#news-ticker li:first-child").outerWidth(true) * animSpeed, "linear", anim)

    function anim() {
        $("#news-ticker")
      .css("marginLeft", 0)
      .append($("#news-ticker li:first-child"))
      .animate({ "marginLeft": 0 - $("#news-ticker li:first-child").outerWidth(true) }, $("#news-ticker li:first-child").outerWidth(true) * animSpeed, "linear", anim)
    }
    function resume() {
        $("#news-ticker")
      .animate({ "marginLeft": 0 - ($("#news-ticker li:first-child").outerWidth(true)) },
        ($("#news-ticker li:first-child").outerWidth(true) * animSpeed) - (($("#news-ticker li:first-child").outerWidth(true) * animSpeed) * (Math.abs(parseInt($("#news-ticker").css("marginLeft"))) / (($("#news-ticker li:first-child").outerWidth(true))))),
        "linear",
        anim
      )
    }

    $("#news-ticker").hover(function () {
        $("#news-ticker").stop()
    }, function () {
        resume()
    })

}


function contextInit(context) {
    $('input[placeholder], textarea[placeholder]').placeholder();

  newsTicker()
      
  var sliderLength = $('#slider-digits li').length;
  var slideWidth = Math.floor($('#slider-digits').width()/sliderLength);
  $('#slider').width((sliderLength-1)*slideWidth);
  $('#slider-digits li').width(slideWidth-1);
  $('#slider',context).slider({
    min: 0,
    max: sliderLength - 1,
    step: 1,
    value: 0,
    slide: function(event, ui) {          
      var t = ui.value;
      $('.product').each(function(){            
        if($('.product').index($(this)) == t){  
          $(this).show().siblings().hide();
          $(this).css({"margin-left":((slideWidth*t)) +"px"});              
        }
      });                
    }
  });
  var mSliderLength = $(".milestone").length;
  var mSlideWidth = Math.floor($('.dates').width()/mSliderLength);
  
  $('#milestones-slider').width((mSliderLength-1)*mSlideWidth);
  
  $('.dates li').width(mSlideWidth-1);
  $('#milestones-slider',context).slider({
    min: 0,
    max: mSliderLength - 1,
    step: 1,
    value: 0,
    slide: function(event, ui) {          
      var t = ui.value;
      $('.milestone').each(function(){            
        if($('.milestone').index($(this)) == t){  
          $(this).show().siblings().hide();
          $(this).css({"margin-left":((mSlideWidth*t)) +"px"});              
        }
      });                
    }
  });

  
  
  
  $("select", context).dropdown();
}

function standardHeightAdjust() {
  var infoTabs = $(".info-tabs");
  var selectors = ".finance + .feature-blocks, .height-management"
    //.products, .link-lists, .home-footer-left, .home-footer-right, .four-column, .contact-content, .generic-content, .search-content, .product-description, .person-detail, .person-image, .landing-feature, .asset-factsheet";
  if (infoTabs.length) {
      heightAdjust(selectors, infoTabs.height());
  } else if ($(".finance").length) {
      heightAdjust(selectors, $(".finance").height());
    } else {
      heightAdjust(selectors, 0);
  }
}

// adjusts all matched elements to the height of the tallest element
function heightAdjust(el, notLessThan) {
    var ph = 0;
    var t = $(el)
    t.each(function () {
    $(this).css("height", "auto");
        var h = $(this).height();
        h > ph ? ph = h : ph = ph;
    });
    if (ph < notLessThan) {
    ph = notLessThan;
    }
    t.css("height", ph);
}




$(document).ready(init);



function showModal(modalvar)
{
    var modal=$(modalvar)
    modal.show();
    
    var modalHeight=modal.height();
    var windowHeight=$(window).height();
    var windowScroll=$(window).scrollTop();
    var top = ((windowHeight-modalHeight)/2) + windowScroll;
    
    var modalWidth=modal.width();
    var windowWidth=$(window).width();
    
    var left=((windowWidth-modalWidth)/2)
    
    modal.css({"top":top, "left":left})
}





