﻿

// this is the interval (in milliseconds) that each image is shown
// this number alwasy has to be higher than (FadeSpeed + 500) to allow the
// overlay time to fade in and out
var FadeInterval = 7500;
// this is the fade speed
// nb this number cannot be less that 500
// as the white overlay needs 500 millisconds to fade out after the image has changed
var FadeSpeed = 750;




var _docHeight = 0;
var _windowHeight = 0;








var hpCurrentPage = 1;

$(document).ready(function () {



    if ($("#productLinksSmall").length > 0) {
        var hpProdL = $("#productLinksSmall .AdvertItem").length;
        $("#productLinksHolder .forward").fadeIn(250);
        var itemWidth = $("#productLinksSmall .AdvertItem:first").outerWidth() + 30;
        var totalWidth = $("#productLinksSmall").parent().parent().innerWidth();
        var visible = Math.ceil(totalWidth / itemWidth);
        var pages = Math.ceil(hpProdL / visible);

        $('#productLinksHolder a.back').hide();
        if (pages > 1) {
            $('#productLinksHolder a.forward').show();
        }

        // 5. Bind to the forward and back buttons
        $('#productLinksHolder a.back').click(function () {
            return hpgotoPage(hpCurrentPage - 1);
        });

        $('#productLinksHolder a.forward').click(function () {
            return hpgotoPage(hpCurrentPage + 1);
        });

        function hpgotoPage(page) {
            var dir = page < hpCurrentPage ? -1 : 1;
            var left = -(itemWidth - 10) * dir * visible; //* n;

            hpCurrentPage = page;

            $("#productLinksSmall").animate({
                "left": "+=" + left
            }, 500, function () {

                if (dir > 0) {
                    $('#productLinksHolder a.back').fadeIn(250);

                    if (hpCurrentPage == pages) {
                        $('#productLinksHolder a.forward').fadeOut(250);
                    }
                }
                else if (dir < 0) {
                    $('#productLinksHolder a.forward').fadeIn(250);

                    if (hpCurrentPage <= 1) {
                        $('#productLinksHolder a.back').fadeOut(250);
                    }
                }
            }
            )


            return false;
        }

    }

    if ($("#ImageSlideShow #ImageSlideHolder .Slide").length > 1) {

        var slides = $("#ImageSlideShow #ImageSlideHolder .Slide");

        slides.each(function () {
            var thisSlide = $(this);

            if ($("a", thisSlide).length > 0) {
                thisSlide.css("cursor", "pointer");
                thisSlide.click(function (e) {
                    e.preventDefault();
                    window.location = $("a", thisSlide).attr("href");
                });
            }
        });

        var div = $("<div id=\"FadeWhitePanel\"><!-- --></div>");
        $("#ImageSlideShow #ImageSlideHolder").append(div);
        //var NumberOfFadeSlides = $("#ImageSlideShow #ImageSlideHolder .Slide").length - 1;
        var NumberOfFadeSlides = slides.length - 1;
        var CurrentSlide = 0;

        slides.css("display", "none");
        slides.eq(CurrentSlide).css("display", "block");
        $("#FadeWhitePanel").fadeOut();

        if (NumberOfFadeSlides > 0) {
            setInterval(function () {
                var thisSlide = CurrentSlide;
                CurrentSlide++;
                if (CurrentSlide > NumberOfFadeSlides) { CurrentSlide = 0; }
                // fade out the current slide
                $("#FadeWhitePanel").fadeIn(FadeSpeed, function () {
                    slides.eq(thisSlide).hide();
                    slides.eq(CurrentSlide).show();
                    $("#FadeWhitePanel").fadeOut(FadeSpeed + 500);
                });

            }, FadeInterval);
        }

    }
    else {
        $("#ImageSlideShow #ImageSlideHolder .Slide:first").show();
    }


    //    // home page adverts
    //    if ($("#topSection #videos .AdvertItem a").length > 0) {


    ////        var img = $("<img src=\"img/playBtnSmallHP.png\"/>");

    ////        $("#topSection #videos .AdvertItem a").html("").append(img).addClass("hpImageButton");
    ////        $("#topSection #videos .AdvertItem a").hover(function () {
    ////            $(this).find("img").attr("src", "img/playBtnSmallHP_over.png");
    ////        }, function () {
    ////            $(this).find("img").attr("src", "img/playBtnSmallHP.png");
    ////        });
    //        var img = $("<img src=\"img/play-btn.png\"/>");
    //        var tDiv = $("<div class=\"v-over\"/>").css("opacity","0.5");

    //        $("#topSection #videos .AdvertItem a").append(tDiv).append(img).addClass("hpImageButton");
    //        //$("#topSection #videos .AdvertItem a").html("").append(tDiv).append(img).addClass("hpImageButton");
    //        $("#topSection #videos .AdvertItem a").hover(function () {
    //            $(this).find(".v-over").css("opacity", "0.7");
    //        }, function () {
    //            $(this).find(".v-over").css("opacity", "0.5");
    //        });

    //    }

    $("#watchInAction a").hover(function () {
        $(this).find("img").attr("src", "img/playBtnLargeGreen.png");
    }, function () {
        $(this).find("img").attr("src", "img/playBtnLargeGreen_off.png");
    });

    _docHeight = document.height || document.body.offsetHeight;
    _windowHeight = document.documentElement.clientHeight
    var theHeight = 0;
    if (_docHeight > _windowHeight) {
        theHeight = _docHeight;
    }
    else {
        theHeight = _windowHeight;
    }




    $("#ModalBackground").css("height", (theHeight + 15) + "px");


    var path = location.href;

    var booSlideDown = false;


    if (path.indexOf(",view-demo") > -1) {
        path = path.substring(0, path.indexOf(","));
        booSlideDown = true;
    }
    else if (path.indexOf(",") > -1) {
        path = path.substring(0, path.indexOf(","));
    }
    // open the parent
    var menu = $("#LHSNav ul li ul li a[href='" + path + "']");
    if (menu.length == 0) {
        $("#LHSNav ul li a[href='" + path + "']").addClass("Open");
    }
    else {
        menu.parent().parent().parent().find("a:first").addClass("Open");
    }

    if ($("#hp-pSlider").length > 0) {

        $("#hp-pSlider").infiniteCarouselWithRollover(23);

    }
    else if ($("#pSlider li").length > 4) {
        $("#pSlider").infiniteCarousel(10);
    }


    $("#LHSNav ul li a[href='" + path + "']").addClass("Selected");



    //$("a.heading").each(function() {
    //    var spn = $("<span>+</span>");
    //    $(this).append(spn);
    //});

    //$(".Open span").text("x").css("text-transform", "lowercase").css("font-weight", "normal");

    $(".Open").parent().children("ul").css("display", "block");

    if (booSlideDown) {
        // scoll the page to the class "ScrollToHere"
        var scrollElement = $(".ScrollToHere:first");
        if (scrollElement.length == 1) {
            setTimeout(function () {
                var scrollPos = scrollElement.offset().top;

                $('html,body')
                .animate({ scrollTop: scrollPos }, 600);

            }, 200);

        }
    }

    //    var topNav = $("#topMenus ul li a[href$='" + parent + "']");
    //    topNav.css("color", "#000");
    //    topNav.parent().css("background-color", "#fff");


    //    $("#topMenus ul li a").hover(
    //        function() {

    //            if ($(this).attr("href").indexOf(parent) == -1) {
    //                $(this).css("color", "#FFF200");
    //            }
    //        },
    //        function() {
    //            if ($(this).attr("href").indexOf(parent) == -1) {
    //                $(this).css("color", "#CCC13B");
    //            }
    //        }
    //    );


    //    $("a.heading span").live("click", function(e) {
    //        e.preventDefault();
    //        var elmt = $(this);
    //        var menu = elmt.parent().parent().children("ul");

    //        if (menu.css("display") == "none") {
    //            menu.slideDown(250);
    //            elmt.text("X").css("text-transform", "lowercase").css("font-weight", "normal");
    //        }
    //        else {
    //            menu.slideUp(250);
    //            elmt.text("+");
    //        }
    //    });

    if ($(".MapLink a").length > 0) {


        $("#ModalPopUpMaps").css("margin-left", "-255px");
        $("#ModalPopUpMaps").css("margin-top", "-255px");

        $(".MapLink a").click(function (e) {

            var elmt = $(this).parent().parent();
            e.preventDefault();

            $("#ModalBackground")
            .css("opacity", "0")
            .css("display", "block")
            .animate({ opacity: 0.5 }, 300, function () {

                var addrs = elmt.find(".address").html().toLowerCase().replace(/<br\/>/g, ",").replace(/<br>/g, ", ");
                var contactDetails = elmt.find(".contactDetails").html();
                var header = elmt.find("h3").html();

                $("#ModalPopUpMaps").fadeIn(500, function () {
                    initialize();
                    showAddress(addrs, header, contactDetails);

                });


            });

        });

        $("#ModalPopUpMaps a.close").click(function (e) {

            e.preventDefault();

            $("#ModalPopUpMaps").fadeOut(300, function () {

                $("#ModalBackground")
                    .animate({ opacity: 0 }, 300, function () {
                        $("#ModalBackground").css("display", "none");
                    });

            });


        });

    }




    $("#topMenus ul li").TopMenuLink();

    var toppath = path.substring(0, path.lastIndexOf("/"));

    if ($("#topMenus ul li a[href$='" + toppath + "/']").length == 0) {
        toppath = toppath.substring(0, toppath.lastIndexOf("/"));
    }

    toppath = toppath.replace("news-item", "news");

    $("#topMenus ul li a[href$='" + toppath + "/']").addClass("Selected");


    $("#topMenus a.Selected").parent().find(".bgTopMenu").css("display", "block");

    $("#topMenus a").hover(function () {

        if ($(this).attr("class") != "Selected") {
            $(this).parent().find(".bgTopMenu").fadeIn();
        }
    }, function () {
        if ($(this).attr("class") != "Selected") {
            $(this).parent().find(".bgTopMenu").stop(true, true).fadeOut();
        }
    });


    $(".smallVid a,#topSection #videos .AdvertItem a").VideoLink();


});

