Cufon.replace('.boxgrid h3, .bio-expand h3, .content-holder h2, .content-holder-right p a, #buy-now .title', {
    hover: true
});

$(document).ready(function () {

    $('.boxgrid.caption').hover(function () {
        $(".cover", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });
    }, function () {
        $(".cover", this).stop().animate({ top: '93px' }, { queue: false, duration: 160 });
    });

    $('.boxgrid.caption-gallery').hover(function () {
        $(".cover-gallery", this).stop().animate({ top: '0px' }, { queue: false, duration: 160 });
    }, function () {
        $(".cover-gallery", this).stop().animate({ top: '145px' }, { queue: false, duration: 160 });
    });

    var $lastVisibleReader = false;

    /*$("#the_readers-link .caption .button-holder a").click(function () {

    var $link = $(this);

    $lastVisibleReader = $($link.attr("href"));

    $lastVisibleReader.fadeToggle("fast", "linear");

    $("img", $lastVisibleReader).loadImage();

    });

    $("#the_readers-link a[href=#the_readers-link]").click(function () {

    if ($lastVisibleReader)
    $lastVisibleReader.fadeToggle("fast", "linear");

    });*/

    $("#the_readers-link a.learn-more").click(function () {

        var $link = $(this);
        var $bio = $("#" + $link.attr("rel"));

        $bio.css("opacity", 0).show().stop().animate({

            opacity: 1

        }, {

            duration: 1000,
            complete: function () {

                $(this).css("opacity", "");

            }

        });

        $("img", $bio).loadImage();

    });

    $("#the_readers-link a.close").click(function () {

        var $bio = $(this).closest("li").find(".bio-expand:visible");

        $bio.stop().animate({

            opacity: 0

        }, {

            duration: 1000,
            complete: function () {

                $(this).hide().css("opacity", "");

            }

        });

    });

    var $lastVisibleGallery = false;

    $("#gallery-link .plus-icon a").click(function () {

        var $link = $(this);

        $lastVisibleGallery = $("#" + $link.attr("class").replace("show-", ""));

        $lastVisibleGallery.fadeToggle("fast", "linear");

        $("img", $lastVisibleGallery).loadImage();

    });

    $("#gallery-link a.back").click(function () {

        if ($lastVisibleGallery)
            $lastVisibleGallery.fadeToggle("fast", "linear");

    });

    $(".boxcaption").mouseenter(function () {

        $(this).addClass("boxcaption-hover");

    }).mouseleave(function () {

        $(this).removeClass("boxcaption-hover");

    });

    $('div.hidden-text> div.new-text').hide();
    $('div.hidden-text> a.button').click(function () {
        $(this).toggleClass("down").prev('div').slideToggle('fast').siblings('div:visible').slideDown('fast');
    });

    $("input[type=text].masked").each(function (idx) {

        $(this).val($(this).attr("title"));

        $(this).bind("focus", function () {

            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
                $(this).addClass("active");
            }

        });

        $(this).bind("blur", function () {

            if ($(this).val() == "") {
                $(this).val($(this).attr("title"));
                $(this).removeClass("active");
            }

        });
    });




    /*$('#btn-submit').click(function () {

    $(".error").hide();
    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    var emailaddressVal = $("#UserEmail").val();
    if (emailaddressVal == '') {
    $("#btn-submit").after('<span class="error">Please enter an email address.</span>');
    $(".error").css("opacity", "0").animate({ top: 3, opacity: 1 }, 200);
    hasError = true;
    }

    else if (!emailReg.test(emailaddressVal)) {
    $("#btn-submit").after('<span class="error">Valid email address is required.</span>');
    $(".error").css("opacity", "0").animate({ top: 3, opacity: 1 }, 200);
    hasError = true;
    }


    if (hasError == true) { return false; }
    else {
    $("#btn-submit").after('<span class="success">Thanks for joining!</span>');

    }

    });*/







    //$('#slider').nivoSlider({ effect: 'fade', slices: 1, animSpeed: 500, pauseTime: 6000 });



    var total = $('#slider img').length;
    var rand = Math.floor(Math.random() * total);
    $('#slider').nivoSlider({
        startSlide: rand,
        effect: 'fade',
        slices: 1,
        animSpeed: 500,
        pauseTime: 6000
    });



    var sliderReaders = $('#slider-readers').bxSlider({
        controls: false
    });

    $('.thumbs-readers a').click(function () {
        var thumbIndex = $('.thumbs-readers a').index(this);
        sliderReaders.goToSlide(thumbIndex);
        $('.thumbs-readers a').removeClass('pager-active');
        $(this).addClass('pager-active');
        return false;
    });

    $('.thumbs-readers a:first').addClass('pager-active');

    var sliderGallery = $('#slider-gallery').bxSlider({
        controls: false
    });

    $('.thumbs-gallery a').click(function () {
        var thumbIndex = $('.thumbs-gallery a').index(this);
        sliderGallery.goToSlide(thumbIndex);
        $('.thumbs-gallery a').removeClass('pager-active');
        $(this).addClass('pager-active');
        return false;
    });

    $('.thumbs-gallery a:first').addClass('pager-active');

    var sliderGallerySub = $('#slider-gallery-sub').bxSlider({
        mode: 'fade',
        controls: false
    });

    $('.thumbs-gallery-sub a').click(function () {
        var thumbIndex = $('.thumbs-gallery-sub a').index(this);
        sliderGallerySub.goToSlide(thumbIndex);
        $('.thumbs-gallery-sub a').removeClass('pager-active');
        $(this).addClass('pager-active');
        return false;
    });

    $('.thumbs-gallery-sub a:first').addClass('pager-active');

    var sliderAdvisory = $('#slider-advisory').bxSlider({

        controls: false

    });

    $('.thumbs-advisory a').click(function () {
        var thumbIndex = $('.thumbs-advisory a').index(this);
        sliderAdvisory.goToSlide(thumbIndex);
        $('.thumbs-advisory a').removeClass('pager-active');
        $(this).addClass('pager-active');
        return false;
    });

    $('.thumbs-advisory a:first').addClass('pager-active');

    // image lazy loader

    var $images = $("img[data-src]");

    var checkImagesInWindowArea = function () {

        var $notLoadedImages = $images.filter(":not(.img-loading):not(.img-loaded)");

        for (var i = 0; i < $notLoadedImages.length; i++) {

            var $image = $notLoadedImages.eq(i);

            if ($image.is(":visible") && $image.offset().top >= $(window).scrollTop() && $image.offset().top <= $(window).scrollTop() + $(window).height()) {

                //console.log("Load: " + $image.data("src") + ": " + $image.offset().left + " x " + $image.offset().top + " // " + $image.is(":visible"));
                $image.loadImage();

            }

        }

    };

    checkImagesInWindowArea();

    $(window).scroll(function () {

        checkImagesInWindowArea();

    });

    $("a.email").each(function () {

        var $link = $(this);
        var email = $link.attr("href").split("||");

        $link.attr("href", "mailto:" + email[1] + email[0]);

    });

    var emailTest = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    $("form#register").submit(function () {

        var $form = $(this);
        $(".error, .success", $form).remove();
        var $email = $("input[name=Email]", $form);
        var go = true;

        if ($email.val().length == 0 || !emailTest.test($email.val())) {

            $("#btn-submit").after('<span class="error">A valid email address is required.</span>');
            $(".error", $form).css("opacity", "0").animate({ top: 3, opacity: 1 }, 200);
            $email.focus();
            go = false;

        }

        if (go) {

            $.ajax({

                url: $form.attr("action"),
                type: $form.attr("method"),
                data: $form.serialize(),
                complete: function () {

                    $("#btn-submit").after('<span class="success">Thanks for joining!</span>');

                }

            });

        }

        return false;

    });

    $("a.buy-now").fancybox({

        onStart: function () { $("#buy-now").show(); },
        onClosed: function () { $("#buy-now").hide(); },
        padding: 0

    });

});
