jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing,
{
    def: 'easeOutExpo',
    swing: function (x, t, b, c, d) {
        //alert(jQuery.easing.default);
        return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
    },
    easeOutExpo: function (x, t, b, c, d) {
        return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
    }
});




// Fightstore home page accordian
(function (jQuery) {
    jQuery.fn.fsAccordian = function (settings) {
		
        var config = { 'autoAnimate': 'true', 'animateDelay': 6000, 'panelToShowOnStart': 0, 'panelAnimateSpeed': 500, 'panelAnimateEasing': 'easeOutExpo', 'animateDirection': 1 };
        var panels = [];
        var panelShadeWidth = 0;
        var panelWidth = 0;
        var animatePos = 0;
        var animateTimer;
//alert("test");
        if (settings) jQuery.extend(config, settings);

        function movePanel(panelIndex, pos) {
            jQuery(panels[panelIndex]).animate({ left: pos + 'px' }, { duration: config.panelAnimateSpeed, easing: config.panelAnimateEasing });
        }

        function showSpecificPanel(panelIndex) {
            // Move the panels above it to the right
            for (var i = panels.length - 1; i > panelIndex; i--) { movePanel(i, (panelWidth + panelShadeWidth * (i - 1))) }
            // Move the panels below it, to the left
            for (var i = 0; i <= panelIndex; i++) { movePanel(i, panelShadeWidth * i); }
            animatePos = panelIndex;

        }

        function animatePanel() {
            animatePos += config.animateDirection;
            if (config.autoAnimate) {
                showSpecificPanel(animatePos);
                if (animatePos <= 0 || animatePos >= panels.length - 1) {
                    config.animateDirection *= -1;
                }

                setTimeout(animatePanel, config.animateDelay)
            }
        }

        this.each(function () {
            // element-specific code here
            // Calculate the initial positions of the details and place the panels in the appropriate position
            panels = jQuery(this).find('ul.fspanel>li');
            panelWidth = jQuery(panels[0]).width();
            panelShadeWidth = (jQuery(this).width() - panelWidth) / (panels.length - 1);

            // Set the width of the control anchors
            jQuery(this).find('h2>a').width(panelWidth - 2);

            // Jump to the correct panel and begin animation
            showSpecificPanel(config.panelToShowOnStart);
            if (config.autoAnimate) {
                animateTimer = setTimeout(animatePanel, config.animateDelay)
            }

            // Add in a node for the shadow on the left of each panel
            jQuery(panels).append('<span class="shadow">&nbsp;</span>')


            // Add the event handlers on to the control anchors to allow them to move
            jQuery(this).find('h2>a').mouseover(function (e) {
                if (config.autoAnimate) {
                    config.autoAnimate = false;
                    clearTimeout(animateTimer);
                }
                // Get the index of the list item that needs to be moved
                var panelIndex = jQuery('.panelAccordion > ul > li').index(jQuery(this).parents('li:first'));
                jQuery(".panelAccordion > ul > li").stop(true, false)

                showSpecificPanel(panelIndex)
            });

            // Add the event handlers on to the control anchors to allow them to move
            jQuery(this).find('span.shadow').mouseover(function (e) {
                if (config.autoAnimate) {
                    config.autoAnimate = false;
                    clearTimeout(animateTimer);
                }
                // Get the index of the list item that needs to be moved
                var panelIndex = jQuery('.panelAccordion > ul > li').index(jQuery(this).parents('li:first'));
                jQuery(".panelAccordion > ul > li").stop(true, false)

                showSpecificPanel(panelIndex - 1)
            });



        });

        return this;

    };
})(jQuery);

// Begin Carousel v2.0

var _DisplayWidthModules;
var _ModuleSize;

var _SteppingToRight = true;
var _LeftSideModule = 0;
var _Offset = 0;
var _StepTick = 3000;
var _AutoAnimationOn = false;
var _AutoStep = 500;
var _ManualStep = 250;



// Initialise the carousel
function InitialiseCarousel() {
    var StripWidth = 0;
    var ViewportWidth = parseInt(jQuery('#CarouselStrip').css('width'));

    _ModuleSize = (parseInt(jQuery('#CarouselStrip ul li:last').css('margin-left')) +
        parseInt(jQuery('#CarouselStrip ul li:last').css('width')) +
        parseInt(jQuery('#CarouselStrip ul li:last').css('margin-right')));

    _DisplayWidthModules = 0;
    jQuery('#CarouselStrip ul li').each(function (index) {
        StripWidth += (parseInt(jQuery(this).css('margin-left')) +
            parseInt(jQuery(this).css('width')) +
            parseInt(jQuery(this).css('margin-right')));

        if (StripWidth <= ViewportWidth) {
            _DisplayWidthModules++;
        }
    });
    jQuery('#CarouselStrip ul').css('width', StripWidth + "px");

    if (jQuery('#CarouselStrip ul li').length > _DisplayWidthModules) {
        jQuery('#CarouselStrip').append('<span class="prevArrow" style="display: none;">Prev</span>');
        jQuery('#CarouselStrip').append('<span class="nextArrow">Next</span>');

        jQuery('#CarouselStrip').click(function (e) {
            _AutoAnimationOn = false;
            if (jQuery(e.target).hasClass('nextArrow')) {
                HandleAnimation(true, _ManualStep);
            }
            if (jQuery(e.target).hasClass('prevArrow')) {
                HandleAnimation(false, _ManualStep);
            }
        });

        jQuery('#CarouselStrip').mouseover(function (e) {
            _AutoAnimationOn = false;
        });

        if (jQuery('#CarouselStrip').hasClass('autoAnimate')) {
            _AutoAnimationOn = true;
            setTimeout('HandleAnimationTick()', _StepTick);
        }
    }
}

