$(function() {
	$('.activity-show-more').click(function(e){
		e.preventDefault();
		$(this).parents('li:first')
			.slideUp()
			.siblings('li')
			.slideDown();
	});
	
	$('ul.activity li').bind('mousemove', function(e){
		$('.preview').hide();
		$(this).find('.preview')
			.css('top', e.pageY + 10)
			.css('left', e.pageX + 10)
			.show();
	});
	
	$('ul.activity li').bind('mouseout', function(e){
		$('.preview').hide();
	});
	
	if (typeof CKEDITOR != 'undefined') {
		CKEDITOR.config.toolbar_rr = [
		    ['Source', 'Preview', '-', 'Undo', 'Redo', 'PasteFromWord', 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock', 'Bold', 'Italic', 'Underline', 'Strike', 'Format', 'Font', 'FontSize'],
		    ['TextColor', 'BGColor', 'Link', 'rr_resources', 'Image']
		];
		
		CKEDITOR.config.toolbar = 'rr';
		CKEDITOR.config.skin = 'rr';
		CKEDITOR.config.enterMode = CKEDITOR.ENTER_BR;
		CKEDITOR.config.shiftEnterMode = CKEDITOR.ENTER_P;
	}
	
	$('form:not(.ajax)').submit(function(e) {
		if (!$(this).parents('.ui-dialog, .account-survey').length) {
			ReviewRoom.blockForm($(this));
		}
	});

	try {
		$('#admin form select:not([multiple="multiple"])').each(function() {
			if (!$(this).find('optgroup').length && !$(this).parents('#restrictions')) {
				$(this).rrSelector();
			}
		});
	} catch(e) {}
});

ReviewRoom = {
	cache: {
		set: function(key, value) {
			$.post(CACHE_URL, {
				method: 'set',
				key: key,
				value: $.toJSON(value)
			}, function() {}, 'json');
		},
		
		get: function(key, deflt) {
			var result;

			$.ajax({
				async: false,
				cache: false,
				dataType: 'json',
				url: CACHE_URL,
				data: {
					method: 'get',
					key: key
				},
				success: function(response) {
					result = response.value;
				}
			});

			return (result !== null)? result : (deflt !== undefined)? deflt : null;
		},

		clear: function(key) {
			$.post(CACHE_URL, {
				method: 'clear',
				key: key
			}, function() {}, 'json');
		}
	},

	block: function(elem, message) {
		message = (message || gettext('Working...'))
			+ '<div><img src="/media/img/reviewroom/ajax-loader.gif"><\/div>';
		
		var config = {
			message: message,
			applyPlatformOpacityRules: false,
			css: {
	            border: 'none', 
	            padding: '3px', 
	            backgroundColor: '#fff', 
	            opacity: .9, 
	            color: '#fff',
				fontSize: '14px',
				color: '#555',
				width: '20%',
				'line-height': '30px',
				'-moz-box-shadow': '0 0 20px #777',
				'-webkit-box-shadow': '0 0 20px #777',
				'box-shadow': '0 0 20px #777'
			},
			overlayCSS: {
				backgroundColor: '#e5e5e5'
			},
			baseZ: 90000			
		};
		
		if (elem) {
			$(elem).block(config)
		}
		else {
			$.blockUI(config);
		}
	},
	
	blockForm: function(form, msg) {
		msg = (msg || gettext('Working...'));
		
		var message = $('<div></div>', {
			'class': 'message',
			text: msg,
			css: {
				top: ((form.height() / 2) - 20)
			}
		})
		
		form.prepend($('<div></div>', {
			'class': 'form-block',
			css: {
				width: form.width(),
				height: form.height()
			}
		}).append(message));
	},
	
	unblock: function(elem) {
		if (elem) {
			$(elem).unblock()
		}
		else {
			$.unblockUI();
			$('.blockUI').remove();				
		}
	},
	
	compressor: {
		init: function() {
			$('body').append('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
				'width="0" height="0" id="json-compressor" ' +
				'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">' +
				'<param name="movie" value="/media/extra/Compressor.swf" />' +
				'<param name="quality" value="high" />' +
				'<param name="bgcolor" value="#869ca7" />' +
				'<param name="allowScriptAccess" value="always" />' +
				'<embed src="/media/extra/Compressor.swf" quality="high" bgcolor="#869ca7"' +
					'width="0" height="0" name="json-compressor" align="middle"' +
					'play="true"' +
					'loop="false"' +
					'quality="high"' +
					'allowScriptAccess="always"' +
					'type="application/x-shockwave-flash"' +
					'pluginspage="http://www.adobe.com/go/getflashplayer">' +
				'</embed>' +
			'</object>');
		},
		
		available: function() {
			return DetectFlashVer(9, 0, 0);
		},
		
		compress: function(data, callbackName) {
			if (this.available) {
				try {
					(window['json-compressor'] || document['json-compressor'] || document.getElementById('json-compressor')).compress(data, callbackName);
				}
				catch(e) {
					(window[callbackName] || document[callbackName])(data, false);
				}
			}
			else {
				(window[callbackName] || document[callbackName])(data, false);
			}
		}
	}	
};

