if ($('#pmlink').length) {

var newmessage = $("#pmlink").attr("href");
var pmtitle = $("#pmlink small:contains(Subject)").next().html();

a= document.getElementById('pmlink').innerHTML.match(/You have \d+ new messages/);

if(!a) {
 $("#pmlink").attr("href","javascript:liveview()");
 s = document.getElementById('pmtoast').innerHTML.match(/Goto Inbox/);
  if(!s) { 
 $("#pmclose").after("<div  id='closethis'><span class='left'><a href='"+newmessage+"' style='font-size:80%;display:block;font-size:90%;padding:1px 10px;'>Goto Inbox</a></span><a id='pmclose' href='#'>Close</a></div>").remove();
 
$("#closethis").css("background","#E8EDF1 url(http://z3.ifrm.com/11/88/0/p23751/th.gif) repeat-x scroll center top");

$("#pmclose").click(function() { $("#pmtoast").hide("slow"); return false; });


  }
 }
 
 function liveview(){
 

$("#pmtoast").css("z-index","51").animate({ 
        width: "50%",
        top: "15%",
        left: "25%",
		height: "285px"
		}, 1500 );

$('body').append("<div id='backG' style='opacity:0.7;background-color:#000000; height:100%; width:100%; position:fixed; top:0; left:0; z-index:50'></div>");


$.get(newmessage, function(x) { 
name = $("#conversation",x).find(".c_postinfo span.left:first").html();
info = $("#conversation",x).find(".c_post:first").html();

 xc = $('input[name=xc]',x).val(), mid = $('input[name=mid]',x).val(), secure = $('input[name=secure]',x).val();

$("#pmlink").remove();
$("#closethis").before("<div style='display:none;position:absolute;margin-left:5px;width:240px; border:2px solid #87A8C5; overflow:hidden;' id='inboxlist'><table ><thead style='font-weight:bold;'><th>Title</th><th>Conversation with:</th></thead><tbody id='thelist' style='padding-left:5px; text-align:left;height:284px;'></tbody></table></div><div style='text-align:left;font-size:13px; height:50px; border-bottom:2px solid #eeeeee'><h2 style='font-weight:bold;padding:6px;'><span class='right'><img src='http://z1.ifrm.com/0/2/0/p401400/pm_folder.png' alt='PM list' title='View PM Listing' id='pmlist' style='cursor:pointer' />  <img src='http://z1.ifrm.com/0/2/0/p401381/lpmconvo.png' alt='Full Convo' title='Full Conversation' id='fullconvo' style='cursor:pointer' />  <img src='http://z1.ifrm.com/0/2/0/p401380/lpmreply.png' alt='Reply' id='replytopm' style='cursor:pointer' title='Reply To The PM'/>  <a href='"+newmessage+"' id='gotopmmessage'><img src='http://z3.ifrm.com/11/88/0/p25701/inbox.png' alt='Go To Message' title='Go to the Message' /></a>  <img id='closepm' src='http://z3.ifrm.com/11/88/0/p26690/decrease.png' alt='Close' title='Close' style='cursor:pointer' /></span>"+name+"<div>Subject: <span id='changetitle'>"+pmtitle+"</span></div></h2></div><div style='padding:5px; text-align:left;height:200px; overflow:auto;' id='fullcon'>"+info+"</div><div style='text-align:center; border-top:2px solid #eeeeee;display:none;' id='quickreply'><textarea id='quickcompose' name='post' cols='30' rows='5'/><div><button id='sendmessage' type='submit' style='cursor:pointer'>Send Message</button></div></div>").remove();

$("#pmlist").click(function() {
if(document.getElementById("inboxlist").style.display == "none") {
if( $("#thelist").html() ) {
$("#inboxlist").fadeIn(1000);
}
else{ 
$("#inboxlist").css("background","#B8C7DA url(http://z3.ifrm.com/11/88/0/p27337/pm_pop.png) repeat-x scroll 0 -10px").fadeIn(1000,function() {$(this).animate({ 
        left: "-245px",
		}, 1500 ); });
		
$.get(main_url + 'msg', function(q) {
$('tr.row1, tr.row2',q).find("a:not(.member)").parent().each(function() { 
pmtitle = $(this).children(a).html();
pmsrc = $(this).children(a).attr("href");
pmname = $(this).next().html();
$('#thelist').append("<tr><td><a href='javascript:void(0)' onclick=\"showmethepm('"+pmsrc+"')\">"+pmtitle+"</a></td><td>"+pmname+"</td></tr>");
});
});

$("#thelist").css("overflow","auto");
}
}
else {$("#inboxlist").fadeOut(1000);}

 });


$("#fullconvo").click(function(){ 
var fullconvopm = $("#conversation",x).html();
$("#fullcon").html("<table id='conversation'>"+fullconvopm+"</table>");
$("#conversation").find(".c_postinfo span.right").remove()
		});

$("#closepm").click(function(){ 
$("#pmtoast").fadeOut(1000);
		$("#backG").remove(); 
		});
	
$("#sendmessage").click(function(){ 
var post = $("#quickcompose").val();
var gethref = $("a#gotopmmessage").attr("href")
var msg =  gethref.split(/id=/)[1];
$.post(main_url + 'msg/?c=3&sd=1', { msg: msg, mid:mid, xc:xc, secure:secure, post:post}, function(return_data) {
$("#quickreply").html("Reply Sent!");
$("#pmtoast").css("height","300px");
}); 		});
	
$("#replytopm").toggle(function() {
b= document.getElementById('quickreply').innerHTML.match(/Reply Sent/);
  if(b) { 
$("#pmtoast").css("height","300px");
$("#quickreply").slideDown(1000);
}else {
$("#pmtoast").css("height","380px");
$("#quickreply").slideDown(1000)
   } },
 function() {
$("#quickreply").slideUp(1000),
$("#pmtoast").css("height","285px");
  });	

});
	
}

  // figure out where this will work  :
 function showmethepm(thesrc) {
 $("#fullconvo,#replytopm").unbind('click').css("opacity",".5");
 $.get(thesrc, function(h) {
 $("a#gotopmmessage").attr("href",thesrc)
var fullconvopm = $("#conversation",h).html();
$("#changetitle").html($("#pm_viewer thead",h).text());
$("h2 a.member").html($(".c_username",h).html());
$("#fullcon").html("<table id='conversation'>"+fullconvopm+"</table>");
$("#conversation").find(".c_postinfo span.right").remove()
});
} 
 $('#pmtoast').resizable();
}
