jQuery(document).ready(function() {
	//if(jQuery('.shareBox .linksBox ul li').length)shareshowHide();
	if(jQuery('.commonTopicsWideCol').length)relatedTopicsExpandCollapse();
	if(jQuery('.widerColtoolList .colInnerContent .rows').length)calculatorShowHide();
	if(jQuery('.smallerColtoolList .colInnerContent .rows').length)toolsShowHide();
	if(jQuery('.goalPlannerList .colInnerContent .rows').length)goalPlannerList();
	if(jQuery('.carouselContainer #hmCarousel').length) carosuelLoadv1();
	if(jQuery('.carouselContainerv2 #hmCarousel2').length) carosuelLoadv2();
	if (jQuery(".addthis_toolbox li").length) reloadaddThis1();
	if (jQuery(".benHide").length) quitBens();
	if (jQuery(".Carousel").length) carosuelShowhide();
	
	
});
// --- Global Show/Hide function for all show links
function globalShowAll(targetLinks,targetObj){
		jQuery(targetLinks).click(function(e){
		e.preventDefault();
		if ( jQuery(this).hasClass('collapse'))
			{
				jQuery(this).removeClass("collapse").addClass('expand');
				jQuery(targetObj).toggleClass('visible');
				jQuery(this).text('Show all');
			}
			else {			
				jQuery(this).removeClass("expand").addClass("collapse");
				jQuery(targetObj).toggleClass('visible');
				jQuery(this).text('Collapse');
			}        
		});
}

// --- M102-R3 Module ToolList V1 show/hide function

function calculatorShowHide(){
  jQuery('.widerColtoolList').each(function() {
  var rowList=jQuery(this).find('.rows');
  var rowsCount=jQuery(rowList).length;
  var targetLinks=jQuery(this).find("a.showLink");
  var parentlinkItem=jQuery(this).find('.showHide');
  var defaultrowCount=0;
  if(rowsCount<=2){
   jQuery(parentlinkItem).css('display','none');
  }
  else{
   jQuery(parentlinkItem).css('display','block');
   globalShowAll(targetLinks,rowList)
   jQuery(rowList).each(function(){
		 if(defaultrowCount>1){
		  jQuery(rowList).eq(defaultrowCount).addClass('hidden');
		 }
		 defaultrowCount++
	 });
	}
});
}

// --- M102-R3 Module ToolList V2 show/hide function
function goalPlannerList(){
	var goalList=jQuery('.goalPlannerList .colInnerContent .rows');
	var goalrowsCount=jQuery(goalList).length;
	var targetLinks=jQuery(".goalPlannerList a.showLink");
	var parentlinkItem=jQuery('.goalPlannerList .colInnerContent .showHide');
	var defaultrowCount=0;
	if(goalrowsCount<=2){
		jQuery(parentlinkItem).css('display','none');
	}
	else{
		jQuery(parentlinkItem).css('display','block');
		globalShowAll(targetLinks,goalList)
		jQuery(goalList).each(function(){
				if(defaultrowCount>1){
					jQuery(goalList).eq(defaultrowCount).addClass('hidden');
				}
				defaultrowCount++
		});
	}
}
// --- M102-R3 Module tool list show/hide function
function toolsShowHide(){
	jQuery('.smallerColtoolList').each(function(){
	var toolList=jQuery(this).find('.rows');
	var toolsrowsCount=jQuery(toolList).length;
	var targetLinks=jQuery(this).find("a.showLink");
	var parentlinkItem=jQuery(this).find('.showHide');
	var defaultrowCount=0;
	if(toolsrowsCount<=1){
		jQuery(parentlinkItem).css('display','none');
	}
	else{
		jQuery(parentlinkItem).css('display','block');
		globalShowAll(targetLinks,toolList)
		jQuery(toolList).each(function(){
				if(defaultrowCount>0){
					jQuery(toolList).eq(defaultrowCount).addClass('hidden');
				}
				defaultrowCount++
			});
	}
	});
}

//Home page Carousel
//sorgalla.com/jcarousel/
function carousel_initCallback(carousel) {
		var getContainerId=carousel.options.id;
		var numScollsItem =parseInt(carousel.options.scroll);
		var getcarouselPagination=jQuery(".hmPagination");
        //carousel.options.scroll = jQuery.jcarousel.intval(numScollsItem);
		jQuery(getcarouselPagination).text("");
        var noLi = jQuery(getContainerId).find('li').length;
        var requiredLi = Math.ceil(noLi / numScollsItem);
        for (var i = 0; i < requiredLi; i++) {
            var count = i + 1;
            var liSrting = "<li>" + count + "</li>";
            jQuery(getcarouselPagination).append(liSrting);
        }
        if (noLi <= numScollsItem) {
            jQuery(getcarouselPagination).hide();
        }
        else {
            jQuery(getcarouselPagination).show();
        }
        jQuery(getcarouselPagination).children(":first").addClass("selected");
        jQuery(".jcarousel-next").click(function() {
            jQuery(getcarouselPagination).find("li.selected").next().addClass("selected");
            jQuery(getcarouselPagination).find("li.selected:last").prev().removeClass("selected");
        })
        jQuery(".jcarousel-prev").click(function() {
            jQuery(getcarouselPagination).find("li.selected:last").prev().addClass("selected");
            jQuery(".hmPagination li.selected").next().removeClass("selected")
        })

        jQuery('.hmPagination li').each(function() {
            jQuery(this).bind("click", function() {
                jQuery('.hmPagination li').removeClass("selected");
                jQuery(this).addClass("selected");
                var noClick = parseInt(jQuery(this).text());
	            var remain = noLi % numScollsItem;
	            if (jQuery(this).text() == "1") {
                    carousel.scroll(jQuery.jcarousel.intval(1))
                }
                if (remain == 0) {
                    var ulLi = jQuery("ul.pagination li").length
                    var scrollNo = ((noClick - 1) * numScollsItem) + 1
                    carousel.scroll(jQuery.jcarousel.intval(scrollNo))
                } else {
                    var scrollNo = ((noClick - 1) * numScollsItem) + 1
                    carousel.scroll(jQuery.jcarousel.intval(scrollNo))
                }
            })
        });
    }

