$(function(){
  $('a[rel=images]').colorbox();


  $("input.replace_text").each(function(){
    val = $(this).val();
    $(this).val('');

    $(this).input_replacement({text: val});
  });

  $('.enquiry').colorbox({inline: true, href: '#enquiry_form'});

  $('#nav-' + $('body').attr('id').substr(2)).addClass('active');


  $("span.safemail").each(function(){
    exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
    match = exp.exec($(this).text());
    addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
    emaillink = match[2] ? match[2] : addr;
    subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
    $(this).after('<a href="mailto:'+addr+subject+'">'+ emaillink + '</a>');
    $(this).remove();
  });
  $("input.safemail").each(function(){
    $(this).val($(this).val().replace(/ at /,"@").replace(/ dot /g,"."));
  });
});




(function($) {
    $.fn.input_replacement = function(options) {
        // Compile default options and user specified options.
        var opts = $.extend({}, $.fn.input_replacement.defaults, options);
        return $(this).each(function() {
            var obj = $(this);
            // Build element specific options.
            obj.o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            // If field is empty, append text, classes, etc...
            if (obj.val() == '') {
                obj.val(obj.o.text);
                if (obj.o.empty_class) {
                    obj.addClass(obj.o.empty_class);
                };
                // Focus on the input has occurred.
                obj.bind('focus', function() {
                    if (obj.val() == obj.o.text) {
                        obj.val('');
                    };
                    if (obj.o.empty_class) {
                        obj.removeClass(obj.o.empty_class);
                    };
                });
                // Focus has been lost.
                obj.bind('blur', function() {
                    if (obj.val() == '') {
                        obj.val(obj.o.text);
                        if (obj.o.empty_class) {
                            obj.addClass(obj.o.empty_class);
                        };
                    };
                });
                // Clear out the values on reload so they arent loaded after refresh.
                $(window).unload(function() {
                   if (obj.val() == obj.o.text) {
                       obj.val('');
                   }; 
                });
                // If nothing was entered, make sure the "text" is not submitted by removing it.
                var form = obj.parents('form'); //.map(function () { return this.tagName; }).get().join(", ");
                if (form) {
                    form.find("input[type=submit]").each(function() {
                        $(this).bind('click', function() {
                            if (obj.val() == obj.o.text) {
                                obj.val('');
                            };
                        });
                    });
                };
            };
        });
    };

    $.fn.input_replacement.defaults = {
        text: '', // The text to put in the empty input field.
        empty_class: '' // A class to be applied to empty input field. Gets removed after 'focus'.
    };
})(jQuery);
