var appendedTeasers = 0;
var videoLists = new Hash();
var activeMetaID = 0;
var wallpaper = 0;

//iterates over all XML children and returns the value of the text child node.
function getXMLTextNode(theNode) {
	ret = "";
	
   for (var j=0; j<theNode.length; j++) {
   	
      if (theNode[j].nodeType == "3") {
      	//textnode
         ret += text + theNode[j].nodeValue;
      }
   }
   alert (ret);
   return ret;
						   
}

//opens the platform menu
function submenu(menuname) {
	//handle all others
	var foldmenus = $$(".foldmenu");
	for (var i=0;i<foldmenus.length;i++) {
		//alert(foldmenus[i].identify());
		
		if (foldmenus[i].identify()!=menuname)
			foldmenus[i].style.display="none";
	}
	
	Effect.toggle(menuname,'appear',{ duration: 0.4});
	
}

var VLInfo = Class.create({
	initialize: function (aTable, size) {
		//neue_xbox_videos...
		this.theTable = aTable;
		this.name = aTable.identify();
		this.elementcount = size; 
		this.position = 0; 
	},
	alterPosition: function (amount) {
		this.position+=amount;
		if (this.position < 0)
			this.position = 0;
	}
});
function scrollTeaser(teaserName, amount) {
	
	var theTable = $(teaserName);
	var listInfo = videoLists.get(teaserName);
	if ( listInfo == undefined ) {
		listInfo = videoLists.set(teaserName, new VLInfo($(teaserName),Math.abs(amount)));
	}
	
	var newPreviews = getPreviews(listInfo,amount);
	listInfo.alterPosition(amount);
	
		var newTable = new Element('div',{'id':teaserName,'style':'display:none'});
		if (newPreviews.length != 0) { 
			for(idx = 0; idx < newPreviews.length; idx++) {
				newPreviews[idx].createDOM(newTable);
			}
			mainPane = theTable.up().up();
			pf = mainPane.down(".pfeilrechts");
			pf.show();
		} else {
			newTable.insert("<h1>Ende</h1>");
			mainPane = theTable.up().up();
			pf = mainPane.down(".pfeilrechts");
			pf.hide();
		}
		new Effect.Fade(theTable, { duration:0.2,
		   afterFinish: function() { 
		   	theTable.replace(newTable);
		   	new Effect.Appear(newTable, { duration:0.2 }); 
		   	prepareBubbles(teaserName); 
		   }
		});	
}
   
function getPreviews(vlinfo,amount) {
		//offset = vlinfo.position+amount;
		//amt = Math.abs(amount);
		//alert ("Hole "+amount+" Videos ab " + offset +  "des Typs " + vlinfo.params);
		var url = '/standalone/ajax.getVideos.php';
		var xparams = 'params='+vlinfo.name+'&amt='+amount;
		new Ajax.Request(url, {
			method: 'post',
			parameters: xparams,
			asynchronous: false,
			onSuccess: function(response) {
				root = response.responseXML.getElementsByTagName("meta")[0];
				metadatas = root.getElementsByTagName("metadata");
				vMetas = new Array();
				for (var idx = 0; idx<metadatas.length; idx++) {
					metadata = metadatas[idx];
					vMeta = new VideoMeta(vlinfo);
					vMeta.byXML(metadata);
					vMetas[idx] = vMeta;
				}
			}
		});
		
		return vMetas;
}

