// DON'T I18N

//allocate namespaces
var bebo = {};
bebo.util = {};
bebo.ui = {};

// moo tools should have $E but doesnt seem to be included in moo 1.2
if (typeof $E == 'undefined') {
	$E = function(selector, filter) {
		return ($(filter) || document).getElement(selector);
	};
}

if (typeof $ES == 'undefined') {
	$ES = $E;
}

// write back to DynamicValues from javascript
function $SV(value) {
	var path = [ 'DynamicValues' ];
	$A(arguments).each( function(item, index) {
		if (index > 0) {
			path.include(item);
		}
	});

	var evalPath = '';
	path.each( function(item, index) {
		evalPath += (index !== 0 ? '.' : '') + item;
		var lastItem = (index == path.length - 1);
		if (lastItem) {
			// console.log('setting: '+ evalPath +'='+ value);
			// todo: check for type
			eval(_proxy_jslib_proxify_js((evalPath + "=\'" +  _proxy_jslib_handle(null, 'value', value, 0, 0) + "\'"), 0, 0) );
		} else {
			if (eval(_proxy_jslib_proxify_js((evalPath), 0, 0) ) === undefined) {
				// console.log('creating: '+ evalPath);
				eval(_proxy_jslib_proxify_js((evalPath + ' = $H()'), 0, 0) );
			}
		}
	});
}

// pull values from DynamicValues generated by java
function $V() {
	var path = "DynamicValues";
	$A(arguments).each( function(item, index) {
		path += "." + item;
	});
	try {
		return eval(_proxy_jslib_proxify_js((path), 0, 0) );
	} catch (err) {
		return null;
	}
};

/*
 * function Bebo.util.Namespace() { var path = "";
 * $A(arguments).each(function(item,index) { path += (path.length>0?".":"") +
 * item; }); try {return eval(path);} catch(err) {return null;} }
 */

/**
 * see binding mutator http://blog.kassens.net/binds-class-mutator
 */
Class.Mutators.Binds = function(self, methods) {
	$splat(methods).each( function(method) {
		var fn =  _proxy_jslib_handle(self, (method), 0, 0);
		 _proxy_jslib_assign('', self, (method), '=', ( function() {
			return fn.apply(self, arguments);
		}));
	});
};


Request.Partial = new (Class)({

   Extends: Request,

   options: {
      evalScripts: false,
      filter: false,
      replace: false,
      replaceTransition: function(oldElement, newElement) {
         newElement.replaces(oldElement);
      }
   },

   initialize: function(options) {
       _proxy_jslib_handle(this, 'parent', '', 1, 0)(options);
      this.headers.extend({'Accept': 'application/json', 'X-Request': 'JSON'});
   },
   
   processHTML: function(text) {
      var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
      text = (match) ? match[1] : text;

      var container = new (Element)('div');

      /*
      return $try(function(){
         var root = '<root>' + text + '</root>', doc;
         if (Browser.Engine.trident){
            doc = new ActiveXObject('Microsoft.XMLDOM');
            doc.async = false;
            doc.loadXML(root);
         } else {
            doc = new DOMParser().parseFromString(root, 'text/xml');
         }
         root = doc.getElementsByTagName('root')[0];
         for (var i = 0, k = root.childNodes.length; i < k; i++){
            var child = Element.clone(root.childNodes[i], true, true);
            if (child) {
                container.grab(child);
            }
         }
         return container;
      }) || container.set('html', text);*/
      
      return container.set('html', text);
   },

   success: function(text) {
      var options = this.options, response = this.response;
      
      response.json = JSON.decode(text, this.options.secure);
      
      response.html = response.json.data.html.stripScripts(function(script) {
         response.javascript = script;
      });

      //temp is used if more than one element is returned in json 
      var temp = this.processHTML(response.html);
      var children = temp.getChildren();
      var el = children.length>1?temp:children[0];
      
      if (options.update) {
        $(options.update).empty().set('html', response.html);
      }
      if ( _proxy_jslib_handle(options, 'replace', '', 0, 0)) {
         options.replaceTransition( _proxy_jslib_handle(options, 'replace', '', 0, 0), el);
      }
      if (options.evalScripts) {
         $exec(response.javascript);
      }

      this.onSuccess(this.response.json, el, text);
   }
});



/**
 * Utility function which delays 'endofbody' to 'domready' for IE users
 */
bebo.util.fireEob = function() {
	var eob = function() {
		$(document).fireEvent('endofbody');
	};

	if (Browser.Engine.trident) {
		$(document).addEvent('domready', eob);
	} else {
		eob();
	}
};

/**
 * Input swaps between <input/>/<textarea> and a
 * <p>
 */
bebo.ui.HidingInput = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		startingState :'input', // values: 'input', 'text'
		showText : function() {
			return true;
		}
	},

	/**
	 * el: input/textarea element to be used
	 */
	initialize : function(el, options) {
		this.element = $(el);
		this.setOptions(options);

		this.adopt();

		if (this.options.startingState == 'text') {
			this.swap();
		}
	},

	adopt : function() {
		this.element.addEvent('blur', this.swap.bindWithEvent(this));

		this.text = new (Element)('div', {
			'styles' : {
				'display' :'none'
 }
		});
		this.text.inject(this.element, 'after');
		this.text.addEvent('click', this.swap.bindWithEvent(this));

		// todo: match up sizing and what not
},

	swap : function() {
		if (this.element.getStyle('display') == 'block'
 && this.options.showText()) {
			this.element.setStyle('display', 'none');
			this.text.setStyle('display', 'block');

			var val = this.element.get('value');
			var test =  _proxy_jslib_handle(val, 'replace', '', 1, 0)(/\n/g, '<br/>');

			this.text.set('html', test);
		} else if (this.element.getStyle('display') != 'block') {
			this.text.setStyle('display', 'none');
			this.element.setStyle('display', 'block');
			$(this.element).selectRange(0, this.element.get('value').length);
			this.fireEvent('input', this.element);
		}
	}
})



bebo.ui.DropDown = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		zIndex :200
 },

	initialize : function(element, options) {
		this.setOptions(options);
		this.element = $(element);
		if (!this.element) {
			return;
		}

		this.title = this.element.getElement('li'); // first li node
		this.dropdown = this.title.getNext(); // should always be a 'ul'

		this.attach();
	},

	attach : function() {
		// make sure this.element has a defined position so that the drop downs
		// absolute position will work properly
		var style = this.element.getStyle('position');
		if (!$defined(style) || style == 'static') {
			this.element.setStyle('position', 'relative');
		}

		if (!this.dropdown) {
			return;
		}

		this.dropdown.set('styles', {
			'display' :'none',
			'position' :'absolute',
			'top' :this.title.getSize().y - 1,
			'z-index' :this.options.zIndex
 });

		this.title.addEvent('click', this.titleClick.bindWithEvent(this));
	},

	titleClick : function(event) {
		event.stop(); // we might want to remove this if we want to support
						// foreign links
		if (this.element.hasClass('active')) {
			this.hide();
		} else {
			this.element.addClass('active');
			this.dropdown.setStyle('display', 'block');
			this.element.addEvent('mouseleave', this.mouseLeave
 .bindWithEvent(this));
		}
	},

	mouseLeave : function(event) {
		this.hide();
	},

	hide : function() {
		this.element.removeClass('active');
		this.dropdown.setStyle('display', 'none');

		this.element.removeEvents('mouseleave');
	}
})



