if (!Function.prototype.bindAsEventListener) {
	Function.prototype.bindAsEventListener = function() {
		var __method = this, args = $A(arguments), object = args.shift();
		return function(event) {
			return __method.apply(object, [event || window.event].concat(args));
		}
	}
}
var $A = Array.from = function(iterable) {
	if (!iterable) return [];
	if (iterable.toArray) {
		return iterable.toArray();
	} else {
		var results = [];
		for (var i = 0; i < iterable.length; i++)
			results.push(iterable[i]);
		return results;
	}
}
if (!Math.customRound) {
	Math.customRound = function (number, digits) {
		if (arguments.length == 1) var digits = 2;
		var d = Math.pow(10, digits);
		return parseInt(number * d) / d;
	}
}
if (typeof(IDTVVWP) == "undefined") {
	IDTVVWP = function () {this.initialize.apply(this, arguments);};
	IDTVVWP.prototype = {
		frame: null,
		initialize: function (ds) {

			this.ds = ds;
			this.element = document.getElementById('IDTVP_' + this.ds.id);
			if (!this.element && document.getElementsByClassName)
				try {
					this.element = document.getElementsByClassName ('idtv_ceb3015a00')[0];
				} catch(e) {}
			if (!this.element) { alert ('The iDesktop.tv video wall is not not embedded properly!'); return;}
			this.element.innerHTML = '<div class="idtv_' + this.ds.id + '"><div class="idtv_inres"></div><div class="idtv_bb">' +
				'<div class="idtv_btn_prev"></div><div class="idtv_btn_next"></div><div class="idtv_bb_pages"></div></div></div>';
			var divs = this.element.getElementsByTagName('div');
			this.elementout = divs[0];
			this.elementin = divs[1];
			this.btn_prev = divs[3];
			this.btn_next = divs[4];
			this.pagers_area = divs[5];
			if (this.ds.data.error) {
				this.elementin.innerHTML = '<div style="text-align:center">' + this.ds.data.error + '</div>';
			} else {
				this.btn_prev.onclick = this.prevP.bindAsEventListener(this);
				this.btn_next.onclick = this.nextP.bindAsEventListener(this);
				if (this.ds.only_public) { // go trough ds.data.results and remove all private videos
					var newdata = new Array();
					for (var i = 0; i < this.ds.data.results.length; i ++)
						if (!this.ds.data.results[i].isprivate)
							newdata.push(this.ds.data.results[i]);
					this.ds.data.results = newdata;
				}
				this.results = new Array();
				var jsonArr = this.ds.data.results;
				this.page = 0; this.resc = jsonArr.length;
				this.maxpage = Math.ceil(this.resc/this.ds.pp) ;
				for (var i = 0, n = this.resc; i < n; i ++) {
					if (this.ds.only_public && jsonArr[i].isprivate) continue;
					var nr = document.createElement('div');
					nr.className = 'idtv_res';
					if ((i % this.ds.cols + Math.floor(i / this.ds.cols)) % 2) nr.className += ' alt';
					jsonArr[i].title_esc = (jsonArr[i].title ? jsonArr[i].title.replace(/"/g, "&quot;") : '');  // comment only to fix code coloring "
					var title = jsonArr[i].title_esc + (this.ds.thumb_show_length && jsonArr[i].length_seconds ?' (' + jsonArr[i].length_seconds + ')':'');
					var t = '<img src="' + jsonArr[i].thumbnail_url + '" alt="' + jsonArr[i].title_esc + '" title="' + title + '" />';
					if (this.ds.thumb_show_title || this.ds.thumb_show_author || this.ds.thumb_show_length || this.ds.thumb_show_rating || this.ds.thumb_show_description) {
						var at = '';
						if (this.ds.thumb_show_title)
							//at = '<a href="http://www.idesktop.tv/?watch=' + jsonArr[i].id + '" title="' + jsonArr[i].title + '" target="_blank">' + jsonArr[i].title + "</a>";
							at = '<span class="t">' + (jsonArr[i].title ? jsonArr[i].title : '') + "</span>";
						if (this.ds.thumb_show_author) {
							at += '<span class="ah">' + (this.ds.thumb_show_labels?'<span class="lb">Author: </span>':'') + (jsonArr[i].author ? jsonArr[i].author : '') + '</span>';
						}
						if (this.ds.thumb_show_length || this.ds.thumb_show_views) {
							at += '<span class="lv">';
							if (this.ds.thumb_show_length) at += (this.ds.thumb_show_labels?'<span class="lb">Length: </span>':'') + (jsonArr[i].length_seconds ? jsonArr[i].length_seconds : '');
							if (this.ds.thumb_show_length && this.ds.thumb_show_views) at += ' | ';
							if (this.ds.thumb_show_views) at += (this.ds.thumb_show_labels?'<span class="lb">Views: </span>':'') + (jsonArr[i].view_count ? this.formatCommasInt(jsonArr[i].view_count) : '');
							at += '</span>';
						}
						if (this.ds.thumb_show_rating)
							at += '<span class="r">' + (this.ds.thumb_show_labels?'<span class="lb">Rating: </span>':'') + (jsonArr[i].rating_avg ? Math.customRound(jsonArr[i].rating_avg, 1) : '') + "</span>";
						if (this.ds.thumb_show_description) {
							at += '<span class="d">' + (jsonArr[i].description ? jsonArr[i].description : '') + "</span>";
						}
						t = t + '<div class="out" title="' + title + '"><div class="in">' + at + '</div></div>';
					}
					if (this.ds.mark_private && jsonArr[i].isprivate)
						nr.innerHTML += '<div class="private_mark">PRIVATE</div>';
					nr.innerHTML += t;
					this.elementin.appendChild(nr);
					nr.style.display = 'none';
					this.results.push(nr);
					nr.onclick = this.onresclick.bindAsEventListener(this, i);
				}
				this.showRes();
			}
			var f = document.createElement('iframe');
			f.src = "http://www.idesktop.tv/embed/ceb3015a00/utr.html?r=" + document.location;
			f.style.cssText = 'width:0;height:0;border:none;';
			f.frameborder = f.frameBorder = f.border = "0";
			this.element.appendChild(f);
		},
		onresclick: function (event,i) {
			var elem = event.target || event.srcElement;
			if (elem.tagName == 'A') return;
			if (this.ds.plpl == "newwin") {
				var loaded = false;
				if (this.frw) {
					try {
						this.frw.Player.playlist.selectItem(i);
						this.frw.Player.playlist.playSelected();
						loaded = true;
					} catch(e) {
						loaded = false;
					}
				}
				if (!loaded) {
					this.frw = window.open ('http://'+this.ds.chost+'/embed/'+this.ds.id+'/play.html?vid='+this.ds.data.results[i].id, 'idtvwin', 'width=' + this.ds.plw + ',height=' + this.ds.plh);
				}
				return;
			}
			if (!this.frame) {
				this.plclbtn = document.createElement('img');
				this.plclbtn.src = this.ds.clbtnsrc;
				this.plclbtn.onclick = this.closeplr.bindAsEventListener(this);
				this.plclbtn.className = 'idtvp_clbtn';
				this.frameholder = document.createElement('div');
				this.frameholder.id = 'idtvpfh' + this.ds.id;
				this.frame = document.createElement('iframe');
				this.frame.id = 'idtvpf' + this.ds.id;
				this.frame.frameborder = this.frame.frameBorder = this.frame.border = "0";
				try {
					document.body.appendChild(this.frameholder);
					this.ba = true;
				} catch (e) {
					this.elementout.appendChild(this.frameholder);
					this.ba = false;
				}
				this.frameholder.appendChild(this.plclbtn);

			}
			/*this.frameholder.style.top = this.results[i].offsetTop + "px";
			this.frameholder.style.left = this.results[i].offsetLeft + this.results[i].offsetWidth + "px";*/
			var scrlX = (window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft);
		    var scrlY = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop);
		    var viewportW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth ;
		    var viewportH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		    var element = this.elementout;
			var elFixedX = 0, elFixedY = 0;
		    do {
		    	elFixedY += element.offsetTop  || 0; elFixedX += element.offsetLeft || 0; element = element.offsetParent || element.parentNode;
		    } while (element);
		    var nx = 0, ny =0;
			switch (this.ds.plpl) {
				case "center":
					nx = scrlX + ((viewportW - this.ds.plw) / 2) - elFixedX;
					ny = scrlY + ((viewportH - this.ds.plh) / 2) - elFixedY;
					break;
				case "left":
					nx = this.results[i].offsetLeft - this.ds.plw;
					ny = this.results[i].offsetTop - this.ds.pldelta;
					break;
				case "right":
					nx = this.results[i].offsetLeft + this.results[i].offsetWidth;
					ny = this.results[i].offsetTop - this.ds.pldelta;
					break;
				case "top":
					nx = this.results[i].offsetLeft - this.ds.pldelta;
					ny = this.results[i].offsetTop - this.ds.plh;
					break;
				case "bottom":
					nx = this.results[i].offsetLeft - this.ds.pldelta;
					ny = this.results[i].offsetTop + this.results[i].offsetHeight;
					break;
				default: return;
			}
			if (this.ba) {
				nx += elFixedX; ny += elFixedY;
				if (nx < 0) nx = 0;
				if (ny < 0) ny = 0;
			} else {
				if (this.ds.plpl != "center") { // some checks if top/left it is out of the viewport
					var rx = nx + elFixedX, ry = ny + elFixedY;
					if (rx < 0) nx -= rx;
					if (ry < 0) ny -= ry;
				}
			}
			this.frameholder.style.left = nx + "px";
			this.frameholder.style.top = ny + "px";

			this.frameholder.style.display = 'block';
			var loaded = false;
			try {
				if (this.frame.contentWindow && this.frame.contentWindow.Player) { // detect if script access is allowed
					this.frame.contentWindow.Player.playlist.selectItem(i);
					this.frame.contentWindow.Player.playlist.playSelected();
					loaded = true;
				}
			} catch (e) {};
			if (!loaded) {
				if (this.frame.parentNode == this.frameholder) this.frameholder.removeChild(this.frame);
				this.frame.src = 'http://'+this.ds.chost+'/embed/'+this.ds.id+'/play.html?vid='+(this.ds.data.results[i].id ? this.ds.data.results[i].id : i);
				this.frameholder.appendChild(this.frame);
			}
		},
		closeplr: function (event) { this.frameholder.style.display='none'; this.frameholder.removeChild(this.frame); },
		showRes: function () {
			if (this.results.length == 0)
				this.pagers_area.innerHTML = 'no results';
			else {
				var s = this.page * this.ds.pp, e = Math.min (s + this.ds.pp, this.results.length) - 1;
				for (var i = 0, n = this.results.length; i < n; i ++)
					this.results[i].style.display =  (i < s || i > e ? 'none' : 'block');
				this.pagers_area.innerHTML = (s+1) + ' - ' + (e+1) + ' (' + this.results.length + ')';
			}
		},
		nextP: function () { if (this.page < this.maxpage - 1) { this.page ++; this.showRes(); } },
		prevP: function () { if (this.page > 0) { this.page --; this.showRes(); } },
		formatCommasInt: function (val) {
			var s = new String(val);
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(s)) s = s.replace(rgx, '$1' + ',' + '$2');
			return s;
		}
	}
}

