// Mootube box
var Mootube = new Class({
    start: function(mt){
	var mootube = this;
	// this = the instance of Mootube
	this.selectorlist = mt.getElement('ul');
	this.videoview = new Element('div', {
	    'class': 'mootubeview'
	});
	this.videoview.injectBefore(this.selectorlist);
	this.selectors = this.selectorlist.getElements('li');
	this.selectors.each(function(li,index){
	    // this = window
	    var ms = new MootubeSelector();
	    ms.start(mootube, li);
	    if (index===0) {
		ms.select(li, 0);
	    }
	});
	
    },
    deselectall: function(){
	this.selectors.removeClass('selected');
    }
});

var MootubeSelector = new Class({
    start: function(mootube, li){
	// this = the instance of MootubeSelector
	this.videoview = mootube.videoview;
	var anchor = li.getElement('a');
	this.videourl = anchor.getProperty('href');
	var theSelector = this;
	anchor.addEvent('click', function(evt){
	    // this = anchor
	    mootube.deselectall();
	    theSelector.select(li, 1); // me: 
	    new Event(evt).stop(); // not necessary in Mootools 1.2?
	    return false; // prevent further event processing in Mootools 1.2?
	});
    },
    getVID: function(vurl, autoplay){
	var vtype, vid, hsize, vsize, color1, color2, autoplaystr, embedhtml;
	embedhtml = '<span class="moodummy">.</span>';
	if (vurl.match(/http:\/\/[^\/]*youtube\.com\//)) {
	    vtype = "YouTube";
	    vid = vurl.replace(/^.*\?/,'').split('&').filter(function(q){
		return q.match(/^v=([^&]*)/);
	    })[0].split('=')[1];
	    hsize = 425; vsize = 355;
	    color1 = "0x282828"; color2 = "0x666666";
	    autoplaystr = autoplay ? '&autoplay=1' : '';
	    embedhtml = embedhtml+
		'<object type="application/x-shockwave-flash"'+
		' width="'+hsize+'" height="'+vsize+'"'+
		' data="http://youtube.com/v/'+vid+'&rel=0&fs=1&hd=1'+
 		'&color1='+color1+'&color2='+color2+autoplaystr+'">'+
		'<param name="movie" value="http://youtube.com/v/'+vid+'&rel=0&fs=1&hd=1'+
 		'&color1='+color1+'&color2='+color2+autoplaystr+'">'+
		'<param name="allowFullScreen" value="true">'+
 		'<param name="wmode" value="transparent"></object>';
	} else if (vurl.match(/http:\/\/[^\/]*veoh\.com\//)) {
	    vtype = "Veoh";
	    vid = vurl.match(/http:\/\/www.veoh.com\/videos\/([^?]*)/)[1];
	    hsize = 425; vsize = 355;  // 540x438
	    autoplaystr = autoplay ? '&videoAutoPlay=1' : '';
	    embedhtml = embedhtml+
		'<embed src="http://www.veoh.com/videodetails2.swf?'+
		'permalinkId='+vid+'&id=anonymous&player=videodetailsembedded'+
		autoplaystr+'" allowFullScreen="true"'+
		' width="'+hsize+'" height="'+vsize+'" bgcolor="#000000"'+
		' type="application/x-shockwave-flash"'+
		' pluginspage="http://www.macromedia.com/go/getflashplayer">'+
		'</embed>';
	} else if (vurl.match(/http:\/\/[^\/]*brightcove\.tv\//)) {
	    vtype = "brightcove";
	    vid = vurl.replace(/^.*\?/,'').split('&').filter(function(q){
		return q.match(/^title=([^&]*)/);
	    })[0].split('=')[1];
	    hsize = 425; vsize = 355;  // 540x438
	    autoplaystr = autoplay ? '&amp;autoStart=true' : '&amp;autoStart=false';
	    embedhtml = embedhtml+
		'<embed src="http://www.brightcove.tv/playerswf"'+
		' bgcolor="#282828"'+
		' flashVars="initVideoId='+vid+
		'&amp;cdnURL=http://admin.brightcove.com'+autoplaystr+'"'+
		' width="'+hsize+'" height="'+vsize+'" allowFullScreen="true"'+
		' name="bcPlayer" quality="high"'+
		' type="application/x-shockwave-flash">'+
		'</embed>';
	} else if (vurl.match(/http:\/\/[^\/]*myvideo\.de\//)) {
	    vtype = "MyVideo.de";
	    vid = vurl.match(/http:\/\/www.myvideo.de\/watch\/([0-9]+).*/)[1];
	    hsize = 425; vsize = 355;
	    autoplaystr = autoplay ? '&amp;autoStart=true' : '';
	    embedhtml = embedhtml+
		'<object style="width:425px;height:355px;"'+
		' width="425" height="355"'+
		' type="application/x-shockwave-flash"'+
		' data="http://www.myvideo.de/movie/'+vid+'">'+
		'<param name="movie" value="http://www.myvideo.de/movie/'+vid+'">'+
		'<param name="AllowFullscreen" value="true">'+
		'</object>';
	}
	return embedhtml;
    },
    select: function(listitem, autoplay){
	var ap = (autoplay == 1) ? 1 : 0;
	this.videoview.set('html', this.getVID(this.videourl, ap));
	listitem.addClass('selected');
    }
});

window.addEvent('domready', function(){
    $$('.mootube').each(function(mt){
	new Mootube().start(mt);
    });
});
