(function(){
	/**
	 * Dean Edwards addEvent script
	 */
	function addEvent(element, type, handler) {
	  // assign each event handler a unique ID
	  if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	  // create a hash table of event types for the element
	  if (!element.events) element.events = {};
	  // create a hash table of event handlers for each element/event pair
	  var handlers = element.events[type];
	  if (!handlers) {
	    handlers = element.events[type] = {};
	    // store the existing event handler (if there is one)
	    if (element["on" + type]) {
	      handlers[0] = element["on" + type];
	    }
	  }
	  // store the event handler in the hash table
	  handlers[handler.$$guid] = handler;
	  // assign a global event handler to do all the work
	  element["on" + type] = handleEvent;
	};
	// a counter used to create unique IDs
	addEvent.guid = 1;

	function removeEvent(element, type, handler) {
	  // delete the event handler from the hash table
	  if (element.events && element.events[type]) {
	    delete element.events[type][handler.$$guid];
	  }
	};

	function handleEvent(event) {
	  var returnValue = true;
	  // grab the event object (IE uses a global event object)
	  event = event || fixEvent(window.event);
	  // get a reference to the hash table of event handlers
	  var handlers = this.events[event.type];
	  // execute each event handler
	  for (var i in handlers) {
	    this.$$handleEvent = handlers[i];
	    if (this.$$handleEvent(event) === false) {
	      returnValue = false;
	    }
	  }
	  return returnValue;
	};

	function fixEvent(event) {
	  // add W3C standard event methods
	  event.preventDefault = fixEvent.preventDefault;
	  event.stopPropagation = fixEvent.stopPropagation;
	  return event;
	};
	
	fixEvent.preventDefault = function() {
	  this.returnValue = false;
	};
	
	fixEvent.stopPropagation = function() {
	  this.cancelBubble = true;
	};


	function loadCSS()
	{
    stylesheet = document.createElement("link");
    stylesheet.rel = "stylesheet";
    stylesheet.type = "text/css";
    stylesheet.href = 'http://www.visitmybaltimore.com/css/widgets.css';
    stylesheet.media = "all";
    document.lastChild.firstChild.appendChild(stylesheet);
	}
	
	function embed_video(video_id, type)
	{
		var url;
		var width;
		var height;
		
	 	if (type == 'youtube')
		{
			url = 'http://www.youtube.com/v/' + video_id + '&amp;rel=0&amp;fs=0';
    	width = 355;
    	height = 292;
		}
		else if (type == 'vimeo')
		{
			url = 'http://vimeo.com/moogaloop.swf?clip_id=' + video_id + '&amp;server=vimeo.com&amp;show_title=0&show_byline=0&amp;show_portrait=0&amp;color=EE611F&amp;fullscreen=0';
      width = 355;
      height = 292;
		}

		var embed = '<object width="' + width + '" height="' + height + '">';
		embed += '<param name="movie" value="' + url + '"></param>';
		embed += '<param name="allowFullScreen" value="false"></param>';
		embed += '<param name="bgcolor" value="#000000"></param>';
		embed += '<param name="wmode" value="transparent"></param>';
		embed += '<embed src="' + url + '" type="application/x-shockwave-flash"';
		embed += 'width="' + width + '" height="' + height + '"';
		embed += 'allowfullscreen="false" bgcolor="#000000" wmode="transparent">';
		embed += '</embed>';
		embed += '</object>';
		
		return embed;
	}
	
	function didClickItem()
	{
		var playlist = document.getElementById('vmb_playlist_items');
		var items = playlist.getElementsByTagName('a');

		for (var i = 0; i < items.length; i++)
		{
			items[i].style.border = '0';
			items[i].style.width = '62px';
			items[i].firstChild.style.margin = '0';
		}
		
		addSelected(this);
		
		var title = document.getElementById('vmb_item_title');
		var vmb_link = document.getElementById('vmb_link');
		var vmb_media = document.getElementById('vmb_media');
		
		var attrs = vmb_items[this.rel];
		if (attrs.type == 'flickr' || attrs.type == 'picasa')
		{
			vmb_media.innerHTML = '<img src="' + this.href + '" border="0" width="355" alt="' + this.title + '" />';
		}
		else
		{	
			vmb_media.innerHTML = embed_video(attrs.identifier, attrs.type);
		}
		
		vmb_link.href = attrs.url;
		title.firstChild.innerHTML = this.title;
		
		return false;
	}
	
	function addSelected(element)
	{
		element.style.border = '3px solid ' + color;
		element.style.width = '56px';
		element.firstChild.style.margin = '-3px';
	}
	
	function previousItem()
	{
		var playlist = document.getElementById('vmb_playlist_items');
		var top = parseInt(playlist.style.top);
		if (top !== 0)
		{	
			playlist.style.top = top + 215 + 'px';
		}
		
		return false;
	}
	
	function nextItem()
	{
		var playlist = document.getElementById('vmb_playlist_items');
		var top = parseInt(playlist.style.top);
		playlist.style.top = top - 215 + 'px';
		
		return false;
	}
	
	var colors = {orange: '#ED6015', green: '#B3BE0D', blue: '#4C7A91', pink: '#F0433D'};
	var color = colors.blue;
	var vmb_items = [];
	
	var html = '<div id="vmb_widget" class="vmb_blue">';
	html += '<div id="vmb_title">Baltimore\'s Inner Harbor</div>';
	html += '<div id="vmb_media">';
		html += embed_video('YQkb8RcYKkM', 'youtube');
	html += '</div>';
	html += '<div id="vmb_playlist">';
	html += '<a href="#" id="vmb_previous">Previous</a>';
	html += '<div id="vmb_playlist_container">';
	html += '<ul id="vmb_playlist_items" style="top: 0;">';
	
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'YQkb8RcYKkM', url: 'http://www.visitmybaltimore.com/members/show/276/64531'});
		
			html += '<a href="#" rel="0" title="Water Balloons"><img src="http://i.ytimg.com/vi/YQkb8RcYKkM/2.jpg" height="62" alt="Water Balloons" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'qzaT1d718Sw', url: 'http://www.visitmybaltimore.com/members/show/7/3980'});
		
			html += '<a href="#" rel="1" title="RickRoll Baltimore Flash Mob"><img src="http://i.ytimg.com/vi/qzaT1d718Sw/2.jpg" height="62" alt="RickRoll Baltimore Flash Mob" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'bihW5s6xf2A', url: 'http://www.visitmybaltimore.com/members/show/85/23729'});
		
			html += '<a href="#" rel="2" title="The Fudgery"><img src="http://i.ytimg.com/vi/bihW5s6xf2A/2.jpg" height="62" alt="The Fudgery" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'Cp9R5XgYol8', url: 'http://www.visitmybaltimore.com/members/show/2/31'});
		
			html += '<a href="#" rel="3" title="Cal Ripken, Jr. is an Oriole fan and a Baltimore fan."><img src="http://i4.ytimg.com/vi/Cp9R5XgYol8/default.jpg" height="62" alt="Cal Ripken, Jr. is an Oriole fan and a Baltimore fan." /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'myE3JDS2ho8', url: 'http://www.visitmybaltimore.com/members/show/2/22211'});
		
			html += '<a href="#" rel="4" title="Maryland Science Center to Harbor East in 2 Minutes"><img src="http://i.ytimg.com/vi/myE3JDS2ho8/2.jpg" height="62" alt="Maryland Science Center to Harbor East in 2 Minutes" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3423507105', url: 'http://www.visitmybaltimore.com/members/show/27/23768'});
		
			html += '<a href="http://farm4.static.flickr.com/3329/3423507105_3cfe7781b7.jpg" rel="5" title="P1030251"><img src="http://farm4.static.flickr.com/3329/3423507105_3cfe7781b7_s.jpg" height="62" alt="P1030251" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '2834815874', url: 'http://www.visitmybaltimore.com/members/show/89/11304'});
		
			html += '<a href="http://farm4.static.flickr.com/3028/2834815874_d07052aea6.jpg" rel="6" title="young juggler in Baltimore\'s Inner Harbor"><img src="http://farm4.static.flickr.com/3028/2834815874_d07052aea6_s.jpg" height="62" alt="young juggler in Baltimore\'s Inner Harbor" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3045616635', url: 'http://www.visitmybaltimore.com/members/show/18/7515'});
		
			html += '<a href="http://farm4.static.flickr.com/3069/3045616635_6ed9067a9e.jpg" rel="7" title="london034"><img src="http://farm4.static.flickr.com/3069/3045616635_6ed9067a9e_s.jpg" height="62" alt="london034" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3100515817', url: 'http://www.visitmybaltimore.com/members/show/27/7296'});
		
			html += '<a href="http://farm4.static.flickr.com/3094/3100515817_51bca0b807.jpg" rel="8" title="USS Constellation Baltimore"><img src="http://farm4.static.flickr.com/3094/3100515817_51bca0b807_s.jpg" height="62" alt="USS Constellation Baltimore" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3423507333', url: 'http://www.visitmybaltimore.com/members/show/27/23767'});
		
			html += '<a href="http://farm4.static.flickr.com/3549/3423507333_74bf432abb.jpg" rel="9" title="P1030353"><img src="http://farm4.static.flickr.com/3549/3423507333_74bf432abb_s.jpg" height="62" alt="P1030353" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3483908081', url: 'http://www.visitmybaltimore.com/members/show/7/30304'});
		
			html += '<a href="http://farm4.static.flickr.com/3625/3483908081_1615cd8d2c.jpg" rel="10" title="Sea monsters"><img src="http://farm4.static.flickr.com/3625/3483908081_1615cd8d2c_s.jpg" height="62" alt="Sea monsters" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3101352440', url: 'http://www.visitmybaltimore.com/members/show/27/7295'});
		
			html += '<a href="http://farm4.static.flickr.com/3165/3101352440_9413ef06e7.jpg" rel="11" title="Inner Harbor Street Performers"><img src="http://farm4.static.flickr.com/3165/3101352440_9413ef06e7_s.jpg" height="62" alt="Inner Harbor Street Performers" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'flickr', identifier: '3424314986', url: 'http://www.visitmybaltimore.com/members/show/27/23769'});
		
			html += '<a href="http://farm4.static.flickr.com/3653/3424314986_c540715864.jpg" rel="12" title="P1040113"><img src="http://farm4.static.flickr.com/3653/3424314986_c540715864_s.jpg" height="62" alt="P1040113" /></a>';
		
		html += '</li>';
		
	html += '</ul>';
	html += '</div>';
	html += '<a href="#" id="vmb_next">Next</a>';
	html += '</div>';
	html += '<div id="vmb_item_title"><p>Water Balloons</p><a href="http://www.visitmybaltimore.com/" target="_blank" id="vmb_link">See it on VMB</a></div>';
	html += '</div>';
	
	loadCSS();
	document.write(html);
	
	// Bind event listeners
	var items = document.getElementById('vmb_playlist_items').getElementsByTagName('a');
	
	for (var i = 0; i < items.length; i++)
	{
		if (i == 0)
		{
			addSelected(items[i]);
		}
		
		addEvent(items[i], 'click', didClickItem);
	}
	
	addEvent(document.getElementById('vmb_previous'), 'click', previousItem);
	addEvent(document.getElementById('vmb_next'), 'click', nextItem);
})();
