
var dCommon = {

	/* when hide/show something when something happens.
	 * generic to be bindable to many different scenrios */
	onCollapse: function(carea) {
		var jcarea = jQuery(carea);
		if(jcarea.is(':visible')) jcarea.slideUp();
		else jcarea.slideDown();
		return;
	},
	
	/* enable a text element to have a default value that gets
	 * cleared when the element is focused, and restored if left
	 * blank.
	 */
	enableTextInputFuss: function(input,deftext,focusclass) {
		jinput = jQuery(input);
		
		jinput.focus(function(){
			if(jQuery.trim(jinput.val()) == deftext) {
				jinput.val('');
				if(focusclass) jinput.addClass(focusclass);
			}
		});
		
		jinput.blur(function(){
			if(jQuery.trim(jinput.val()).length == 0) {
				jinput.val(deftext);
				if(focusclass) jinput.removeClass(focusclass);
			}
		});
		
		return;
	},
	
	/* enable a text element to update a counter element with
	 * the number of characters in it. if the current value
	 * is set to an elements default text then the count is 0
	 */
	enableTextInputCounter: function(input,counter,deftext) {
	
		jQuery(input).bind('keyup focus blur',function(){
			if(deftext && jQuery.trim(jQuery(input).val()) != deftext) {
				var length = jQuery.trim(jQuery(input).val()).length;
				jQuery(counter).text(length);
			} else {
				jQuery(counter).text('0');
			}
		}).blur();
		
		return;
	}
}

//////////////////////////////////////////
//////////////////////////////////////////

var dWriter = {
	onEditor: function() {

		jQuery('#editor').ckeditor(
			function(){},
			{'toolbar':'BlogsterPost'}
		);
		
		var deftext = 'Write your post title here...';
		dCommon.enableTextInputFuss('#title',deftext,'writer-entry1-active');
		dCommon.enableTextInputCounter('#title','#title-counter',deftext);
	
		return;
	},

	onSavePost: function() {

		return;
	}
}

//. adding some stuff to ck when we are done.
jQuery(document).ready(function(){/*
	CKEDITOR.config.toolbar_BlogsterPost = [
		[
		 'Bold','Italic','Underline','-',
		 'Cut','Copy','Paste','-',
		 'Link','Unlink','-',
		 'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-',
		 'Outdent','Indent','Blockquote'],'/',
		[
		 'Styles','-','Format','Font','FontSize','-','Image'
		]
	];
	
	return;
*/});

//////////////////////////////////////////
//////////////////////////////////////////

var dNotify = {

	/* hide/show the notification bar. i think i want to build some sort of state
	 * memory so that it remembers when it was hidden
	 */
	onHideShow: function() {
		var bar = jQuery('#notebar');

		if(bar.is(':visible')) {
			bar.slideUp();
		} else {
			bar.slideDown();
		}

		return;
	},
	
	onGetNext: function() {
	
		jQuery.post(
			'/ai/notify-get.api/' + (new Date()).getTime(),
			{ },
			function(obj) {
				if(obj.data.length) {
					jQuery('#notebar-content').html(
						'<a class="delete" href="javascript:dNotify.onDelete(' + obj.data[0].id + ');">&times;</a> ' +
						'<a class="link" href="' + obj.data[0].link + '">' + obj.data[0].text + '</a>'
					);
				} else {
					jQuery('#notebar-content').html('Welcome to Blogster!');
				}
				return;
			},'json'
		);
	
		return;
	},
	
	onDelete: function(id) {
	
		jQuery('#notebar-content').html('<em>loading...</em>');
	
		jQuery.post(
			'/ai/notify-delete.api/' + (new Date()).getTime(),
			{ 'id':id },
			function(obj) {
				dNotify.onGetNext();
				return;
			},'json'
		);
	
		return;
	}

}

jQuery(document).ready(function() {

	dNotify.onGetNext();
	
	return;
});

//////////////////////////////////////////
//////////////////////////////////////////

