﻿
Cufon.replace('.innovators .home h2');
Cufon.replace('.subcontentquote');

$(document).ready(function() {
    // Home news ticker to display
    $(".home_content_ticker ul").css('display', 'block');

    // Set the configuration for the first set of carousels
    $('.content_carousel').jCarouselLite({
        btnPrev: ".slideshow_prev",
        btnNext: ".slideshow_next",
        visible: 1,
        circle: true,
        auto: null, // time in ms before the slide
        speed: 1000, // the speed of the slide
        start: 0
    });


    if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
        $('.slide ul').addClass('noopacity');
    }

    // Cycle images for sub content quotes

    $('#subContentFade, #homeContentFade').cycle({
        fx: 'fade',
        speed: 1000
    });

    $('#subContentFade2').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 10000
    });

    $('.slideshow').cycle({
        fx: 'fade',
        speed: 1000
    });

    // Initialise Accordians

    $(".accordion").accordion({ autoHeight: false });


    // START LOADING EXTERNAL CONTENT IN DIVS

    $('.ajaxload').click(function() {

        function loadContent() {
            $(displayLocation).load(toLoad, '', showNewContent());
        }

        function showNewContent() {
            $(displayLocation).fadeIn('normal');
        }
        return false;

        var displayLocation = '#' + $(this).attr('rel');
        // We use '>' to make sure we only get the children of the <div> not the <div> itself
        var toLoad = $(this).attr('href') /* + ' ' + displayLocation + ' >'*/;

        $(displayLocation).fadeOut('fast', loadContent);



    });

    // END LOADING EXTERNAL CONTENT IN DIVS 

    // START BACKGROUND COLOR CHANGE 

    $('.home_secondary_3col').hoverIntent(function() {
        var activePanel = $(this).attr('id');
        $(this).addClass(activePanel + '_active');
        slidePanelUp(activePanel, '.home_slidepanel_3col');
    },
	function() {
	    var activePanel = $(this).attr('id');
	    $(this).removeClass(activePanel + '_active');
	    slidePanelDown(activePanel, '.home_slidepanel_3col');
	});
    $('.home_secondary').hoverIntent(function() {
        var activePanel = $(this).attr('id');
        $(this).addClass(activePanel + '_active');
        slidePanelUp(activePanel, '.home_slidepanel_3col');
    },
	function() {
	    var activePanel = $(this).attr('id');
	    $(this).removeClass(activePanel + '_active');
	    slidePanelDown(activePanel, '.home_slidepanel_3col');
	});

    $('.home_secondary_4col, .home_secondary_4col_b').hoverIntent(function() {
        var activePanel = $(this).attr('id');
        $(this).addClass(activePanel + '_active');
        slidePanelUp(activePanel, '.home_slidepanel_4col');
    },
	function() {
	    var activePanel = $(this).attr('id');
	    $(this).removeClass(activePanel + '_active');
	    slidePanelDown(activePanel, '.home_slidepanel_4col');
	});

    function slidePanelUp(activePanel, activePanelClass) {
        $('#' + activePanel + "_holder " + activePanelClass).animate({ top: '-165px' }, 'slow', 'swing');
    }

    function slidePanelDown(activePanel, activePanelClass) {
        $('#' + activePanel + "_holder " + activePanelClass).animate({ top: '0px' }, 'slow', 'swing');
    }

    // END BACKGROUND COLOR CHANGE

    // SET THE HOMEPAGE COLS TO BE EQUAL HEIGHT

    function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }

    equalHeight($(".home_secondary_3col"));
    equalHeight($(".home_secondary"));
    equalHeight($(".home_secondary_4col"));
    equalHeight($(".home_secondary_4col, .home_secondary_4col_b, .home_secondary_4col_last"));
    equalHeight($(".home_secondary_panel1, .home_secondary_panel2, .home_secondary_panel3, .home_secondary_panel4"));

    $('.cssdropdown').hover(
      function() {
          $('.cssdropdown ul li ul').slideToggle('medium');
          $('.cssdropdown').parent().parent().parent().css({ 'z-index': '1000' });
      },
      function() {
          obj = this;
          $('.cssdropdown ul li ul').slideToggle('medium');
      }
    );

    // Revised dropdown jQuery script

    $('.hover-menu').hoverIntent(function() {

        var direction = "";
        $(this).find('ul ul').css('margin-top', '0px');

        var direction = $(this).attr("class");

        if (direction == "hover-menu up") {
            // var subMenuVerticalOffset = $(this).find('ul ul').height() + $(this).find('ul').height();
            var subMenuVerticalOffset = $(this).find('ul ul').height() + 24;
            $(this).find('ul ul').css('margin-top', '-' + subMenuVerticalOffset + 'px');
        }

        $('#home_secondary_1_holder').css({ 'z-index': '3000' });
        $(this).find('ul ul').fadeIn('fast');
    },
    function() {
        $(this).find('ul ul').fadeOut('fast');
    });


    // Country select menu

    $('.countryselectbutton').hoverIntent(function() {
        var subMenuVerticalOffset = $('.countryselect').height();

        $(this).find('.countryselect').css('margin-top', '-' + subMenuVerticalOffset + 'px');

        $('#home_secondary_2_holder').css({ 'z-index': '3000' });
        $(this).find('.countryselect').fadeIn('fast');
    },
    function() {
        $(this).find('.countryselect').fadeOut('fast');
    });

    // Country select menu 2 (Global Ops page)

    $('.countryselectbutton2').hoverIntent(function() {
        var subMenuHorizontalOffset = ($('.countryselect').width() / 2);

        $(this).find('.countryselect').css('margin-left', '-' + subMenuHorizontalOffset + 'px');
        $('.countryselect').css('margin-top', '20px');
        $('#home_secondary_2_holder').css({ 'z-index': '3000' });
        $(this).find('.countryselect').fadeIn('fast');
    },
    function() {
        $(this).find('.countryselect').fadeOut('fast');
    });

});