var VideoMeta = Class.create({
	initialize:function(vlinfo) {
		this.vlinfo = vlinfo;
	},
	byXML:function(xml) {
		this.xmetaid = xml.attributes[0].nodeValue;
		try {
			this.xtitle = decodeURIComponent(xml.getElementsByTagName("title")[0].firstChild.nodeValue);
			
		} catch (e) {
			this.xtitle = "Kein Titel";
		}
		try {
			this.xsubtitle = decodeURIComponent(xml.getElementsByTagName("subtitle")[0].firstChild.nodeValue);
		} catch (e) {
			this.xsubtitle = "Kein SubTitel";
		}	
		
		this.xthumburl = xml.getElementsByTagName("thumb")[0].firstChild.nodeValue;
		this.xdurationM = xml.getElementsByTagName("durationM")[0].firstChild.nodeValue;
		this.xdurationS = xml.getElementsByTagName("durationS")[0].firstChild.nodeValue;
	},
	createDOM:function(appender) {
		theTable = $(this.vlinfo.name);
		var href = new Element('a',{'href':'/play/'+this.vlinfo.name+'/'+this.xmetaid+'.html'});
		var newDiv = new Element('div',{'class':'videoPreview'});	
		newDiv.identify('main'+this.xmetaid);
		if (theTable.up().up().hasClassName('videoreel') == true) {
			if (activeMetaID == this.xmetaid) {	
				newDiv.addClassName("active");
			}
		}
				
		var tnail = new Element('span', {'class':'thumbnail'});
		var tnailimg = new Element('img', {'src':this.xthumburl});
		var title = new Element('h3').update(this.xtitle);
		var subtitle = new Element('span',{'class':'subtitle'}).update(this.xsubtitle);
		var duration = new Element('span',{'class':'duration'}).update(this.xdurationM+":"+this.xdurationS);
			
		tnail.update(tnailimg);
		href.update(tnail);
		href.insert(title);
		newDiv.update(href);
		newDiv.insert(subtitle);
		newDiv.insert(duration);
		
		appender.insert(newDiv);
	}
});

function showBubble(bubDiv,event) {
	previewElem = event.element();
	if (!(previewElem.hasClassName("videoPreview"))) {
		previewElem = previewElem.up(".videoPreview");
	}
	var subt = previewElem.select(".subtitle");
	var tit  = previewElem.select("h3");
	var dur  = previewElem.select(".duration");
	
	_subtitle = subt[0].firstChild.nodeValue;
	_title    = tit[0].firstChild.nodeValue;
	_duration = dur[0].firstChild.nodeValue;
	
	var titElem = bubDiv.select('h4');
	var pElem = bubDiv.select('p');
	var spElem = bubDiv.select('.dur');
	titElem[0].firstChild.nodeValue = _title;
	pElem[0].firstChild.nodeValue = _subtitle;
	pElem[1].firstChild.nodeValue = _duration;
	
	var pos = previewElem.positionedOffset();
	w = (previewElem.getWidth());
	l = (pos['left']);
	newl = l + (w/2);
	
	bubDiv.setStyle({"left":newl+"px"});
	//bubDiv.update(previewElem.identify());
	bubDiv.show();
}
// preview.observe('mouseleave',function(event){bubDiv.hide()});
function prepareBubbles(containerName) {
	
	var theContainer = $(containerName);
	if (theContainer.hasClassName("videoteaser")) {return; }
	var allDivs = theContainer.select(".videoPreview");
	var bubDiv = $(containerName+"Bubble");
	if (bubDiv == null) 
		return;
		
	for (var i = 0; i<allDivs.length;i++) {
		var preview = allDivs[i];
   		preview.observe('mouseenter',function(event){showBubble(bubDiv,event)});
   	}
   theContainer.observe('mouseleave',function() {bubDiv.hide()});
 }
 
 function switchTab(_href) {
 	var href = Element.extend(_href);
 	var hrefname = href.identify();
 	var tabname =  "t"+hrefname+"Tab";
 	var  tabliname = "t"+hrefname;
 	var  thetabli = $(tabliname);
 	var  thetab = $(tabname);
 	
 	var tabview = $("tabview");
 	lis = tabview.select("li");
 	
 	for (i=0;i<lis.length;i++) {
 		nam = lis[i].identify();
 	    if (nam != tabliname) {
 	    	lis[i].removeClassName("current");
 	    	_tbnam = nam + "Tab";
 	    	$(_tbnam).hide();
 	    }
 	}
 	
 	thetabli.addClassName("current");
 	thetab.show();
 }
 
 function addComment() {	
 	var username = encodeURIComponent($F($("commentusername")));
 	var body = encodeURIComponent($F($("commentbody")));
 	var metaid = $F($("commentMetaID"));
 	
 	var url = '/standalone/ajax.postComment.php';
	var xparams = 'metaid='+metaid+'&username='+username+'&body='+body;
	
 	new Ajax.Request(url, {
		method: 'post',
		parameters: xparams,
		asynchronous: false,
		onSuccess: function(response) {
			new Effect.BlindUp('commentform',{duration:0.5,
				afterFinish: function() {
					$('commentform').remove();
					$('tKommentareTab').insert(response.responseText);
				}
			});
		}
	});	
 }