﻿
//console.trace();
$jq(document).ready(function () {

    $jq('#top-promos').cycle({
        fx: 'fade',
        delay: 500,
        speed: 3500,
        pause: 1,
        pager: '#promospager',
        pagerAnchorBuilder: function (idx, slide) {
            return '<li><a href="#"><img src="' + $jq('#promoImg' + idx).html() + '" width="172" height="79" /></a></li>';
        }
    });

    //pauses the scroll function if the thumbs are rolled over
    $jq('ul#promospager').mouseover(function () {
        $jq('#top-promos').cycle('pause');
    });

    $jq('ul#promospager').mouseout(function () {
        $jq('#top-promos').cycle('resume');
    });

    $jq('.upBtn').bind("mouseenter", function () {
        $jq('#promospagerWrap').stop().animate({ bottom: 0 }, { duration: 1000, easing: 'easeOutQuint', complete: function () { $jq('.upBtn').css({ backgroundPosition: "0px -27px" }); } });
    });

    $jq('#promospagerWrap').bind("mouseleave", function () {
        $jq('#promospagerWrap').stop().animate({ bottom: -95 }, { duration: 1000, easing: 'easeOutQuint', complete: function () { $jq('.upBtn').css({ backgroundPosition: "0px 0px" }); } });
    });

    $jq('.mid-scroll').fadeIn('slow', function () {
        // Animation complete
    });

    $jq('.mid-scroll').cycle({
        fx: 'fade',
        delay: 500,
        speed: 2000,
        pause: true,
        after: function (curr, next, opts) { midAfterChanged(curr, next, opts); isAnimating = false; },
        before: function (curr, next, opts) { midBeforeChanged(curr, next, opts); isAnimating = true; },
        pager: '#scrollpager',
        requeueOnImageNotLoaded: true
    });

    var promoAnimating = false;
    var heroSpeed = 800;

    $jq('.promo-1-off-map').bind("mouseenter mousemove", function () {
        if ($jq(".top-promos").is(":animated")) return;
        stopHeros();
        open1();
        close2();
        close3();
    });

    $jq('.promo-2-off-map').bind("mouseenter mousemove", function () {
        if ($jq(".top-promos").is(":animated")) return;
        stopHeros();
        open2();
    });

    $jq('.promo-3-off-map').bind("mouseenter mousemove", function () {
        if ($jq(".top-promos").is(":animated")) return;
        stopHeros();
        open3();
        $jq('.promo-2').animate({ left: 148 }, { duration: heroSpeed, easing: 'easeOutQuint' }, function () {
        });
        $jq('.promo-1-off').fadeIn(heroSpeed);
        $jq('.promo-2-off').fadeIn(heroSpeed);
    });

    function open1() {
        $jq('.promo-1-on').fadeIn(heroSpeed);
        $jq('.promo-1-off').fadeOut(heroSpeed);
    }

    function open2() {
        $jq('.promo-1-off').fadeIn(heroSpeed);
        $jq('.promo-2').animate({ left: 148 }, { duration: heroSpeed, easing: 'easeOutQuint' }, function () {
        });
        $jq('.promo-2-off').fadeOut(heroSpeed);
        close3();
    }

    function close2() {
        promoAnimating = true;
        $jq('.promo-2').animate({ left: 638 }, { duration: heroSpeed, easing: 'easeOutQuint' }, function () {
        });
        $jq('.promo-2-off').fadeIn(heroSpeed);
    }

    function open3() {
        $jq('.promo-3').animate({ left: 310 }, { duration: heroSpeed, easing: 'easeOutQuint' }, function () {
        });

        $jq('.promo-3-on').css({ 'display': 'block' });
        $jq('.promo-3-off').fadeOut(heroSpeed);
    }

    function close3() {
        $jq('.promo-3').animate({ left: 788 }, { duration: heroSpeed, easing: 'easeOutQuint' }, function () {
        });
        $jq('.promo-3-off').fadeIn(heroSpeed);
    }

    function stopHeros() {
        $jq(".promo-1").stop();
        $jq(".promo-2").stop();
        $jq(".promo-3").stop();
    }

    $jq('#storeSearchInput').val('Enter your postcode');

    $jq('#storeSearchInput').focus(function () {
        $jq('#storeSearchInput').val('');
    });

    $jq('#storeSearchInput').blur(function () {
        if ($jq('#storeSearchInput').val() == '') {
            $jq('#storeSearchInput').val('Enter your postcode');
        }
    });

    $jq('#storeSearchInput').keypress(function (event) { if (event.keyCode == '13') { $jq('#storeSearchButton').click(); event.preventDefault(); } });

    $jq('#storeSearchButton').click(function () {

        var searchTerm = $jq('#storeSearchInput').val();

        if (searchTerm != '' && searchTerm != 'Enter your postcode') {
            window.location = '/Customer-Services/Before-you-buy/Find-a-Store/Search-Results.aspx?q=' + searchTerm;
        }
    });

    jQuery('#mycarousel').jcarousel({
        scroll: 1, 			// number of items it scrolls by 3 is the default
        animation: 300,  	// controls the speed of the animation
        wrap: 'circular'	// makes the scroller move in a circular motion so never ending at the start or beginning.
    });
});

function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function () {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto();
    });
};

var isAnimating = false;

function promoPrev() {
    if (isAnimating) return;
    $jq('.mid-scroll').cycle('prev');
    $jq('.mid-scroll').cycle('pause');
}

function promoNext() {
    if (isAnimating) return;
    $jq('.mid-scroll').cycle('next');
    $jq('.mid-scroll').cycle('pause');
}

function midAfterChanged(curr, next, opts) {
    if (opts.currSlide == 0) $jq('.inspControlBar').css({ background: 'url(/~/media/83F6386577EC4BDC9F13C46DF3FC201E.ashx) no-repeat #3F3F3F' });
    if (opts.currSlide != 0) $jq('.inspControlBar').css({ background: 'url(~/media/A535C8C0C66A46BE9980039A1902B03F.ashx) no-repeat #3F3F3F' });
}

function midBeforeChanged(curr, next, opts) {
    if (opts.nextSlide == 0) $jq('.inspControlBar').css({ background: 'url(/~/media/83F6386577EC4BDC9F13C46DF3FC201E.ashx) no-repeat #3F3F3F' });
    if (opts.nextSlide != 0) $jq('.inspControlBar').css({ background: 'url(~/media/A535C8C0C66A46BE9980039A1902B03F.ashx) no-repeat #3F3F3F' });
    //console.trace();
}