jQuery.fn.liScroll = function(settings) {
    settings = jQuery.extend({
        travelocity: 0.07
    }, settings);
    return this.each(function() {
        var $strip = jQuery(this);
        $strip.addClass("newsticker");
        var stripWidth = 50;
        var $mask = $strip.wrap("<div class='mask'></div>");
        var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");
        var containerWidth = $strip.parent().parent().width(); //a.k.a. 'mask' width

        $strip.find("li").each(function(i) {
            stripWidth += jQuery(this, i).outerWidth(); // thanks to Michael Haszprunar
            $strip.width(stripWidth);
        });
        $strip.width(stripWidth);
        
        var totalTravel = stripWidth + containerWidth;
        var defTiming = totalTravel / settings.travelocity; // thanks to Scott Waye		
        function scrollnews(spazio, tempo) {
            $strip.animate({ left: '-=' + spazio }, tempo, "linear", function() { $strip.css("left", containerWidth); scrollnews(totalTravel, defTiming); });
        }
        scrollnews(totalTravel, defTiming);
        $strip.hover(function() {
            jQuery(this).stop();
        },
				function() {
				    var offset = jQuery(this).offset();
				    var residualSpace = offset.left + stripWidth;
				    var residualTime = residualSpace / settings.travelocity;
				    scrollnews(residualSpace, residualTime);
				});
    });
};

