$(document).ready(function() {
    // Print page button
    $('#printPage').click(function() {
        window.print();
        return false;
    });

    // Slide out behaviour
    $('.sideTools').hover(function() {
        $('.slideBox').fadeIn();
    }, function() {});
    $('.slideBox').hover(function() {
        $('.slideBox').show();
    }, function() {
        $('.slideBox').fadeOut();
    });

    // Bookmark button
    $('#bookmarkPage').click(function() {
        bookmarksite($('title').text(), document.URL);
        return false;
    });

    // Popup email form
    openDialog = function(w,h,t,m,e) {
       jQuery.fn.dialog.open({
          title: t,
          element: e,
          width: w,
          modal: m,
          height: h
       });
    }

    $('#emailPage').click(function() {
        $('#dialogForm').html($('#emailPageFormWrapper').html());
        $('#dialogForm form').addClass('dynamicForm');
        $('.dynamicForm #yourEmail').val(null);
        $('.dynamicForm #theirEmail').val(null);
        $('.dynamicForm #notes').html(null);
        openDialog('300','250','Email a friend',false,'#dialogForm .emailPageForm');
        $('.dynamicForm').validate();
    });
    
    $('.dynamicForm .submit').live('click', function() {
       // validation is automatic
       $.ajax({
           url: CCM_TOOLS_PATH + '/emailpage',
           data: $('.dynamicForm').serialize(),
           success: function(result) {
               if (result == 'sent') {
                   $('.dynamicForm .result').addClass('sent');
                   $('.dynamicForm .result').html('Thank you.<br />A link to this page has been sent to your friend.<br /><br /><a href="#" class="jbx-dialog-close" title="Close this window">Close this window</a>');
               } else {
                   $('.dynamicForm .result').addClass('failed');
                   $('.dynamicForm .result').html('Unfortunately, something seems to have gone wrong.<br />Please try again later.<br /><br /><pre>' + result + '</pre><br /><br /><a href="#" class="ccm-dialog-close" title="Close this window">Close this window</a>');
               }
           }
       });

       return false;
    });

    $('.jbx-dialog-close').live('click', function() {
        $('.dynamicForm #cancel').trigger('click');
    });


});
