/** @logorev: $Revision: 116125 $, $LastChangedBy: peverils $ */
/** 
 * javascript code specific to user account menu
 * @dependencies jquery.js, jquery.cookie.js, logodrupal.js
 */

/** 
 * register module anticipates module
 * AfterlUser already exists, and will only add
 * previouly undefined properties
 */
LogoDrupal.register_module('AfterlUser', {
	prepped : false,
	readied : false,
	active : false,
	flip : function() {
		if (!this.active) {
			$('#afterl-user-account-buttons li.toggle').addClass('active');
			this.active = true;
		}
		else {
			$('#afterl-user-account-buttons li.toggle').removeClass('active');
			this.active = false;
		}
	},
	ready : function() {//due to differing event models this method must be directly callable too
		if (!this.readied && ($('#afterl-user-account-buttons li.toggle').length > 0)) {
			$('#afterl-user-account-buttons li.toggle').click(
			  function() {
				$('#afterl-user-account-menu').slideToggle('fast', 
				  function() {
					LogoDrupal.exec('AfterlUser','flip');
					LogoDrupal.exec('AfterlUser','prep_menu');
				  }
				);
			  }
			);
			this.readied = true;
		}
	},
	
	pull_notifications : function() {
		$.getJSON('/user/notifications?ord=' + Math.floor(Math.random() * 100000000000),
			function(data) {
				if ( data.hasNotifications ) {
					var notification_box = $('#afterl-user-account-notifications')
					var account_box = $('#afterl-user-account-box')
					var notifications = []
					var html = ''
					
					if ( data.messages ) notifications.push('<span id="notification_messages"><a href="/privatemsg">(' + data.messages + ') Message' + (data.messages > 1 ? 's' : '') + '</a></span>')
					if ( data.buddyrequests ) notifications.push('<span id="notification_buddyrequests"><a href="/buddylist/' + data.uid + '/buddies/requests">(' + data.buddyrequests + ') Buddy Request' + (data.buddyrequests > 1 ? 's' : '') + '</a></span>')
					
					notification_box.html(notifications.join(''))
					account_box.addClass('wNotifications')
					
					notification_box.css('opacity', 0)
					notification_box.animate({'opacity':1})
				}
			}
		)
	},
	
	prep_menu : function() {
		$.fn.wait = function(time, type) {
			time = time || 1000;
			type = type || "fx";
			return this.queue(type, function() {
				var self = this;
				setTimeout(function() {
					$(self).dequeue();
				}, time);
			});
		};
		
		if (!this.prepped) {
			if ( $('#afterl-user-account-menu-staff').length > 0 ) { 	
				//todo: wrap this in a test of whether the page is actually cached before presenting
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/user/search_users" id="search-users">Search Users</a></li>');

				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/privatemsg/inbox" id="inbox">Inbox</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="javascript:void(0);" id="afterl-user-account-menu-cachebust">Bust cache of this page &#8984;</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/contributors" id="archives">Contributors</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/buddylist" id="buddylist">Buddylist</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/archives" id="archives">Archives</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/#overlay=admin/content" id="archives">Content List</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/#overlay=node/add/page" id="archives">Add Page</a></li>');
				$('#afterl-user-account-menu-staff li:first').before('<li><a href="/#overlay=node/add/article" id="archives">Add Article</a></li>');
				
				if (typeof Drupal!='undefined' && window.location!= '/' && typeof Drupal.settings!='undefined' && typeof Drupal.settings.afterl_akamai_editlink!='undefined') {
					$('#afterl-user-account-menu-staff li:first').before('<li><a href="'+ Drupal.settings.afterl_akamai_editlink +'" id="afterl-user-account-menu-editlink" class="accessible" accesskey="e">Edit this Page</a></li>');
				}
				
				$('#afterl-user-account-menu li:odd').addClass('odd');
				$('#afterl-user-account-menu li:even').addClass('even');
						
				//Send cache clear request on click of cache bust menu item
				$('#afterl-user-account-menu-cachebust').click(function(){
					var thisPageURL = window.location;
					
					$.ajax({
						type: 'GET',
						url: '/?q=admin/config/caching/akamai_cache_buster/',
						data: 'value=' + thisPageURL,
						success: function(msg){
							if (msg == 'success') {
								cache_bust_success();
							}
							else {
								cache_bust_failure();
							}
						},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							cache_bust_failure();
						}
					});
				});
				
				function cache_bust_success() {
					$('#afterl-user-account-menu-cachebust').fadeOut("medium", function() {	
						$(this).css('color', '#007F0E')
							.html('Cache clear request sent!')
							.fadeIn()
							.unbind('click');
					});
				}
				
				function cache_bust_failure() {
					$('#afterl-user-account-menu-cachebust').fadeOut("medium", function() {	
						$(this).css('color', '#F00')
							.html('Cache clear error. Click to try again.')
							.fadeIn();
					});
				}
			} else {				
				var form_element_bg = $('#afterl-user-account-menu #edit-name').css('background-image');
				var un_box = $('#afterl-user-account-menu #edit-name')
				var pw_box = $('#afterl-user-account-menu #edit-pass')
				un_box.focus(function() { un_box.css('background-image','none'); });
				pw_box.focus(function() { pw_box.css('background-image','none'); });
				un_box.blur(function() { if (un_box.val() == '') un_box.css('background-image',form_element_bg); });
				pw_box.blur(function() { if (pw_box.val() == '') pw_box.css('background-image',form_element_bg); });
				if (un_box.val() != '') un_box.css('background-image','none');
				if (pw_box.val() != '') pw_box.css('background-image','none');				
			}
			
			this.prepped = true;
		}
	}
});
