// Homepage slide switcher

$(document).ready(function() {

    $(function() {
        var tabContainers = $('div.carousel > div.panel');

        /* Click behaviour */
        function switchSlide(type, el, speed) {
            //stop any auto-advance
            $('div.carousel').stopTime('carousel');

            var currentSelElem = $('div.carousel ul li.here a');
            var newSelPanel = '#' + el.id.replace('Link', 'Panel');
            var isSelectedAlready = false;
            if (currentSelElem.length > 0 && currentSelElem.get(0).id == el.id) { //already selected, flash onward link
                isSelectedAlready = true;
            }
            if (isSelectedAlready && type == 'click') {
                var currentOnwardLink = $(tabContainers).filter(newSelPanel).find('div.panel-text > a');
                currentOnwardLink.css('color', 'rgb(27,54,99)').animate({ color: '#FFFFFF' }, speed).animate({ color: '#2C8CC6' }, speed);
            } else {
                tabContainers.hide().filter(newSelPanel).fadeIn(isSelectedAlready ? (speed / 2) : speed);
                $('div.carousel ul li').removeClass('here');
                $(el).parents('li').addClass('here');
            }

            return false;
        }
        $('div.carousel ul a').click(function() {
            return switchSlide('click', this, 400);
        });
//        $('div.carousel ul a').click(function() {
//            return switchSlide('click', this, 400);
//        }).hover(function() {
//            return switchSlide('hover', this, 200);
//        }); //.filter(':first').click();


        /* Keypress behaviour */
        // Define variables
        var currentElem = $('div.carousel ul li.here a');
        var currentHref = currentElem.attr('id');
        var currentIndex = parseInt(currentHref.substring(8, 9));
        var tabContainers = $('div.carousel > div.panel');

        function ChangeSlide(speed) {
            var currentElem = $('div.carousel ul li.here a');
            var currentHref = currentElem.attr('id');
            var currentIndex = parseInt(currentHref.substring(8, 9));
            var tabContainers = $('div.carousel > div.panel');

            if (currentIndex == 4) {
                next = 1
            }
            else {
                next = currentIndex + 1;
            }

            var nextElem = $('div.carousel > div#caroPanel' + next);
            var nextLink = $('a#caroLink' + next);

            // Set all panels to hide
            tabContainers.hide();
            nextElem.fadeIn(speed);

            // Remove the active class from all links
            $('div.carousel ul li').removeClass('here');
            nextLink.parents('li').addClass('here');

        }

        //set up autoadvance timer
        //$('div.carousel').everyTime('5s', 'carousel', function() {return ChangeSlide(600);});

        // Bind listener to document
        $(document).keydown(function(e) {
            // If focus does not equal input
            if (e.target.nodeName.toLowerCase() !== 'input') {
                //If focus does not equal a
                if (e.target.nodeName.toLowerCase() !== 'a') {
                    //If focus does not equal button
                    if (e.target.nodeName.toLowerCase() !== 'button') {
                        switch (e.keyCode) {
                            case 0:
                                e.preventDefault();
                                ChangeSlide(400);

                                break;

                            case 32:
                                e.preventDefault();
                                ChangeSlide(400);

                                break;

                            case 13:
                                e.preventDefault();
                                ChangeSlide(400);

                                break;
                        }
                    }
                }
            }
        });

    });

});
