/** [rdc] 26/08/10
 * js for lli homepage carousel
 * Scrolls through the [6] images every [5] seconds
 * User can hover over a link or image and the scroll will stop. Clicking on that link goes to the specified channel homepage.
 * Mousing away from the links or image allows the carousel to scroll again, continuing from where it was stopped.
 */

    // create global variables
    var counter = 1; // used to store where in the loop we are
    var numOfImages = 6; // specify how many images are in the carousel
    var delay = 5000; // specify [milliseconds] how long to wait before progressing to the next image

    // preload the images for the carousel
    if (document.images) {
      var pic1= new Image(454,213);
      pic1.src="../../../llint/images/reskin/homepageChannelImages/tankers.jpg";
      var pic2= new Image(454,213);
      pic2.src="../../../llint/images/reskin/homepageChannelImages/gas.jpg";
      var pic3= new Image(454,213);
      pic3.src="../../../llint/images/reskin/homepageChannelImages/dryBulk.jpg";
      var pic4= new Image(454,213);
      pic4.src="../../../llint/images/reskin/homepageChannelImages/insurance.jpg";
      var pic5= new Image(454,213);
      pic5.src="../../../llint/images/reskin/homepageChannelImages/lawRegulation.jpg";
      var pic6= new Image(454,213);
      pic6.src="../../../llint/images/reskin/homepageChannelImages/credit_carousel.jpg";
    }

    // create array to hold the imageText for each image
    var imageText = {};
    imageText[0] = "Track the world's tanker fleet with movements, AIS mapping, vessel characteristics and company details. Analysis, news headlines, market outlooks and direct access to our team of analysts are all included.";
    imageText[1] = "Track global gas carriers with movements, AIS mapping, vessel characteristics and company details. Expert analysis, news headlines, market outlooks and direct access to our analysts are all included.";
    imageText[2] = "Track the world's dry bulk fleet with movements, AIS mapping, vessel characteristics and company details. Expert analysis, news headlines, market outlooks and direct access to our analysts are all included.";
    imageText[3] = "Track casualties with an alerting service and archive. Access unique business tools including our hull risk profiler, specialist investigations service and track the world's merchant fleet with Seasearcher.";
    imageText[4] = "Track casualties with an alerting service and archive. Conduct investigations with our Ask the Analyst service. Access case updates, commentary and track the world's merchant fleet with Seasearcher.";
    imageText[5] = "Access over 19,000 company credit reports, track cargo and vessel movements with real-time AIS positioning and monitor trends with key market data.";

    // declare variable to hold the interval so we can clear it later
    var carouselInterval = "";

    $(document).ready(function() {
        // Fire the carousel rotator function every n seconds if the carousel is present
        if ($("#carousel").length !== 0) {
            //set intial image text
            $("#imageText").text(imageText[0]);
            //set initial image link
            $("#imageForChannelCarousel").attr("href", $("#link1").attr("href"));
            setIntervalForCarousel();
        }

        // prepare all mouse events
        $(".channelList li a").mouseover(function() {
            // halt the carousel
            clearInterval(carouselInterval);
            // remove all selecteds
            $(".channelList li a").removeClass("selected"); // remove all 'selected' classes
            // switch to current channel selected
            $("#imageForChannelCarousel").removeClass("image"+counter);
            $("#imageForChannelCarousel").addClass("image"+$(this).attr("id").split("link")[1]);
            $("#imageText").text(imageText[$(this).attr("id").split("link")[1]-1]);
            //on mouseover refresh the image url...
            $("#imageForChannelCarousel").attr("href", $("#link"+$(this).attr("id").split("link")[1]).attr("href"));
            $("#imageForChannelCarousel").attr("title", $("#link"+$(this).attr("id").split("link")[1]).attr("title"));
            // update counter
            counter = $(this).attr("id").split("link")[1];
        });

        $(".channelList li a").mouseout(function() {
            // make the current link the 'selected' link
            $(this).addClass("selected");
            // restart the carousel
            setIntervalForCarousel();
        });

        $("#imageForChannelCarousel").mouseover(function() {
            // halt the carousel
            clearInterval(carouselInterval);
        });

        $("#imageForChannelCarousel").mouseout(function() {
            // restart the carousel
            setIntervalForCarousel();
        });

    });

    /**
    * sets the interval to n seconds
    */
    function setIntervalForCarousel() {
            carouselInterval = setInterval("carousel()", delay);
    }

    /**
    * main carousel function used to switch the image, highlighted links and imageText
    */

    function carousel() {
        if (counter >= numOfImages) {
            counter = 1;
            $("#imageForChannelCarousel").removeClass("image"+numOfImages);
            $("#imageForChannelCarousel").addClass("image"+counter);
            $("#imageText").text(imageText[counter-1]);
        } else {
            $("#imageForChannelCarousel").removeClass("image"+counter);
            counter++;
            $("#imageForChannelCarousel").addClass("image"+counter);
            $("#imageText").text(imageText[counter-1]);
        }
        $(".channelList li a").removeClass("selected"); // remove all 'select's
        $(".channelList li a#link"+counter).addClass("selected");
        $("#imageForChannelCarousel").attr("href", $("#link"+counter).attr("href"));
        $("#imageForChannelCarousel").attr("title", $("#link"+counter).attr("title"));
    }

