﻿function preloadBackgrounds()
{
    var images = new Array();
    var imageObj = new Image();

    images[0] = "assets/images/backgrounds/background1.jpg";
    images[0] = "assets/images/backgrounds/background2.jpg";
    images[0] = "assets/images/backgrounds/background3.jpg";
    images[0] = "assets/images/backgrounds/background4.jpg";

    var i = 0;
    for (i = 0; i < images.length; i++) {
        imageObj.src = images[i];
    }
}

$(document).ready(function () {

    if (window.location.href.indexOf("email-us") > 0) {
        $(".social-bookmarks").remove();
    }

    // #mainNav .topnav li:hover
    $("#mainNav ul.topnav>li").hover(function () {
        $(this).children("ul.subnav").stop(true, true).fadeIn(300);
    },
        function () {
            $(this).children("ul.subnav").stop(true, true).fadeOut(300);
        }
    );

    //alternate column cell background colour
    $("table.financial tr").each(function () {

        $(this).children("th:odd").addClass("alt");
        $(this).children("td:odd").addClass("alt");

    });

    //open all external links in new window except the absolute ones on current domain
    $("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function () {
        $(this).attr("target", "_blank").attr("title", "Opens external webpage in a new window");
    });


    //open all pdf documents in a new window and add alt text to links
    $('a[href$=".pdf"]').each(function () {

        //for the toolbar pdf icon
        if ($(this).parent("li").attr("id") == "liCreateThisPagePDF") {
            $(this).attr("title", "Opens PDF of page in a new window");
            $(this).attr("target", "_blank");
        }
        //for all the rest of pdf download links on the site
        else {
            $(this).attr("title", "Opens PDF in a new window");
            $(this).attr("target", "_blank");
        }

    });


    // case studies
    if ($('.casestudy.lightbox').size() > 0) {

        $('.case-study-nav a').live('click', function (e) {
            var myHref = $(this).attr('href');
            $('#cboxLoadedContent').fadeOut(500, function () {
                $('#cboxLoadedContent').load(myHref + " #content", function () {
                    $('#cboxLoadedContent').fadeIn(500);
                    $.colorbox.resize();
                });
            });
            e.preventDefault();
        });


        $('.casestudy.lightbox').click(function (e) {
            var myHref = $(this).attr('href');
            $.colorbox({ href: myHref + " #content", innerWidth: '860px' });
            e.preventDefault();
        });
    }

    //videos
    if ($('#divVideos').size() > 0) {
        $('a.vidLink').colorbox({ innerWidth: '550px', innerHeight: '360px', iframe: true });

    }

    //print basket
    $('#view-basket').colorbox({ innerWidth: '700px', innerHeight: '600px', iframe: true });


    // Sliders

    //Home fader
    if ($("#homeFader").size() > 0) {
        preloadBackgrounds();
        $('#homeFader').ryfader({
            pageSelector: "#fadeControls",
            speed: 200,
            autoPlayInterval: 10000,
            enablePlayStop: true,
            autoPlay: true,
            renderNext: false,
            renderPrevious: false,
            renderPageButtons: true,
            renderPlayButton: false,
            renderStopButton: false,
            renderStateInfo: false,
            beforeFadeFunction: function (container, currentFrameNumber, itemsToMoveBy, opts, direction) {
                var target = currentFrameNumber + (direction == 'next' ? itemsToMoveBy : -itemsToMoveBy);
                $("#fakeOut").fadeIn("normal", function () {
                    $("body").removeClass("bg" + currentFrameNumber).addClass("bg" + target);
                    $(this).fadeOut("slow");
                });
            },
            afterFadeFunction: function () {
                $(".fader_content.active").css("filter", "none");
            }
        });
    }
    // pollFader
    if ($(".pollFader").size() > 0) {
        $('.pollFader').parents('ul').addClass('active');
        $(".pollFader").ryfader({
            speed: 1000,
            autoPlay: false,
            renderNext: true,
            renderPrevious: true,
            renderPageButtons: false,
            renderStateInfo: true,
            waitToFinish: true,
            afterCreateFunction: function (container) {
                $("<span> polls</span>").appendTo(container.siblings(".state_container"));
            }
        });

        $(".pollWrapper ul li h2").wrapInner("<a href='#'></a>").click(function (e) {

            var $me = $(this); // this is the h2, not the a.
            if ($me.parents('ul').find('.fader_wrapper:animated').size() <= 0) {

                $me.parents('li').siblings('li').find('div.fader_wrapper:visible').fadeOut(500, function () {
                    $(this).parent().removeClass('active');
                    $me.siblings('.fader_wrapper').fadeIn(500);
                    $me.parent().addClass('active');
                });
            }
            e.preventDefault();
        });
        $(".fader_wrapper:first").show().parent().addClass('active');
    }

    var requestVar;
    //our performance
    if ($("#performanceMachine").size() > 0) {
        var $pm = $("#performanceMachine");
        $pm.addClass('active');
        $pm.find('.linkToPage').hide();
        $pm.find('h2:first, ul:first, li:first').addClass('active'); // default to first section
        /* may or may not want to do some of these things. easy enough to swap out*/
        var initUrl = $pm.find('ul:first li:first .description .linkToPage a').attr('href');
        requestVar = $('#results').load(initUrl + " #content > *", function () {
            $pm.find('li:first .description').show();
            $('#results').fadeIn(500);
            $(this).parent().removeClass('active');
        });
        $pm.children('h2').wrapInner("<a href='#'></a>").click(function (e) {
            var $me = $(this); // this is the h2, not the a.
            if ($pm.find(':animated').size() <= 0) {
                if (!$me.next('ul').hasClass('active')) {
                    $('#results').fadeOut(500);
                    $me.siblings('ul:visible').fadeOut(500, function () {
                        $me.addClass('active').siblings().removeClass('active');
                        $(this).removeClass('active').children('li.active').removeClass('active').children('.description').hide();
                        $me.next('ul').fadeIn(500, function () {
                            $(this).addClass('active');
                            try { requestVar.abort(); } catch (er) { }
                            var dynHref = $(this).find('.description .linkToPage a').attr('href');
                            requestVar = $('#results').load(dynHref + " #content > *", function () {
                                $('#results').fadeIn(500);
                                $(this).parent().removeClass('active');
                            });
                        }).children('li:first').addClass('active').children('.description').show();
                    });
                }
            }
            e.preventDefault();
        });

        $pm.find('h3').wrapInner("<a href='#'></a>").click(function (e) {
            var $me = $(this); // this is the h3, not the a.
            if ($pm.find(':animated').size() <= 0) {
                $('#results').fadeOut(500);
                $me.parent().siblings('li.active').children('.description').fadeOut(500, function () {
                    var $desc = $(this);
                    $desc.parent().removeClass('active');
                    try { requestVar.abort(); } catch (er) { }
                    var dynHref = $me.parent().find('.description .linkToPage a').attr('href');

                    $me.next('.description').fadeIn(500, function () {
                        requestVar = $('#results').load(dynHref + " #content > *", function () {
                            $('#results').fadeIn(500);
                            $(this).parent().removeClass('active');
                        });

                    }).parent().addClass('active');
                });
            }
            e.preventDefault();
        });

        /**/
    }

    //accordions
    if ($(".accordion").size() > 0) {
        $('.accordion').ryaccordion({
            handle: 'h2.handle'
        });
    }
    // performance
    if ($("#performance").size() > 0) {
        $.getScript('assets/scripts/LandG.performance.js', function () { });
    }

    $(".pollWrapper").removeClass("noJS");
    // performance
    if ($("#pollWrapper").size() > 0) {
        
        $.getScript('assets/scripts/LandG.poll-organizer.js', function () { });
    }


    $(".case-studies-slider").ryslider({ enablePlayStop: true, wrapSlider: true, itemsToDisplay: 3 });

    $(".poll").googlechartspoll();

});
