// Framework
function sA(o,a,v){
	o.setAttribute(a,v);	
}
function aC(p,c){
	p.appendChild(c);
}
function setClass(o,c){
	o.className=c;
}
function rC(p){
	while(p.firstChild) p.removeChild(p.firstChild);
}
function dump(t){
	document.getElementById('testoutput').innerHTML += "\n<br />"+t;
}
function gEBTN(p,t){
	return p.getElementsByTagName(t);
}
function gIE(p,t){
	var a = p.getElementsByTagName(t);
	return (a.length<1?"Empty":a[0].firstChild.nodeValue);
	
}
function readDate(at){
	if(at!=null){
		var t = at.split(":");
		return new Date(t[0],t[1],t[2]);
	}else return new Date();
}
function gNV(p,t){
	return p.getElementsByTagName(t)[0].firstChild.nodeValue;
}
addEvent = function(o, e, f, s){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e){
		try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
	}
};
removeEvent = function(o, e, f, s){
	for(var i = (e = o["_on" + e] || []).length; i;) 
		if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
			return delete e[i];
	return false;
};

// Functions
// Type 0: album, type 1 is foto, type 2 is else
function collection(n,t,de,da,th,s,k,i){
	this.name = n;this.title = t;
	this.desc = de;this.date = da;
	this.subs = s;this.thumb = th;
	this.key = k;this.id = i;
	//dump("A collection instance was created.");
}
collection.prototype.addSub = function(s){
	this.subs[this.subs.length++] = s;	
};
function media(n,t,de,da,m,s,th){
	this.name = n;this.title= t;
	this.desc = de;this.date = da;
	this.mime = m;this.src  = s;
	this.thumb = th;
	//dump("A media instance was created.");
}