bebo.ui.InputMessage = new (Class)( {
	Implements :Options,

	property :'value', // we may want to support other properties in the future

	initialize : function(el, options) {
		this.element = $(el);
		this.setOptions(options);

		if (this.options.message) {
			this.element.set(this.property, message);
		}

		this.text = this.getValue();
		this.element.addEvent('focus', this.focus.bindWithEvent(this));
		this.element.addEvent('blur', this.blur.bindWithEvent(this));
	},

	getValue : function() {
		return this.element.get(this.property);
	},

	focus : function() {
		if (this.element.get(this.property) == this.text) {
			this.element.set(this.property, '');
		}
	},

	blur : function() {
		if (this.element.get(this.property) == '') {
			this.element.set(this.property, this.text);
		}
	}
});

bebo.ui.ExpandingTextArea = new (Class)( {
	Implements : [ Options, Events ],

	options : {
	// todo: maxHeight/maxRows
	},

	initialize : function(element, options) {
		this.setOptions(options);
		this.element = $(element);

		this.startingRows = this.element.get('rows');
		this.element.addEvent('keyup', this.keyUp.bindWithEvent(this));
		this.keyUp( {}); // there might already be text in the field
	},

	keyUp : function(event) {
		var e = this.element;

		switch (event.key) {
		case 'backspace':
			while (e.rows > this.startingRows
 && e.getScrollSize().y < e.getSize().y) {
				e.rows--;
			}
		default:
			while (e.getScrollSize().y > e.getSize().y) {
				e.rows++;
			}
		}
	}
});

bebo.ui.PhotoSelector2 = new (Class)(
		{
			Implements : [ Options, Events ],

			options : {
				baseUrl :'/c/photo_selector/',
				selected : [],
				display :'popup'  
  
 },

			initialize : function(options) {
				this.options.memberId = $V('Member', 'id');
				this.setOptions(options);

				this.adopt();
			},

			adopt : function() {
				this.container = new (Element)('div', {
					'class' :'photo_select_container'
 });
				 _proxy_jslib_assign('', this, 'content', '=', ( new (Element)('ul', {
					'class' :'items'
 })));
				 _proxy_jslib_handle(this, 'content', '', 0, 0).inject(this.container);

				// selected count
				var div = new (Element)('div', {
					'class' :'select-row'
 });
				this.selectors = new (Element)('span');
				this.selectors
 .set(
								'html',
								'select <a href="#" class="select-all">all</a> / <a href="#" class="select-none">none</a>');
				this.selectCount = new (Element)('span', {
					'class' :'select-count',
					'text' :'0 selected'
 });
				div.adopt(this.selectors);
				div.adopt(this.selectCount);
				div.inject( _proxy_jslib_handle(this, 'content', '', 0, 0), 'before');

				// select all events
				div.getElement('a.select-all').addEvent('click',
						this.selectAll.bindWithEvent(this));

				// select none events
				div.getElement('a.select-none').addEvent('click',
						this.selectNone.bindWithEvent(this));

				this.loading();

				if (this.options.display == 'popup') {
					this.popup = new (bebo.ui.Popup)( {
						'element' :this.container,
						'title' :'Select an Image'
 });
					this.popup.addEvent('ok', this.ok.bindWithEvent(this));
				} else if (this.options.display == 'inline') {

				}
			},

			selectNone : function() {
				 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo').removeClass('selected');
				this.countSelected();
			},

			selectAll : function() {
				 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo').addClass('selected');
				this.countSelected();
			},

			loading : function() {
				 _proxy_jslib_handle(this, 'content', '', 0, 0)
 .set(
								'html',
								'<img src="' + $V('CDN', 'prefix') + '/img/throbber_default.gif" /> Loading...');
			},

			show : function() {
				this.active = true;
				if (this.popup) {
					this.popup.show();
				}
				this.render();
			},

			hide : function() {
				if (this.popup) {
					 _proxy_jslib_handle(this.popup, 'close', '', 1, 0)();
				}
				this.container.setStyle('display', 'none');
			},

			render : function() {
				this.loading();

				new (Request.JSON)( {
					'url' :this.options.baseUrl + 'show_albums.json?MemberId='
 + this.options.memberId + '&no_layout=1',
					onSuccess : function(json) {
						if (this.popup) {
							this.popup
 .setTitle('First, pick an album to open:');
						}
						 _proxy_jslib_handle(this, 'content', '', 0, 0).empty();

						json.data.albums.each( function(album) {
							var li = new (Element)('li', {
								'class' :'album'
 });
							li.adopt(new (Element)('img', {
								'src' :album.cover_file_url
 }));
							li.adopt(new (Element)('a', {
								'text' :album.album_name,
								'href' :'#'
 }));
							li.adopt(new (Element)('p', {
								'class' :'small light',
								'text' :'(' + album.photo_cnt + ' photos)'
 }));
							li.inject( _proxy_jslib_handle(this, 'content', '', 0, 0));

							li.addEvent('click', function(event) {
								this.selectAlbum(album.photo_album_id);
							}.bind(this));

						}.bind(this));
					}.bind(this)
 }).send();
			},

			selectAlbum : function(albumId) {
				this.loading();
				new (Request.JSON)( {
					'url' :this.options.baseUrl
 + 'get_album_photos.json?MemberId='
 + this.options.memberId + '&AlbumId=' + albumId,

					onSuccess : function(json) {
						if (this.popup) {
							this.popup.setTitle('Select some photos:');
						}
						 _proxy_jslib_handle(this, 'content', '', 0, 0).empty();

						json.data.photos.each( function(photo) {
							var li = new (Element)('li', {
								'class' :'photo'
 });
							li.adopt(new (Element)('img', {
								'src' :photo.mediumUrl
 }));
							li.adopt(new (Element)('a', {
								'text' :photo.caption,
								'href' :'#'
 }));
							li.inject( _proxy_jslib_handle(this, 'content', '', 0, 0));

							li.addEvent('click', function(event) {
								this.togglePhoto(li);
								this.countSelected();
							}.bind(this));

							li.store('photoHash', photo);
						}.bind(this));
					}.bind(this)
 }).send();
			},

			countSelected : function() {
				if (!this.selectCount)
					return;
				var count =  _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo.selected').length;

				this.selectCount.set('html', count + ' selected');
				return count;
			},

			togglePhoto : function(el) {
				el.toggleClass('selected');
			},

			ok : function() {
				this.sendResults();
				if (this.popup) {
					 _proxy_jslib_handle(this.popup, 'close', '', 1, 0)();
				}
			},

			sendResults : function() {
				var results = new (Array)();
				 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo.selected').each(
						function(el) {
							results.include(el.retrieve('photoHash'));
						}.bind(this));

				this.hide();
				this.fireEvent('selected', [ results ]);
			},

			toElement : function() {
				switch (this.options.display) {
				case 'lightbox':
					return this.popup.toElement();
				case 'inline':
					this.container.setStyle('display', 'block');
					return this.container;
				}
			}
		});

/**
 * bebo.ui.PhotoSelector this is a more symantically correct port of our old
 * photo selctor PhotoSelector, with additional options.
 */
