/*
  Dynamic features for coyotejunctionequestrian.com

  Copyright (c) 2009 guns
*/

$(document).ready(function() {
  CJ.imageZoom();
  // CJ.emailForm();
});

CJ = (function() {
  var self = {
    zoom:             'a.zoom',
    zoomButton:       '<img src="/images/zoom.png" class="hide zoom-button">',
    emailContactForm: 'form#email-form',
    thankYouMsg:      '<div class="success large bold center">Thank you for your email!</div>'
  };

  return {
    imageZoom: function() {
      $(self.zoom).each(function() {
        // place zoom buttons on top of images
        $(this)
        .append(self.zoomButton)
        .parent('div').css('position', 'relative')
        .children().children('img.zoom-button').css({
          opacity: 0,
          position: 'absolute',
          top: 6,
          right: 6
        // and then bind mouseover fadein
        }).parent(self.zoom).hover(function() {
          $(this).children('img.zoom-button').stop().show().fadeTo(400, 1.0);
        }, function() {
          $(this).children('img.zoom-button').stop().fadeTo(400, 0);
        // bind fancyzoom
        }).fancyZoom({
          scaleImg: true,
          closeOnClick: true
        });
      });
    },

    emailForm: function() {
      $(self.emailContactForm).submit(function(event) {
        event.preventDefault();
        var thisHere = $(this);
        // disable that button!
        thisHere.children('button').attr('disabled', 'disabled');

        // post the data
        $.post(thisHere.attr('action'),
          $.param($('#name, #email, #message')),
          function() {
            thisHere.children('button').removeAttr('disabled');
            thisHere.fadeTo(1000, 0, function() {
              thisHere.parent('div').prepend(self.thankYouMsg).show();
            });
        });
      });
    }
  };
})();

