﻿$(function () {
    //search text function
    //puts "Search" in the search box, removes it on 
    //click inside box, then replaces it on loss of focus.
    if ($("#header .searchBox .headerSearchText").val() == '' ||
        $.trim($("#header .searchBox .headerSearchText").val()) == '') {
        $("#header .searchBox .headerSearchText").attr("value", "Search");
    }

    $("#header .searchBox .headerSearchText").focus(function () {
        if ($.trim($(this).attr("value")) == "Search") {
            $(this).attr("value", "");
        }
        
        $(this).blur(function () {
            if ($.trim($(this).attr("value")) == '') {
                $(this).attr("value", "Search");
            }
        }
		);
    });
    //end search text function
    //brand bar panel function
    //prepends the panel to the top of the page if JS on
    $(".groupNav").prependTo("body");
    //append the collapse - expand button if JS on.
    $('<div class="panelButton"><a>Part of AXA UK</a></div>').prependTo('.panelUtilities');
    //collapse the brand panel
    $(".groupNav .groupPanel").hide();
    $(".panelButton").click(function () {
        $(this).toggleClass("expanded");
        $(".groupPanel").slideToggle(1000);
    });
    //end brand bar panel function

    //margin adjustments + auto height for any componentContent within the main and a row.
    $(".mainContentColumn .componentRow:not(table)").each(function () {
        var contentHeight = 0;
        var headHeight = 0;
        var ctaHeight = 0;
        $(".componentContent", this).each(function () {
            if ($(this).height() > contentHeight) {
                contentHeight = $(this).height();
            }
        });
        $(".componentHeader", this).each(function () {
            if ($(this).height() > headHeight) {
                headHeight = $(this).height();
            }
        });
        $(".componentCTA", this).each(function () {
            if ($(this).height() > ctaHeight) {
                ctaHeight = $(this).height();
            }
        });

        $(".componentContent", this).css("height", contentHeight);
        $(".componentHeader", this).css("height", headHeight);
        $(".componentCTA", this).css("height", ctaHeight);

        listHeight = $(".component", this).height();
        //listHeight = listHeight + 1;
        $(".ctaLinkList", this).css("height", listHeight);
        //now go through ALL new heights and equalise.
        var finalHeight = $(".component", this).height();
        $(".component", this).css("height", finalHeight);
        //kill off the last margin right

        $(".componentRow .component").css("margin-right", "10px");
        $(".componentRow .component:last-child").css("margin-right", "0px");

    });

    // Template 2 has a drop zone called singleColumnComponentRow, for 4 singleColumnContent components
    // The components heights need balancing
    $(".mainContentColumn .singleColumnComponentRow").each(function () {
        var headerHeight = 0;
        var contentHeightWithFooter = 0;
        var contentHeightWithoutFooter = 0;
        var footerHeight = 0;

        $(".componentHeader", this).each(function () {
            if ($(this).height() > headerHeight) {
                headerHeight = $(this).height();
            }
        });
        $(".singleColumn", this).each(function () {

            if ($(this).find(".componentFooter").length > 0) {
                $(".componentContent", this).each(function () {
                    if ($(this).height() > contentHeightWithFooter) {
                        contentHeightWithFooter = $(this).height();
                    }
                });
            }
            else {
                $(".componentContent", this).each(function () {
                    if ($(this).height() > contentHeightWithoutFooter) {
                        contentHeightWithoutFooter = $(this).height();
                    }
                });
            }
        });
        $(".componentFooter", this).each(function () {
            if ($(this).height() > footerHeight) {
                footerHeight = $(this).height();
            }
        });

        // We need to work out what the heights of the contents should be
        if (contentHeightWithoutFooter < contentHeightWithFooter + footerHeight) {
            contentHeightWithoutFooter = contentHeightWithFooter + footerHeight + 2;  // The + 2 accounts for the missing border

            // This is a hack for IE7 and below because of the box model used
            var browserVersion = parseInt($.browser.version, 10);
            if ($.browser.msie && browserVersion < 8) {
                contentHeightWithoutFooter += 7;
            }
        }
        else {
            contentHeightWithFooter = contentHeightWithoutFooter - footerHeight;
        }

        // All the components will have a header
        $(".componentHeader", this).css("height", headerHeight);

        // Set the height of the component footers, where the component HAS a footer
        $(".componentFooter", this).css("height", footerHeight);

        // Go through each table and set the height of the content depending on whether the table has a footer or not
        $(".singleColumn", this).each(function () {

            if ($(this).find(".componentFooter").length > 0) {
                $(".componentContent", this).css("height", contentHeightWithFooter);
            } else {
                $(".componentContent", this).css("height", contentHeightWithoutFooter);
            }
        });

    });

    // we need to line up the four elements of the single component row
    $(".mainContentColumn.right .singleColumnComponentRow .ctaSingleColumn").last().css("margin-right", "0px");
    $(".mainContentColumn.right .singleColumnComponentRow .ctaSingleColumn").last().css("margin-left", "2px");


    $(".contentWrapper").find("table tbody").each(function () {
        $(this).find("tr:even").addClass("row");
        $(this).find("tr:odd").addClass("altRow");
        $(this).find("tr:last").addClass("lastRow");
    });

    //end margins
    //hero images' + spotlight components' fancy curved dropshadow:
    $(".hero").each(function () {
        var dshadow = $(this).width();
        $(this).addClass("shadow-" + dshadow);
    });
    //end of hero images
    //video click through play button
    var vidIcon = $('<span class="videoPlay"><span>Video Link</span></span>');
    $(".componentContent .videoThumbWrapper a").prepend(vidIcon);
    //end add vid icon
    //accordion control - show hide sections of in page content
    $(".mainContentColumn .accordion .component .tabContent").addClass("hidden");

    //$(".mainContentColumn .accordion .component .tabContent:first").toggleClass("hidden");

    // Bug 34225 accordion should begin collapsed - $(".mainContentColumn .accordion .tab:first h4").addClass("expanded");
    $(".mainContentColumn .accordion .tab h4").click(function () {
        $(this).parents(".component").find(".tabContent").toggleClass("hidden");
        $(this).toggleClass("expanded");
    });
    //end accordion 
    //carousel
    function promoSlideShow() {
        var images = window.promoBanner;
        var numberOfImages = images.length;
        if (!images || (images.length <= 1)) return;
        var timer = images.TransitionTime || 3000;
        var index = 0;
        var link = $(".promoBanner a");
        var image = $("img", link);
        var loadingImage = new Image();
        var timerID = null;
        var dots = null;
        dotBar();
        var inTransition = false;
        image.load(function () {
            $(this).show()
        });

        function imageLoaded() {
            dots.removeClass('bannerDotActive').eq(index).addClass('bannerDotActive');
            link.css("background-image", 'url(' + loadingImage.src + ')');

            image.fadeOut(timer, function () {
                image.attr({ alt: images[index].alt, title: images[index].alt, src: loadingImage.src });
                link.attr({ title: images[index].alt, href: images[index].href });
                if (timerID != null) {
                    timerID = setTimeout(function () { advanceBanner(); }, timer);
                }
                inTransition = false;
            });

        }

        function imageFail() {
            inTransition = false;
            advanceBanner();
        }
        loadingImage.onload = imageLoaded;
        loadingImage.onerror = imageFail;

        function dotBar() {
            var strDots = '<div class="bannerNavigation"><a class="action prevSlide" href="#"><span>Previous</span></a>';
            for (i = 0; i < numberOfImages; i++) {
                strDots += '<a href="#" data-slideIndex="' + i + '" class="bannerDot' + (i == 0 ? ' bannerDotActive' : '') + '">' + (i + 1) + '</a>';
            }

            strDots += "<a class='action nextSlide' href='#'><span>Next</span></a></div>";
            //$(strDots).appendTo($('.promoBanner'));
            $('.promoBanner').after(strDots);
            dots = $('.bannerNavigation .bannerDot').click(function () {
                if (this.className.match(/active/i)) return false;
                advanceBanner($(this).attr("data-slideIndex") * 1, true);
                return false;
            });
        }

        function advanceBanner(movement, isAbsolute) {
            if (inTransition) return false;
            inTransition = true;
            if (!movement && isNaN(movement)) movement = 1;
            if (isAbsolute) {
                //imageLoaded.direction = index < movement;
                index = movement;
            }
            else {
                index = ((index + (images.length + movement)) % images.length);
            }
            loadingImage.src = images[index].src;
            //dots.removeClass('carouselDotActive').eq(index).addClass('carouselDotActive');
        }

        $('.prevSlide,.nextSlide').click(function () {

            advanceBanner(this.className.match(/prevSlide/i) ? -1 : 1);
            return false;
        });

        $(".promoBanner, .bannerNavigation").mouseenter(function () {
            clearTimeout(timerID);
            timerID = null;
        })
	.mouseleave(function () {
	    timerID = setTimeout(function () { advanceBanner(); }, timer);
	});

        timerID = setTimeout(function () { advanceBanner(); }, timer);
    }

    if (window.promoBanner) {
        promoSlideShow();
    }


    //end carousel functionality


    //sitemap show hide
    $(".mainSitemap ul li.parent").each(function () {
        var plusimg = $('<a class="plus"><img src="assets/cssimages/components/sitemap-1-plus.gif" alt="expand level"/></a>');
        $(this).prepend(plusimg);
        $("ul", this).addClass("hidden");
        $("a:nth-child(2)", this).addClass("falsePlus");
        $("a.plus", this).click(function () {
            $(this).parent().find("ul").toggleClass("hidden");
            $(this).toggleClass("plus");
            $(this).parent().find("a:first").toggleClass("active");
        });
    });

});