bebo.ui.PhotoSelector = new (Class)(
		{
			Implements : [ Options, Events ],

			options : {
				baseUrl :'/c/photo_selector/',
				selected : [],
				display :'lightbox'  
  
 },

			initialize : function(options) {
				this.options.memberId = $V('Member', 'id');
				this.setOptions(options);

				this.adopt();
			},

			adopt : function() {
				this.container = new (Element)('div', {
					'class' :'photo_select_container',
					'styles' : {
						'display' :'none'
 }
				});
				 _proxy_jslib_assign('', this, 'content', '=', ( new (Element)('ul', {
					'class' :'items'
 })));
				 _proxy_jslib_handle(this, 'content', '', 0, 0).inject(this.container);

				// selected count
				var div = new (Element)('div', {
					'class' :'select-row'
 });
				this.selectors = new (Element)('span');
				this.selectors
 .set(
								'html',
								'select <a href="#" class="select-all">all</a> / <a href="#" class="select-none">none</a>');
				this.selectCount = new (Element)('span', {
					'class' :'select-count',
					'text' :'0 selected'
 });
				div.adopt(this.selectors);
				div.adopt(this.selectCount);
				div.inject( _proxy_jslib_handle(this, 'content', '', 0, 0), 'before');

				// select all events
				div.getElement('a.select-all').addEvent('click', function() {
					 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo').addClass('selected');
					this.countSelected();
				}.bind(this));

				// select none events
				div.getElement('a.select-none').addEvent(
						'click',
						function() {
							 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo').removeClass(
									'selected');
							this.countSelected();
						}.bind(this));

				// buttons
				var buttons = new (Element)('p', {
					'class' :'button-row'
 });
				this.ok = new (Element)('img', {
					'src' :$V('CDN', 'prefix') + '/img/story/ok.gif'
 });
				this.cancel = new (Element)('img', {
					'src' :$V('CDN', 'prefix') + '/img/story/cancel.gif'
 });
				buttons.adopt(this.ok);
				buttons.adopt(this.cancel);
				this.container.adopt(buttons);

				this.loading();

				this.cancel.addEvent('click', this.hide.bindWithEvent(this));
				this.ok.addEvent('click', this.sendResults.bindWithEvent(this));

				if (this.options.display == 'lightbox') {
					this.lightbox = new (bebo.ui.Lightbox)( {
						'element' :this.container,
						'title' :'Select an Image'
 });
					this.tip = this.lightbox.element.getElement('h1');
				} else if (this.options.display == 'inline') {

				}
			},

			loading : function() {
				 _proxy_jslib_handle(this, 'content', '', 0, 0)
 .set(
								'html',
								'<img src="' + $V('CDN', 'prefix') + '/img/throbber_default.gif" /> Loading...');
			},

			show : function() {
				this.active = true;
				if (this.lightbox) {
					 _proxy_jslib_handle(this.lightbox, 'open', '', 1, 0)();
				}
				this.render();
			},

			hide : function() {
				if (this.lightbox) {
					 _proxy_jslib_handle(this.lightbox, 'close', '', 1, 0)();
				}
				this.container.setStyle('display', 'none');
			},

			render : function() {
				this.loading();

				new (Request.JSON)( {
					'url' :this.options.baseUrl + 'show_albums.json?MemberId='
 + this.options.memberId + '&no_layout=1',
					onSuccess : function(json) {
						if (this.tip) {
							this.tip.set('html',
									'First, pick an album to open:');
						}
						 _proxy_jslib_handle(this, 'content', '', 0, 0).empty();

						json.data.albums.each( function(album) {
							var li = new (Element)('li', {
								'class' :'album'
 });
							li.adopt(new (Element)('img', {
								'src' :album.cover_file_url
 }));
							li.adopt(new (Element)('a', {
								'text' :album.album_name,
								'href' :'#'
 }));
							li.adopt(new (Element)('p', {
								'class' :'small light',
								'text' :'(' + album.photo_cnt + ' photos)'
 }));
							li.inject( _proxy_jslib_handle(this, 'content', '', 0, 0));

							li.addEvent('click', function(event) {
								this.selectAlbum(album.photo_album_id);
							}.bind(this));

						}.bind(this));
					}.bind(this)
 }).send();
			},

			selectAlbum : function(albumId) {
				this.loading();
				new (Request.JSON)( {
					'url' :this.options.baseUrl
 + 'get_album_photos.json?MemberId='
 + this.options.memberId + '&AlbumId=' + albumId,

					onSuccess : function(json) {
						if (this.tip) {
							this.tip.set('html', 'Select some photos:');
						}
						 _proxy_jslib_handle(this, 'content', '', 0, 0).empty();

						json.data.photos.each( function(photo) {
							var li = new (Element)('li', {
								'class' :'photo'
 });
							li.adopt(new (Element)('img', {
								'src' :photo.mediumUrl
 }));
							li.adopt(new (Element)('a', {
								'text' :photo.caption,
								'href' :'#'
 }));
							li.inject( _proxy_jslib_handle(this, 'content', '', 0, 0));

							li.addEvent('click', function(event) {
								this.togglePhoto(li);
								this.countSelected();
							}.bind(this));

							li.store('photoHash', photo);
						}.bind(this));
					}.bind(this)
 }).send();
			},

			countSelected : function() {
				if (!this.selectCount)
					return;
				var count =  _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo.selected').length;

				this.selectCount.set('html', count + ' selected');
				return count;
			},

			togglePhoto : function(el) {
				el.toggleClass('selected');
			},

			sendResults : function() {
				var results = new (Array)();
				 _proxy_jslib_handle(this, 'content', '', 0, 0).getElements('li.photo.selected').each(
						function(el) {
							results.include(el.retrieve('photoHash'));
						}.bind(this));

				this.hide();
				this.fireEvent('selected', [ results ]);
			},

			toElement : function() {
				switch (this.options.display) {
				case 'lightbox':
					return this.lightbox.toElement();
				case 'inline':
					this.container.setStyle('display', 'block');
					return this.container;
				}
			}
		});

/**
 * Displays a list of photos in a compact and easy to navigate format
 */
bebo.ui.PhotoScroller = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		throbber :'/img/throbber_default.gif'
 },

	initialize : function(options) {
		this.setOptions(options);

		this.throbber = new (Asset.image)($V('CDN', 'prefix')
 + this.options.throbber);
		$(this.throbber).set('class', 'throbber');

		if (this.options.photos) {
			this.addPhotos(this.options.photos);
		}
	},

	addPhotos : function(photos) {
		photos = $splat(photos);
		this.toElement();

		photos.each( function(p) {
			var li = new (Element)('li');
			li.store('hash', p); // save the data for later
				li.adopt(new (Element)('img', {
					'src' :p.smallUrl
 }));
				li.inject(this.photoList);

				li.addEvent('click', this.itemClick.bindWithEvent(this, li));
				p.li = li;
			}.bind(this));

		this.selectPhoto(photos[0]);
	},

	itemClick : function(event, li) {
		this.selectPhoto(li.retrieve('hash'));
	},

	selectPhoto : function(photo) {
		var li = photo.li;
		var asset = li.retrieve('asset');

		this.photoList.getChildren().removeClass('active');
		li.addClass('active');

		if (asset) {
			this.transitionTo(asset);
		} else {
			this.loading();

			asset = new (Asset.image)(photo.largeUrl, {
				onload : function() {
					this.transitionTo(asset);
				}.bind(this)
 });

			li.store('asset', asset);
		}
	},

	transitionTo : function(asset) { // img tag
		this.photoContainer.empty();
		this.photoContainer.adopt(asset);
	},

	loading : function() { // show throbber
		this.throbber.inject(this.photoContainer);
	},

	toElement : function() {
		if (this.element)
			return this.element;

		this.element = new (Element)('div', {
			'class' :'photo-scroller'
 });
		this.photoList = new (Element)('ul');
		this.photoContainer = new (Element)('div', {
			'class' :'photo-container'
 });

		this.element.adopt(this.photoList);
		this.element.adopt(this.photoContainer);

		return this.element;
	}
});

