function echoarray(obj,lvl){ var str=''; for(var i in obj){ /*if(lvl>0&&isarray(obj[i])){}*/ str+=i+' = '+obj[i]+'<br/>'; } return str; }
function echoarraytype(obj,lvl){ var str=''; for(var i in obj){ /*if(lvl>0&&isarray(obj[i])){}*/ str+=i+' = '+typeof(obj[i])+'<br/>'; } return str; }


/*
 * @fileoverview Set a bunch of callback functions to update content based on the carousel
 *
 * Using an object literal approach to avoid global variable issues
 *
 * @author PG
 * 	
 */
var carouselHelper = {

    /*
    * The categories (links) carousel
    * 
    * @var object
    */
    categoryLinksCarousel: null,

    /*
    * The sections carousel
    * 
    * @var object
    */
    sectionsCarousel: null,

    /*
    * The currently displayed link element
    * 
    * @var object
    */
    currentElement: null,

    /*
    * The counter of the currently displayed element
    * 
    * @var int
    */
    currentElementCounter: 0,

    /*
    * Whether the sections carousel is currently animating?
    * 
    * @var bool
    */
    currentlyAnimating: false,

    /*
    * The two footer banners
    * 
    * @var object
    */
    bannerOne: null,
    bannerTwo: null,

    /*
    * Banners to display on each page
    *
    * @var object
    */
    bannerPages: {
        'businesses': [2, 8],
        'eating-and-drinking': [8, 9],
        'cinema-times': [3, 8],
        'train-times': [8, 6],
        'ask-us-anything': [7, 8],
        'people-finder': [8, 4]
    },

    /*
    * List of available banners & their links
    * used to change banners when the search page updates
    *
    * @var array
    */
    banners: [
		{
		    banner: 'banner_11850TVAd.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_57118.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_11850TVAd.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_57118.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_11850TVAd.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_57118.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_11850TVAd.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_57118.jpg',
		    link: 'javascript:carouselHelper.setVisibleCarousel("#search-eating-and-drinking")'
		},
		{
		    banner: 'banner_11850TVAd.jpg',
		    link: '/Results.aspx/restaurant/london?cuisine=47&bn=prezzo&d1=&d2=&book=1&deliver=0&take=0&menu=0'
		},
		{
		    banner: 'new-ad-banner.jpg',
		    link: 'static_pages/tv-ads.html'
		}
	],


    /**
    * This is the callback function which receives notification
    * right after initialisation of the categories carousel (links)
    * 
    * @param carousel {object} the carousel, passed through from jcarousel
    * @return void
    * @access public
    */
    initCategories: function(carousel) {

        // make jcarousel object globally available
        carouselHelper.categoryLinksCarousel = carousel;

        // set listener to update main section, for click & keyboard navigation
        $(carousel.container).find('a').each(function(counter) {

            var element = this;

            // set an internal variable to each object, to know it's position
            $(element).attr('_counter', counter);

            // bind listeners
            $(element).bind('click', function(e) {
                e.preventDefault();
                carouselHelper._showSection(element, counter);
            });
            $(element).bind('keypress', function(e) {
                if (e.keyCode == 13) {
                    e.preventDefault();
                    carouselHelper._showSection(element, counter);
                }
            });
        });

        // find the two footer links, for later use
        carouselHelper.bannerOne = $('#footer-banners').find('a:eq(0)');
        carouselHelper.bannerTwo = $('#footer-banners').find('a:eq(1)');

    },



    /**
    * This is the callback function which receives notification
    * right after initialisation of the sections carousel (big blocks of content)
    * 
    * @param carousel {object} the carousel, passed through from jcarousel
    * @return void
    * @access public
    */
    initSections: function(carousel) {

        // make jcarousel object globally available
        carouselHelper.sectionsCarousel = carousel;

        // show first selected element
        if (carouselHelper.categoryLinksCarousel) {
            var element = carouselHelper.currentElement = $(carouselHelper.categoryLinksCarousel.container).find("a:eq(0)");
            var counter = carouselHelper.currentElementCounter = 0;


            // or, if an initial section is selected (through the URL), display it instead

            //$ (element).click(element);
            // set the selected link
            carouselHelper._setSelectedLink(element, counter);
        }
        // carouselHelper.sectionsCarousel.scroll(counter + 1, true);
        //carouselHelper._showSection(element, counter)
        //}
        //else {
        // set the selected link
        //carouselHelper._setSelectedLink(element, counter);
        //carouselHelper._showSection(element, counter);
        //}
    },

    scrollOnLoad: function() {
        // look for initially selected block
        var initiallySelectedBlock = window.location.href.split('#')[1];
        if (!!initiallySelectedBlock) {
            element = carouselHelper.currentElement = $(carouselHelper.categoryLinksCarousel.container).find("a[href='#search-" + initiallySelectedBlock + "']");
            counter = carouselHelper.currentElementCounter = parseInt($(element).attr('_counter'));

            carouselHelper._setSelectedLink(element, counter);

            // scroll to correct starting section
            if (carouselHelper.sectionsCarousel) carouselHelper.sectionsCarousel.scroll(counter + 1, false);
        }
    },

    /*
    *   Function called adhoc to set visible carousel and highlited link
    *
    */
    setVisibleCarousel: function(blockId) {
        element = carouselHelper.currentElement = $(carouselHelper.categoryLinksCarousel.container).find("a[href='" + blockId + "']");
        if (element) {
            counter = carouselHelper.currentElementCounter = parseInt($(element).attr('_counter'));
            carouselHelper._setSelectedLink(element, counter);
            if (carouselHelper.sectionsCarousel) carouselHelper.sectionsCarousel.scroll(counter + 1, false);
        }
    },


    /*
    * Function called by jcarousel at the start of an animation
    * 
    * @return void
    * @access public
    */
    animationStarting: function() {
        carouselHelper.currentlyAnimating = true;
    },



    /*
    * Function called by jcarousel at the end of an animation
    * 
    * @return void
    * @access public
    */
    animationEnding: function() {
        carouselHelper.currentlyAnimating = false;

        // update URL (remove the word search, to stop jumping to the section directly)
        var elementLink = $(carouselHelper.currentElement).attr('href').split('#')[1];
        elementLink = elementLink.replace(/search-/, '');

        //window.location.hash = "#" + elementLink;

        carouselHelper._updateFooterBanner(elementLink);
    },



    /*
    * show a section, based on a selected element
    * 
    * 
    */
    _showSection: function(element, counter) {

        // condition : update selected state if the site isn't animating
        if (carouselHelper.currentlyAnimating === false) {

            // change the selected link element
            carouselHelper._setSelectedLink(element, counter);

            // scroll section
            if (carouselHelper.sectionsCarousel) carouselHelper.sectionsCarousel.scroll(counter + 1, true);
        }
    },



    /**
    * set a selected state on the supplied link element
    *
    */
    _setSelectedLink: function(element, counter) {

        // set selected button state
        $(carouselHelper.categoryLinksCarousel.carousel).find('li').removeClass('selected');

        // set the selected state on the clicked element
        $(element).parent().addClass('selected');

        carouselHelper.currentElement = element;
        carouselHelper.currentElementCounter = counter;
    },



    /*
    * Update the footer banners, based on a selected link
    *
    * @var [string] element the link that has been clicked
    */
    _updateFooterBanner: function(element) {

        // find the index of the new banner
        var firstIndex = carouselHelper.bannerPages[element][0];
        var secondIndex = carouselHelper.bannerPages[element][1];

        // get the file path for the images
        var currentSrc = carouselHelper._getFilePath(carouselHelper.bannerOne.find('img').attr('src'));

        // set the new footer links
        carouselHelper.bannerOne.attr('href', carouselHelper.banners[firstIndex]['link']);
        carouselHelper.bannerTwo.attr('href', carouselHelper.banners[secondIndex]['link']);

        // set the new footer images
        carouselHelper.bannerOne.find('img').attr('src', currentSrc + '/' + carouselHelper.banners[firstIndex]['banner']);
        carouselHelper.bannerTwo.find('img').attr('src', currentSrc + '/' + carouselHelper.banners[secondIndex]['banner']);
    },



    /*
    * get directory path of a file
    *
    * @var string filepath
    */
    _getFilePath: function(path) {

        // windows
        if (path.indexOf("/") == -1) {
            return path.substring(0, path.lastIndexOf('\\'));

            // unix
        } else {
            return path.substring(0, path.lastIndexOf('/'));
        }
    }
};