var pdfImages = ['blue', 'green', 'orange', 'pink', 'yellow'];

$(document).ready(function () {

    //pdf-download image randomizer
    var imgSrc = '/images/global/icon_pdfs_' + pdfImages[Math.floor(Math.random() * pdfImages.length)] + '.png';
    var imgSrc2 = '/images/global/icon_pdfs_' + pdfImages[Math.floor(Math.random() * pdfImages.length)] + '.png';
    $('#icon-rotate').attr('src', imgSrc);
    $('#icon-rotate2').attr('src', imgSrc2);

    //categories dropdown
    $('a.expands').click(function () {
        $(this).parent().toggleClass("active");
    });

    //no-landing dropdown specific
    $('ul.categories li a.dropper').click(function () {
        $(this).parent().toggleClass("active");
    });


    // Initialise the first and second carousel by class selector.
    // Note that they use both the same configuration options (none in this case).
    $('#guides_carousel').jcarousel();
    $('#videos_carousel').jcarousel();
    $('#tutorial_carousel').jcarousel({ scroll: 1 });

    $('.recipe-jcarousel').jcarousel({
        auto: 5,
        scroll: 1,
        vertical: false,
        initCallback: mycarousel_initCallback
    });

    startFirstSlideshow();

    //faqs dropdown
    $('.faqs h2').click(function () {
        $(this).parent().children().toggleClass("answered");
    });

    $('.sf_recipeSubmit').click(function () {
        var keyword = $('.sf_searchText').val();
        var searchString = "/recipes.aspx?keyword=" + keyword;
        window.location = searchString;
    });

    $('.sf_searchSubmit').click(function () {
        if ($('.sf_searchText').val().trim() != '') {
            var keyword = $('.sf_searchText').val();
            var searchString = "/results.aspx?IndexCatalogue=SiteSearch&SearchQuery=" + keyword;
            window.location = searchString;
        } else {
            return false;
        }
    });

    $('.sf_searchText').keypress(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            $('.sf_searchSubmit').click();
	        return false; 
        }
    });

    $('#coupon-link').click(function () {
        alert("test");
        $('#footer .legalese').hide();
        window.print();
    });

});

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();
    });
};
function startFirstSlideshow() {
    stopShows();

    $('#slideshow1').cycle({
        fx: 'none',
        speed: 'fast',
        timeout: 5000,
        pager: '#slidenav',
        autostop: true,
        end: function () {
            startSecondSlideshow();
        }
    });

    $('#btnNew').addClass('orange');
    $('#slideshow1').show();
}

function startSecondSlideshow() {
    stopShows();

    $('#btnStarted').addClass('orange');
    $('#slideshow2').show();
    window.setTimeout(startThirdSlideshow, 5000);
}

function startThirdSlideshow() {
    stopShows();

    $('#slideshow3').cycle({
        fx: 'none',
        speed: 'fast',
        timeout: 5000,
        pager: '#slidenav',
        autostop: true,
        end: function () {
            startFirstSlideshow();
        }
    });

    $('#btnFeatured').addClass('orange');
    $('#slideshow3').show();
}

function clearButtons() {
    $('#btnNew').removeClass('orange');
    $('#btnStarted').removeClass('orange');
    $('#btnFeatured').removeClass('orange');
}

function stopShows() {
    $('#slidenav').html('');

    $('#slideshow1').hide();
    $('#slideshow2').hide();
    $('#slideshow3').hide();

    $('#slideshow1').cycle('stop');
    $('#slideshow2').cycle('stop');
    $('#slideshow3').cycle('stop');

    clearButtons();
}