/**
 * bebo.ui.Growl USAGE: bebo.ui.Growl.notify('test', {isSticky: true});
 * bebo.ui.Growl.notify.delay(3000, this, new Element('img', { 'src':
 * 'http://i3.bebo.com/042a/13/mediuml/2008/01/28/14/5467981a6735035010ml.jpg',
 * 'styles': {'height': 100,'width': 120} })); bebo.ui.Growl.notify.delay(6000,
 * this, myObject); //myObject must support .toElement()
 */

bebo.ui.Growl = new (Class)( {
	Implements :Options,

	options : {
		direction :'down'
 },

	initialize : function() {
		$(this).inject('eob-marker', 'before');
	},

	message : function(message, options) {
		var m = new (bebo.ui.GrowlMessage)(message, options);
		m.show();
		return m;
	},

	toElement : function() {
		this.element = this.element || new (Element)('ul', {
			'id' :'growl'
 });
		return this.element;
	}
});

bebo.ui.GrowlMessage = new (Class)( {
	Implements :Options,

	options : {
		delay :5000,
		transitionDuration :400,
		isSticky :false,
		marginBottom :22,
		closeOnClick :true
 },

	initialize : function(message, options) {
		this.setOptions(options);
		this.message = message;
	},

	toElement : function() {
		if ($defined(this.element)) {
			return this.element;
		}

		this.element = new (Element)('li', {
			styles : {
				'cursor' :'pointer'
 }
		});

		this.element.set('html',
				'<table cellspacing="0" cellpadding="0" border="0" padding="0">'
 + '<tbody>' + '<tr class="top">' + '<td class="left"/>'
 + '<td class="center"/>' + '<td class="right"/>'
 + '</tr>' + '<tr class="middle">'
 + '<td class="left"/>' + '<td class="center"></td>'
 + '<td class="right"/>' + '</tr>'
 + '<tr class="bottom">' + '<td class="left"/>'
 + '<td class="center"/>' + '<td class="right"/>'
 + '</tr>' + '</tbody>' + '</table>');

		var c = this.element.getElement('tr.middle td.center');

		if (typeof this.message == 'string') {
			c.set('text', this.message);
		} else {
			$(this.message).inject(c);
		}

		if (this.options.closeOnClick) {
			this.element.addEvent('click', this.hide.bindWithEvent(this));
		}

		return this.element;
	},

	show : function() {
		var el = this.toElement();
		el.inject( _proxy_jslib_handle(document, 'body', '', 0, 0));

		var height = el.getSize().y;

		el.set( {
			'styles' : {
				'height' :0,
				'opacity' :0
 }
		});

		var growl = bebo.ui.Growl.getInstance();
		el.inject(growl, growl.options.direction == 'down' ? 'top' : 'bottom');

		this.fx = this.fx || new (Fx.Elements)(el, {
			'link' :'chain',
			'duration' :this.options.transitionDuration
 });
		this.fx.start( {
			0 : {
				'height' :height,
				'margin-bottom' :this.options.marginBottom
 }
		});
		this.fx.start( {
			0 : {
				'opacity' :1
 }
		});

		if (!this.options.isSticky) {
			this.hide.delay(this.options.delay, this);
		}
	},

	hide : function() {
		this.fx.start( {
			0 : {
				'opacity' :0
 }
		});
		this.fx.start( {
			0 : {
				'height' :0,
				'margin-bottom' :0
 }
		}).chain( function() {
			this.element.destroy();
		}.bind(this));
	}
});

bebo.ui.Growl.getInstance = function() {
	if (!$defined(bebo.ui.Growl.instance)) {
		bebo.ui.Growl.instance = new (bebo.ui.Growl)();
	}
	return bebo.ui.Growl.instance;
};

// returns bebo.ui.GrowlMessage
bebo.ui.Growl.notify = function(message, options) {
	return bebo.ui.Growl.getInstance().message(message, options);
};

bebo.ui.Button2 = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		theme :'button', // possible values: 'button', 'button small',
							// 'cancel', 'cancel small'
		'type' :'button',
		name :'',
		text :'Submit'
 },

	initialize : function(options) {
		this.setOptions(options);
	},

	toElement : function() {
		this.element = this.element || this.buildElement();
		return this.element;
	},

	buildElement : function() {
		var span = new (Element)('span', {
			'class' :'button-wrapper ' + this.options.theme
 });

		span.addEvent('click', function() {
			this.fireEvent('click', this);
		}.bind(this));

		var input = new (Element)('input', {
			'class' :'button',
			'type' :this.options.type,
			'name' :this.options.name,
			'value' :this.options.text
 });

		span.adopt(input);

		return span;
	}
});

bebo.ui.Popup = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		title :"Title",
		className :'popup',
		showClose :true
 },

	initialize : function(options) {
		this.setOptions(options);
	},

	toElement : function() {
		this.element = this.element || this.buildElement();
		return this.element;
	},

	buildElement : function() {
		var e = new (Element)('div', {
			'class' :this.options.className,
			'styles' : {
				'z-index' :'9998',
				'position' :'absolute'
 }
		});

		e.adopt(new (Element)('span', {
			'class' :'x1'
 }));

		var c = new (Element)('div', {
			'class' :'popup-content'
 });
		e.adopt(c);

		var hd = new (Element)('div', {
			'class' :'hd'
 });
		var bd = new (Element)('div', {
			'class' :'bd'
 });
		var ft = new (Element)('div', {
			'class' :'ft'
 });
		c.adopt(hd, bd, ft);

		var h3 = new (Element)('h3', {
			'text' :this.options.title
 });
		var a = new (Element)('a', {
			'href' :'#',
			'class' :'close'
 });
		a.addEvent('click', this.cancel.bindWithEvent(this));
		hd.adopt(h3, a);

		var ok = new (bebo.ui.Button2)( {
			'text' :'Ok'
 });
		ok.addEvent('click', this.ok.bindWithEvent(this));
		var cancel = new (bebo.ui.Button2)( {
			'text' :'Cancel',
			'theme' :'cancel'
 });
		cancel.addEvent('click', this.cancel.bindWithEvent(this));

		ft.adopt(ok, cancel);

		this.element = e;
		 _proxy_jslib_assign('', this, 'body', '=', ( bd));

		if (this.options.element) {
			 _proxy_jslib_handle(this, 'body', '', 0, 0).adopt(this.options.element);
		}

		return e;
	},

	setTitle : function(title) {
		this.toElement().getElement('h3').set('text', title);
	},

	show : function() {
		this.toElement(); // haxorville
	this.element.inject('eob-marker', 'before'); // render to get dimentions

	// display
	this.element.setStyle('display', 'block');
	this.element.setOpacity(1);

	// hide all ads
	$$('div.advertisement').setStyle('visibility', 'hidden');

	var eleSize = this.element.getSize();

	// center
	elementLeft = parseInt(window.getWidth() / 2 - eleSize.x / 2);
	if (this.options.position == 'top') {
		elementTop = 80;
	} else {
		// todo: convert to 1/3 top
	elementTop = parseInt(window.getHeight() / 3 - eleSize.y / 2);
	elementTop = elementTop < 80 ? 80 : elementTop;
}

this.element.set('styles', {
	'left' :elementLeft,
	'top' :elementTop,
	'position' :'fixed'  
});

},

