﻿(function($) {
    // Create a plugin with the name 'watermark'
    $.fn.watermark = function(settings) {
        // Defaults
        var config = {
            watermarkClass: 'watermark',
            defaultText: 'type here...'
        };

        // merge the passed in settings with our default config
        if (settings) $.extend(config, settings);

        // Loop through the elements in the selector that we call the plug-in on
        this.each(function() {
            // Apply defaults to the box
            $(this).addClass(config.watermarkClass).val(config.defaultText);

            // Apply our focus and blur events
            // When we click on the field, we expect it to clear the field and remove the watermark
            $(this).focus(function() {
                $(this).filter(function() {
                    // Check to see if we have a blank field or the default text
                    return $(this).val() === "" || $(this).val() === config.defaultText;
                }).val("").removeClass(config.watermarkClass);
            });

            // When we click off of the field, we expect it to replace the watermark,
            // unless we have entered text
            $(this).blur(function() {
                $(this).filter(function() {
                    // Check to see if the field is blank
                    return $(this).val() === "";
                }).addClass(config.watermarkClass).val(config.defaultText);
            });
        });
    };
})(jQuery);


$(document).ready(function() {
    $("#txtNSName").watermark({ defaultText: 'Name' });
    $("#txtNSEmail").watermark({ defaultText: 'Email Address' });
    $("#subscribe-button").click(subscribe);

    if(typeof($.colorbox) != "undefined")
        $("#send-to-friend").colorbox({href:'send-to-a-friend.aspx?title=' + escape(document.title) + '&url=' + escape(location.href), iframe:true, opacity:.65, width:'400px', height:'520px'});

});

function subscribe() {

    var name = $.trim($("#txtNSName").val());
    var email = $.trim($("#txtNSEmail").val());

    if (name.length == 0 || name == "Name" || email.length == 0 || email == "Email Address") {
        alert("Please enter your name and email address");
        return false;
    }
    /*$(".subscribe input, .subscribe button").attr("disabled", "disabled");*/
    $("#subscribe-button").hide();
    
    $("#subscribe-action").text("Please wait...");


    $.get("newsletter.axd", { "name": escape(name), "email": escape(email) }, subscribeResult);

    return false;
}

function subscribeResult(data) {


    var r = $("#subscribe-action");
    if (data == "0")
        r.html("Thanks,<br/>you've been subscribed");
    else {
        r.html("Sorry,<br/>you were not subscribed.<br/>Please check your email address is entered correctly");
        $("#subscribe-button").show();
    }
    /*$(".subscribe input, .subscribe button").removeAttr("disabled");*/

}