// Mediacenter class
var mcinstance = null;
function mediacenter(w){
	this.name="MediaCenter";this.version="v1.0.0";this.author="Herman Banken";
	this.wrapperid = w;
	this.file = null;
	this.collections = new Array();
	this.xml = null;
	this.loaded = {site:false,file:false};
	this.currentlvl = urlParameters()['album']||0;
	this.slideshow = new iSlide();
}
function urlParameters(){
	var l = window.location.href.toString();
	if(typeof(l.split("?")) != "string" && l.split("?")[1]){ 
		var q = l.split("?")[1].split("&");var d = new Array();
		if(!q[1]) d[q.toString().split("=")[0]] = q.toString().split("=")[1];
		else for(var i=0;i<q.length;i++){
			var a = q[i].split("=");
			d[a[0]] = a[1];
		}return d;
	}
	return false;
}
mediacenter.prototype.defaultthumb = function(n,src){
	switch(n){
		case 'image/jpeg': return src;
		case 'application/msword': return "/style/msword.png";
		default: return "/style/empty.png";
	}
}
mediacenter.prototype.addCollection = function(dom){
	var name = dom.name||dom.getAttribute('name')||"Naamloos";
	var title= dom.title||dom.getAttribute('title')||null;
	var date = readDate(dom.timestamp || dom.getAttribute('timestamp') || null);
	var desc = dom.description||dom.getAttribute('description')||"-";
	var key  = dom.key||dom.getAttribute('key')||name;
	var id   = dom.id||dom.getAttribute('id')||"-";;
	var c = new collection(name,title,desc,date,this.defaultthumb(),new Array(),key,id);
	if(dom.getElementsByTagName('file').length>0){
		var files = dom.getElementsByTagName('file');
		for(var i=0;i<files.length;i++){
			var nm = files[i].name||files[i].getAttribute('name')||"Naamloos";
			var ti = files[i].title||files[i].getAttribute('title')||null;
			var da = readDate( files[i].timestamp || files[i].getAttribute('timestamp') || null);
			var de = files[i].description||files[i].getAttribute('description')||"-";
			var mi = files[i].mime||files[i].getAttribute('mime')||"txt/plain";
			var sr = files[i].src||files[i].getAttribute('src')||this.defaultthumb;
			var th = (files[i].thumb||files[i].getAttribute('thumb'))||this.defaultthumb(mi,sr);
			c.subs[c.subs.length] = new media(nm,ti,de,da,mi,sr,th);
			if(i==0) c.thumb = th;
		}
	}
	this.collections[this.collections.length++] = c;
};
mediacenter.prototype.sortByColumn = function(a,b){
	var x = eval("a."+this.sortby);
	var y = eval("b."+this.sortby);
	return ((x<y)?-1:((x>y)?1:0));
};
mediacenter.prototype.renderCollections = function(){
	var os = this.collections;
	var w = document.getElementById(this.wrapperid);rC(w);aC(w,document.createElement('ul'));
	for(var x=0;x<os.length;x++){
		var o=os[x];
		var l = document.createElement('li');
		var a = document.createElement('a');
		var i = document.createElement('img');
		var s = document.createElement("span");
		setClass(l,"album");setClass(s,"tape2");
		sA(a,"href","javascript:mc.render("+(x+1)+");");sA(i,"src",o.thumb);
		sA(i,"alt",o.title);sA(a,"title",o.title);
		s.appendChild(document.createTextNode(o.name));
		aC(a,s);aC(a,i);aC(l,a);aC(w.firstChild,l);
	}
	//rC(h2);h2.appendChild(document.createTextNode("MediaCenter v1.0 by Herman Banken"));
	var ul = document.getElementById("mcheader").getElementsByTagName('ul')[0];
	rC(ul);ul.appendChild(document.createTextNode(""));
};
mediacenter.prototype.searchCollectionId = function(key,value){
	var os = this.collections;
	for(var x=0;x<os.length;x++){
		if(os[x][key] && (os[x][key] == value || os[x][key].substr(0,value.length) == value)){
			return (x+1);
		}
	}
	return 0;
};
mediacenter.prototype.renderCollection = function(){
	var os = this.collections[this.currentlvl-1].subs;
	var w = document.getElementById(this.wrapperid);rC(w);aC(w,document.createElement('ul'));var photo = 0;
	for(var x=0;x<os.length;x++){
		var o=os[x];
		var l = document.createElement('li');
		var a = document.createElement('a');
		var i = document.createElement('img');
		var s = document.createElement("span");
		setClass(l,"album");setClass(s,"cutedge");
		sA(i,"src",o.thumb);
		sA(i,"alt",o.title);sA(a,"title",o.title);
		aC(a,s);aC(a,i);aC(l,a);aC(w.firstChild,l);
		if(o.mime!="image/jpeg"){ sA(a,"href",o.src); }else{
			sA(a,"href","javascript:mc.startSlideshow("+photo+");");photo++;
		}
	}
	var ul = document.getElementById("mcheader").getElementsByTagName('ul')[0];rC(ul);
	var li= document.createElement('li');setClass(li,'button_up');li.onclick = function(){ mc.render(0); };
	li.appendChild(document.createTextNode("Terug"));aC(ul,li);
	var li= document.createElement('li');setClass(li,'button_slide');li.onclick = function(){ mc.startSlideshow(); };
	li.appendChild(document.createTextNode("Slideshow starten"));aC(ul,li);
	this.slideshow.loadPhotos(os);
};
mediacenter.prototype.renderMapview = function(id){
	var u = document.getElementById(id).getElementsByTagName('ul')[0];rC(u);
	var cs = this.collections;
	for(var x=0;x<cs.length;x++){
		var c=cs[x];
		var l = document.createElement('li');
		if(this.currentlvl==x+1) setClass(l,"current");
		l.rel=x+1;
		l.onclick = function(){ mc.render(this.rel); };
		sA(l,"title",c.title);aC(l,document.createTextNode(c.name));
		aC(u,l);
	}
};
mediacenter.prototype.render = function(i){
	this.currentlvl = i;
	this.renderMapview("mappen");
	if(i==0) this.renderCollections();
	else this.renderCollection();
};
mediacenter.prototype.process = function(xml){
	if(!xml){ return false; }
	var cs = gEBTN(xml,'collection');
	for(var x=0;x<cs.length;x++){
		this.addCollection(cs[x]);
	}
	this.sortby = 'name';
	var a = this.currentlvl;
	if(urlParameters()){
		if(urlParameters()['alb']) a = mc.searchCollectionId("key",urlParameters()['alb']);
		if(urlParameters()['pnr']){ this.render(a);mc.startSlideshow(urlParameters()['pnr']); }
	}
	this.render(a);
};
mediacenter.prototype.order = function(n){
	this.sortby = n;
	if(this.currentlvl==0)
		this.collections.sort(this.sortByColumn);
	else
		this.collections[this.currentlvl-1].subs.sort(this.sortByColumn);
	this.render(this.currentlvl);
};
mediacenter.prototype._reverse = function(){
	if(this.currentlvl==0)
		this.collections.reverse();
	else
		this.collections[this.currentlvl-1].subs.reverse();
	this.render(this.currentlvl);
};
mediacenter.prototype.startSlideshow = function(n){
	if(n==null) n=0;
	if(this.currentlvl==0) return;
	else{
		this.slideshow.popup();
		this.slideshow.display(n);
	}
};
mediacenter.prototype.loadFile = function(f){
	this.file = f;
	mcinstance = this;
	var a = new Ajax.Request(f,{
		method: 'get',
		onSuccess: function(transport){
			mcinstance.process(transport.responseXML||false);
			//mcinstance = null;
		},
		onFailure: function(){ alert('Update uw browser s.v.p. om gebruik te kunnen maken van dit mediasysteem.') }
	});
};
