jQuery.noConflict();
jQuery(document).ready(function() {
	// toggle the component with class msg_body
	
	jQuery('a[name=getFullMsg]').live("click", function() {
		var msg_body = jQuery(this).parent().parent().nextAll(".msg_body");
		var msg_id = msg_body.attr('id');
		if(msg_body.text() == "")
		{
			jQuery.post(siteurl + "/getMessage.php", {
				id : msg_id.split("-")[1],
				action : "getMessage"
			}, function(xml) {
				msg_body.empty();
				msg_body.append("<p>"  + jQuery("text", xml).text() + "</p><br />");
				msg_body.slideToggle(100);
			});
		}
		else
		{
			msg_body.slideToggle(100);
		}
	});
	
	
	function insertPopUp(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var target = jQuery(this);
		var id = jQuery(this).attr('href');
		
		//Get the screen height and width
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		jQuery('#mask').fadeIn(100);	
		jQuery('#mask').fadeTo("slow",0.8);	
		
		//Set the popup window to center
		jQuery(id).css('top',  e.pageY-400);
		jQuery(id).css('left', 100);

	    jQuery('#replytextid').data('parent_id', jQuery(this).attr('data-parent_id'));
	    jQuery('#replytextid').data('topic_id', jQuery(this).attr('data-topic_id'));
	    
	    //transition effect
		jQuery(id).fadeIn(100);
	}
	
	function insertReplyBox(e)
	{
		var replyDiv = jQuery(this).parent().nextAll('.msg_reply');
		if(replyDiv.text() == "")
		{
			var replyId = replyDiv.attr('id');
			var msgId = replyId.split("-")[1]
			replyDiv.append( 
					'<div id="post-new-topic">' + 
					'<h4>Reply:</h4>' + 
					'<p id="post-new"></p>' + 
					'<label>Title:</label><br/>' + 
					'<input type="text" name="subject" id="subjectId-' + msgId + '" maxlength="100" size="105" value=""><br/>' + 
					'<label>Content:</label><br/>' +
					'<textarea rows=7 cols=80 name="replyText" id="replyTextId-' + msgId + '">.</textarea>' + 
					'<div class="sendReply" id="sendReply-' + msgId + '">' + 
					'<input type="button" name="submit_reply" id="submitReply" value="Post Reply">' + 
					'</div>' + 
					'</div>'
			);
			jQuery('#replyTextId-' + msgId).data('parent_id', jQuery(this).attr('data-parent_id'));
		    jQuery('#replyTextId-' + msgId).data('topic_id', jQuery(this).attr('data-topic_id'));
		}
		else
		{
			replyDiv.toggle(100);
		}
	}
	
	jQuery('a[name=reply]').live("click", insertReplyBox);
	jQuery('.sendReply').live("click", function (e) {
		//gather the variables then post back to getMessage.php
		var sendReplyId = jQuery(this).attr("id");
		var msgId = sendReplyId.split("-")[1];
		var replyText   = jQuery('#replyTextId-' + msgId);
		var subjectText = jQuery('#subjectId-' + msgId);
		jQuery.post(siteurl + "/getMessage.php", {
			subject :    subjectText.val(),
			reply_text : replyText.val(),
			topic :      replyText.data('topic_id'),
			parent_id :  replyText.data('parent_id'),
			action : "insertMessage"
		}, function(xml) {
			var insertPointId = "#" + jQuery.trim(jQuery("insertPoint", xml).text());
			var posttext_id = jQuery(insertPointId);
			var posthtml = jQuery("posthtml", xml).text();
			posttext_id.prepend(posthtml).find('a[name=reply]').click(function() { });
			jQuery(insertPointId).parent().siblings(".msg_reply").toggle(100)
		});
	}); 
	
	function Close(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		jQuery('#mask').hide();
		jQuery('.window').hide();
	}
	
	//if close button is clicked
	jQuery('.window .close').click(Close);
	
	jQuery('.window .cancel').click(Close);
	
	//if send button is clicked
	jQuery('.window .send').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//gather the variables then post back to getMessage.php 
		jQuery.post(siteurl + "/getMessage.php", {
			subject : jQuery('#subjecttextid').val(),
			reply_text : jQuery('#replytextid').val(),
			topic : jQuery('#replytextid').data('topic_id'),
			parent_id : jQuery('#replytextid').data('parent_id'),
			action : "insertMessage"
		}, function(xml) {
			var insertPointId = "#" + jQuery.trim(jQuery("insertPoint", xml).text());
			var posttext_id = jQuery(insertPointId);
			var posthtml = jQuery("posthtml", xml).text();
			posttext_id.prepend(posthtml).find('a[name=reply]').click(function() { });
		});
		
		jQuery('#mask').hide();
		jQuery('.window').hide();
	}); 
	
	//if mask is clicked
	jQuery('#mask').live("click", function () {
		jQuery(this).hide();
		jQuery('.window').hide();
	});
});