//directory template Carousel
function carosuelLoadv1(){
	if($('#hmCarousel li').size() > 6){
		jQuery(".carouselContent .showHide").show();
		var hmCarousel = document.getElementById("hmCarousel");
	    if (hmCarousel) {
	        jQuery(hmCarousel).jcarousel({
	            scroll: 6,
				id:"#hmCarousel",
	            initCallback: carousel_initCallback
	        });
	        var getWidth = jQuery(hmCarousel).width();
	        var setWidth = getWidth + 200;
	        jQuery(hmCarousel).width(setWidth);
			// set height of <a> to be equal - if more than one line length
			$('#hmCarousel > li > a').equalHeight();
		}
    } else {
		jQuery('.carouselContent p.showHide').remove();
		jQuery('#hmCarousel > li > a').equalHeight();
	}
}

// hub template carousel
function carosuelLoadv2(){
	if($('#hmCarousel2 li').size() > 5){
		jQuery(".carouselContent .showHide").show();
	    var hmCarousel2 = document.getElementById("hmCarousel2");
	    if (hmCarousel2) {
	        jQuery(hmCarousel2).jcarousel({
	            scroll: 5,
				id:"#hmCarousel2",
	            initCallback: carousel_initCallback
	        });
	        var getWidth = jQuery(hmCarousel2).width();
	        var setWidth = getWidth + 200;
	        jQuery(hmCarousel2).width(setWidth);
			// set height of <a> to be equal - if more than one line length
			$('#hmCarousel2 > li > a').equalHeight();
	    }
	}
	else {
		jQuery('.carouselContent p.showHide').remove();
		jQuery('#hmCarousel2 > li > a').equalHeight();
	}
}

function reloadaddThis1(){
    var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
	var addthis_brand = "Bupa.co.uk";
	var addthis_header_color = "#0099ff";
    jQuery.getScript(script);
}

function quitBens() {
	jQuery(".benHide").hide();
	jQuery(".timeQuitBenefits").click(function() {
		jQuery(this).next().toggle();
	});
}
// --- collapse/expand for common topic module show all links
function relatedTopicsExpandCollapse(){
	if(jQuery(".listContentCollapsed").length){
		jQuery(".listContentCollapsed").removeClass("hide");
	}
	if(jQuery(".listContentExpand").length){
		jQuery(".listContentExpand").addClass("hide");
	}
	jQuery(".commonTopicsWideCol p.showHide").css('display','block');
	jQuery(".commonTopicsWideCol p.showHide a").click(function(){
			var currentStatus = jQuery(this).attr("class");	
			if(currentStatus == "expand"){
				 jQuery(this).attr("class","collapse");
				 jQuery(".listContentCollapsed").addClass("hide");
				 jQuery(".listContentExpand").removeClass("hide");
				 jQuery(".commonTopicsWideCol p.showHide a").text("Collapse");

			}
			else{
				 jQuery(this).attr("class","expand");
				 jQuery(".listContentCollapsed").removeClass("hide");
				 jQuery(".listContentExpand").addClass("hide");
				 jQuery(".commonTopicsWideCol p.showHide a").text("Show all");
			}
				 return false;
	   });
}

function carosuelShowhide(){
	jQuery(".carouselContent .showHide").show();
	jQuery(".carouselContent .showHide a").click(function(e){
		e.preventDefault();
		var targetElm=jQuery(e.target);
		var targetedElem=jQuery(targetElm).parents().parent().find("div.Carousel");
		if(jQuery(targetElm).hasClass("expand")){
			 jQuery(targetedElem).find(".jcarousel-skin-tango").before("<div class='expandedCarousel'><ul></ul></div>");
			 jQuery(targetedElem).find(".jcarousel-skin-tango li").each(function(){
				 var elementLi=jQuery(this).html();
				 jQuery(targetedElem).find(".expandedCarousel ul").append("<li>"+elementLi+"</li>");
			 });
			 jQuery(targetedElem).find(".jcarousel-skin-tango").hide();
			 jQuery(targetedElem).find("ul.hmPagination").hide();
			 jQuery(e.target).removeClass("expand").addClass("collapse")
			 jQuery(e.target).html("Collapse");
	 
		}
		 else{
		   jQuery(targetElm).parents().parent().find("div.expandedCarousel").remove();
		   jQuery(targetedElem).find(".jcarousel-skin-tango").show();
		   jQuery(targetedElem).find("ul.hmPagination").show();
		   jQuery(e.target).removeClass("collapse").addClass("expand")
		   jQuery(e.target).html("Show all");
	   
			}
	});
 }
 
/* equal height plug-in that checkes height of objects 
   sent to it and returns all objects with the largest height in px */
jQuery.fn.equalHeight = function()
{
	var maxSize = 0;
	
	// iterate over each object and get tallest size
	this.each(function()
 	{
		var currentSize = $(this).height();
		if( currentSize > maxSize)
		{
			maxSize = currentSize;
		}
	});

	// return the objects with the height set to the same size
	return $(this).height(maxSize);
};