function printSharePrice() {

    if (window.hour) {
        // Get feed time
        if (hour > 12) {
            var hsTempAMPM = "PM";
        } else {
            var hsTempAMPM = "AM";
        }
        var hsTempTime = hour + ":" + min + hsTempAMPM;
        // Get current time
        now = new Date();
        var rbTempHours = now.getHours();
        if (rbTempHours > 12) {
            var rbTempAMPM = "PM";
        } else {
            var rbTempAMPM = "AM";
        }
        rbTempHours = rbTempHours.toString();
        if (rbTempHours.length == 1) {
            rbTempHours = "0" + rbTempHours;
        }
        var rbTempMinutes = now.getMinutes();
        rbTempMinutes = rbTempMinutes.toString();
        if (rbTempMinutes.length == 1) {
            rbTempMinutes = "0" + rbTempMinutes;
        }
        var rbTempSeconds = now.getSeconds();
        rbTempSeconds = rbTempSeconds.toString();
        if (rbTempSeconds.length == 1) {
            rbTempSeconds = "0" + rbTempSeconds;
        }
        var rbTempTime = rbTempHours + ":" + rbTempMinutes + rbTempAMPM;
        document.write("<div class='homeshareprice'>");
        document.write("<span class='homesharepriceheading'>Share price</span><br>");
        document.write("<span class='homesharepricelive'>" + prices + "<br />");

        if (change > 0) {
            document.write("+");
        }

        document.write("" + change + "p</span>");


        document.write("</div>");
        document.write("<div class='homesharedetails'>");
        document.write("Date: " + dateddmmmyyyy + "<br>");
        document.write("Time now: " + rbTempTime + "<br>");
        document.write("Trade time: " + hsTempTime + "<br>");
        document.write("</div>");
    }
}

function sharePricePopUp(theURL, winName, features) { //v2.0
    window.open(theURL, winName, features);
}

function printSharePricePopUp() {
    // Get feed time
    if (hour > 12) {
        var hsTempAMPM = "PM";
    } else {
        var hsTempAMPM = "AM";
    }
    var hsTempTime = hour + ":" + min + hsTempAMPM;
    // Get current time
    now = new Date();
    var rbTempHours = now.getHours();
    if (rbTempHours > 12) {
        var rbTempAMPM = "PM";
    } else {
        var rbTempAMPM = "AM";
    }
    rbTempHours = rbTempHours.toString();
    if (rbTempHours.length == 1) {
        rbTempHours = "0" + rbTempHours;
    }
    var rbTempMinutes = now.getMinutes();
    rbTempMinutes = rbTempMinutes.toString();
    if (rbTempMinutes.length == 1) {
        rbTempMinutes = "0" + rbTempMinutes;
    }
    var rbTempSeconds = now.getSeconds();
    rbTempSeconds = rbTempSeconds.toString();
    if (rbTempSeconds.length == 1) {
        rbTempSeconds = "0" + rbTempSeconds;
    }
    var rbTempTime = rbTempHours + ":" + rbTempMinutes + rbTempAMPM;
    document.write("<span class='sharepricepopupprice'>" + prices + "</span><br>");
    if (change < 0) {
        document.write("<img src='../UploadedResources/rb_sharepricepopup_arrow_down.gif' alt='down arrow' style='margin-right:5px;'>");
    } else if (change > 0) {
        document.write("<img src='../UploadedResources/rb_sharepricepopup_arrow_up.gif' alt='up arrow' style='margin-right:5px;'>");
    }

    document.write("<span class='sharepricepopupchange'>" + change + "p</span><br>");
    document.write("<div class='sharepricepopupdetails'>");
    document.write("Date: " + dateddmmmyyyy + "<br>");
    document.write("Time now: " + rbTempTime + "<br>");
    document.write("Trade time: " + hsTempTime + "<br>");
    document.write("</div>");
}

/* This function opens a new window, or changes the URL, to display the appropriate content */

var winContent;