ok : function() {
this.fireEvent('ok', this);
},

cancel : function() {
 _proxy_jslib_handle(this, 'close', '', 1, 0)();
this.fireEvent('cancel', this);
},

close : function() {
this.element.set('styles', {
	opacity :0,
	display :'none'
});

// show ads again
	$$('div.advertisement').setStyle('visibility', 'visible');
},

hide : _proxy_jslib_handle(this, 'close', '', 0, 0),

destroy : function() {
	this.element.destroy();
},

isVisible : function() {
	return !(this.element.getStyle('display') == 'none');
},

toggle : function() {
	if (!this.isVisible()) {
		 _proxy_jslib_handle(this, 'open', '', 1, 0)();
	} else {
		 _proxy_jslib_handle(this, 'close', '', 1, 0)();
	}
}
});

bebo.ui.Lightbox = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		// title: will add a header
		showClose :true,
		position :'default', // 'default', 'top'
		margin :15, // pixels
		className :'bebobox',
		overlayAll :false
  
 },

	initialize : function(options) {
		this.setOptions(options);
		this.adopt();
	},

	adopt : function() {
		this.element = new (Element)('div', {
			'class' :this.options.className
 });

		if (!this.options.element) {
			return;
		}

		this.element.inject('eob-marker', 'before');
		this.element.adopt(this.options.element);
		$(this.options.element).set('styles', {
			display :'block'
 });

		if ($defined(this.options.title)) {
			new (Element)('h1', {
				'text' :this.options.title
 }).inject(this.element, 'top');
		}

		if (this.options.showClose) {
			this.closeLink = new (Element)('img', {
				'src' :'http://s.bebo.com/img/icon_notattending.gif',
				'styles' : {
					'position' :'absolute',
					'top' :-5,
					'right' :-5,
					'z-index' :'9999',
					'cursor' :'pointer'
 }
			});
			this.closeLink.addEvent('click', function() {
				 _proxy_jslib_handle(this, 'close', '', 1, 0)()
 }.bind(this));
			this.closeLink.inject(this.element);
		}
	},

	buildBackground : function() {
		var bg = new (Element)('div', {
			'id' :'bebobox_bg',
			'styles' : {
				'z-index' :'9997',
				'background-color' :'black',
				'position' :'absolute'
 }
		});
		bg.inject(this.element, 'after');

		return bg;
	},

	open : function() {
		// init background
	this.bg = $('bebobox_bg');
	if (!this.bg) {
		this.bg = this.buildBackground();
	}

	// display
	this.element.setStyle('display', 'block');
	this.element.setOpacity(1);

	// hide all ads
	$$('div.advertisement').setStyle('visibility', 'hidden');

	var eleSize = this.element.getSize();

	// center
	elementLeft = parseInt(window.getWidth() / 2 - eleSize.x / 2);
	if (this.options.position == 'top') {
		elementTop = 80;
	} else {
		// todo: convert to 1/3 top
	elementTop = parseInt(window.getHeight() / 3 - eleSize.y / 2);
	elementTop = elementTop < 80 ? 80 : elementTop;
}

this.element.set('styles', {
	'left' :elementLeft,
	'top' :elementTop,
	'position' :'fixed'
});

this.bg.set('styles', {
	'display' :'block',
	'position' :'fixed',
	'top' :this.options.overlayAll ? 0 : elementTop - this.options.margin,
	'left' :this.options.overlayAll ? 0 : elementLeft - this.options.margin,
	'width' :this.options.overlayAll ? window.getWidth() : eleSize.x
 + this.options.margin * 2,
	'height' :this.options.overlayAll ? window.getHeight() : eleSize.y
 + this.options.margin * 2,
	'opacity' :0.4
});

// show and position close button
this.visible = true;
},

show : function() {
 _proxy_jslib_handle(this, 'open', '', 1, 0)();
},

close : function() {
this.bg.set('styles', {
	opacity :0,
	display :'none'
});

this.element.set('styles', {
	opacity :0,
	display :'none'
});
this.visible = false;

// show ads again
	$$('div.advertisement').setStyle('visibility', 'visible');
},

hide : _proxy_jslib_handle(this, 'close', '', 0, 0),

destroy : function() {
	this.element.destroy();
},

toggle : function() {
	if (!this.visible) {
		 _proxy_jslib_handle(this, 'open', '', 1, 0)();
	} else {
		 _proxy_jslib_handle(this, 'close', '', 1, 0)();
	}
}
});

/**
 * 
 */
bebo.ui.ConfirmationBox = new (Class)( {
	Extends :bebo.ui.Lightbox,

	options : {
		className :'bebobox confirm',
		message :'Message Body',

		onCancel : function() {
			 _proxy_jslib_handle(this, 'close', '', 1, 0)();
		}
	},

	initialize : function(options) {
		this.setOptions(options);

		var container = new (Element)('div');
		container.adopt(new (Element)('p', {
			'text' :this.options.message
 }));

		// buttons
	var buttons = new (Element)('p', {
		'class' :'button-row'
 });
	this.ok = new (Element)('img', {
		'src' :'/img/story/ok.gif'
 });
	this.cancel = new (Element)('img', {
		'src' :'/img/story/cancel.gif'
 });
	buttons.adopt(this.ok);
	buttons.adopt(this.cancel);
	container.adopt(buttons);

	this.options.element = container;

	this.ok.addEvent('click', function() {
		this.fireEvent('ok', this);
	}.bind(this));

	this.cancel.addEvent('click', function() {
		this.fireEvent('cancel', this);
	}.bind(this));

	 _proxy_jslib_handle(this, 'parent', '', 1, 0)(this.options);
}
})



bebo.ui.Button = new (Class)( {
	Implements : [ Options, Events ],

	options : {
		text :'Submit',
		onClick :this.click
 },

	initialize : function(options) {
		this.setOptions(options);

		if ($defined(options.element)) {
			var el = $(options.element);
			this.options.text = el.get('text');
			$(this).inject(el, 'after');
			el.destroy();
		}
	},

	toElement : function(text) {
		var text = this.options.text;

		var a = new (Element)('a', {
			'href' :'#',
			'class' :'submit-button'
 });
		a.addEvent('click', function() {
			this.fireEvent('click');
		}.bind(this));

		new (Element)('span', {
			'text' :text
 }).inject(a);

		return a;
	},

	click : function() {
		alert('clicked: ' + this.text);
	}
});

/* rounding */
bebo.ui.Rounder = new (Class)(
		{
			initialize : function() {
			},

			round : function(el) {
				var content = new (Element)('div', {
					'class' :'content'
 });
				 _proxy_jslib_handle(null, 'content', content, 0, 0).set('html', el.get('html'));
				el.empty();
				el.adopt( _proxy_jslib_handle(null, 'content', content, 0, 0));

				new (Element)(
						'b',
						{
							'class' :'rtop',
							'html' :'<b class="r1"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b>'
 }).inject(el, 'top');

				new (Element)(
						'b',
						{
							'class' :'rbottom',
							'html' :'<b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b>'
 }).inject(el, 'bottom');
			},

			roundElementsBySelector : function(selector) {
				$$(selector).each( function(el) {
					this.round(el);
				}.bind(this));
			}
		});

