;function string_to_slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  
  // remove accents, swap ñ for n, etc
  var from = "ÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛàáäâèéëêìíïîòóöôùúüûÑñÇç·/_,:;";
  var to   = "aaaaeeeeiiiioooouuuuaaaaeeeeiiiioooouuuunncc------";
  for (var i=0, l=from.length ; i<l ; i++) {
    str = str.replace(new RegExp(from[i], "g"), to[i]);
  }

  str = str.replace(/[^a-zA-Z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/[-]+/g, '-') //replace multiple consecutive '-' with a single '-'
    .toLowerCase();
  return str;
};

;jQuery(function($){
  $('#VideoPopup').dialog({ autoOpen:false, height: 410, width: 630, modal:true, closeOnEscape:true, beforeclose:function() { $('#VideoPopup').html(''); }});
});

var AttachedVideo = (function($) {
	var me = {};
	
	/**
	 * Show a popup dialog to play the video.
	 * 
	 *  @param embed The flash embed code
	 *  @param width The width of the video
	 *  @param height The height of the video
	 *  @param title The title of the Dialog
	 */
	me.popup = function(embed, width, height, title) {
		var VideoPopup = $('#VideoPopup');
		VideoPopup.dialog('option', 'title', title);
		VideoPopup.dialog('option', 'width', width + 28);
		VideoPopup.dialog('option', 'height', height + 60);
		VideoPopup.html(embed);
		VideoPopup.dialog('open');
		
		//Track the Ajax slide change in Google
		if ('object' == typeof(pageTracker)) {
			if ('undefined' == typeof(title) || '' == title) {
				var slug = 'unknown';
			}
			else {
				var slug = string_to_slug(title);
			}
			var url = window.location.pathname + '/videopopup/' + slug;
			url = url.replace(/[/]+/g, '/');
			pageTracker._trackPageview(url);
		}
	};
	
	return me;
})(jQuery);