jQuery.fn.TopMenuLink = function() {

    this.each(function() {
        var linkWidth = $(this).children(0).width() + 28;
        var bgdiv = $("<div class=\"bgTopMenu\"  style=\"width:" + linkWidth + "px;\"><img src=\"img/btn-left.gif\"/></div>");

        $(this).prepend(bgdiv);

    });

};

jQuery.fn.VideoLink = function () {

    this.each(function () {



        var img = $("<img src=\"img/play-btn.png\"/>");
        var tDiv = $("<div class=\"v-over\"/>").css("opacity", "0.5");
        var tDivBot = tDiv.clone().addClass("v-over-bot");
        //$(this).html("").append(tDiv).append(img).addClass("hpImageButton");
        var h = $(this).text();



        $(this).html("<span>" + h + "</span>").append(tDiv).append(img).append(tDivBot).addClass("hpImageButton");
        $(this).hover(function () {
            $(this).find(".v-over").css("opacity", "0.7");
        }, function () {
            $(this).find(".v-over").css("opacity", "0.5");
        });








        //        var div = $("<div class=\"videoImage\"></div>");
        //        var img = $("<img src=\"img/playButtonSmall.png\"/>");
        //        div.append(img).css("opacity", "0.3");
        //        $(this).text("").append(div);
        //        $(this).hover(function() {
        //            $(this).find(".videoImage").stop(true, true).animate({ opacity: .8 }, 150);
        //        }, function() {
        //            $(this).find(".videoImage").animate({ opacity: 0.3 }, 700);
        //        });

    });

};

