
/*
	Copyright (c) 2007-2009 JB Interactive Pty. Ltd.
	All Rights Reserved
	http://www.jbinteractive.com.au/
*/

(function ($) {
	var ConnectBooks = {
		cookie: {
			name:    'cb_announcements',
			options: {
				path:    '/',
				expires: 365
			}
		},
		
		init: function () {
			var self = this;
			
			// Announcement
			self.bindAnnouncement();
			
			// Form submissions
			self.formSubmissions();
		},
		
		bindAnnouncement: function () {
			var self = this;
			
			$('.announcement a[href=#close]').click(function (e) {
				e.preventDefault();
				
				self.hideAnnouncement($(this).parent('div'));
			});			
		},
		
		hideAnnouncement: function (item) {
			var self = this, id = item.attr('id');
			
			item.hide();
			
			var viewed = $.cookie(self.cookie.name);
			
			if (viewed) {
				viewed += ',' + id;
			} else {
				viewed = id;
			}
			
			$.cookie(self.cookie.name, viewed, self.cookie.options);
		},
		
		formSubmissions: function () {
			// Make IE6 buttons submittable
			if ($.browser.msie) {
				$('button').click(function (e) {
					e.preventDefault();
					$(this).parents('form').submit();
				});
			}
			
			// Clear default text
			$('form').bind('submit', function () {
				$(this).find('input[type=text]').each(function () {
					var $this = $(this);
					
					if ($this.val() == $this.attr('title')) {
						$this.val('');
					}
				});
			});
		},
		
		writeCss: function () {
			if (!$.cookie) {
				return;
			}
			
			var self = this, viewed = $.cookie(self.cookie.name);
			
			if (viewed) {
				var ids = viewed.split(',');
				
				document.write('<style type="text/css">');
				
				$.each(ids, function (x, id) {
					document.write('#' + id + ' { display: none; }');
				});
				
				document.write('</style>');
			}
		}
	};
	
	ConnectBooks.writeCss();
	
	// Rewrite of textbox.js, this code is less buggy :)
	$(':text[title]')
		.live('focusin', function () {
			var $this = $(this);
			if ($this.val() == $this.attr('title'))
			{
				$this
					.css('color', 'black')
					.val('');
			}
		})
		.live('focusout', function () {
			var $this = $(this);
			if (!$this.val().length)
			{
				$this
					.css('color', '#AAA')
					.val($this.attr('title'));
			}
		});
	
	$(function() {
		ConnectBooks.init();
		$(':text[title]').trigger('focusout');
		$('td:empty').html('&nbsp;');
	});
	
})(jQuery);

