$(function () {
    function switchImages( i ) {
        var i = $(this);
        i.attr('src', i.attr('data-original'));
        i.removeAttr('data-original');
    }

    $.fn.splitUp=function(splitBy,wrapper){
        $all= $(this).find('>*');
        var fragment=Math.ceil($all.length/splitBy);
        for (i=0;i<fragment;i++)
            $all.slice(splitBy*i,splitBy*(i+1)).wrapAll(wrapper);
        return $(this);
        console.log('splitting');
    }

    if($('#hero').length) {

        /*$('#featPager').jcarousel(
            {
                wrap: "both",
                scroll: 4,
                itemVisibleInCallback: { 
                    onBeforeAnimation: function(i,e) { 
                        var e = $(e);
                        if(e.hasClass('secondary')) { 
                            var i = e.find('img[data-original]').each( switchImages ); 
                            e.removeClass('secondary');
                        }
                    }
                }
            }
        );*/

        $('#featured').cycle(
            {
                delay: 2000,
                loopAfter: 4,
                pager: '#featPager',
                pagerAnchorBuilder: function( idx, dom ) {
                    a = $('#featPager>li:eq('+idx+')');
                    return($('#featPager>li:eq('+idx+')'));
                },
                updateActivePagerLink: function(pager, activeIndex) { 
				$(pager).find('li').removeClass('activeSlide');
				$(pager).find('li:eq('+activeIndex+')').addClass('activeSlide'); 
			    },
                before: function( currSlideElement, nextSlideElement, options, forwardFlag ) {
                    var next = $(nextSlideElement);
                    next.find('img[data-original]').each( switchImages );
                },
                pagerClick: function() {
                    $("#featured").cycle('pause');
                }
            }
        );

        $('#featPager')
        .before('<a href="#" class="leftArrow"><img src="/i/arrows-fs8.png" alt="Previous" /></a>')
        .after('<a href="#" class="rightArrow"><img src="/i/arrows-fs8.png" alt="Next" /></a>') 
		.splitUp(4,'<div>').cycle({
			fx: 'scrollHorz',
			speed: 500,
			timeout: 0,
			next:   '.rightArrow', 
			prev:   '.leftArrow',
			cleartypeNoBg: true
		})
                

    } 

    if ($('#videoHome, #video').length) {
        
        // $('.featItem') 
        // .before('<a href="#" class="left"><img src="/i/lArrow.gif" alt="Previous" /></a>')
        // .after('<a href="#" class="right"><img src="/i/rArrow.gif" alt="Next" /></a>') 
        // .cycle({ 
        //     fx:     'scrollHorz', 
        //     speed:  500, 
        //     timeout: 7000, 
        //     next:   '.right', 
        //     prev:   '.left'
        // });

        $('.moreEpisodes div.videoGroup ul.videoList').each( function(i) {
            $(this).before('<span class="pager'+i+'"></span>')
            .splitUp(4,'<div class="videoPageSet">')
            .cycle({
                fx:     'scrollHorz', 
                speed:  500, 
                timeout: 0, 
                pager:  'span.pager'+i,
            });
        });
        
        var tabContainers = $('.play div.detailsContent > div.wrapper > div');
        tabContainers.hide().filter('#moreInfo').show();

        $('div.detailsTabs ul.tabs a, .play .topTabsNav a').click(function () {
            var hasht = this.hash;
            tabContainers.hide().filter(hasht).show();
            $('div.detailsTabs ul.tabs a').removeClass('selected');
            $('div.detailsTabs ul.tabs a').each ( function() {
                if(this.hash == hasht) {
                    $(this).addClass('selected');
                }
            });
        }).filter('li:first-child a').addClass('selected'); 

        // $('.videoList li').each(function () {
        //     // options
        //     var time = 250;
        //     var showDelay = 500;
        //     var showDelayTimer = null;
        //     
        //     var popup = $('.popup', this)

        //     var showAnimation = function() {
        //         popup.css({ display: 'block', })
        //         .animate({ opacity: 1 }, time, 'swing' );
        //     }

        //     $(this).mouseover(function () {
        //         if(popup.queue().length>0) {
        //             popup.stop(true);
        //             showAnimation();
        //         } else {
        //             showDelayTimer = setTimeout(showAnimation, showDelay);
        //         }
        //     }).mouseout(function () {
        //       // reset the timer if we get fired again - avoids double animations
        //       if (showDelayTimer) clearTimeout(showDelayTimer);

        //         popup.stop(true);
        //         popup.animate({ opacity: 0 }, time, 'swing', function () {
        //           popup.css('display', 'none');
        //         });
        //     });
        //   });

    }

});

