$(function () {
    // Action Alert
    $('.actionAlert').css('width', $('.actionAlert').css('width'));
    function animate(el, marginLeft, callback) {
        return function () {
            start = $(el).css('margin-left');
            $(el)
            .animate({opacity: 0}, 1000)
            .animate({marginLeft: marginLeft}, 0)
            .animate({marginLeft: start, opacity: 1}, 2000, callback);
        };
    }
    function animateAlert() {
        $('.actionAlert p').each(function () {
            animate(this, '-=500px')();
        });
    }
    animateAlert();
    setInterval(animateAlert, 15000);

    // Default values
    // An argument of force:true will force the replacement of any existing text
    jQuery.fn.DefaultValue = function (text, args) {
        return this.each(function () {
            if (this.type !== 'text' && this.type !== 'password' && this.type !== 'textarea') {
                return;
            }
            $(this).focus(function () {
                if (this.value === text || this.value === '') {
                    this.value = '';
                    $(this).css({'color' : 'black'});
                }
            });
            $(this).blur(function () {
                if (this.value === text || this.value === '') {
                    this.value = text;
                    $(this).css({'color' : 'gray'});
                }
            });
            var fld_current = this;
            $(this).parents("form").each(function () {
                $(this).submit(function () {
                    if (fld_current.value === text) {
                        fld_current.value = '';
                    }
                });
            });
            if (this.value !== '' && (!args || args['force'] !== true)) {
                return;
            }
            this.value = text;
            $(this).css({'color' : 'gray'});
       });
    };

$(function () {
    $('#firstname').DefaultValue('First Name');
    $('#surname').DefaultValue('Last Name');
    $('#from').DefaultValue('From');
    $('#to').DefaultValue('To');
    $('#profirstname').DefaultValue('Your First Name');
    $('#prosurname').DefaultValue('Your Last Name');

    // Date picker
    $("#from").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#to").datepicker({ dateFormat: 'dd/mm/yy' });
});

    // Form validation
    $('#searchnames').submit(function () {
        if (this.elements.surname.value === 'Last Name') {
            alert("Please enter a surname. It is a required field.");
            this.elements.surname.focus();
            return false;
        }
        return true;
    });
    $('#searchpublications').submit(function () {
        if (this.elements.publication.selectedIndex === 0) {
            alert("Please select a publication. It is a required field.");
            this.elements.publication.focus();
            return false;
        }
        return true;
    });
     $('#professionalsearch').submit(function () {
        if (this.elements.profirstname.value === '') {
            alert("Please enter a first name. It is a required field.");
            this.elements.profirstname.focus();
            return false;
        }
        if (this.elements.prosurname.value === '') {
            alert("Please enter a surname. It is a required field.");
            this.elements.prosurname.focus();
            return false;
        }
        if (this.elements.country.value === 'not') {
            alert("Please select a country. It is a required field.");
            this.elements.country.focus();
            return false;
        }
        return true;
    });
   
    // External links in a new window
    $("a[href^='http']").live('click', function (evt) {
        var button = evt.which || evt.button;
        if (button > 1) {
            return;
        }
        if (this.href.indexOf('amemorytree.co.nz') === -1) {
            window.open(this.href);
            return false;
        }
        return false;
    });

    // Prevent listings from being copied
    $('#recentListings, .newspaperresults, #cemeteryList').each(function () {
        this.onselectstart = function () { return false; };
        this.onmousedown = function () { return false; };
        this.onclick = function () { return true; };
    });
});
