var lastLoad = null;

$(document).ready(function() {
	lastLoad = new Date();

	$(".dpicker").datepicker();
	
	if ($("#allCheckbox").length) {
		$("#allCheckbox").click(function() {
			$(".cbox").attr('checked', $("#allCheckbox").attr('checked'));
		});
	}
	
	if ($("#hdTabs").length) {
		$("#hdTabs").tabs();
		
		jQuery(".hdMsg").click(function(e) {
			$("#hdDlg").load('/overview/loadmsg/' + $(this).attr('href'), function() {
				$(".replyLink").click(function(e) {
					$("#hdDlg").load("/overview/replytomsg/" + $(this).attr('href'), function() {
						$("#reply").focus();
						
						$("#replyForm").ajaxForm(function(r) {
							if (r == '1') {
								alert('Message Sent!');
								var dts = new Date();
								
								location.href='/overview?ts=' + dts.getTime() + '#Sent';
								$("#hdDlg").dialog('close');
							} else {
								$("#hdDlg .feedback").html(r);
								$("#reply").focus();
							}
						});
					});
					e.preventDefault();
				});
				$("#hdDlg").dialog('option', 'title', $("#hdDlg .dlgTitle").val());
				$("#hdDlg").dialog('open');
			});
//			alert('load message ' + $(this).attr('href'));
//			$('#hdViewMsg').load('');
			e.preventDefault();
		});
		
		$("#hdDlg").dialog({autoOpen: false, width: 500, height: 300});
		
		$("#hdDraftForm").ajaxForm(function(r) {
			if (r == '1') {
				alert('Message Sent!');
				var dts = new Date();
								
				location.href='/overview?ts=' + dts.getTime() + '#Sent';
				//	$("#hdDlg").dialog('close');
			} else {
				$("#hdDraftForm .feedback").html(r);
				$("#hd_msg").focus();
			}
		});

	}
	
	if ($("#helpDesk").length) {
		$("#hd_folderList li a").click(function(e) {
			$("#hd_folderList li a").removeClass('active');
			$(this).addClass('active');
			var myPanel = '#hd_' + $(this).attr('href').replace('#', '').toString();
			
			$(".hdPanel").hide();
			$(myPanel).show();
			
			if (myPanel == '#hd_Inbox') {
				var dts = new Date();
				
				if ((dts.getTime()-lastLoad.getTime()) > 60) {
					location.href='/overview?ts=' + dts.getTime() + '#Inbox';
				}
			} else {
				e.preventDefault();
			}
		});
		
		var hash = window.location.hash;
		hash = hash.replace('#', '');
		
		if (hash.length) {
			if ($("#hd_folderList li a[rel=" + hash + "]").length) {
				$("#hd_folderList li a[rel=" + hash + "]").click();
			}
		}
		
		jQuery(".hdMsg").click(function(e) {
			$("#hdDlg").load('/overview/loadmsg/' + $(this).attr('href'), function() {
				$(".replyLink").click(function(e) {
					$("#hdDlg").load("/overview/replytomsg/" + $(this).attr('href'), function() {
						$("#reply").focus();
						
						$("#replyForm").ajaxForm(function(r) {
							if (r == '1') {
								alert('Message Sent!');
								var dts = new Date();
								
								location.href='/overview?ts=' + dts.getTime() + '#Sent';
								$("#hdDlg").dialog('close');
							} else {
								$("#hdDlg .feedback").html(r);
								$("#reply").focus();
							}
						});
					});
					e.preventDefault();
				});
				$("#hdDlg").dialog('option', 'title', $("#hdDlg .dlgTitle").val());
				$("#hdDlg").dialog('open');
			});
//			alert('load message ' + $(this).attr('href'));
//			$('#hdViewMsg').load('');
			e.preventDefault();
		});
		
		$("#hdDlg").dialog({autoOpen: false, width: 500, height: 300});
		
		$("#hdDraftForm").ajaxForm(function(r) {
			if (r == '1') {
				alert('Message Sent!');
				var dts = new Date();
								
				location.href='/overview?ts=' + dts.getTime() + '#Sent';
				//	$("#hdDlg").dialog('close');
			} else {
				$("#hdDraftForm .feedback").html(r);
				$("#hd_msg").focus();
			}
		});
	}
});
