﻿$(document).ready(function() {

// initialise plugins
    $('ul.sf-menu').supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({
        hoverClass:    'sfHover',          // the class applied to hovered list items 
        //pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
        //pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
        delay:         800,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing
        animation: { opacity: 'show', height: 'show' },   // an object equivalent to first parameter of jQuery’s .animate() method 
        speed:         'normal',           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
        autoArrows:    true,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
        dropShadows:   false,               // completely disable drop shadows by setting this to false 
        disableHI:     false              // set to true to disable hoverIntent detection 
        //onInit:        function(){},       // callback function fires once Superfish is initialised – 'this' is the containing ul 
        //onBeforeShow:  function(){},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
        //onShow:        function(){},       // callback function fires once reveal animation completed – 'this' is the opened ul 
        //onHide:        function(){}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed
    }).find('ul').bgIframe({ opacity: false });

    // Simple menu
    /*
    $('ul#nav li').hover(function() {
        $('ul.sub', this).show();
    }, function() {
        $('ul.sub', this).hide();
    });
    $('ul#nav li ul.sub li').hover(function() {
        $('a:first', this).css('color', '#fff');
        $('div:first', this).css('background-color', '#464545');
    }, function() {
        $('a:first', this).css('color', '#333');
        $('div:first', this).css('background-color', '');
    });
    */

    $.validator.addMethod(
        "AUDate",
        function(value, element) {
            return value.match(/^(?:(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2})$/);
        },
        "Please enter a date in the format dd/mm/yyyy"
    );

    // validation
    $('form.validatable').validate();

    // z card login
    $('#zcard form input[name=email]').click(function() {
        if ($(this).val() == 'Email Address') { $(this).val(''); $(this).css('color', '#000'); }
    });
    $('#zcard form input[name=email]').focus(function() {
        if ($(this).val() == 'Email Address') { $(this).val(''); $(this).css('color', '#000'); }
    });
    $('#zcard form input[name=rewards]').click(function() {
        if ($(this).val() == 'Z Card Number') { $(this).val(''); $(this).css('color', '#000'); }
    });
    $('#zcard form input[name=rewards]').focus(function() {
        if ($(this).val() == 'Z Card Number') { $(this).val(''); $(this).css('color', '#000'); }
    });

    // photo galleries
    $('div.photo-gallery a').lightBox({
        imageLoading: '/content/images/lib/lightbox/loading.gif',
        imageBtnClose: '/content/images/lib/lightbox/close.gif',
        imageBtnPrev: '/content/images/lib/lightbox/prev.gif',
        imageBtnNext: '/content/images/lib/lightbox/next.gif'
    });
});


function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

//Validate the Recaptcha' Before continuing with POST ACTION
function validateCaptcha(form) {
    if ($(form).valid()) {
        challengeField = $("input#recaptcha_challenge_field").val();
        responseField = $("input#recaptcha_response_field").val();
        //console.log(challengeField);
        //console.log(responseField);
        var success = false;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/forms/ajax/validate-captcha",
            data: "recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
            async: false,
            success: function(result) {
                if (result != null && result.success) {
                    success = true;
                }
            },
            error: function(req, status, error) {
                success = false;
            }
        });

        //console.log( html );
        if (success) {
            $("#captchaError").hide();
            return true;
        } else {
            $("#captchaError").html("The text you entered did not match. Please try another one.");
            $("#captchaError").show();
            Recaptcha.reload();
            return false;
        }
    }
    return false;
}

