/* functions for showing a list of YouTube-videos */

// global vars:
var ytFeed, errorTimeout;

if (!ytFeed){
	// load js from google:
	insertIntoHead("http://gdata.youtube.com/feeds/users/" + ytUserName + "/favorites?max-results=16&alt=json-in-script&callback=listVideos", "javascript");
	
	// error handling via timeout:
	var errorTimeout = setTimeout(errorMessage, 10000);
}

// display of error-message (called from timeout):
function errorMessage(){
	var pEl = new Element("p").addClassName("errormessage");
	pEl.innerHTML = "Der YouTube-Username \"" + ytUserName + "\" ist scheinbar ungültig.";
	$("videoList").insert(pEl);
}

// callback-function (called from google-script):
function listVideos(data){
	// cancel the error-timeout:
	clearTimeout(errorTimeout);
	
	// display parameters (columns and max cells):
	var rows = typeof(ytRows) == "undefined" ? 4 : ytRows;
	var cols = typeof(ytCols) == "undefined" ? 4 : ytCols;
	var maxCount = rows * cols;

	if (!ytFeed)
		ytFeed = data;
	// elements or rendering:
	var tbl = new Element("table", {
		"class": "list blocks videos",
		"cellspacing": 0,
		"cellpadding": 0,
		"border": 1
	});
	var tbody = new Element("tbody");
	tbl.insert(tbody);
	
	$("videoList").insert(tbl);
	var tr = new Element("tr");
	var td = new Element("td");
	var div = new Element("div");
	var sep = td.clone();
	sep.writeAttribute({"class": "sep", "colspan": cols});
	sep.insert(div.clone());
	var a = new Element("a", {"class": "arrow page-options"});
	var img = new Element("img", {"width": 94, "height": 70});
	var br = new Element("br");
	var span = new Element("span");
	var arrows = span.clone().addClassName("arrow");
	arrows.innerHTML = ">> ";
	var link = new Element("strong").insert(arrows);
	var _tr, _td, _a;
	// two counters: one for the entries (i), one for the rendered cells (count), entries can be skipped if not embeddable:
	var i = 0, count = 0, entry = null;
	
	while (entry = data.feed.entry[i]){
		if (maxCount && count == maxCount)
			break;
		// check if video is embeddable, if not, skip to next entry:
		if (entry.yt$noembed || !entry.media$group.media$thumbnail){
			i++;
			continue;
		}
		_td = td.clone()
		// add new tr and separator:
		if (count % cols == 0){
			if (count)
				tbody.insert(tr.clone().insert(sep.clone()));
			_tr = tr.clone();
			tbody.insert(_tr);
			// first td in row:
			_td.addClassName("first");
		}
		_a = a.clone();
		_a.insert(
			img.clone().writeAttribute({
				"src": entry.media$group.media$thumbnail[0].url,
				"alt": entry.media$group.media$title.$t,
				"title": entry.media$group.media$title.$t
			})
		);
		_a.insert(br.clone());
		_link =	link.clone();
		_span = span.clone().addClassName("name");
		_span.innerHTML = entry.media$group.media$title.$t;
		_link.insert(_span);
		_a.insert(_link);
		_a.writeAttribute("href", getContentHref(entry));
		
		_a.observe('click', function(e){
			Event.stop(e);
			if (!myLightWindow) {
				alert('The video presenter is not ready yet, please wait a little.');
			}
			else
			{
				myLightWindow.activateWindow({
					href: '/components/youTubeVideo/?width=445&height=375&url=' + encodeURIComponent(this.href), 
					title: this.title,
					iframeEmbed: false,
					width: 445,
					height: 375,
					type: 'external'
				});
			}
		})
		_td.insert(_a);
		_tr.insert(_td);
		i++;
		count++;
	}
	/*
	// add empty cells (not necessary):
	while (count % cols != 0){
		td.clone().setHTML("&nbsp;").injectInside(_tr);
		i++;
	}
	*/
	
	// init lightbox:
	/*var vb = new Lightbox({
		relString: "videobox",
		initialWidth: 445,
		initialHeight: 375
	});*/
}

function getContentHref(entry){
	for (var i = 0, content; content = entry.media$group.media$content[i]; i++){
    if (content.type == "application/x-shockwave-flash")
      return content.url;
  }
  return null;
}