function HandleAnimationTick() {
    if (_AutoAnimationOn) {
        HandleAnimation(_SteppingToRight, _AutoStep);
        if (_LeftSideModule < 1) {
            _SteppingToRight = true;
        }
        else if ((_LeftSideModule + _DisplayWidthModules) >= jQuery('#CarouselStrip ul li').length) {
            _SteppingToRight = false;
        }
        setTimeout('HandleAnimationTick()', _StepTick);
    }
}

function HandleAnimation(MoveRight, AnimationSpeed) {
    var StepSize = 0;

    if ((MoveRight) && ((_LeftSideModule + _DisplayWidthModules) < jQuery('#CarouselStrip ul li').length)) {
        _LeftSideModule++;
        StepSize = -_ModuleSize;
        if ((_LeftSideModule + _DisplayWidthModules) == jQuery('#CarouselStrip ul li').length) {
            jQuery('#CarouselStrip span.nextArrow').fadeOut();
        }
        if (_LeftSideModule == 1) {
            jQuery('#CarouselStrip span.prevArrow').fadeIn();
        }
    }
    else if ((!MoveRight) && (_LeftSideModule > 0)) {
        _LeftSideModule--;
        StepSize = _ModuleSize;
        if (_LeftSideModule == 0) {
            jQuery('#CarouselStrip span.prevArrow').fadeOut();
        }
        if ((_LeftSideModule + _DisplayWidthModules) == (jQuery('#CarouselStrip ul li').length - 1)) {
            jQuery('#CarouselStrip span.nextArrow').fadeIn();
        }
    }
    _Offset += StepSize;
    jQuery('#CarouselStrip ul').animate({ left: (_Offset) + 'px' }, { duration: AnimationSpeed, easing: 'easeOutExpo', complete: function () { } });
}
// End Carousel v2.0

// fightstore Carousel functionality v1.0
(function (jQuery) {
    jQuery.fn.fsCarousel = function (settings) {
        var config = { 'positionChangeSpeed': 500, 'animateEasing': 'easeOutExpo' };
        var carousel
        var listToMove
        var widthOfContainer
        var widthOfElement
        var noElementsInCarousel
        var maxPosition

        if (settings) jQuery.extend(config, settings);

        function fadeNavigation(newPosition) {
            var listPos = jQuery(listToMove).position().left + newPosition
            var showNextArrow = false
            var showPrevArrow = false
            var nextArrow = carousel.find('span.nextArrow')[0]
            var prevArrow = carousel.find('span.prevArrow')[0]

            if (listPos < 0) { showPrevArrow = true }
            if (listPos > maxPosition) { showNextArrow = true }

            if (showNextArrow) { jQuery(nextArrow).fadeIn(); } else { jQuery(nextArrow).fadeOut() }
            if (showPrevArrow) { jQuery(prevArrow).fadeIn(); } else { jQuery(prevArrow).fadeOut() }

        }

        this.each(function () {
            carousel = jQuery(this);
            widthOfContainer = carousel.width();
            widthOfElement = carousel.find('li:first').width()
            noElementsInCarousel = carousel.find('li').length
            maxPosition = (noElementsInCarousel - (widthOfContainer / widthOfElement)) * widthOfElement * -1

            listToMove = carousel.find('ul:first')

            if (noElementsInCarousel * widthOfElement > widthOfContainer) {
                jQuery(listToMove).css('width', noElementsInCarousel * widthOfElement + "px")
                carousel.append('<span class="prevArrow" style="display: none;">Prev</span>');
                carousel.append('<span class="nextArrow">Next</span>');

                carousel.click(function (e) {
                    if (carousel.find('ul:animated').length == 0) {
                        if (jQuery(e.target).hasClass('nextArrow')) {
                            fadeNavigation(widthOfElement * -1)
                            jQuery(listToMove).animate({ 'left': jQuery(listToMove).position().left - widthOfElement + "px" }, { duration: config.positionChangeSpeed, easing: config.animateEasing });
                        }
                        if (jQuery(e.target).hasClass('prevArrow')) {
                            fadeNavigation(widthOfElement)
                            jQuery(listToMove).animate({ 'left': jQuery(listToMove).position().left + widthOfElement + "px" }, { duration: config.positionChangeSpeed, easing: config.animateEasing });
                        }
                    }
                })
            }
        })
    }
})(jQuery);