function getScrollY() {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

var map = null;
var geocoder = null;

function initialize() {
    if (GBrowserIsCompatible()) {

        map = new GMap2(document.getElementById("map_canvas"));
        //map.setCenter(new GLatLng(52.061000, 0.191500), 10); // long lat zoom
        geocoder = new GClientGeocoder();

        //map.openInfoWindowHtml(map.getCenter(), "<p>Hiya</p><p><a href=\"http://localhost:2065/GoogleMaps/Default.aspx\">link</a></p>");
        map.setUIToDefault();

        //showAddress("Queen's Business Park, Wilbraham Road, Fulbourn, Cambridge, CB21 5ET, UK")

    }
}

function showAddress(address,header,contactDetails) {
    if (geocoder) {
        geocoder.getLatLng(
                    address,
                    function(point) {
                        if (!point) {
                            alert(address + " not found");
                        } else {
                            map.setCenter(point, 13);
                            var marker = new GMarker(point);
                            map.addOverlay(marker);
                            //marker.openInfoWindowHtml(address.capitalize().replace(/,/g, "<br/>"));
                            marker.openInfoWindowHtml("<img src=\"img/BB-head.png\" align=\"left\" style=\"margin-right:10px\"/><div style=\"float:left;display:block; width:200px;\"><b style=\"display:block;padding-top:10px;\">" + header + "</b><p style=\"font-size:11px;\">" + contactDetails + "</p></div>");

                        }
                    }
                );
    }
}

String.prototype.capitalize = function() {
return this.replace(/(^|\s)([a-z])/g,
    function(m, p1, p2) {
return p1 + p2.toUpperCase();
}); 
    }; 

