/*
 * $Id:$
 *
 * This is a javascript file used for implementing the author contact
 * links shown in the by line of an article.
 */


function closeAuthorContact() { 
  $('#author-contact-box').remove();
  return false;
}


function sendContactAuthorRequest(name, status)  {
  return function(evt) {
    var from = this.form.elements['email'].value;
    var message = this.form.elements['message'].value;
    
    if(from == '' || !isValidEmail(from)) {
      alert("You must provide a valid e-mail");
    } else if(message == '') {
      alert("You must provide a message");
    } else {
      $(status).text('Sending your message...');
      $.post("/contact_author.php",
             { from: from, message: message, name: name },
             function(data, ajaxStatus) {  
               $(status).replaceWith($E('div', {className: 'status'}, 'Message Sent. ',
                                        $E('a', {onclick: closeAuthorContact, href: '#'}, 'Close')));
             });
      return false;
    };
  }
}

function buildContactDialog(realName, twitterUsername, blogUrl, title, imageUrl) {
  function mailto(v) { return "mailto:" + v; };
  function http(v) { return "http://" + v; };
  function entry(value, urlBuilder, icon) {
    var row = icon ? 
      function(chunk) {
        return $E('div', {className: 'row'}, $E('img', {src: 'images/contact-author/' + icon}), ' ', chunk);
      } : function(chunk) { return $E('div', {className: 'row'}, chunk); };
    if(value) {
      if(urlBuilder) {
        return row($E('a', {href: urlBuilder(value), target: '_new'}, value));
      } else {
        return row(value);
      }
    } else {
      return '';
    }
  }
  return $E('div', {id: 'author-contact-box',
                    style_display: 'block' },
            $E('a', {className: 'close',
                     onclick: closeAuthorContact,
                     href: '#'},
               "x"),
            $E('div', {className: 'top'}, $E('h2', {}, 'Meet Our Editors')),
            $E('div', { className: 'body'},
               $E('img', {src: imageUrl, className: 'photo'}),
               $E('div', {className: 'info'},
                  $E('p', {className: 'name'}, realName),
                  entry("pocketnow.com"),
                  entry(title),
                  entry(blogUrl ?
                        "Visit http://" + blogUrl  : false, function(u) { return "http://" + blogUrl },
                       "web_icon.png"),
                  entry(twitterUsername ? "Follow @" + twitterUsername + " on twitter" : false, 
                        function(u) { return "http://twitter.com/" + twitterUsername }, "twitter_icon.png"),
                  $E('form', {},
                     $E('p', {}, $E('b', {}, "Contact " + realName)),
                     $E('p', {}, "Your E-mail: ", 
                        $E('input', {type: 'text', className: 'text', name: 'email'})),
                     $E('textarea', {rows: 3, cols: 20, name: 'message'}),
                     $E('input', {type: 'button', 
                                  value: 'Send Your Message', 
                                  onclick: sendContactAuthorRequest(realName,
                                                                    '#contact-author-status')}),
                     $E('div', { className: 'status', id: 'contact-author-status'}, ''))),
               $E('div', {className: 'clear' })),
               $E('div', {className: 'bottom'}, ""));
}

function showContactPopup(anchor, realName, twitterUsername, blogUrl, title, imageUrl) {
  var dialog =buildContactDialog(realName, twitterUsername, blogUrl, title, imageUrl);

  var pos = $(anchor).offset();
  dialog.style.top = (pos.top + 15) + "px";
  dialog.style.left = (pos.left - 50) + "px";
  

  $('body').append(dialog);

}