bebo.ui.Rounder.getInstance = function() {
	if (!$defined(bebo.ui.Rounder.instance)) {
		bebo.ui.Rounder.instance = new (bebo.ui.Rounder)();
	}
	return bebo.ui.Rounder.instance;
};
/* rounding: END */

bebo.ui.TitleRotator = new (Class)( {
	delay :3000, // ms

	initialize : function() {
	},

	addMessage : function(message) {
		if (!$defined(this.messages)) {
			this.title = document.title;
			this.messages = [ document.title ];
		}
		this.messages.include(message);

		if (!this.rotating) {
			this.stopNext = false;
			this.rotate();
		}
	},

	rotate : function() {
		this.rotating = true;

		if (this.stopNext) {
			this.stopNext = null;
			this.rotating = null;
			return;
		}

		if (!$defined(this.position)
 || this.position >= this.messages.length - 1) {
			this.position = -1;
		}
		this.position = this.position + 1;
		document.title =  _proxy_jslib_handle(this.messages, (this.position), 0, 0);
		this.rotate.delay(this.delay, this);
	},

	stop : function() {
		if (!this.rotating)
			return;

		this.stopNext = true;
		document.title = this.title;

		if (!$defined(this.messages))
			return;
		this.messages.empty();
		this.messages = null;
	},

	hasMessage : function(message) {
		if (!$defined(this.messages))
			return false;
		return this.messages.contains(message);
	},

	removeMessage : function(message) {
		if (!$defined(this.messages))
			return false;
		this.messages.erase(message);
	}
});

bebo.ui.TitleRotator.getInstance = function() {
	if (!$defined(bebo.ui.TitleRotator.instance)) {
		bebo.ui.TitleRotator.instance = new (bebo.ui.TitleRotator)();
	}
	return bebo.ui.TitleRotator.instance;
};

bebo.ui.Util = {
	hideAdsForOverlay : function() {
		if (!Browser.Engine.trident)
			return;
		$$('div.advertisement').setStyle('visibility', 'hidden');
	},

	showAds : function() {
		$$('div.advertisement').setStyle('visibility', 'visible');
	}
};

/* jason - for app icons on profile_jsp */
var loadTips = function() {
	window.addEvent('load', function(e) {
		new (Tips)($$('.fixed_tip'), {
			showDelay :100,
			hideDelay :100,
			fixed :true
 });
	});
};
/* end fixed tips */

var BeboModule = {

	show : function(id, profileMemberId, set) {
		var e = $('content_' + id);
		var a = $(id);
		var p = $('content_' + id);

		var url = "/c/profile/minimize?ProfileMemberId=" + profileMemberId
 + "&ModuleId=" + id;

		var arrow = a.getElement(".arrow");
		if (!arrow) {
			return;
		}

		arrow.addClass("arrow-down");
		arrow.removeClass("arrow-right");
		e.removeClass("not-shown");
		e.addClass("shown");

		if (e.get('text').trim() == "") {
			 _proxy_jslib_assign('', p, 'innerHTML', '=', ( "<div class=\"content\">Loading...</div>"));
			if (id.contains('app')) {
				var content_url = "/c/apps/get_app_content?ProfileMemberId="
 + profileMemberId + "&AppId=" + id;
				if (set !== false) {
					url += "&HideModule=N";
					var transaction = YAHOO.util.Connect.asyncRequest('GET',
							url, null, null);
				}
			} else {
				var content_url = url += "&GetContent=Y&HideModule=N";
			}
			new (Request.HTML)( {
				url :content_url,
				method :'GET',
				update :p,
				evalScripts :true
 }).send();
		} else {
			if (set !== false) {
				url += "&HideModule=N";
				var transaction = YAHOO.util.Connect.asyncRequest('GET', url,
						null, null);
			}
		}
	},
	hide : function(id, profileMemberId) {

		var e = $('content_' + id);

		var a = $(id);

		var arrow = a.getElement(".arrow-down");
		if (!arrow) {
			return;
		}

		arrow.addClass("arrow-right");
		arrow.removeClass("arrow-down");
		e.removeClass("shown");
		e.addClass("not-shown");

		var url = "/c/profile/minimize?ProfileMemberId=" + profileMemberId
 + "&ModuleId=" + id;
		url += "&HideModule=Y";
		var transaction = YAHOO.util.Connect.asyncRequest('GET', url, null,
				null);

	},
	hideshow : function(id, profileMemberId) {
		var e = $('content_' + id);
		if (e.hasClass('not-shown')) {
			this.show(id, profileMemberId);
		} else {
			this.hide(id, profileMemberId);
		}

	}
}

/* begin scroll to apps - jason */
// todo: namespace
var scrollToApp = function(id, profileMemberId) {
	if (typeof profileScroll == 'undefined') {
		profileScroll = new (Fx.Scroll)(window, {
			wait :false,
			duration :1000,
			offset : {
				'y' :-50
 },
			transition :Fx.Transitions.Cubic.easeOut
 });
	}

	if ($defined($(id))) {
		BeboModule.show(id, profileMemberId, false);
		profileScroll.toElement(id);
	} else {
		var div = new (Element)('div');
		div.inject('Comment', 'before');

		new (Request.HTML)( {
			'url' :"/c/apps/get_app_content?ProfileMemberId=" + profileMemberId
 + "&AppId=" + id + "&AsProfileModule=Y&Display=N",
			method :'get',
			evalScripts :true,
			update :div,
			onComplete : function(resp) {
				// fix this
			BeboModule.show(div.getChildren()[0], profileMemberId, false);
			profileScroll.toElement(div);
		}
		}).send();
	}
}
/* end scroll to apps */

/* ad placment */
var adMoveTimer = null; // will hold reference to interval that checks for new
// ad content

// this code moves ads rendered at the end of the document to the spot in the
// page where they belong
function _moveAd(startNode, destinationNode, endNodeId) {
	if (destinationNode == null) {
		return;
	}

	var curNode = startNode.nextSibling;
	while (curNode != null && curNode.id != "ad-block-hack-end") {
		// store nextSibling now, because it will be different if elCurrent is
		// removed from the DOM and appended to elAdBlock
		var nextSibling = curNode.nextSibling;

		// copy all content except script tags to the ad-block container
		if (curNode.nodeName != 'SCRIPT') {

			// adding a try/catch for Ken's WebTest
			try {

				// try turning on and off display to make sure that flash
				// anmiations start after they are moved
				if (curNode.nodeType != 3) { // node type 3 is #text
					var oldDisplay = curNode.style.display;
					curNode.style.display = "none";
				}

				destinationNode.appendChild(curNode);

				// set the display back to it's previous value
				if (curNode.nodeType != 3) { // #text node
					curNode.style.display = oldDisplay;
				}

			} catch (e) {
			}

		}
		curNode = nextSibling;
	}
	return;
}

// try to move the ad when the domready event fires, which is when the browser
// has finished parsing the HTML document,
// but before all of the images and external files have loaded
$(window).addEvent("domready", function() {

	var startNode = $('ad-block-hack-begin');
	if (!$defined(startNode)) {
		return;
	}

	var destinationNode = $("ad-spot-0");
	var endNodeId = "ad-block-hack-end";

	_moveAd(startNode, destinationNode, endNodeId);
});

