<?xml version="1.0" encoding="UTF-8"?>
<Module>
	<ModulePrefs
		title="Jaiku Beta 0.83.01"
		height="360"
		author="Rick Measham"
		author_email="jaiku@isite.net.au"
		title_url="http://jaiku.com/"
		description="Update and view your Jaiku.com presence"
		scrolling="true"
		screenshot="http://jaikugadget.googlecode.com/svn/trunk/previews/preview.png"
	>
		<Require feature="minimessage"/>
		<Require feature="setprefs"/>
		<Require feature="settitle"/>
	</ModulePrefs>
	<UserPref name="status" display_name="Allow updates" datatype="bool"  default_value="false"/>
	<UserPref name="username" display_name="Username" />
	<UserPref name="userkey" display_name="API Key (see jaiku.com/api/key)" />
	<UserPref name="feed" display_name="Show feed" datatype="enum" default_value="public">
		<EnumValue value="none" display_value="None"/>
		<EnumValue value="mine" display_value="Only Mine"/>
		<EnumValue value="friends" display_value="Friends"/>
		<EnumValue value="public" display_value="Public"/>
	</UserPref>
	<UserPref name="showComments" display_name="Show comments" datatype="bool"  default_value="false"/>
	<UserPref name="showFeeds" display_name="Show feeds" datatype="bool"  default_value="true"/>
	<UserPref name="refresh" display_name="Refresh" datatype="enum" default_value="300">
		<EnumValue value="60" display_value="60s"/>
		<EnumValue value="120" display_value="2m"/>
		<EnumValue value="300" display_value="5m"/>
		<EnumValue value="600" display_value="10m"/>
	</UserPref>
	<UserPref name="length" display_name="Timeline length" datatype="enum" default_value="5">
		<EnumValue value="1" display_value="Latest only"/>
		<EnumValue value="5" display_value="Most recent five"/>
		<EnumValue value="10" display_value="Most recent 10"/>
		<EnumValue value="20" display_value="Most recent 20"/>
		<EnumValue value="100" display_value="As many as jaiku provides"/>
	</UserPref>
	<UserPref name="avatar" datatype="hidden" />
	<Content type="html"><![CDATA[

		<style type="text/css">

			div#jaikus, div#newJaiku {
				max-width: 520px;
				font-size: 0.84em;
			}

			div#jaikus ul#jaikuList {
				list-style-type: none;
				padding: 0;
				margin: 0;
			}

			div#jaikus ul#jaikuList li.presence,
			div#jaikus ul#jaikuList li.comment {
				margin: 0 0 0.5em 0;
				clear: left;
				padding: 0;
			}

			div#jaikus ol.content li {
				margin: 0 0 0 15px;
			}
			div#jaikus ul.content li {
				list-style-type: square;
				margin: 0 0 0 15px;
			}

			div#jaikus ul#jaikuList li.presence .content,
			div#jaikus ul#jaikuList li.comment .content {
				font: 1em/1.2em Arial, Helvetica, sans-serif;
				padding: 0.5em 0 0 5px;
				margin-bottom: 3px;
				color: #000;
				overflow: hidden;
			}

			div#jaikus ul#jaikuList li.presence .commenttitle,
			div#jaikus ul#jaikuList li.presence .commenttitle a {
				margin: 1em 0 -0.5em 5px;
				color: #888;
				font-style: italic;
			}

			div#jaikus ul#jaikuList li.presence div.bottom,
			div#jaikus ul#jaikuList li.comment div.bottom {
				background: transparent url(http://jaiku.com/images/bg-stream-bottom.gif) no-repeat 0 100%;
				margin: 0 0 5px 37px;
			}
			div#jaikus ul#jaikuList li.comment div.bottom {
				background: transparent url(http://jaiku.com/images/bg-comment-bottom.gif) no-repeat 0 100%;
			}

			div#jaikus ul#jaikuList li.presence div.top,
			div#jaikus ul#jaikuList li.comment div.top {
				background: transparent url(http://jaiku.com/images/bg-stream-top.gif) no-repeat;
				padding: 0 7px 0 13px;
				position:relative;
				overflow: visible;
				min-height: 40px;
			}
			div#jaikus ul li.comment div.top {
				background: transparent url(http://jaiku.com/images/bg-comment-top.gif) no-repeat !IMPORTANT;
			}

			div#jaikus ul#jaikuList li.presence div.topright,
			div#jaikus ul#jaikuList li.comment div.topright {
				position: absolute;
				right: 0;
				width: 6px;
				height: 14px;
				background: transparent url(http://jaiku.com/images/bg-stream-top.gif) no-repeat 100% 0;
			}
			div#jaikus ul li.comment div.topright {
				background: transparent url(http://jaiku.com/images/bg-comment-top.gif) no-repeat 100% 0 !IMPORTANT;
			}

			div#jaikus ul#jaikuList li.presence div.right,
			div#jaikus ul#jaikuList li.comment div.right {
				position: absolute;
				right: 0;
				width: 6px;
				height: 100%;
				background: transparent url(http://jaiku.com/images/bg-stream-bottom.gif) no-repeat 100% 100%;
			}
			div#jaikus ul#jaikuList li.comment div.right {
				background: transparent url(http://jaiku.com/images/bg-comment-bottom.gif) no-repeat 100% 100%;
			}


			div#jaikus ul#jaikuList li.presence p,
			div#jaikus ul#jaikuList li.comment p {
				padding: 0 0 0 5px;
				margin: 0;
				font-size: 0.7em;
				line-height: 1.4em;
				color: #999;
			}

			div#jaikus ul#jaikuList li.presence img.icon {
				margin: 5px 5px 0 5px;
			}

			div#jaikus ul#jaikuList li.presence img.photo,
			div#jaikus ul#jaikuList li.comment img.photo {
				margin: 5px 10px 5px 0px;
				float: left;
				border: 1px solid #ccc;
				padding: 2px;
			}

			div#jaikus a {
				text-decoration: none;
				background: transparent;
				color: #000;
			}

			div#jaikus .footer a {
				color: #666;
			}
			div#jaikus .footer a:hover {
				color: #000;
			}

			div#jaikus a:hover {
				text-decoration: underline;
			}

			div#jaikus ul li img.ext {
				margin: 4px 5px 4px 6px;
				background: transparent url(http://jaiku.com/images/bg-photo-multiple.gif) no-repeat;
				padding: 5px 5px 11px 12px;
				height: 60px;
				width: 60px;
			}

			div#newJaiku table {
				width: 100%;
			}
			div#newJaiku textarea {
				width: 100%;
				height: 50px;
				background-repeat: no-repeat;
				background-position: 0 50%;
			}

			div#newJaiku div#chars {
				float: right;
				font-size: 0.60em;
				color: #999;
			}

			div#newJaiku img#jaikuAvatar {
				float: left;
			}


			div#jaikuIcons, div#jaikuChannels {
				border: solid 5px #9e9e9e;
				background: #fff;
				position: absolute;
				overflow: hidden;
				padding: 0;
				z-index: 3;
				display: none;
				left: 0px;
				top: 80px;
				cursor: pointer;
			}

			div#jaikuIcons img, div#jaikuChannels img {
				background: #fff;
				cursor: pointer;
				padding: 1px;
				height: 36px;
				width: 36px;
				margin: 1px;
			}

			div#jaikuIcons span {
				border-bottom: solid 1px #d9d9d9;
				border-right:  solid 1px #d9d9d9;
				display: block;
				float: left;
			}

			ddiv#jaikuChannels div {
				border-bottom: solid 1px #d9d9d9;
				display: block;
			}
			div#jaikuIcons span:hover, div#jaikuChannels div:hover {
				background-color: #009bc7;
			}

			div#actions,
			div#actions span,
			div#actions input {
				vertical-align: middle;
			}

			div#jaikuDownBack {
				display: none;
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
				background: #ccc;
				opacity: 0.5;
			}
			div#jaikuDown {
				display: none;
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
			}
			div#jaikuDown img {
				position: absolute;
				left: 50%;
				margin-left: -75px;
				top: 50%;
				margin-top: -50px;
			}
		</style>
		<script type="text/javascript" src="http://jaikugadget.googlecode.com/svn/trunk/jaiku.js?force-1.12"></script>