var IDTVLBDataSourceceb3015a00 = {
	id: 'ceb3015a00', chost:'www.idesktop.tv',clbtnsrc:'http://www.idesktop.tv/images/player_icons/close.gif',
	width: 408, height: 77, pp: 4,
	rows: 1, cols: 4,
	plw: 500, plh: 420,
	createP: function () {
		if (this.reverse_playlist) this.data.results = this.data.results.reverse();
		this.p = new IDTVVWP(this);
	},
	plpl: 'center',
	only_public:true,mark_private:true,
	thumb_show_title : true, thumb_show_author : false,
	thumb_show_rating : false, thumb_show_views: false,
	thumb_show_length : false, thumb_show_description : false,
	thumb_show_labels : false,
	thumbnail_text: 'O',
	reverse_playlist: false,
	pldelta: 0,
		data: {results: [{id:'HcOZ6xFxJqg',title:'Safety Dance- Men Without Hats',author:'taze',real_length:'162',length_seconds:'02:42',rating_avg:'4.85',rating_count:'5609',view_count:'1919921',description:'Best Music Video EVER',tags:'Dance, Safety',thumbnail_url:'http://img.youtube.com/vi/HcOZ6xFxJqg/2.jpg',upload_time:'1141273220',comment_count:'0',url:'http://gdata.youtube.com/feeds/api/videos/HcOZ6xFxJqg/responses',ord:'11'},{id:'HSoVKUVOnfQ',title:'robot dance',author:'swingcheese',real_length:'102',length_seconds:'01:42',rating_avg:'4.77',rating_count:'47226',view_count:'17289357',description:'hip hop champions',tags:'dance, hip, hop, robot',thumbnail_url:'http://img.youtube.com/vi/HSoVKUVOnfQ/2.jpg',upload_time:'1118755861',comment_count:'0',url:'http://gdata.youtube.com/feeds/api/videos/HSoVKUVOnfQ/responses',ord:'1'},{id:'XMjgSkfQPSY',title:'Flight of the Conchords Ep 12 Bret\'s Angry Dance',author:'r8dkid',real_length:'92',length_seconds:'01:32',rating_avg:'4.91',rating_count:'1091',view_count:'490062',description:'From the 12th and final ep on Season 1 :(',tags:'conchords, flight, flight.of.the.conchords, hbo, of, sub.pop, subpop, the',thumbnail_url:'http://img.youtube.com/vi/XMjgSkfQPSY/2.jpg',upload_time:'1188807044',comment_count:'0',url:'http://gdata.youtube.com/feeds/api/videos/XMjgSkfQPSY/responses',ord:'28'},{id:'rGkIUlYEQT8',title:'Break Machine - street dance',author:'danny729',real_length:'225',length_seconds:'03:45',rating_avg:'4.62',rating_count:'2135',view_count:'2186899',description:'freestyle',tags:'Music',thumbnail_url:'http://img.youtube.com/vi/rGkIUlYEQT8/2.jpg',upload_time:'1137925652',comment_count:'0',url:'http://gdata.youtube.com/feeds/api/videos/rGkIUlYEQT8/responses',ord:'18'}]},
		initialize: function () {this.createP()}

};
var IDTVLBDataSourceceb3015a00IF = function () {IDTVLBDataSourceceb3015a00.initialize.apply(IDTVLBDataSourceceb3015a00, []);};
if (document.getElementById('IDTVP_ceb3015a00'))
	IDTVLBDataSourceceb3015a00IF ();
else if (window.addEventListener)
	window.addEventListener("load", IDTVLBDataSourceceb3015a00IF, true);
else
	window.attachEvent("onload", IDTVLBDataSourceceb3015a00IF);