// Unfortunately we need to be prepared for domready to fire too early and not
// see the ad content in the page.
// We'll define the same handler for window.onload too in case this happens. If
// domready succeedes, then this will just return.
$(window)
 .addEvent("load", function() {

			var startNode = $('ad-block-hack-begin');
			var destinationNode = $("ad-spot-0");
			var endNodeId = "ad-block-hack-end";

			_moveAd(startNode, destinationNode, endNodeId);

			// in IE6 and IE7, keep checking for ad content to move into
				// its spot
				if (destinationNode != null
 && (document.all
 || (MooTools.version == '1.2dev' && Browser.Engine.gecko) || (MooTools.version == '1.11' && window.gecko))) {
					 _proxy_jslib_handle(null, 'setInterval', setInterval, 1, 0)( function() {
						_moveAd(startNode, destinationNode, endNodeId)
 }, 250);
				}
			});

/* ad placement: END */

function hide(id) {
	return display(id, "none");
}

function show(id) {
	return display(id, "");
}

function toggle_element(element) {
	var display = element.style.display;
	if (display == "none") {
		element.style.display = "";
	} else {
		element.style.display = "none";
	}
}

function toggle(id) {
	var element =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(id);
	if (element != null) {
		toggle_element(element);
	} else {
		alert("could not find " + id);
	}
}

function display(elementName, display) {
	var elem =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(elementName);

	if (elem != null) {
		elem.style.display = display;
	} else {
		alert("could not find " + elementName);
	}
	return false;
}

function hiFlag() {
	var arrow =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("lang_arrow");
	 _proxy_jslib_assign('', arrow, 'src', '=', ( "/img/explore_arrow_white.gif"));
}

function lowFlag() {
	var arrow =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("lang_arrow");
	 _proxy_jslib_assign('', arrow, 'src', '=', ( "/img/explore_arrow_grey.gif"));
}

function showFlagsDiv(siteElem) {
	var tabBar =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('tab-bar');
	var flagDiv =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('flagDiv');

	if (flagDiv != null) {
		flagDiv.style.left = (tabBar.offsetLeft + 752) + "px";
		toggle_element(flagDiv);
	} else {
		alert("flagDiv not found");
	}
}

function verifySearch(searchForm, searchDefaultText) {
	// make sure not a search for '' or 'Search...'
	if ( _proxy_jslib_handle(searchForm.SearchTerm, 'value', '', 0, 0) == searchDefaultText
 ||  _proxy_jslib_handle(searchForm.SearchTerm, 'value', '', 0, 0) == '') {
		 _proxy_jslib_assign('', searchForm.SearchTerm, 'value', '=', ( ''));
		searchForm.SearchTerm.focus();
		return false;
	} else {
		return true;
	}
}

var BeboJS = {
	show : function(name) {
		var el =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(name);
		if (el) {
			el.style.display = 'inline';
		}
	},
	hide : function(name) {
		var el =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(name);
		if (el) {
			el.style.display = 'none';
		}
	}
};

var ampUnescaped = '&';

/* BEGIN: flags */
// TODO: cleanup and convert to namespace
// TODO: this could be simplified using moo tools
function closeFlagDiv() {
	var flagDiv =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('flagDiv');
	if (!flagDiv) {
		return;
	}
	if (flagDiv && flagDiv.style.display != 'none')
		flagDiv.style.display = 'none';
}

function _hideFlagsOnDocumentClick(e) {
	if ( _proxy_jslib_handle(document, 'getElementById', '', 0, 0) != null) { // aplatti: hack for QA WebTest
		if (!$('flagDiv')) {
			return;
		}
		var element = e.target || e.srcElement;
		var flagDiv =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("flagDiv");
		if (!flagDiv) {
			return;
		}
		if (element.className != null
 && element.className.indexOf('flagDiv') == -1
 && element.className.indexOf('currentFlag') == -1
 && flagDiv.style.display == "") {
			closeFlagDiv();
		}
	}
}
/*
 * if (window.addEventListener) { // W3C document.addEventListener("click",
 * _hideFlagsOnDocumentClick, false); } else if (window.attachEvent) { //
 * Microsoft document.attachEvent("onclick", _hideFlagsOnDocumentClick); }
 */

/* BEGIN: mp3 player */
// TODO: cleanup and convert to namespace
// JavaScript Document
var swfButtonIndex = 0;
function getFlashMovieObject(movieName) {
	if ( _proxy_jslib_handle(window.document, (movieName), 0, 0)) {
		return  _proxy_jslib_handle(window.document, (movieName), 0, 0);
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[(movieName)])
			return document.embeds[(movieName)];
	} else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(movieName);
	}
}

function getPlayer(vars, title) {

	if (vars == "") {
		var flashMovie = getFlashMovieObject("bebo_player");
		vars = flashMovie.GetVariable("_root.vars");
	}

	function flashObject(win, type) {
		if (type == "button") {
			swfButtonIndex++;
			idName = "bebo_player_button" + swfButtonIndex;
			swfWidth = 20;
			swfHeight = 20;
			swfName = "/mp3_player_button";
			vars = "songurl=" + vars;
		} else {
			idName = "bebo_player";
			swfWidth = 404;
			swfHeight = 354;
			swfName = "/mp3_player";
		}
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" id="'
 + idName + '" align="middle">');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<param name="allowScriptAccess" value="sameDomain" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<param name="FlashVars" value="' + vars + '" />');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<param name="movie" value="' + swfName + '.swf" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<param name="quality" value="high" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<param name="bgcolor" value="#000000" />');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<embed src="'
 + swfName
 + '.swf" quality="high" FlashVars="'
 + vars
 + '" bgcolor="#000000" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" name="'
 + idName
 + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</object>');
	}
	if (title) {
		winww = 404;
		winhh = 354;

		LeftPosition = (screen.width) ? (screen.width - winww) / 2 : 100;
		TopPosition = (screen.height) ? (screen.height - winhh) / 2 : 100;

		settings = 'width='
 + winww
 + ',height='
 + winhh
 + ',top='
 + TopPosition
 + ',left='
 + LeftPosition
 + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

		win =  _proxy_jslib_handle(null, 'open', open, 1, 0)("", 'bebo_player', settings);
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<head>');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<title>' + title + '</title>');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</head>');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<body bgcolor="#000000" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">');
		flashObject(win);
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</body>');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</html>');
		win.focus();
	} else {
		if (vars.substr(0, 4).toLowerCase() == "http") {
			flashObject(window, "button");
		} else {
			flashObject(window);
		}
	}
}

