
var g_currentEmail = 0;
var g_forceSection = "";

//----------------------------------------------------------------
//            Initialization
//----------------------------------------------------------------

jQuery(document).ready(function($) {
  // Do nothing if there is not email
  if ($("#sec_deleted").length==0) return;
  
  // Initialize the editor
  InitEditor();
  
  // Activate the section
  $("#dossiers div.dossierline,.dossierselected")
    .click(changeSection)
    .mouseover(function(){
    $(this).css("cursor","pointer");
  }) 
  $("#sec_inbox").addClass("dossierselected");
  
  // Hide the option
  $(".deleteemail").show();
  $(".replyemail").hide();
  $(".sendemail").hide();
  
  
  // Force the download of the emails 
  $.get("ajax_getmails.php?location=sec_inbox" , maillistReceived);
  
  
  
  
  // Initialize history plugin.
		// The callback is called at once by present location.hash. 
		//$.historyInit(pageload);
		
		// set onlick event for buttons
		$("a[rel='history']").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			$.historyLoad(hash);
			return false;
		});

 
 
 // PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			$("#load").load(hash + ".html");
		} else {
			// start page
			$("#load").empty();
		}
	}



 
//----------------------------------------------------------------
//            Section management
//----------------------------------------------------------------
 
function maillistReceived(data)
{
   // Prepare display
  $("#mailinfo").html("");
  $("#mailinfo").hide();
  $("#maillist").show();
  $("#mailreply").hide();
  
  // Display the content
  $('#inboxcontent').html(data);
  
} 

function changeSection(event)
{
	// Selected element
	var selected = $(this).attr('id')
	if (g_forceSection!="") selected = g_forceSection;
	g_forceSection = "";
	
	if (selected=="sec_sent") $("#tofrom").html("A"); 
	else $("#tofrom").html("De"); 
	// Update each section status
	$("#dossiers div.dossierline,.dossierselected").each(function() {
	   if ($(this).attr('id')==selected) $(this).attr("class", "dossierselected");
	   else $(this).attr("class", "dossierline"); 
  });

  // Display corect button
  $(".emaildelete").show();
  $(".replyemail").hide();
  $(".openemail").show();
  $(".sendemail").hide();
  
  
  // Ask for the emails
  $.get("ajax_getmails.php?location=" + selected, maillistReceived);
}



//----------------------------------------------------------------
//            Mail selection
//----------------------------------------------------------------
function selectEmail(event) {
  // Clean all previous emails
  $("#inboxcontent .maillineselect").attr("class", "mailline");
  // Set the new email selected
  $(this).attr("class", "maillineselect");
}

function deleteEmail(event)
{
  var mailid;
  
  //Delete the current email
  if ($("#mailinfo").is(':visible')) { 
    // Get the id of the current email
    mailid = $("#inboxcontent .maillineselect").attr("id");
    mailid = mailid.replace("line", "");
    
    // Delete the email
    $.get("ajax_manageemail.php?action=delete&id=" + mailid, function(event) {
        g_forceSection = "sec_inbox";
        changeSection(0);
    }); 
  }
  
  else if ($("#maillist").is(':visible')) {
    var list = "";
    
    $(".checkline").each(function (i) {
        mailid = $(this).attr("name");
        mailid = mailid.replace("check_", "");
        if ($(this).attr("checked")) list = list + mailid + "|";
    });
    
    if (list!="") {
      $.get("ajax_manageemail.php?action=deleteall&list=" + list, function(event) {
          g_forceSection = $(".dossierselected").attr("id");
          changeSection(0);
      }); 
    }
    

  }
}

function checkAllMails(event)
{
  var status = $(this).attr("checked");
  
  $(".checkline").each(function (i) {
    $(this).attr("checked", status);
    });
}
$("#inboxcontent .mailline,.maillineselect").live("click", selectEmail)
$("#inboxcontent .mailline,.maillineselect").live("mouseover", function(event) {
  $(this).css("cursor", "pointer");
});
$("#inboxcontent .mailline,.maillineselect").live("dblclick", displayMail);
$(".openemail").live("click", displayMail2);

$(".deleteemail").live("click", deleteEmail);
$("#checkall").live("click", checkAllMails);

//----------------------------------------------------------------
//            Display email
//----------------------------------------------------------------
function displayMail(event) {
  // Clean all previous emails
  $("#inboxcontent .maillineselect").attr("class", "mailline");
  // Set the new email selected
  $(this).attr("class", "maillineselect");
  
  
  var id = $(this).attr("id");
  id = id.replace("line", "");
  g_currentEmail = id;
  
  // Ask for the emails
  $.get("ajax_getmailcontent.php?id=" + id, mailReceived);
}
function displayMail2(event) {
  var mail = $("#inboxcontent .maillineselect");
  if (mail.length==0) return;
  mail = mail.attr("id").replace("line", "");
  if (mail>0) {
    // Ask for the emails
    $.get("ajax_getmailcontent.php?id=" + mail, mailReceived);
  }
}


function mailReceived(data) {
  // Prepare display
  $("#maillist").hide();
  $("#mailreply").hide();
  $("#mailinfo").html(data);
  $("#mailinfo").show();
  
  
  // Display corect button
  $(".emaildelete").show();
  $(".replyemail").show();
  $(".openemail").hide();
  $(".sendemail").hide();
  
  
}


//----------------------------------------------------------------
//            Reply email
//----------------------------------------------------------------
$(".replyemail").live("click", replyEmail);

function InitEditor()
{
	if ( document.all) {
	  iView.document.designMode = 'On';
  }
	else {
    var elem = document.getElementById('iView');
    if (elem) elem.contentWindow.document.designMode = "on";
  }

} 

function setCommand(action)
{
	if ( document.all) iView.document.execCommand(action, false, null);
	else document.getElementById('iView').contentWindow.document.execCommand(action, false, null);

}

function replyEmail(event) {
  // Get the dossier selected
  var dossier = $(".dossierselected").attr("id");
  
  
  // Get current email information
  var subject = $("#mail_subject").text();
  var sender;
  if (dossier=="sec_sent") sender = $("#receiver_name").text();
  else sender = $("#mail_name").text();
  var content = $("#mailcontent").html();
  
  // Set reply information
  $("#reply_name").text(sender);
  $("#reply_subject").text("RE: " + subject);
  
  // Display correct div
  $("#maillist").hide();
  $("#mailinfo").hide();
  $("#mailreply").show();
  
  // Display corect button
  $(".deleteemail").hide();
  $(".replyemail").hide();
  $(".sendemail").show();
  
  // Put content
  var iframe = $("#iView").contents();
  
  content = "<BR/>------------------------------------------<BR/>" + content;
  $("body", iframe).html(content);
  
}


//----------------------------------------------------------------
//            Send email
//----------------------------------------------------------------
function replyMessageSent(event)
{
  // Return to the inbox section
  $("#sec_inbox").trigger("click")

  alert("Votre message a \351t\351 envoy\351");
  
  
}
function sendEmail(event)
{
  // Get content
  var content = $("#iView").contents().find('body').html();
  
  // Send the message
  data = {"id":g_currentEmail, "content":content};
  $.post("ajax_sendmail.php", data, replyMessageSent);
  
}
$(".sendemail").live("click", sendEmail);


});
 