jQuery(document).ready(function () {
   

    // Set up the homepage accordion
    if (jQuery('#HomePageAccordion').length) {
		
        jQuery('#HomePageAccordion').fsAccordian();
   }

   
});

// BEGIN Product Category Page Slider

(function (jQuery) {
    jQuery.fn.AttachSlider = function (Settings) {

        var ThisSlider;
        var Frames = [];
        var CurrentFrame = 0;
        var SlideEnabled = false;
        var SlideTimer = 0;
        var SliderWidth = 0;
        var AnimationInProgress = false;

        var TimeoutController;

        var Config = {
            'SlideEnabled': false,
            'DelayOnLoad': 7000,
            'FrameHoldDuration': 7000,
            'FrameMovementDuration': 1000
        };

        if (Settings) jQuery.extend(Config, Settings);

        return this.each(function () {
            // Get our actual slider top operate within and set up the vitals
            ThisSlider = jQuery(this)[0];
            SliderWidth = jQuery(ThisSlider).width()

            // Initialise the frames
            InitialiseFrames();

            // Initialise the popups to have closers
            InitialisePopupClosers();

            // Attach click event to all "next offer" links
            AttachClickHandlers();

            // Start things moving
            if (Config.SlideEnabled) setTimeout(AnimateFrames, Config.DelayOnLoad);
        });

        function InitialiseFrames() {
            // Get all frames as marked by the .frame class
            Frames = jQuery('.frame', ThisSlider);

            // If we have more than zero frames, move the first one to display position
            if (Frames.length > 0) jQuery(Frames[0]).css({ left: 0 });

            // If we have more than one frame, enable the slider
            if (Frames.length > 1) Config.SlideEnabled = true;
        }

        function AnimateFrames() {
            // Get our next frame index
            var NextFrame = CurrentFrame + 1;
            if (NextFrame >= Frames.length) NextFrame = 0;

            // Set up the next frame ready to slide in
            jQuery(Frames[NextFrame]).css({ 'left': SliderWidth + 'px', top: '0' });

            // Set the frames moving and update current frame
            jQuery(Frames[CurrentFrame]).animate({ 'left': -SliderWidth + 'px' }, Config.FrameMovementDuration, 'easeInQuint');
            jQuery(Frames[NextFrame]).animate({ 'left': '0' }, Config.FrameMovementDuration, 'easeInQuint');
            CurrentFrame = NextFrame;

            // Handle animation in progress to prevent a raft of clicks
            AnimationInProgress = true;
            setTimeout(EndAnimation, Config.FrameMovementDuration);
        }

        function EndAnimation() {

            AnimationInProgress = false;

            if (Config.SlideEnabled) { 
                TimeoutController = setTimeout(AnimateFrames, Config.FrameHoldDuration) 
            };
        }

        function InitialisePopupClosers() {

            jQuery('<p class="closer"><a href="#">[X]</a></p>').insertAfter('.offerBox h2');
        }

        function AttachClickHandlers() {
            jQuery('.nextOffer a', ThisSlider).click(function (e) {

                // Prevent default action
                e.preventDefault();

                // Stop animation
                Config.SlideEnabled = false;
                clearTimeout(TimeoutController);

                // Step to next frame
                if (!AnimationInProgress) AnimateFrames();

            });

            jQuery('.offerOpener a', ThisSlider).click(function (e) {

                // Prevent default action
                e.preventDefault();

                // Stop animation
                Config.SlideEnabled = false;
                clearTimeout(TimeoutController);

                var OfferBoxName = '#' + jQuery(e.target).parents('.frame').attr('id') + 'Detail';
                jQuery('#SelectedOfferBox').html(jQuery(OfferBoxName).html());

                jQuery('#OfferPopBackground').css({
                    'display': 'block',
                    'z-index': '1500'
                });
                jQuery('#OfferPopBackground').height(jQuery(window).height());
                jQuery('#OfferPopBackground').width(jQuery(window).width());

                // Calculate the height of the box
                var BoxHeight = 190 + (jQuery('#SelectedOfferBox select').length * 26);
                var BoxTop = 0;
                if (jQuery(window).height() > BoxHeight) {
                    BoxTop = ((jQuery(window).height() - BoxHeight) / 2);
                }

                jQuery('#SelectedOfferBox').css({
                    'display' : 'block',
                    'position' : 'fixed',
                    'top' : BoxTop + 'px',
                    'left' : ((jQuery(window).width() - 400) / 2) + 'px',
                    'background' : '#ffffff',
                    'border' : '2px solid #ccc',
                    'z-index' : '2001'});

                // Offer box has to reside OUTSIDE slider due to MSIE z-index bug, so set up its closer here
                jQuery('#SelectedOfferBox .closer a').click(function (e) {

                    // Prevent default action
                    e.preventDefault();

                    jQuery('#OfferPopBackground').css({ 'display': 'none' });
                    jQuery('#SelectedOfferBox').css({ 'display': 'none' });
                });
            });
        }
    };
})(jQuery);
