(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">Family Fun in Baltimore</div>';
	html += '<div id="vmb_media">';
		html += embed_video('PRT1NO4va4k', '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: 'PRT1NO4va4k', url: 'http://www.visitmybaltimore.com/members/show/2/18096'});
		
			html += '<a href="#" rel="0" title="B&amp;O Museum REV 1 1"><img src="http://i.ytimg.com/vi/PRT1NO4va4k/2.jpg" height="62" alt="B&amp;O Museum REV 1 1" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'qRLTogPkM7g', url: 'http://www.visitmybaltimore.com/members/show/2/5182'});
		
			html += '<a href="#" rel="1" title="Fall in Baltimore"><img src="http://i.ytimg.com/vi/qRLTogPkM7g/2.jpg" height="62" alt="Fall in Baltimore" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'GuBIa1rDDaA', url: 'http://www.visitmybaltimore.com/members/show/2/57984'});
		
			html += '<a href="#" rel="2" title="Star-Spangled Trails in Baltimore"><img src="http://i.ytimg.com/vi/GuBIa1rDDaA/2.jpg" height="62" alt="Star-Spangled Trails in Baltimore" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: '0eekqzt6ERk', url: 'http://www.visitmybaltimore.com/members/show/2/18185'});
		
			html += '<a href="#" rel="3" title="Geppi\'s Entertainment Museum"><img src="http://i.ytimg.com/vi/0eekqzt6ERk/2.jpg" height="62" alt="Geppi\'s Entertainment Museum" /></a>';
		
		html += '</li>';
		
		html += '<li>';
		vmb_items.push({type: 'youtube', identifier: 'LTSbtZTu_eU', url: 'http://www.visitmybaltimore.com/members/show/2/15794'});
		
			html += '<a href="#" rel="4" title="National Great Blacks in Wax Museum"><img src="http://i.ytimg.com/vi/LTSbtZTu_eU/2.jpg" height="62" alt="National Great Blacks in Wax Museum" /></a>';
		
		html += '</li>';
		
	html += '</ul>';
	html += '</div>';
	html += '<a href="#" id="vmb_next">Next</a>';
	html += '</div>';
	html += '<div id="vmb_item_title"><p>B&amp;O Museum REV 1 1</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);
})();
