$(document).ready(function () {

    //remove class so we can style for non javascript
    $('html').removeClass('no-js');

    var animationTime = 400; //time it takes for the sliding animation to happen

    var isOldIE = false;
    //if ($('html').hasClass('ie6') || $('html').hasClass('ie7')) {
    if ($('html').hasClass('ie6')) {
        isOldIE = true;
    }

    //check for hash in url and go to the right section
    if (window.location.hash != "" && !isOldIE) {
        if (window.location.hash == '#aboutCFT') {
            goToAboutCFT();
        }
        if (window.location.hash == '#productRange') {
            goToProductRange()
        }
    }

    //hide bottom sections
    if (!isOldIE) {
        $('#aboutCFT').addClass('hidden');
        $('#productRange').addClass('hidden');
    }

    //get the correct number for the tree counter #### removed from 1st phase ####
    /*
    $('#treesPlanted span').load('TreesPlanted.ashx');
    */

    //homepage Carousel
    $('#carousel .frames').cycle({
        fx: 'fade',
        timeout: 5000,
        prev: '#prevButton, #prevButtonSmall',
        next: '#nextButton, #nextButtonSmall',
        speed: 1000,
        pager: '#carousel .controls ol',
        pagerAnchorBuilder: function (idx, slide) {
            return '#carousel .controls ol li:eq(' + idx + ') a';
        },
        pause: true
    });

    //open coupon lightbox
    $('#couponCTA').click(function () {
        $.scrollTo('0px', 500);
        $('#overlay').show();
        $('#couponLightbox').fadeIn();
        return false;
    });

    //close coupon lightbox
    $('#overlay, #closeButton').click(function () {
        $('#overlay').hide();
        $('#couponLightbox').fadeOut();
        return false;
    });

    //click on aboutCFT link
    $('#aboutCFTExpander').click(function () {
        if (!isOldIE) {
            //if it's not showing then show it
            if ($('#aboutCFT').hasClass('hidden')) {
                $('#productRangeExpander').addClass('stuckBottom').hide();
                goToAboutCFT();
            }
            //if it is showing then close it
            else {
                $('#productRangeExpander').removeClass('stuckBottom').hide();
                $(this).removeClass('open');
                $('#aboutCFT .expandableContent').slideUp(animationTime);
                $('#aboutCFT').animate({
                    paddingTop: 250
                }, animationTime, function () {
                    $('#productRangeExpander').show();
                }).addClass('hidden');
            }
        }

        return false;
    });

    //click on product range link
    $('#productRangeExpander').click(function () {

        if (!isOldIE) {
            //if it's not showing then show it
            if ($('#productRange').hasClass('hidden')) {
                if ( ! $('#aboutCFT').hasClass('hidden')) {
                    $('#productRangeExpander').removeClass('stuckBottom').hide();
                }
                goToProductRange();
            }
            //if it is showing then close it
            else {
                $(this).removeClass('open');
                $('#productRange .expandableContent').slideUp(animationTime);
                $('#productRange').animate({
                    paddingTop: 225
                }, animationTime).addClass('hidden');
            }
        }
        return false;
    });

    $('#carousel .balmLink').click(function () {
        if (!isOldIE) {
            //goToProductRangeBalm();
            openVideo("/videos/Velvet__30sec.flv");
            $('#overlay').show();
            return false;
        }
        else {
            $.scrollTo('#productRange', animationTime, { offset: 120 });
        }
        return false;
    });

    $('#carousel .rangeLink').click(function () {
        if (!isOldIE) {
            goToProductRange();
        }
        else {
            $.scrollTo('#productRange', animationTime, { offset: 120 });
        }
        return false;
    });

    $('#carousel .cftLink').click(function () {
        if (!isOldIE) {
            goToAboutCFT();
        }
        else {
            $.scrollTo('#aboutCFT', animationTime, { offset: 35 });
        }
        return false;
    });

    $('#carousel .videoLink').click(function () {
        openVideo("/videos/velvet_ad.flv");
        $('#overlay').show();
        return false;
    });

    $('#videoCloseButton').live('click', function () {
        closeVideo();
        $('#overlay').hide();
        return false;
    });

    //function to open aboutCFT section and scroll the window to it.
    function goToAboutCFT() {
        $('#aboutCFT .expander').addClass('open');
        $('#aboutCFT .expandableContent').slideDown(animationTime);
        $('#aboutCFT').animate({
            paddingTop: 115
        }, animationTime, function () {
            $('#productRangeExpander').show();
        }).removeClass('hidden');

        $('#productRangeExpander').removeClass('open');
        $('#productRange .expandableContent').slideUp(animationTime);
        $('#productRange').animate({
            paddingTop: 225
        }, animationTime).addClass('hidden');
        var t = setTimeout(function () { $.scrollTo('#aboutCFT', animationTime, { offset: 35 }); }, animationTime);

    }

    //function to open productRange section and scroll the window to it.
    function goToProductRange() {
        $('#productRange .expander').addClass('open');
        $('#productRange .expandableContent').slideDown(animationTime);
        $('#productRange').animate({
            paddingTop: 230
        }, animationTime, function () {
            $('#productRangeExpander').show();
        }).removeClass('hidden');

        $('#aboutCFTExpander').removeClass('open');
        $('#aboutCFT .expandableContent').slideUp(animationTime);
        $('#aboutCFT').animate({
            paddingTop: 250
        }, animationTime).addClass('hidden');

        var t = setTimeout(function () { $.scrollTo('#aboutCFT', animationTime, { offset: 270 }); }, animationTime);

    }

    //function to open productRange section and scroll the window to the BALM product.
    /*
    function goToProductRangeBalm() {
        $('#productRange .expander').addClass('open');
        $('#productRange .expandableContent').slideDown(animationTime);
        $('#productRange').animate({
            paddingTop: 230
        }, animationTime).removeClass('hidden');

        $('#aboutCFT .expander').removeClass('open');
        $('#aboutCFT .expandableContent').slideUp(animationTime);
        $('#aboutCFT').animate({
            paddingTop: 250
        }, animationTime).addClass('hidden');

        var t = setTimeout(function () { $.scrollTo('#aboutCFT', animationTime, { offset: 510 }); }, animationTime);

    }
    */

    function openVideo(src) {
        $('#pageContainer').append('<div id="videoOverlay"><a href="#" id="videoCloseButton">Close</a><div id="videoContainer"></div></div>');
        var flashvars = {};
        flashvars.vidSrc = src;
        var params = {};
        params.wmode = "transparent";
        var attributes = {};
        swfobject.embedSWF("swf/flv_player.swf", "videoContainer", "581", "327", "9.0.0", false, flashvars, params, attributes);
        $('#videoOverlay').fadeIn(animationTime);
    }

    function closeVideo() {
        $('#videoOverlay').fadeOut(animationTime, function () { $('#videoOverlay').remove() });
    }





});