function getPlayer2(vars, title, htmlParentId) {

	if (vars == "") {
		var flashMovie = getFlashMovieObject("bebo_player");
		vars = flashMovie.GetVariable("_root.vars");
	}

	function flashObject(win, type, parentId) {
		if (type == "button") {
			swfButtonIndex++;
			idName = "bebo_player_button" + swfButtonIndex;
			swfWidth = 20;
			swfHeight = 20;
			// swfName = "http://s.bebo.com/music_player_button.swf?v=3";
			swfName = "/music_player_button.swf?v=4";
			// vars = "songurl="+vars;
			vars = vars;
		} else {
			idName = "bebo_player";
			swfWidth = 404;
			swfHeight = 354;
			swfName = "/mp3_player.swf";
		}

		if (parentId) {
			// aplatti: if an parent element ID was supplied, set the HTML
			// directly. (Used for MP3 buttons in modules dynamically added
			// through expand/collapse)

			var mp3Html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" id="'
 + idName
 + '" align="middle">'
 + '<param name="allowScriptAccess" value="sameDomain" />'
 + '<param name="FlashVars" value="'
 + vars
 + '" />'
 + '<param name="movie" value="'
 + swfName
 + '" />'
 + '<param name="quality" value="high" />'
 + '<param name="bgcolor" value="#000000" />'
 + '<embed src="'
 + swfName
 + '" quality="high" FlashVars="'
 + vars
 + '" bgcolor="#000000" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" name="'
 + idName
 + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true"/>'
 + '</object>';

			var parentElem =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(parentId);
			if (parentElem) {
				 _proxy_jslib_assign('', parentElem, 'innerHTML', '=', ( mp3Html));
			}

		} else {
			 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" id="'
 + idName + '" align="middle">');
			 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<param name="allowScriptAccess" value="sameDomain" />');
			 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<param name="FlashVars" value="' + vars + '" />');
			 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<param name="movie" value="' + swfName + '" />');
			 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<param name="quality" value="high" />');
			 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<param name="bgcolor" value="#000000" />');
			 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<embed src="'
 + swfName
 + '" quality="high" FlashVars="'
 + vars
 + '" bgcolor="#000000" width="'
 + swfWidth
 + '" height="'
 + swfHeight
 + '" name="'
 + idName
 + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true"/>');
			 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</object>');
		}
	}
	if (title) {
		winww = 404;
		winhh = 354;

		LeftPosition = (screen.width) ? (screen.width - winww) / 2 : 100;
		TopPosition = (screen.height) ? (screen.height - winhh) / 2 : 100;

		settings = 'width='
 + winww
 + ',height='
 + winhh
 + ',top='
 + TopPosition
 + ',left='
 + LeftPosition
 + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

		win =  _proxy_jslib_handle(null, 'open', open, 1, 0)("", 'bebo_player', settings);
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<head>');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('<title>' + title + '</title>');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</head>');
		 _proxy_jslib_handle(win.document
 , 'write', '', 1, 0)('<body bgcolor="#000000" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">');
		flashObject(win);
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</body>');
		 _proxy_jslib_handle(win.document, 'write', '', 1, 0)('</html>');
		win.focus();
	} else {
		if (vars.substr(0, 4).toLowerCase() == "song") {
			flashObject(window, "button", htmlParentId);
		} else {
			flashObject(window);
		}
	}
}

function playTrack(song) {
	if (song) {
		var objs =  _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)("object");
		var numObjs = objs.length;
		for ( var i = 0; i < numObjs; i++) {
			var curObj =  _proxy_jslib_handle(objs, (i), 0, 0);
			if (curObj.id.indexOf("bebo_player_button") == 0) {

				var flashMovie = getFlashMovieObject(curObj.id);

				if (flashMovie.GetVariable("_root.haskey") == "play") {
					if (song == flashMovie.GetVariable("_root.songId")) {
						flashMovie.SetVariable("_root.control", "play");
					}
				}
			}
		}
	}
}

function playTrack2(song) {
	if (song) {
		var objs =  _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)("object");
		var numObjs = objs.length;
		for ( var i = 0; i < numObjs; i++) {
			var curObj =  _proxy_jslib_handle(objs, (i), 0, 0);
			if (curObj.id.indexOf("bebo_player_button") == 0) {

				var flashMovie = getFlashMovieObject(curObj.id);

				if (flashMovie.GetVariable("_root.haskey") == "play") {
					if (song == flashMovie.GetVariable("_root.songId")) {
						flashMovie.SetVariable("_root.control", "play");
					}
				}
			}
		}
	}
}

function stopTrack(song) {
	var objs =  _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)("object");
	var numObjs = objs.length;
	for ( var i = 0; i < numObjs; i++) {
		var curObj =  _proxy_jslib_handle(objs, (i), 0, 0);
		if (curObj.id.indexOf("bebo_player_button") == 0) {
			var flashMovie = getFlashMovieObject(curObj.id);
			if (flashMovie && flashMovie.GetVariable("_root.haskey") == "play") {
				if (song != flashMovie.GetVariable("_root.songurl")) {
					flashMovie.SetVariable("_root.control", "stop");
				}
			}
		}
	}
}

function stopTrack2(song) {

	// aplatti: changed this to remove the need for a sequential number suffix
	// on the mp3 player button objects.

	var objs =  _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)("object");
	var numObjs = objs.length;
	for ( var i = 0; i < numObjs; i++) {
		var curObj =  _proxy_jslib_handle(objs, (i), 0, 0);
		if (curObj.id.indexOf("bebo_player_button") == 0) {

			var flashMovie = getFlashMovieObject(curObj.id);

			if (flashMovie.GetVariable("_root.haskey") == "play") {
				if (song != flashMovie.GetVariable("_root.songId")) {
					flashMovie.SetVariable("_root.control", "stop");
				}
			}
		}
	}
}

/* END mp3 player */

var LangSelector = new (Class)( {

	initialize : function(element) {
		this.selectorEl = $(element);

		this.currentLangEl = this.selectorEl.getElement('li'); // first li node
		this.langListEl = this.selectorEl.getElement('ul'); // should always be
		// a 'ul'

		this.attach();
	},

	attach : function() {
		this.selectorEl.addClass('lang-selector');
		this.currentLangEl.addClass('lang-current');
		this.langListEl.addClass('lang-list');

		this.selectorEl.setStyle('display', 'block');
		this.selectorEl.setStyle("pointer", "cursor");

		this.currentLangEl.setStyle('display', 'block');

		this.currentLangEl.addEvent('click', this.titleClick
 .bindWithEvent(this));
	},

	titleClick : function(event) {
		if (this.selectorEl.hasClass('active')) {
			this.hide();
		} else {
			this.selectorEl.addClass('active');
			this.langListEl.setStyle('display', 'block');
			this.selectorEl.addEvent('mouseleave', this.mouseLeave
 .bindWithEvent(this));
		}
	},

	mouseLeave : function(event) {
		this.hide();
	},

	hide : function() {
		this.selectorEl.removeClass('active');
		this.langListEl.setStyle('display', 'none');

		this.selectorEl.removeEvents('mouseleave');
	}
});

bebo.ui.GlobalSearchBox = new (Class)( {
	initialize : function(form) {
      this.element = $(form);
      
		this.input = this.element.getElement('input');
		this.button = this.element.getElement('button');
		
		this.text = this.input.get('value');

		this.input.addEvent('focus', this.focus.bindWithEvent(this));
		this.input.addEvent('blur', this.blur.bindWithEvent(this));
		
		this.button.addEvent('click', this.submit.bindWithEvent(this));
	},
	
	submit: function(event) {
	   if(this.input.get('value') != this.text) return;
	   
      event.stop();
      this.focus();
      this.input.focus();
	},

	focus : function() {
		if (this.input.get('value') == this.text) {
			this.input.set('value', '');
		}
	},

	blur : function() {
		if (this.input.get('value') == '') {
			this.input.set('value', this.text);
		}
	}
});

// TODO: this should eventually be moved to its own JS file since bebo.js is
// becoming more of a library
YAHOO.util.Event.onContentReady('more-menu', function() {
	new (bebo.ui.DropDown)('more-menu');
});
YAHOO.util.Event.onContentReady('yahoo-search-form', function() {
	new (bebo.ui.GlobalSearchBox)('yahoo-search-form');
});

$(document).addEvent('endofbody', function(event) {
	$$("ul.lang-selector").each( function(el) {
		new (LangSelector)(el);
	});
});
 ;
_proxy_jslib_flush_write_buffers() ;