﻿/* common jquery etc. extensions for use throughout the application */

(function ($) {
    $.fn.toggleDisabled = function (val) {
        return this.each(function () {
            this.disabled = (val == undefined) ? !this.disabled : !!val;
        });
    };
    $.fn.toggleReadOnly = function (val) {
        return this.each(function () {
            if (val) $(this).attr('readonly', 'readonly');
            else $(this).removeAttr('readonly');
        });
    };

    $.fn.bindFirst = function (name, fn) {
        // bind as you normally would
        // don't want to miss out on any jQuery magic
        this.bind(name, fn);

        // Thanks to a comment by @Martin, adding support for
        // namespaced events too.
        var handlers = this.data('events')[name.split('.')[0]];
        // take out the handler we just inserted from the end
        var handler = handlers.pop();
        // move it at the beginning
        handlers.splice(0, 0, handler);
    };

})(jQuery);

$(function () {
    $('.toggleActionShow').click(function () {
        $('.toggleText').show('fast');
        $(this).hide();
        $('.toggleActionHide').show();
    });
    $('.toggleActionHide').click(function () {
        $('.toggleText').hide('fast');
        $(this).hide();
        $('.toggleActionShow').show();
    });
    //$('.show-tooltip').tooltip();
    //$('.show-popover').popover();
    $('.btn-inverse:contains("Cancel")').click(function () {
        return confirm("Are you sure you want to cancel this transaction?");
    });
    $('.first-step').click(function () {
        return confirm("You are navigating away from this transaction and the data you have entered so far will be lost.  Are you sure?");
    });

    // clear error class on focus
    $('#wrapper').on('blur', '.error input', function () { ClearError($(this)) });
    $('#wrapper').on('blur', '.error select', function () { ClearError($(this)) });
    $('#wrapper').on('click', '.address-checkbox', function () { ClearAddressError($(this)) });

    //// scrollTo
    //$(document).ready(function () {
    //    $('#mobile_icon').localScroll({
    //        duration: 250
    //    });
    //});

    $("input.date-picker").blur(function () { SetDateInputWatermark($(this)) });
    $("input.date-picker").focus(function () { ClearDateInputWatermark($(this)) });
    $("input.date-picker").each(function () { SetDateInputWatermark($(this)) });


    $("input.datepicker-dob-ownerorjoint").datepicker(
      {
          maxDate: "-16y",
          minDate: "-100y",
          dateFormat: "dd/mm/yy",
          changeMonth: true,
          changeYear: true,
          yearRange: "-100:-16"

      }
      )

    $("input.dog-date-picker").blur(function () { SetDateInputWatermark($(this)) });
    $("input.dog-date-picker").focus(function () { ClearDateInputWatermark($(this)) });
    $("input.dog-date-picker").each(function () { SetDateInputWatermark($(this)) });

    // this is required to make sure the event below is bound before anything else (e.g. validator)
    if ($('form').length > 0) {
        $('form').bindFirst('submit', function () {
            $("input.date-picker").each(function () { ClearDateInputWatermark($(this)) });
            $("input.dog-date-picker").each(function () { ClearDateInputWatermark($(this)) });
        });
    }

    $('input.email-reminder').click(function () {
        $('span.email-reminder').toggleClass('hide', !$(this).attr('checked'));
    });

    $('span.email-reminder').toggleClass('hide', !$('input.email-reminder').attr('checked'));

    $('input.suburb-city').blur(function () { ClearCityError($(this)) });
});

function ClearCityError(obj) {
    obj.closest('.row').find('.suburb-city').each(function () { ClearError($(this)) });
}

function SetDateInputWatermark(obj) {
    if (obj.val() != '') return;
    obj.val('DD/MM/YYYY');
}

function ClearDateInputWatermark(obj) {
    if (obj.val() != 'DD/MM/YYYY') return;
    obj.val('');
}

function ClearError(obj) {
    obj.closest('.error').removeClass('error');
    obj.parent().find('.field-validation-error').html('');
}

function ClearAddressError(obj) {
    if (!obj.attr('checked')) return;
    obj.closest('fieldset').find('.error input').each(function () { ClearError($(this)) });
}
$(function () {
    $('.popover-disc').hover(function () {
        $('.popover-inner').width(183);
        $('.popover-inner').css('text-align', 'center');
    });
    $('.popover-tag').hover(function () {
        $('.popover-inner').width(400);
        $('.popover-inner').css('text-align', 'center');
    });
});