<!--		<script type="text/javascript" src="http://rick.measham.id.au/paste/jaiku.js?force-1.11"></script>-->
		<script type="text/javascript">
			var prefs = new _IG_Prefs( __MODULE_ID__ );

			var requiredJaikuVersion = 1.12

			_IG_RegisterOnloadHandler( function(){
				mini = new _IG_MiniMessage( __MODULE_ID__, document.getElementById("messageBox") );

				if(Jaiku.VERSION() < requiredJaikuVersion)
					mini.createDismissibleMessage("<b>Requires jaiku.js version "+requiredJaikuVersion+". Version "+Jaiku.VERSION()+" loaded. Please <i>force</i>-reload in your browser.</b>");

				_IG_FetchContent(
					'http://jaikufans.com/_tools/igoogle.txt?'+(new Date()).getTime(),
					function(x){
						var xList = x.split(/\n/);
						for(var i = 0; i < xList.length; i++)
							if( /\S/.test(xList[i]) )
								mini.createTimerMessage(xList[i], 10);
					}
				);

				Jaiku.get = function(url, param, callback){
					_IG_FetchContent(
						url + ((/\?/.test(url)) ? '&' : '?') + serialize(param),
						callback
					);
				};

				Jaiku.post = function(url, param, callback){
					if(/\?/.test(url)){
						url = url.replace(/\?/, ';') + '&'
					}
					else {
						url = url + ';';
					}

					_IG_FetchContent(
						'http://jaikufans.com/_tools/post.pl?' + url + serialize(param),
						callback
					);
				};

				window.jaiku = new Jaiku(prefs.getString('username'), prefs.getString('userkey'));


				if (prefs.getBool('status') && prefs.getString('username')){
					document.getElementById('newJaiku').style.display = '';
				}

				if (prefs.getString('username')){

//					if (!prefs.getString('avatar')){
						refreshAvatar();
//					} else {
//						document.getElementById('jaikuAvatar').src = prefs.getString('avatar');
//					}

					_IG_SetTitle("Jaiku for "+prefs.getString('username'));
					window.title = "Jaiku for "+prefs.getString('username');

					jaiku.getMyInfo(
						function(rv){
							console.log( rv.toSource() );
							if( rv.nick == "" ){
								document.getElementById('newJaiku').style.display = 'none';

								mini.createDismissibleMessage("<h1>Incorrect API Key</h1><p>Please check your api key at <a href=\"http://jaiku.com/api/key\">http://jaiku.com/api/key</a> and update the widget preferences. If you continue to have problems, please email <a href=\"mailto:admin@jaikufans.com\">admin@jaikufans.com</a> with your username.</p>");
							}
						}
					);

				} else {
					prefs.set('feed', 'public');
				}
				refreshFeed();
				refreshChannels();

				// Create the icons
				var jaikuIcons = document.getElementById('jaikuIcons');
				for(var c in Jaiku.iconsByCategory) if (Jaiku.iconsByCategory.hasOwnProperty(c)){
					for(var j=0; j < Jaiku.iconsByCategory[c].length; j++) {
						var i = Jaiku.iconsByCategory[c][j];

						var span = document.createElement('span');
						var img  = _IG_GetImage("http://jaiku.com/images/icons/web-"+Jaiku.iconById[i]+".gif");
//						var img  = document.createElement('img');
//							img.src = "http://jaiku.com/images/icons/web-"+Jaiku.iconById[i]+".gif";
							img.className = 'icon';
							img.alt = Jaiku.iconById[i];
						span.appendChild(img);

						jaikuIcons.appendChild(span);

						img.onclick=function(){
							setJaikuIcon(this.alt.toLowerCase());
							document.getElementById('jaikuIcons').style.display='none';
							document.body.scrollTop = 0;
							document.getElementById('remote_'+'__MODULE_ID__').scrollTop = 0;
							document.getElementById('jaikuStatus').focus();
						}
					}
				}
				// Another icon to remove the currently set icon

				var span = document.createElement('span');
				var img  = _IG_GetImage("http://jaikugadget.googlecode.com/svn/trunk/images/icon-remove-icon.gif");

//				var img  = document.createElement('img');
//					img.src = "http://jaikugadget.googlecode.com/svn/trunk/images/icon-remove-icon.gif";
					img.className = 'icon';
					img.alt = 'Remove current icon';
					img.title = img.alt;
				span.appendChild(img);

				jaikuIcons.insertBefore(span, jaikuIcons.firstChild);

				img.onclick=function(){
					setJaikuIcon('');
					document.getElementById('jaikuIcons').style.display='none';
					document.body.scrollTop = 0;
					document.getElementById('remote_'+'__MODULE_ID__').scrollTop = 0;
					document.getElementById('jaikuStatus').focus();
				}

				// Set the location icon if we have a location
				if( readCookie('jaikuLocation') ){
					var newLocation = readCookie('jaikuLocation');
					createCookie('jaikuLocation', newLocation, 365); // set it to expire in another year every time
					document.getElementById('locationIcon').src = document.getElementById('locationIcon').src.replace(
						/globe(\-set)?\.gif$/,
						'globe-set.gif'
					);
					document.getElementById('locationIcon').title = newLocation ? newLocation : 'Click to set location';
				}
			});

			function refreshAvatar(){
				jaiku.getMyInfo(function(rv){
					if (rv.avatar){
						document.getElementById('jaikuAvatar').src = rv.avatar;
						prefs.set('avatar', rv.avatar)
					}
				})
			}

			function refreshFeed() {

				if ( prefs.getString('feed') == 'friends' ){
					jaiku.getContactsFeed(jaikuCallback)
				}
				else if ( prefs.getString('feed') == 'mine' ){
					jaiku.getMyFeed(jaikuCallback)
				}
				else if ( prefs.getString('feed') == 'public' ){
					jaiku.getFeed(jaikuCallback)
				}
			}
			function jaikuCallback(rv){

				var jaikus = [];
				var jaikuListCN = document.getElementById('jaikuList').childNodes;
				for( var i = 0; i < jaikuListCN.length; i++ ){
					if(jaikuListCN[i].tagName.toUpperCase() == 'LI')
						jaikus.push(jaikuListCN[i]);
				}

				stream: for( var i = 0; i < rv.stream.length; i++ ){

					var this_j = rv.stream[i];

					// Stream bug fixes:

					// Turn http:\/\/#devku.jaiku.com/presence/1670286#c-45938
					// into http://jaiku.com/channels/devku/presence/1670286#c-45938
					this_j.url = this_j.url.replace(/http:\/+\#([^.]+)\.jaiku.com\//, 'http://jaiku.com/channel/$1/');

					// Skip comments
					if (this_j.comment_id && ! prefs.getBool('showComments'))
						continue stream;

					// Skip feeds
					if (! /^http:\/\/([^.]+\.)?jaiku\.com/.test(this_j.url) && ! prefs.getBool('showFeeds') )
						continue stream;

					if (/flickr/.test(this_j.content) && ! this_j.comment_id){
						this_j.source = 'flickr';
					}
					else if ( /^http:\/\/jaiku\.com\/channel\/./.test(this_j.url) ) {
						this_j.source = 'channel';
						this_j.channel = this_j.url.match(/^http:\/\/jaiku\.com\/channel\/([^\/]+)/)[1];
					}

					// Step 1: Check if we already have this jaiku and if not, where to insert it
					var insert_before = -1;
					var replace_point = -1;

					for( var j = 0; j < jaikus.length; j++ ){
						if (jaikus[j].id == 'jaiku'+this_j.id){
							replace_point = j;
							break;
						}
						if (jaikus[j].id == 'jaikuC'+this_j.comment_id){
							replace_point = j;
							break;
						}
						if (jaikus[j].getAttribute('created_at') < this_j.created_at.getTime().toString()){
							insert_before = j;
							break;
						}
					}

					// Step 2: Duplicate the output node
					var output = document.getElementById('repeatable').cloneNode(true);

					// If it's a comment, change the CSS
					if(this_j.comment_id) {
						output.className += ' comment';
					}

					// Step 3: Get the div
					var content = output.getElementsByTagName('div')[0].getElementsByTagName('div')[0];


					// If it's a comment, put the original at the top
					if (this_j.comment_id) {
						var ct = document.createElement('p');

						var u_a_b = document.createElement('b');
						var u_a = document.createElement('a');
						u_a.href = this_j.url.replace(/\/presence\/.+/,'');
						u_a.appendChild( document.createTextNode( this_j.url.replace(/http:\/\/([^.]+)\..+$/,'$1') ) );

						u_a_b.appendChild(u_a);
						ct.appendChild( u_a_b );
						ct.appendChild( document.createTextNode( ' said ' ) );

						var p_a = document.createElement('a');
						p_a.href = this_j.url.replace(/#.+/,'');
						p_a.appendChild( document.createTextNode( this_j.entry_title ) );

						ct.appendChild( p_a );
						ct.className = 'commenttitle';
						content.appendChild(ct);
					}

					// Step 4: Add the primary content
					if (this_j.comment_id) {
						var tempDiv = document.createElement('div');
						tempDiv.innerHTML = this_j.pretty_content;

						for( var j = 0; j < tempDiv.childNodes.length; j++ ){
							if (( (tempDiv.childNodes[j].nodeType == 3 && /\S/.test(tempDiv.childNodes[j].innerHTML)) || tempDiv.childNodes[j].nodeType == 1 ) && tempDiv.childNodes[j].innerHTML ) {
								var c = document.createElement(tempDiv.childNodes[j].tagName);
								if(tempDiv.childNodes[j].tagName.toLowerCase() == 'ol' || tempDiv.childNodes[j].tagName.toLowerCase() == 'ul'){
									var IH = tempDiv.childNodes[j].innerHTML;
									// Need to filter out some Jaiku silliness
									c.innerHTML = IH.replace(/<li><p>/gi, '<li>').replace(/<\/p><\/li>/gi, '</li>');
								} else {
									c.innerHTML = tempDiv.childNodes[j].innerHTML;
								}
								c.className = 'content';
								content.appendChild(c);
							}
						}
					} else if(! /jaiku\.com/.test(this_j.url) ) {
						var feeda = document.createElement('a');
						feeda.href = this_j.url;
						feeda.appendChild( document.createTextNode( this_j.title ) );
						var c = document.createElement('p');
						c.className = 'content';
						c.appendChild( feeda );
						content.appendChild( c );
					} else {
						var c = addLinks( this_j.title );
						c.className = 'content';
						content.appendChild(c);
					}

					// Step 4a: Handle flickr feeds
					if (this_j.source == 'flickr'){
						var f = document.createElement('a');
						f.target = '_new';
						f.href = this_j.url;
						var img = _IG_GetImage( this_j.content.replace(/_m\./,'_s.') );
						f.appendChild(img);
						content.appendChild(f);
					}

					// Step 5: Add the icon
					if (this_j.icon){
						var icon = _IG_GetImage( this_j.icon );
//						var icon = document.createElement('img');
//						icon.src = this_j.icon;
						icon.align = 'left';
						c.insertBefore(icon, c.firstChild);
					}

					// Step 6: Add the footer
					var p = document.createElement('p');
					p.className = 'footer';

					p.appendChild(document.createTextNode(
						(this_j.comment_id)
							? 'Comment from '
							: 'By '
					));

					var n = document.createElement('a');
					n.appendChild(document.createTextNode( this_j.user.nick ));
					n.href = this_j.user.url;
					n.target = '_new';
					p.appendChild(n);
					p.appendChild(document.createTextNode(' '));

					if(this_j.source == 'channel'){
						p.appendChild(document.createTextNode('in '));
						var chan_a = document.createElement('a');
						chan_a.href = "http://jaiku.com/channel/"+this_j.channel;
						chan_a.appendChild(document.createTextNode( this_j.channel ));
						p.appendChild(chan_a);
						p.appendChild(document.createTextNode(' '));
					}

					if(this_j.comment_id){
						p.appendChild(document.createTextNode('on '));
						var r = document.createElement('a');
						r.target = '_new';
						r.appendChild(document.createTextNode(
							this_j.title.replace(/Comment from \S+ on /,'')
						));
						r.href = this_j.url;
						p.appendChild(r);
					}

					p.appendChild(document.createTextNode(' '+this_j.created_at_relative));

					if(! this_j.comment_id && ! this_j.flickr && this_j.content){
						p.appendChild(document.createTextNode(' :: '));

						var linkedContent = addLinks(this_j.content, 'span');

						if (linkedContent.innerHTML == this_j.content){
							if(/,/.test(this_j.content)){
								var l = document.createElement('a');
								l.href = "http://maps.google.com/maps?q="+encodeURIComponent(this_j.content);
								l.target = '_new';
								l.appendChild(document.createTextNode(this_j.content));
								p.appendChild(l);
							} else {
								p.appendChild(document.createTextNode(this_j.content));
							}
						} else {
							p.appendChild(linkedContent);
						}
					}

					if(! this_j.comment_id){
						p.appendChild(document.createTextNode(' :: '));
						var comments = document.createElement('a');
						comments.target = '_new';
						comments.href = (! /jaiku\.com/.test(this_j.url) )
							? this_j.user.url+'/presence/'+this_j.id+'#comments'
							: comments.href = this_j.url+'#comments';
						comments.appendChild(
							document.createTextNode(
								(this_j.comments > 1)
									? this_j.comments + ' comments'
									: (
										(this_j.comments > 0)
											? '1 comment'
											: 'Add comment'
									)
							)
						);
						p.appendChild(comments);
					}
					content.appendChild(p);




					// Step 7: Replace the avatar
					output.getElementsByTagName('a')[0].href = this_j.user.url;
					output.getElementsByTagName('a')[0].getElementsByTagName('img')[0].src
						= _IG_GetImageUrl( this_j.user.avatar.replace(/_t\./, '_u.') );
					output.getElementsByTagName('a')[0].getElementsByTagName('img')[0].alt = this_j.user.nick;

					// Step 8: update the created_at so we can look for it later
					output.setAttribute('created_at', this_j.created_at.getTime());


					// Step 9: Send all hrefs through a redirector to clean the referrer
					var ahrefs = output.getElementsByTagName('a');
					for( var k = 0; k < ahrefs.length; k++ ){
						cleanHREF( ahrefs[k] );
						ahrefs[k].target = '_blank';
					}

					// Step 10: insert into the stream
					if (insert_before >= 0 && jaikus[insert_before]) {
						output.style.opacity = 0;
						jaikus[insert_before].parentNode.insertBefore( output, jaikus[insert_before] );
						fadeIn(output);
					} else if (replace_point >= 0 && jaikus[replace_point]) {
						jaikus[replace_point].parentNode.replaceChild( output, jaikus[replace_point] );
					} else {
						document.getElementById('jaikuList').appendChild(output);
					}

					// Step 10: update the ID so we can replace it with an update
					output.id = (this_j.id)
						? 'jaiku'  + this_j.id
						: 'jaikuC' + this_j.comment_id;

					// Step 11: make it visible;
					output.style.display = '';
				}


				// Reap older jaikus
				var cuttoff = 100;
				while(jaikus.length > prefs.getInt('length') && --cutoff > 0){
					if(jaikus[ jaikus.length - 1 ] && jaikus[ jaikus.length - 1 ].parentNode)
						jaikus[ jaikus.length - 1 ].parentNode.removeChild(jaikus[ jaikus.length - 1 ]);
					delete(jaikus[ jaikus.length - 1 ]);
				}

				window.jaikuTimer = window.setTimeout( refreshFeed, prefs.getInt('refresh') * 1000 );
			}

			function updateCharCount(num){
				window.jaikuCharCount = window.jaikuCharCount || document.getElementById('charCount');
				// horrible, but faster than DOM
				window.jaikuCharCount.innerHTML = 140 - num;
			}

			function toggleStatusIcon(){
				var jaikuIcons = document.getElementById('jaikuIcons');
				jaikuIcons.style.display =
					(jaikuIcons.style.display == 'block')
						? 'none'
						: 'block';
			}

			function setJaikuIcon(title){
				var jStatus = document.getElementById('jaikuStatus');
				if (title && Jaiku.iconByName[title]){
					document.getElementById('jaikuStatusIcon').value = Jaiku.iconByName[title];
					jStatus.style.backgroundImage = 'url(http://jaiku.com/images/icons/web-'+title+'.gif)';
					jStatus.style.paddingLeft = '42px';
				} else {
					document.getElementById('jaikuStatusIcon').value = '';
					jStatus.style.backgroundImage = '';
					jStatus.style.paddingLeft = '';
				}
			}

			function toggleStatusChannel(){
				var channelSelector = document.getElementById('channelSelector');
				var jaikuChannels = document.getElementById('jaikuChannels');
				jaikuChannels.style.left = channelSelector.offsetLeft + 'px';
				jaikuChannels.style.display =
					(jaikuChannels.style.display == 'block')
						? 'none'
						: 'block';
			}

			function refreshChannels(){
				jaiku.getMyChannels( function(cl){
					if(cl.length > 0){
						document.getElementById('channelSelector').style.display = '';
						var jaikuChannels = document.getElementById('jaikuChannels');
						while(jaikuChannels.firstChild) jaikuChannels.removeChild( jaikuChannels.firstChild );

						cl.unshift({
							avatar: 'http://jaikugadget.googlecode.com/svn/trunk/images/icon-remove-icon.gif',
							nick: '[none]'
						});

						for(var i=0; i < cl.length; i++){

							var div = document.createElement('div');
							var img = _IG_GetImage( cl[i].avatar );
//							var img  = document.createElement('img');
//								img.src = cl[i].avatar;
								img.className = 'icon';
								img.alt = cl[i].nick;
							div.appendChild(img);
							div.appendChild(document.createTextNode( cl[i].nick ));

							jaikuChannels.appendChild(div);

							div.onclick=function(){
								setJaikuChannel(this.getElementsByTagName('img')[0].alt, this.getElementsByTagName('img')[0].src);
								document.getElementById('jaikuChannels').style.display='none';
								document.body.scrollTop = 0;
								document.getElementById('remote_'+'__MODULE_ID__').scrollTop = 0;
								document.getElementById('jaikuStatus').focus();
							}

						}

					} else {
						document.getElementById('channelSelector').style.display = 'none';
					}
				});
			}

			function setJaikuChannel(name, avatar){
				document.getElementById('jaikuStatusChannel').value = name;
				cs = document.getElementById('channelSelector');
				while(cs.firstChild) cs.removeChild( cs.firstChild );
				if(name && name != '[none]'){
//					var img = document.createElement('img');
//					img.src = avatar;
					var img = _IG_GetImage( avatar );
					img.width = 16;
					img.height = 16;
					cs.appendChild(img);
					cs.appendChild(document.createTextNode(name));
				} else {
					cs.innerHTML = '+ Channel';
				}
			}

			function setLocation(){
				var currentLocation = readCookie('jaikuLocation') || '';
				var newLocation = prompt("Location:", currentLocation);
				if( newLocation !== null ) {
					createCookie('jaikuLocation', newLocation, 365);
					document.getElementById('locationIcon').src = document.getElementById('locationIcon').src.replace(
						/globe(\-set)?\.gif$/,
						(newLocation ? 'globe-set.gif' : 'globe.gif')
					);
					document.getElementById('locationIcon').title = newLocation ? newLocation : 'Click to set location';
				}
			}

			function setJaikuPresence(){
				var jStatus = document.getElementById('jaikuStatus');
				if(! jStatus.value) return;
				var jSubmit = document.getElementById('jaikuSubmit');
				jStatus.disabled = true;
				jSubmit.disabled = true;
				jSubmit.value    = "Sending status...";

				var presence = {
					message: (document.getElementById('jaikuStatusChannel').value)
						? document.getElementById('jaikuStatusChannel').value + ': ' + jStatus.value
						:  jStatus.value
				};

				if(document.getElementById('jaikuStatusIcon').value)
					presence.icon = document.getElementById('jaikuStatusIcon').value;
				if(readCookie('jaikuLocation'))
					presence.location = readCookie('jaikuLocation');

				jaiku.setPresence(presence, function(rv){
					if(rv == 'OK'){

						mini.createTimerMessage("Presence Set (It might not show up until the next update)", 4)

						jStatus.value = '';
						updateCharCount(0);

						setJaikuIcon('');
						setJaikuChannel('');

						window.clearTimeout(window.jaikuTimer);
						refreshFeed();

					} else {

						mini.createDismissibleMessage("An error occurred while setting your presence. Sorry")

					}
					jStatus.disabled = false;
					jSubmit.disabled = false;
					jSubmit.value    = "Set my Jaiku";
				});
			}

			function addLinks(text, tagName){
				// [a-zA-Z][a-zA-Z0-9.+\-]*
				var urlRE = /(http:[\;\/\?\\@\&\=\+\$\,\[\]A-Za-z0-9\-_\.\!\~\*\'\(\)%][\;\/\?\:\@\&\=\+\$\,\[\]A-Za-z0-9\-_\.\!\~\*\'\(\)%#]*)/g;
				var cruft = /([\]\)\,\.\'\"\;]+)$/;
				var span = document.createElement(tagName || 'p');
				var c=0;
				text.replace(urlRE, function(m,a1,p){
					span.appendChild(document.createTextNode(text.substring(c,p)));
					a1 = a1.replace(cruft,'');
					c=p+a1.length;
					var a = document.createElement('a');
					a.href = a1;
					a.className = 'url';
					a.target = '_new';
					a.appendChild(document.createTextNode(a1));
					span.appendChild(a);
				});
				if(text.substr(c))
					span.appendChild(document.createTextNode(text.substr(c)));
				return span;
			}

			function serialize(obj){

				obj.refresh = (new Date()).getTime();

				var RV = '';
				for(var X in obj) {
					if(obj.hasOwnProperty(X)) {
						if (typeof(obj[X]) == 'object' && obj[X].constructor == Array) {
							for (var n = 0; n < obj[X].length; n++)
								RV += '&' + X + '=' + encodeURIComponent( obj[X][n] );
						} else if (typeof(obj[X]) == 'object') {
							RV += '&' + X + '=' + encodeURIComponent( serialize(obj[X]) );
						} else {
							RV += '&' + X + '=' + encodeURIComponent( obj[X] );
						}
					}
				}
				return RV.replace(/^&/,'');
			}

			function fadeIn(node){
				if (node.style.opacity < 1) {
					node.style.opacity = parseFloat(node.style.opacity,10) + 0.10;
					window.setTimeout(_fadeInClosure(node), 100);
				}
			} function _fadeInClosure(node){ return function(){ fadeIn(node) } }

			// To be used for linking @user and #channels .. one day
			function getAncestorByTagName(el, tn){
				if(el.parentNode) {
					if ( el.parentNode.nodeType == 1
						&& el.parentNode.tagName.toLowerCase() == tn.toLowerCase()
					) return el.parentNode;
					return getAncestorByTagName( el.parentNode, tn );
				}
				return null
			}

			function createCookie(name,value,days) {
				if (days) {
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				}
				else var expires = "";
				var gDomain = window.location.hostname.match(/(google|gmodules).+/)[0];
				document.cookie = name+"="+value+expires+"; path=/; domain="+gDomain;
			}

			function readCookie(name) {
				var nameEQ = name + "=";
				var ca = document.cookie.split(';');
				for(var i=0;i < ca.length;i++) {
					var c = ca[i];
					while (c.charAt(0)==' ') c = c.substring(1,c.length);
					if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
				}
				return null;
			}

//			var bounce_url = "/url?url=";
			var bounce_url = "http://jaikufans.com/_tools/redirect.pl?";
			function cleanHREF( anchor ){
				if ( anchor.href.indexOf( bounce_url ) !== 0 )
					anchor.href = bounce_url + encodeURIComponent( anchor.href );
			}
		</script>

		<div id="newJaiku" style="display: none">

			<table><tr><td width="0">
				<img id="jaikuAvatar" src="http://jaiku.com/image/avatar_default_t.jpg">
			</td><td width="100%">
				<textarea onkeypress="return (event.which == 8) || (this.value.length < 140 && event.which != 10)" onkeyup="updateCharCount(this.value.length)" id="jaikuStatus"></textarea>
			</tr></tr></table>

			<div id="chars"><span id="charCount">140</span> characters left</div>

			<div class="actions">
				<input type="button" value="Set my Jaiku" onclick="setJaikuPresence()" id="jaikuSubmit">

				<span style="cursor:pointer;" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" onclick="setLocation()"><img src="http://jaikugadget.googlecode.com/svn/trunk/images/icon-globe.gif" width="16" height="16" border="0" id="locationIcon"></span>

				<span style="cursor:pointer;" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" onclick="toggleStatusIcon()">+ Icon</span>

				<span style="cursor:pointer; display:none" onmouseover="this.style.textDecoration='underline'" onmouseout="this.style.textDecoration='none'" onclick="toggleStatusChannel()" id="channelSelector">+ Channel</span>
			</div>
			<input type="hidden" id="jaikuStatusIcon">
			<input type="hidden" id="jaikuStatusChannel">

			<div id="jaikuIcons"></div>

			<div id="jaikuChannels"></div>

		</div>

		<div id="messageBox"></div>

		<div id="jaikus">

			<ul id="jaikuList"></ul>

		</div>

		<div id="jaikuDownBack">
		</div>
		<div id="jaikuDown">
			<img src="http://jaikugadget.googlecode.com/svn/trunk/images/jaiku.down.gif">
		</div>

		<ul style="display: none">
			<li class="presence" id="repeatable">
				<a class="url" href="http://USERNAME.jaiku.com" target="_new">
					<img src="AVATAR" class="photo" alt="USERNAME" width="30" height="30">
				</a>
				<div class="bottom">
					<div class="top">
						<div class="right"></div>
						<div class="topright"></div>
					</div>
				</div>
			</li>
		</ul>

]]></Content>
</Module>
<Module>
<ModulePrefs title="hello world example" />
<Content type="html"><![CDATA[
Hello, world!
]]></Content>
</Module>
