$(document).ready(function () {

 if ($('#insights').length) {
 if ($('#documentTagListComponent ul li').length != 0) {
 $('#newsAndInsightsStatic').hide();
 }
 };


 $('#newsScrollerCarousel').jcarousel({
 auto: 5,
 scroll: 1,
 wrap: "last",
 animation: "slow",
 initCallback: mycarousel_initCallback,
 buttonNextHTML: null,
 buttonPrevHTML: null,
 itemLoadCallback: mycarousel_setActiveControl
 });

 $('#csrScrollerCarousel').jcarousel({
 auto: 4,
 scroll: 1,
 wrap: "last",
 animation: "slow",
 initCallback: mycarousel_initCallback,
 buttonNextHTML: null,
 buttonPrevHTML: null,
 itemLoadCallback: mycarousel_setActiveControl
 });

 $('#awardScrollerCarousel').jcarousel({
 auto: 4,
 scroll: 1,
 buttonNextHTML: null,
 buttonPrevHTML: null,
 wrap: 'circular'
 });

 $(function () {
 $("#recentArticles").tabs({ selected: 0 });
 });

 function mycarousel_initCallback(carousel) {
 $('.jcarousel-control a.carousel-story-link').bind('click', function () {
 carousel.scroll($.jcarousel.intval($(this).text()));
 return false;
 });

 $('#newsScroller-next').bind('click', function () {
 carousel.next();
 return false;
 });

 $('#newsScroller-prev').bind('click', function () {
 carousel.prev();
 return false;
 });
 };

 function mycarousel_setActiveControl(carousel, state) {
 $("div.jcarousel-control ul li a").removeClass("activeJCarouselControl").addClass("inactiveJCarouselControl");
 $("#newsScrollerControl-" + carousel.first + " a").removeClass("inactiveJCarouselControl").addClass("activeJCarouselControl");
 }


 if ($.browser.msie && $.browser.version == "6.0") {
 DD_belatedPNG.fix('#siteStrapline img');
 DD_belatedPNG.fix('.jcarousel-prev');
 DD_belatedPNG.fix('.jcarousel-next');
 DD_belatedPNG.fix('#introComponentAreaRight img');
 DD_belatedPNG.fix('div#accordion h4');
 DD_belatedPNG.fix('div#siteStrapline');
 DD_belatedPNG.fix('ul#sectionNavigation a.nav-selected');
 DD_belatedPNG.fix('div.testimonialComponent');
 DD_belatedPNG.fix('li.ui-state-active h3 a');
 }



 $(function () {
 $("#accordion").accordion({
 header: 'h4.accordionHandle',
 animated: 'false',
 collapsible: true,
 active: false,
 autoHeight: false
 });
 });


 $(function () {
 $("#newsAndInsightsAccordion").accordion({
 header: 'div.newsAndInsightsHandle',
 animated: 'false',
 collapsible: true,
 active: false,
 autoHeight: false
 });
 });


 $("#practiceAreaBodyComponentAreaCentre div.newsAndInsightsComponent div#newsAndInsightsAccordion").accordion("disable");
 $("#practiceAreaBodyComponentAreaCentre div.newsAndInsightsComponent div#newsAndInsightsAccordion div.newsAndInsightsHandle").addClass("ui-state-active").removeClass("ui-state-disabled").removeClass("ui-state-default");
 $("#practiceAreaBodyComponentAreaCentre div.newsAndInsightsComponent div#newsAndInsightsAccordion div.ui-accordion-content").css({
 "display": "block"
 }).addClass("ui-accordion-content-active").removeClass("ui-state-disabled");

 $(function () {
 $("#contactsAccordion").accordion({
 header: 'h4.accordionHandle',
 animated: 'false',
 collapsible: true,
 active: false,
 autoHeight: false
 });
 });

 //$(".peopleSearchOption").hide();
 //$("#searchbySurnameArea").show()

 $("ul#sectionNavigation li a").click(function () {
 $(".peopleSearchOption").hide();
 });

// $("#searchByLocationLink").click(function () {
// $("#sectionNavigation a").removeClass("nav-selected");
// $("a#searchByLocationLink").addClass("nav-selected");
// $("#searchByLocationArea").show();
// return false;
// });

// $("#searchByServiceAreaLinK").click(function () {
// $("#sectionNavigation a").removeClass("nav-selected");
// $("a#searchByServiceAreaLinK").addClass("nav-selected");
// $("#searchByServiceArea").show();
// return false;
// });

// $("#searchBySurnameLink").click(function () {
// $("#sectionNavigation a").removeClass("nav-selected");
// $("a#searchBySurnameLink").addClass("nav-selected");
// $("#searchbySurnameArea").show();
// });

// $("#searchByPartnerLink").click(function () {
// $("#sectionNavigation a").removeClass("nav-selected");
// $("a#searchByPartnerLink").addClass("nav-selected");
// $("#searchbySurnameArea").show();
// });


 $(".peopleSearchSubmit").click(function () {
 //$("#searchbySurnameArea").show();
 return true;
 });


 $.fn.preload = function () {
 this.each(function () {
 $('<img/>')[0].src = this;
 });
 }
 var imgSwap = [];
 $(".photoItem img").each(function () {
 imgUrl = this.src.replace("/grey/", "/thumb/");
 imgSwap.push(imgUrl);
 });
 $(imgSwap).preload();

 $(".photoItem img").hover(function () {
 this.src = this.src.replace("/grey/", "/thumb/");
 },
 function () {
 this.src = this.src.replace("/thumb/", "/grey/");
 });

insertSuperMenus();

 function menuMouseOver(id) {
 $(".nav-parent").not("#topnav_" + id).removeClass("top_nav_selected");
 $(".nav-parent").not("#topnav_" + id).removeClass("top_nav_over");
 $("#topnav_" + id).addClass("top_nav_selected");
 $("#topnav_" + id).next().addClass("top_nav_over");
 }
 function menuMouseOut(id) {
 $("#topnav_" + id).removeClass("top_nav_selected");
 $("#topnav_" + id).next().removeClass("top_nav_over");
 }

 function megaHoverOver() {
 var blnCallShowMenu = true;
 if ($(this).hasClass('top_nav_selected') == false) {
 if ($(this).attr("class") == "sub") {
 var id = parseInt($(this).attr("id").replace("sub_", ""));
 if ($(this).is(':visible') == true) { blnCallShowMenu = false; }
 } else {
 var id = parseInt($(this).attr("id").replace("topnav_", ""));
 }
 if (blnCallShowMenu == true) {
 menuMouseOver(id);
 }
 }

 $(this).find(".sub").stop().fadeTo('fast', 1).show();
 (function ($) {
 //Function to calculate total width of all ul's
 jQuery.fn.calcSubWidth = function () {
 rowWidth = 0;
 //Calculate row
 $(this).find("ul").each(function () { //for each ul...
 rowWidth += $(this).width(); //Add each ul's width together
 });
 };
 })(jQuery);

 if ($(this).find(".row").length> 0) { //If row exists...

 var biggestRow = 0;

 $(this).find(".row").each(function () {	//for each row...
 $(this).calcSubWidth(); //Call function to calculate width of all ul's
 //Find biggest row
 if (rowWidth> biggestRow) {
 biggestRow = rowWidth;
 }
 });

 $(this).find(".sub").css({ 'width': biggestRow }); //Set width
 $(this).find(".row:last").css({ 'margin': '0' }); //Kill last row's margin

 } else { //If row does not exist...

 $(this).calcSubWidth(); //Call function to calculate width of all ul's
 $(this).find(".sub").css({ 'width': rowWidth }); //Set Width

 }
 }

 function megaHoverOut() {
 if ($(this).hasClass('top_nav_selected') == true) {
 var id = parseInt($(this).attr("id").replace("topnav_", ""));
 menuMouseOut(id);
 };

 $(this).find(".sub").stop().fadeTo('fast', 0, function () { //Fade to 0 opactiy
 $(this).hide(); //after fading, hide it
 });
 }

 var config = {
 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
 interval: 100, // number = milliseconds for onMouseOver polling interval
 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
 timeout: 500, // number = milliseconds delay before onMouseOut
 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
 };

 $("ul#topnav li .sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
 $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

 $('a[rel*="external"]').click(function () {
 this.target = '_blank';
 });

 $("input#txtSearchTerm").focus(function (e) {
 if ($(this).val() == "Search") {
 $(this).val("");
 };
 });
 $("input#txtSearchTerm").blur(function (e) {
 if ($(this).val() == "") {
 $(this).val("Search");
 };
 });

 $("div#practiceAreasListComponent").addClass("activePracticeAreasList");
 $(function () {
 $("div#practiceAreasListComponent> ul").each(function () {
 var list = $(this);
 /*var existingSize = $(this).children().size();
 var size = existingSize / 2;*/
 var size = 8;
 var current_size = 0;
 list.children().each(function () {
 if (++current_size> size) {
 var new_list = $("<ul class='right'></ul>").insertAfter(list);
 list = new_list;
 current_size = 1;
 }
 list.append(this);
 });
 });
 });

 $("#corporate-services div#practiceAreasListComponent").addClass("activePracticeAreasList");
 $(function () {
 $("#corporate-services div#practiceAreasListComponent> ul").each(function () {
 var list = $(this);
 /*var existingSize = $(this).children().size();
 var size = existingSize / 2;*/
 var size = 3;
 var current_size = 0;
 list.children().each(function () {
 if (++current_size> size) {
 var new_list = $("<ul class='right'></ul>").insertAfter(list);
 list = new_list;
 current_size = 1;
 }
 list.append(this);
 });
 });
 });


 $("div#documentTagListComponent").addClass("activeDocumentTagList");
 $(function () {
 $("div#documentTagListComponent> ul").each(function () {
 var list = $(this);
 /*var existingSize = $(this).children().size();
 var size = existingSize / 2;*/
 var size = 9;
 var current_size = 0;
 list.children().each(function () {
 if (++current_size> size) {
 var new_list = $("<ul class='right'></ul>").insertAfter(list);
 list = new_list;
 current_size = 1;
 }
 list.append(this);
 });
 });
 });







});