function popUpWindow(sURL, popWidth, popHeight, popLeft, popTop) {
    if (!winContent) {
        winContent = window.open(sURL, 'PopUp', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + popWidth + ',height=' + popHeight + ',left=' + popLeft + ',top=' + popTop + '');
    }
    else {
        if (winContent.closed) {
            winContent = window.open(sURL, 'PopUp', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=' + popWidth + ',height=' + popHeight + ',left=' + popLeft + ',top=' + popTop + '');
        }
        else {
            winContent.document.location = sURL;
        }
    }

    winContent.focus();
}

function ThreeColDropdownMenu() {
    $('.splitcol').each(function() {
        var colsize = Math.round($(this).find("li").size() / 3);

        $(this).find("li").each(function(i) {
            if (i >= ((colsize * 2))) {
                $(this).addClass('col1_flag');
            }
            else if (i >= colsize) {
                $(this).addClass('col2_flag');
            }
        });

        $(this).find('.col1_flag').insertAfter(this).wrapAll("<ul class='splitcol1'></ul>").removeClass("col1_flag");
        $(this).find('.col2_flag').insertAfter(this).wrapAll("<ul class='splitcol2'></ul>").removeClass("col2_flag");
    });
}


function getTweets() {
    $(".tweet").tweet({
        username: "DiscoverRB",
        join_text: "... ",
        avatar_size: null,
        count: 1,
        auto_join_text_default: "we said,",
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
        loading_text: "loading tweets..."
    });
}

// RANDOM IMAGE PLUGIN

(function($) {
    $.randomImage = {
        defaults: {
            //you can change these defaults to your own preferences.
            path: '../UploadedResources/',
            imageArrayName: '',
            panel1Images: [['rb_home_panel1_01.jpg', '', 'http://annualreport2009.rb.com/RB-Annual-Review-2009', '_blank'], ['rb_home_panel1_02.jpg', '', 'http://annualreport2009.rb.com/RB-Annual-Review-2009', '_blank'], ['rb_home_panel1_03.jpg', '', 'http://annualreport2009.rb.com/RB-Annual-Review-2009', '_blank']],
            panel2Images: [['rb_home_panel2_01.jpg', 'Carbon 20', 'http://www.rb.com/Our-responsibility/Environment', '_blank'], ['rb_home_panel2_02.jpg', 'My RB Opportunity', 'http://www.myrbopportunity.com/', '_blank'], ['rb_home_panel2_03.jpg', 'Save the Children', 'http://www.rb.com/Our-responsibility/Community', '_blank']],
            panel3Images: [['rb_home_panel3_01.jpg', 'The talk about RB', 'http://www.rb.com/Investors-media/The-talk-about-RB', '_parent'], ['rb_home_panel3_02.jpg', 'The talk about RB', 'http://www.rb.com/Investors-media/The-talk-about-RB', '_parent'], ['rb_home_panel3_03.jpg', 'The talk about RB', 'http://www.rb.com/Investors-media/The-talk-about-RB', '_parent']],
            panel4Images: [['rb_home_panel4_01.jpg', 'Airwick Dynamic', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_02.jpg', 'Calgon Logo', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_03.jpg', 'Cillit Bang production line', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_04.jpg', 'Clearasil dynamic', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_05.jpg', 'Dettol brown dynamic', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_06.jpg', 'Finish dynamic', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_07.jpg', 'Frenchs Lifestyle', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_08.jpg', 'Gaviscon logo', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_09.jpg', 'Harpic lifestyle', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_10.jpg', 'Lysol dynamic', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_11.jpg', 'Mortein lifestyle', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_12.jpg', 'Mucinex lifestyle', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_13.jpg', 'Nurofen dynamic', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_14.jpg', 'Strepsils lifestyle', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_15.jpg', 'Vanish dynamic', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent'], ['rb_home_panel4_16.jpg', 'Veet lifestyle', 'http://www.rb.com/Our-brands/Products-around-the-home', '_parent'], ['rb_home_panel4_17.jpg', 'Woolite dynamic', 'http://www.rb.com/Our-brands/Powerbrands-on-the-shelf', '_parent']
            ]
        }
    };
    $.fn.extend({
        randomImage: function(config) {

            var config = $.extend({}, $.randomImage.defaults, config);

            return this.each(function() {

                var imageArray = eval("config." + config.imageArrayName);

                //get size of array, randomize a number from this
                var imageNamesSize = imageArray.length;
                var lotteryNumber = Math.floor(Math.random() * imageNamesSize);

                var fullPath = config.path + imageArray[lotteryNumber][0];
                var altText = imageArray[lotteryNumber][1];
                var imageLink = imageArray[lotteryNumber][2];
                var linkTarget = imageArray[lotteryNumber][3];

                //put this image into DOM at class of randomImage
                $(this).attr({
                    src: fullPath,
                    alt: altText
                });

                //if a link exists wrap the image in the link
                if (imageLink.length > 1) {
                    $(this).wrap("<a href=\"" + imageLink + "\" target=\"" + linkTarget + "\"></a>");
                }
            });
        }
    });
})(jQuery);