﻿
$(document).ready(function () {
    /*
    ** Test Mode: true/false
    */
    var _testMode = false;

    if (_testMode) {
        $("#message").css("display", "block");
    }

    /*
    ** Moves sub-menu to its own tree
    */
    $(".minor-left-navigation li").remove();
    $(".main-left-navigation .selected li").detach().appendTo(".minor-left-navigation");

    $(".main-left-navigation .selected").each(function () {
        if ($(this).find("a").length == 0) $(this).remove();
    });

    $(".main-left-navigation .selected ul").remove();

    /*
    ** Ticker initialisation
    */
    if ($("#news-ticker").length != 0) {
        var options = {
            newsList: "#news-ticker",
            startDelay: 10,
            restartDelay: 2000,
            placeHolder1: "_",
            controls: false,
            resumeOffHover: true
        }
        $().newsTicker(options);
    }

    /*
    ** Text label overlay
    */
    $('input[type="text"]').each(function () {
        this.value = $(this).attr("title");
        $(this).addClass("text-label");

        $(this).focus(function () {
            if (this.value == $(this).attr("title")) {
                this.value = "";
                $(this).removeClass("text-label");
            }
        });

        $(this).blur(function () {
            if (this.value == "") {
                this.value = $(this).attr("title");
                $(this).addClass("text-label");
            }
        });
    });


    /*
    ** Some cosmetics
    */
    $(".column-split-1 .sfnewsListItem:last").css("border-bottom", "none");
    $(".column-split-2 .sfnewsListItem:last").css("border-bottom", "none");

    /*
    ** Homepage Extensions
    **
    ** This section is essentially a hack - it allows the main graphic to be clicked to allow linking to the renewables
    ** area of the site. I chose to do this here rather than elsewhere, to make it easier to maintain (without the need
    ** to push a new master page definition into the CMS, and rebuild the sections accordingly.
    */
    $(".main-image-container").css("cursor", "pointer");
    $(".main-image-container").click(function () {
        location.href = "/house-builders/renewable-solutions";
    });

    /*
    ** Quotes
    */
    var quotes = $("#quotes li");
    var activeQuote = Math.floor(Math.random() * ($(quotes).length));

    if ($(quotes).length > 1) {
        $(quotes[0]).removeClass("active-quote");
        $(quotes[activeQuote]).fadeIn("slow", function () {
            $(this).addClass("active-quote");
        });

        setTimeout(rotateQuotes, 10000);
    } else if ($(quotes).length == 1) {
        $(quotes[0]).removeClass("active-quote");

        setTimeout(function () {
            $(quotes[0]).fadeIn("slow", function () {
                $(this).addClass("active-quote");
            });
        }, 1000);
    }

    function rotateQuotes() {
        $(quotes[activeQuote]).fadeOut("slow", function () {
            $(quotes).each(function () {
                $(this).removeClass("active-quote").hide();
            });

            if (activeQuote >= $(quotes).length - 1) {
                activeQuote = -1;
            }

            $(quotes[++activeQuote]).fadeIn("slow", function () {
                $(this).addClass("active-quote");
            });
        });

        setTimeout(rotateQuotes, 10000);
    }

    /*
    ** FAQ rollovers
    */
    var faqsTitle = $(".sflistItemTitle");

    $(faqsTitle).each(function (index) {
        $(this).hover(function () {
            if (!$(this).hasClass("sflistListItemExpandedHover") && !$(this).hasClass("sflistListItemExpanded")) {
                $(this).addClass("sflistListItemExpandedHover");
                $(this).after("<div class=\"sflistItemContentHover\"></div>");
            }
        }, function () {
            $(this).removeClass("sflistListItemExpandedHover");
            $(".sflistItemContentHover").remove();
        });

        $(this).find("a").click(function () {
            $(".sflistItemContentHover").remove();

            if ($(this).parent().hasClass("sflistListItemExpanded")) {
                $(this).parent().removeClass("sflistListItemExpanded");

                $(this).parent().addClass("sflistListItemExpandedHover");
                $(this).parent().after("<div class=\"sflistItemContentHover\"></div>");
            } else {
                $(this).parent().addClass("sflistListItemExpanded");
            }
        });
    });

    /*
    ** News rollovers
    */
    var newsItems = $(".sfnewsListItem");

    $(newsItems).each(function (index) {
        $(this).html("<div class=\"top\">" + $(this).html() + "</div><div class=\"bottom\"></div>");

        $(this).hover(function () {
            if (!$(this).hasClass("selected")) {
                $(this).addClass("selected");
            }
        }, function () {
            $(this).removeClass("selected");
        });
    });

    /*
    ** Document Library rollovers
    */
    var docTitles = $(".document-library-title");
    var docLists = $(".document-library-list");

    $(docTitles).each(function (index) {
        $(this).prepend("<img class=\"document-library-icon\" src=\"/img/icon_expand.png\" alt=\"\" />");

        $(this).click(function () {
            if (!$(docLists[index]).hasClass("selected")) {
                $(docLists[index]).addClass("selected");
                $(this).addClass("selected");

                $(this).find(".document-library-icon").attr("src", "/img/icon_contract.png");
            } else {
                $(docLists[index]).removeClass("selected");
                $(this).removeClass("selected");

                $(this).find(".document-library-icon").attr("src", "/img/icon_expand.png");
            }
        });

        $(this).hover(function () {
            if (!$(docLists[index]).hasClass("selected")) {
                $(this).addClass("active");
                $(this).css("cursor", "pointer");
            }
        }, function () {
            $(this).removeClass("active");
        });
    });

    /*
    ** Map region floats
    */
    var mapRegion = $("#content-map-img");
    var mapRegionAreas = $("#content-map-imagemap area");
    var mapRegionItems = $("#content-map-image-data li.map-image-data");
    var mapRegionFloat = $("#map-image-float");

    var mapRegionRect = new Array();
    var mapRegionCentre = new Array();

    var mapRegionCurrentPopupID = -2;
    var mapRegionPopupVisible = false;
    var mapRegionPopupDelay = 300;

    if (mapRegionAreas.length > 0) {
        $(mapRegionFloat).hide();
        $(mapRegionFloat).css("position", "absolute");
        $(mapRegionFloat).removeClass("hidden");

        $(mapRegionAreas).each(function (index) {
            mapRegionRect[index] = new Array(0, 0, 0, 0);
            mapRegionCentre[index] = new Array(0, 0);

            var coords = this.coords;

            for (var i = 0; i < coords.split(",").length; i++) {
                if (i % 2 == 0) {
                    if (parseInt(coords.split(",")[i]) < mapRegionRect[index][0] || mapRegionRect[index][0] == 0)
                        mapRegionRect[index][0] = parseInt(coords.split(",")[i]);
                    if (parseInt(coords.split(",")[i]) > mapRegionRect[index][2] || mapRegionRect[index][2] == 0)
                        mapRegionRect[index][2] = parseInt(coords.split(",")[i]);
                } else {
                    if (parseInt(coords.split(",")[i]) < mapRegionRect[index][1] || mapRegionRect[index][1] == 0)
                        mapRegionRect[index][1] = parseInt(coords.split(",")[i]);
                    if (parseInt(coords.split(",")[i]) > mapRegionRect[index][3] || mapRegionRect[index][3] == 0)
                        mapRegionRect[index][3] = parseInt(coords.split(",")[i]);
                }
            }

            mapRegionCentre[index][0] = Math.floor(mapRegionRect[index][0] + (mapRegionRect[index][2] - mapRegionRect[index][0]) / 2);
            mapRegionCentre[index][1] = Math.floor(mapRegionRect[index][1] + (mapRegionRect[index][3] - mapRegionRect[index][1]) / 2);

            $(this).hover(function (e) {
                mapRegionCurrentPopupID = index;

                $(mapRegionFloat).css("left", mapRegionCentre[index][0] + $(mapRegion).position().left - 10);

                if ($(mapRegionFloat).height() < 280) {
                    $(mapRegionFloat).css("top", mapRegionCentre[index][1] + $(mapRegion).position().top - 280);
                } else {
                    $(mapRegionFloat).css("top", mapRegionCentre[index][1] + $(mapRegion).position().top - $(mapRegionFloat).height());
                }

                $(mapRegionFloat).html($(mapRegionItems[index]).html());

                window.setTimeout(mapRegionShowPopup, mapRegionPopupDelay);
            }, function (e) {
                mapRegionCurrentPopupID = -2;

                window.setTimeout(mapRegionCheckPopup, mapRegionPopupDelay);
            });
        });

        $(mapRegionFloat).hover(function (e) {
            mapRegionCurrentPopupID = -1;
        }, function (e) {
            mapRegionCurrentPopupID = -2;

            window.setTimeout(mapRegionCheckPopup, mapRegionPopupDelay);
        });

        $(mapRegionItems).each(function () {
            $(this).addClass("hidden");
        });
    }

    function mapRegionShowPopup() {
        if (mapRegionCurrentPopupID > -2) {
            $(mapRegionFloat).fadeIn("slow");
        }
    }

    function mapRegionCheckPopup() {
        if (mapRegionCurrentPopupID > -2) {
        } else {
            $(mapRegionFloat).fadeOut("slow");
        }
    }

    function debugMessage(m) {
        $("#message").html($("#message").html() + "<br />" + m);
    }

    // Move the Map blurb
    //
    if ($("#content-map-blurb").length > 0) {
        $("#content-map-blurb").html($("#content-map-text-area").detach());
    }

    // Flyover parsing (used in Accreditations)
    //
    var flyOutContainer = $(".image-flyout-container");
    var flyOutItems = $(".image-flyout-text");
    var flyOutArea = $(".image-flyout");
    var flyOutCentre = new Array();

    var flyOutCurrentPopupID = -2;
    var flyOutPopupVisible = false;
    var flyOutPopupDelay = 300;

    if ($(flyOutContainer).length > 0) {
        $("#container").append(
            $("<div></div>")
                .attr({ id: "image-flyout-float" })
                .addClass("image-flyout-float")
        );

        var flyOutFloat = $("#image-flyout-float");

        $(flyOutFloat).hide();
        $(flyOutFloat).css("position", "absolute");

        $(flyOutArea).each(function (index) {
            var flyOutImgCache = new Image();
            flyOutImgCache.src = $(flyOutArea).attr("src");

            flyOutCentre[index] = new Array(0, 0);

            $(flyOutContainer[index]).width(Math.max(flyOutImgCache.width, 110));
            $(flyOutContainer[index]).css("padding-right", "10px");

            $(this).hover(function () {
                flyOutCurrentPopupID = index;

                flyOutCentre[index][0] = Math.floor($(flyOutArea[index]).position().left + ($(flyOutArea[index]).width() / 2));
                flyOutCentre[index][1] = Math.floor($(flyOutArea[index]).position().top + ($(flyOutArea[index]).height() / 2));

                $(flyOutFloat).css("left", flyOutCentre[index][0] - 10);
                $(flyOutFloat).css("top", flyOutCentre[index][1] - 220);

                $(flyOutFloat).html($(flyOutItems[index]).html());

                window.setTimeout(flyOutShowPopup, flyOutPopupDelay);
            }, function (e) {
                flyOutCurrentPopupID = -2;

                window.setTimeout(flyOutCheckPopup, flyOutPopupDelay);
            });
        });

        $(flyOutFloat).hover(function (e) {
            flyOutCurrentPopupID = -1;
        }, function (e) {
            flyOutCurrentPopupID = -2;

            window.setTimeout(flyOutCheckPopup, flyOutPopupDelay);
        });

        $(flyOutItems).each(function () {
            $(this).addClass("hidden");
        });

        function flyOutShowPopup() {
            if (flyOutCurrentPopupID > -2) {
                $(flyOutFloat).fadeIn("slow");
            }
        }

        function flyOutCheckPopup() {
            if (flyOutCurrentPopupID > -2) {
            } else {
                $(flyOutFloat).fadeOut("slow");
            }
        }
    }

    /*
    ** Search results pager
    */
    var resultsPager = $(".sf_pagerNumeric");
    var currentPage = parseInt($(".sf_PagerCurrent").html());

    var firstPage = 1;
    var lastPage = parseInt($(".sf_pagerNumeric > a:last").html());
    var previousPage = currentPage - 1;
    var nextPage = currentPage + 1;

    if ($(resultsPager).length > 0) {
        var currentURL = location.href;
        var rootURL = "/search-results/page/";
        var queryString = currentURL.split("?")[1];

        if (previousPage > 0) {
            $(resultsPager).prepend("<a href=\"" + rootURL + previousPage + "/?" + queryString + "\">&lt; Prev</a>");
        }

        if (nextPage <= lastPage) {
            $(resultsPager).append("<a href=\"" + rootURL + nextPage + "/?" + queryString + "\">Next &gt;</a>");
        }
    }
});
