﻿/*////////////////////////////////////*/
/* ---- =GLOBAL FUNCTIONS --*/

$(document).ready(function () {

    /*////////////////////////////////////*/
    /* ---- =ROTATOR --*/

    $('#rotator')
	.after('<div id="paging">')
	.cycle({
	    fx: 'fade',
	    speed: 800,
	    timeout: 8000,
	    pager: '#paging',
	    pause: 1
	});

    $('#play').click(function () {
        $('#rotator').cycle('resume');
        $(this).hide().next().show();
        return false;
    }).hide();

    $('#pause').click(function () {
        $('#rotator').cycle('pause');
        $(this).hide().prev().show();
        return false;
    });

    /*////////////////////////////////////*/
    /* ---- =MASONRY --*/

    $('#siteMap').masonry({
        singleMode: true,
        itemSelector: '.siteMapItem'
    });

    $('a.lewis img').css('opacity', 0.5);


    // STYLE CHECKBOXES
    $("input:checkbox").checkbox({
        cls: 'jquery-safari-checkbox',
        empty: '/Images/empty.png'
    });

    // STYLE RADIO BUTTONS
    $("input:radio").checkbox({
        cls: 'jquery-safari-radio',
        empty: '/Images/empty.png'
    });

    //Dropdown menu
    $('#navMenu div.drop').slice(-3).addClass('dropEnd');
	 
    $("#navMenu li").hover(function () {
        $(this).find("div.drop").css('visibility', 'visible');
        $(this).find("div.dropEnd").css('visibility', 'visible');
    }, function () {
        $(this).find("div.drop").css('visibility', 'hidden');
        $(this).find("div.dropEnd").css('visibility', 'hidden');
    });


    //FAQs
    $("dl.revealList a.linkArrowSmall").click(function (event) {
        event.preventDefault();
        var answer = $(this).parent().parent().find(".revealAnswer");
        if (answer.is(":visible")) {
            answer.slideToggle("fast");
            $(this).html("REVEAL ANSWER");
            $(this).attr("title", "REVEAL ANSWER");
            $(this).removeClass("linkArrowClose");
        }
        else {
            answer.slideToggle("fast");
            $(this).html("CLOSE");
            $(this).attr("title", "CLOSE");
            $(this).addClass("linkArrowClose");
        }
    });

    //Buy a brick
    $(".brickSelector li").hover(
        function () {
            $(this).addClass("hover");
        },
        function () {
            $(this).removeClass("hover");
        }
    );

    $(".brickSelector li").click(function (event) {
        event.preventDefault();
        var selectedBrickType = $(this).find("input[type = hidden]").attr("value");
        $(".brickSelector").find("input:checkbox").removeAttr("checked");
        $(".brickSelector li").removeClass("selected");
        $(this).find("input:checkbox").attr("checked", "true");
        $(this).addClass("selected");
        $("#customiseBrick").css({ 'background-image': 'url(/Images/bricks/Designs/' + selectedBrickType + '.png)' });
    });


    //Breadcrumb - Add the on class to the last breadcrumb
    $("ul.crumb li a").last().addClass("on");

    //Donate and buy a brick forms
    $(".oneColFormContents label").each(function () {
        $tb = $("#" + $(this).attr("for"));
        $tb.attr("title", $(this).html());
        if ($tb.val() == "") {
            $tb.val($(this).html());
        }
    });

    $(".oneColFormContents input:text,.oneColFormContents textarea").focus(function () {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
    });

    $(".oneColFormContents input:text,.oneColFormContents textarea").blur(function () {
        if ($(this).val() == "") $(this).val($(this).attr("title"));
    });

    //Gift Aid calculation
    $(".txtDonationAmount").blur(function () { calculatGiftAid(); })
    $("#giftAidRadioButtons input:radio").bind(("check"), function () {
        calculatGiftAid();
    });

});

(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)


function calculatGiftAid() {
    
//    var giftAidPercentage = 28.2;
    
    var giftAidPercentage = 25.0;
    var giftAidAmount = 0.00;
    var DonationAmount = $(".txtDonationAmount,.AdditionalDonation").val();
    
    if (isNaN(DonationAmount) || DonationAmount == "") {
        DonationAmount = 0.00;
    }

    var isEligible = $("#giftAidRadioButtons input:nth(1)").is(':checked');
    if (isEligible) {
        giftAidAmount = DonationAmount * (giftAidPercentage / 100);
    }

    $("#giftAidAmount").html(giftAidAmount.toFixed(2));
    $("#giftaid").html("£" + parseFloat(DonationAmount).toFixed(2));

    var total = parseFloat($("#ctl00_cphContent_ctl00_hfTotal").val()) + parseFloat(DonationAmount);
    $("#total").html("£" + total.toFixed(2));

}
