
var vdiddy = {
	COLS: 5,
	DEFAULT_SITE: 'youtube',
	ROWS: 2,
	STATIC_BASE: 'http://static.vdiddy.com/channels/',
//	STATIC_BASE: 'channels/',
	THUMB_W: 120,
	THUMB_H: 90,
	ImgOnload: function(img) {		
		if (Math.floor(img.index / this.COLS) == this.ROWS - 1)
			Reflection.add(img);
		$('#vpThumb' + img.index).css({left: -this.THUMB_W});
	},
	ImgOnmouseout: function(img) {
		$('#vpHilite' + img.index).css({visibility: 'hidden'});
		$('#vpTitle').html('');
	},
	ImgOnmouseover: function(img) {
		$('#vpHilite' + img.index).css({visibility: 'visible'});
		$('#vpTitle').html(this.channels[this.currentChannel].feed.items[img.index].title);
	},
	Init: function() {
		this.MAX_VIDEOS = this.COLS * this.ROWS;
		this.InitPanel();
		$.getScript(this.STATIC_BASE + 'channels.js', this.InitChannels);
	},
	InitChannel: function(channel) {
		vdiddy.currentChannel = channel;
		$('#vpTitle').html('');
		var items = this.channels[channel].feed['items'];
		var n = Math.min(this.MAX_VIDEOS, items.length);
		var i;
		for (i = 0; i < this.MAX_VIDEOS; i++)
			$('#vpHilite' + i).css({visibility: 'hidden'});
		for (i = 0; i < n; i++) {			
			$('#vpSpinner' + i).show();
			var item = items[i];
    	var img = document.createElement('img');
			img.index = i;
			img.onload = function() {vdiddy.ImgOnload(this);};
			img.onmouseover = function() {vdiddy.ImgOnmouseover(this);};
			img.onmouseout = function() {vdiddy.ImgOnmouseout(this);};
//			img.src = 'resize?img=' + item.thumb;
			img.src = this.STATIC_BASE + channel + '/' + channel + i + '.jpg';			
			var a = document.createElement('a');
			a.href = 'javascript:vdiddy.Play(' + i + ')';			
			$('#vpThumb' + i).css({left: 0}).empty().append($(a).append(img));
		}
		for (; i < this.MAX_VIDEOS; i++)
			$('#vpSpinner' + i).hide();
	},
	InitChannels: function() {
		var s = document.createElement('select');
		s.onchange = function() {
			if (s.options[s.selectedIndex].value != 'disabled')
				vdiddy.LoadChannel(s.options[s.selectedIndex].value);
		};
		var i = 0;
		var selectedIndex;
		for (var id in vdiddy.channels) {		
			if (vdiddy.channels[id].nsfw) {
				var option = new Option('Sometimes NSFW:', 'disabled'); 
				option.disabled = true;
				option.style.color = 'gray';
				s.options.add(option);
			}
			var option = new Option(String.fromCharCode(160, 160) + vdiddy.channels[id].title, id);
			if (id == vdiddy.DEFAULT_SITE)
				selectedIndex = i;
			s.options.add(option);
			i++;
		}
		s.selectedIndex = selectedIndex;
		$('#vpSelect').empty().append(s);
		vdiddy.LoadChannel(vdiddy.DEFAULT_SITE);
	},
	InitHilite: function(vpanel, id, which) { 
		var row = document.createElement('div');
		$(row).addClass('vpHiliteBar').css({clear: 'left'}).appendTo(vpanel);
		for (var i = id; i < id + this.COLS; i++) {
			var hilite = document.createElement('div');
			$(hilite).attr('id', 'vpHilite' + i).addClass('vpHilite' + which).css({float: 'left', visibility: 'hidden', width: this.THUMB_W}).appendTo(row);
		}
	},	
	InitPanel: function() {
		this.panelW = this.COLS * this.THUMB_W;
		var vpanel = $('#vpanel');
		vpanel.css({width: this.panelW});
		var i = 0;
		for (var y = 0; y < this.ROWS; y++) {
			var lastRow = y == this.ROWS - 1;
			if (lastRow) {
				var titleRow = document.createElement('div');
				$(titleRow).attr('id', 'vpTitleBar').css({clear: 'left', overflow: 'hidden', width: this.panelW}).appendTo(vpanel);
				var title = document.createElement('div');
				$(title).attr('id', 'vpTitle').appendTo(titleRow);				
				this.InitHilite(vpanel, i, 1);
			}
			var h = lastRow ? this.THUMB_H * 2 : this.THUMB_H;
			var row = document.createElement('div');
			$(row).css({clear: 'left'}).appendTo(vpanel);
			for (var x = 0; x < this.COLS; x++, i++) {
				var clip = document.createElement('div');
				$(clip).addClass('clip').css({float: 'left', overflow: 'hidden', width: this.THUMB_W, height: h}).appendTo(row);
				var item = document.createElement('div');
				$(item).css({width: this.THUMB_W * 2}).appendTo(clip);	 
				var loading = document.createElement('div');
				$(loading).css({float: 'left', overflow: 'hidden', width: this.THUMB_W, height: h}).appendTo(item);
				var bg = document.createElement('div');
				$(bg).css({width: this.THUMB_W, height: h}).appendTo(loading);
    		var img = document.createElement('img');
				$(img).attr('src', 'img/black.gif').appendTo(bg);
				if (lastRow)
					Reflection.add(img);
				var spinner = document.createElement('div');
				$(spinner).attr('id', 'vpSpinner' + i).css({position: 'relative', top: -h, width: this.THUMB_W}).hide().appendTo(loading);
    		var img = document.createElement('img');
				$(img).attr('src', 'img/spinner.gif').addClass('vpSpinner').appendTo(spinner);
				var thumb = document.createElement('div');
				$(thumb).attr('id', 'vpThumb' + i).css({float: 'left', position: 'relative', width: this.THUMB_W, height: h}).appendTo(item);		
			}
			if (!lastRow)
				this.InitHilite(vpanel, i - this.COLS, 0);
		}
	},
	LoadChannel: function(channel) {	
		$('.hilite').css({visibility: 'hidden'});
		if (!this.channels[channel].feed)
			$.getScript(this.STATIC_BASE + channel + '/' + channel + '.js', function() {vdiddy.InitChannel(channel)});
		else
			this.InitChannel(channel);
	},	
	Play: function(i) {
		var channel = this.channels[this.currentChannel];
		var feed = channel.feed;
		var item = feed.items[i];		
		var url = 'play' +
							'?title=' + encodeURIComponent(item.title) +
							'&w=' + feed.swf_w +
							'&h=' + feed.swf_h +
							'&log=' + (Number(channel.id) * 10 + i + 1);		
		if (item.embed)
			url += '&embed=' + encodeURIComponent(item.embed);
		else
			url += '&swf=' + encodeURIComponent(item.swf);
		new_window = window.open(url,
														 this.currentChannel + i,
														 'width=' + Number(feed.swf_w) * 2 + ',height=' + feed.swf_h);
	},
	StripSlashes: function(str) {
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\\\/g,'\\');
		str=str.replace(/\\0/g,'\0');
		return str;	
	}
};
