//this document contains all the functions needed handle
//the ajax popup form that will send the article as an emial;
/*  NOTE: jquery must included prior to the inclusion of this
          script in order for it to function;
*/



$(function() {
  var dialoghtml ='<div id="emial-article-dialog" title="Send this article to a friend">'
      dialoghtml+='  <div id="email-article-form">'
      dialoghtml+='    <div id="error-msg"></div>'
      dialoghtml+='    <label for="article-from-name">Your Name:</label>'
      dialoghtml+='    <input type="text" name="fromname" id="article-from-name" value="" class="text ui-widget-content ui-corner-all" />'
      dialoghtml+='    <label for="article-from-email">Your Email:</label>'
      dialoghtml+='    <input type="text" name="email" id="article-from-email" value="" class="text ui-widget-content ui-corner-all" />'
      dialoghtml+='    <label for="article-recipient">Friend\'s Email:</label>'
      dialoghtml+='    <input type="text" name="recipient" id="article-recipient" class="text ui-widget-content ui-corner-all" />'
      dialoghtml+='      <em class="small-text" style="display:block;margin-bottom:5px;">You may enter up to 5 friend\'s email addresses separated by commas.</em>'
      dialoghtml+='    <label for="article-comments">Comments: <em class="small-text">( optional )</em></label>'
      dialoghtml+='    <textarea name="comments" id="article-comments" class="text ui-widget-content ui-corner-all"></textarea>'
      dialoghtml+='  </div>'
      dialoghtml+='  <div id="email-article-processing">'
      dialoghtml+='    Processing...<br/>Please Wait...'
      dialoghtml+='  </div>'
      dialoghtml+='  <div id="email-article-sent">'
      dialoghtml+='    An email has been sent to <span id="email-recipients">[email address]</span> with a link to this article. '
      dialoghtml+='  </div>'
      dialoghtml+='</div>'

  $("body").append(dialoghtml);
  $("#emial-article-dialog").dialog({
    bgiframe: true,
    autoOpen: false,
    width: 450,
    modal: true,
    buttons: {
      'Send Article': function(e) {
        var formdata = {
          recipient:$("#article-recipient").val(),
          fromaddress:$("#article-from-email").val(),
          fromname:$("#article-from-name").val(),
          comments:$("#article-comments").val(),
          headline:$('.mastHeader p:not(.date)').text(),
          uri:window.location.href
        };
        
        $(e.target).attr('disabled','disabled');
        $("#error-msg").empty();
        $("#email-article-form").hide();
        $("#email-article-processing").show();
        
        $.ajax({
          async:true,
          cache:false,
          data:formdata,
          dataType:'json',
          type:'GET',
          url:'/includes/email_article/send-article.asp',
          success:function(json,textStatus)
          {
            if( json.status.email )
            { //info valid and email sent
              $("#email-article-sent #email-recipients").text($("#article-recipient").val());
              $("#email-article-sent").show();
              e.target.style.display="none";
            }
            else //errors were returned
            {
              for( var i=0;i<json.messages.length;i++ )
              {
                $("#error-msg").append("<div>"+json.messages[i]+"</div>");
              }
              $("#email-article-form").show();
              $(e.target).attr('disabled','');
            }
          },
          error:function(xhr,textStatus,errorThrown)
          {
            //display error
            $("#error-msg").html('We were unable to send this article to your friend. Please try again.')
          },
          complete:function(xhr,textStatus)
          {
            //re-enable buttons
            $("#email-article-processing").hide();
          }
        })//end $.ajax
      }, //end send article button
      'Close': function() {
        $(this).dialog('close');
      }
    }, //end buttons
    close: function() {
      $("#error-msg").empty();
      $("#emial-article-dialog").nextAll(".ui-dialog-buttonpane:first").children("button").each(function(){this.disabled=false;this.style.display='block';});
    },
    open:function(){
      $("#email-article-form").show();
      $("#email-article-processing").hide();
      $("#email-article-sent").hide();
      $("#article-recipient").val('')
    }
  });//end options
  
  
  
  $('.emial-article-link').click(function() {
    $('#emial-article-dialog').dialog('open');
  })
  .hover(
    function(){
      $(this).addClass("email-link-hover"); 
    },
    function(){
      $(this).removeClass("email-link-hover"); 
    }
  )
  .mousedown(function(){
    $(this).addClass("ui-state-active"); 
  })
  .mouseup(function(){
      $(this).removeClass("ui-state-active");
  });

});

