//	JS ENHANCEMENTS

if(typeof logc !== 'function'){
	function logc(inp){
		if (window.console){
			console.log(inp);
		}
		return inp;
	}
}

(function($) {

//	UPDATE INBOX HEADER

	$.chem.updateInboxHeader = function(){

		try{
			if ($('#currentfolder').length == 0){
				return;
			}
			var num = 0,
			href = $('#currentfolder').attr('value').toLowerCase();	   			//		Determine current folder
			switch (href){
				case('inbox'):
					num = 0;
					break;
				case('sent'):
					num = 1;
					break;
				case('trash'):
					num = 2;
					break;
				default:
			}
			$('#inbox_head li').children('.arrow').remove().end().eq(num).append('<span class="arrow"></span>');

			var $h2 = $('#inbox h2');											//		Update page sub title
			switch(num){
				case (0):
					$h2.html('<span>my</span> inbox');
					break;
				case (1):
					$h2.html('<span>my</span> sent messages');
					break;
				case (2):
					$h2.html('<span>my</span> trash');
					break;
			}

			var $alert = $('#inbox span.alert'),
			newmessages = $('#inbox tr.newmessage').length;
			if (num == 0 && newmessages != 0){							   		//		Turn alert on depending on circumstances
				$alert.removeClass('off').text(newmessages);
				$('#inbox ul.folders li').eq(0).addClass('inbox');
			}
		}
		catch(error){
			logc('email header update error');
			logc(error);
		}

	},

//	UPDATE EMAIL UI

	$.chem.updateEmailUI = function(){
		$.chem.updateUI();
		$.chem.updateInboxHeader();

		try{
			$('#inbox input[type="checkbox"]').styleCheckBox();		//	Checkbox styling
			$('#messages th input[type="checkbox"]').selectAll({				//	Select All checkbox for Inbox
				selectboxes: '#messages td input[type="checkbox"]'
			});

			$('#emailtips').psPopup({						//	Email Tips Popup for Inbox
				ajaxrequest: true,
				showoverlay: true,
				ajaxurl: 'emailtips',
				popupid: '#email_match',
				ajaxdata: { spid: $('#spid').attr('value') },
				install: function($box){
					$box.find('a.button').click(function(){
						$box.find('a.close').trigger('click');
					});
				}
			});
			$('#meetingtips').psPopup({						//	Meeting Tips Popup for Inbox
				ajaxrequest: true,
				showoverlay: true,
				ajaxurl: 'meetingtips',
				popupid: '#meet_match',
				ajaxdata: { spid: $('#spid').attr('value') },
				install: function($box){
					$box.find('a.button').click(function(){
						$box.find('a.close').trigger('click');
					});
				}
			});
			$('#getchst').psPopup({							//	Cheatsheet Popup for Inbox
				popupid: '#chst',
				ajaxrequest: true,
				showoverlay: true,
				ajaxurl: 'cheatsheet',
				ajaxdata: { spid: $('#spid').attr('value') },
				error404: 'Cheatsheet is unavailable at this time. Please try again later.',
				error500: 'Cheatsheet is unavailable at this time. Please try again later.'
			});

			$('#inbox input[type="text"]').clearDefaultText({	//	Clear default text in subject and message boxes
				minlength: 2
			});
			$('#inbox textarea').clearDefaultText({
				minlength: 2
			});
			$('#inbox-dropdown span.alert, #inbox-dropdown span.dropdown-count').updateCount();
		}
		catch(error){
			logc('email ui update error');
			logc(error);
		}
	},

//	PLUGIN: UPDATE NEW MESSAGE COUNT

	$.fn.updateCount = function(){
		if (this.length == 0){
			return $(this);
		}
		var $alert = $(this),

		updateAlert = function(){
			var scb = function(data){
				if (data != 0){
					$alert.show().text(data);
				}
			},
			s = {
				successcallback: scb
			};
			$.chem.getNewMessageCount(s);
		}

		updateAlert();

/*
		isSubscriber = function(){
			var scb = function(data){
				if (data == true){
					updateAlert();
				}
			},
			s = {
				successcallback: scb
			};
			$.chem.isSub(s);
		};

		isSubscriber();
*/

		return $(this);
	}

})(jQuery);

//	INITIATE EMAIL UI

$(function(){

	try{
		$.chem.updateEmailUI();
	}
	catch(error){
		logc(error)
	}

});

