/* Feeds */

var YJS = new ajax_widget();
YJS.initWidget = function()
	{
	this.container=document.getElementById('youtube');
	this.name='video';
	this.content=document.getElementById(this.name);
	};
YJS.initConfig = function(values)
		{
		this.config['source_type']=(values[0]?values[0].toLowerCase():'search');
		this.config['source_id']=(values[1]?values[1].toLowerCase():'elitwork');
		this.config['max_results']=(values[2]?values[2].toLowerCase():5);;
		this.config['update_browser']=(values[3]&&values[3]!='false'?values[3]:false);
		};
YJS.requestData = function()
		{
		var path = "http://gdata.youtube.com/feeds/";
		switch (this.config['source_type']) {
			case 'search':
				path += 'videos?vq='+this.config['source_id']+'&';
				break;
			case 'user':
				path += 'users/'+this.config['source_id']+'?';
				break;
			case 'playlist':
				path += 'playlists/'+this.config['source_id']+'?';
				break;
		}
		path+='alt=json-in-script&callback=YJS.handleResponse&max-results='+this.config['max_results'];
		this.requestJSonData(path);
		};

YJS.handleResponse = function(rsp)
		{
		if (!rsp.feed)
			{
			this.error(rsp);
			}
		else
			{
			switch (this.config['source_type'])
				{
				//case 'search':
				default:
					if(rsp.feed.openSearch$totalResults.$t>0)
						{
						this.data = rsp.feed.entry;
						}
					else
						{
						this.notice('Pas de video pour le moment !');
						}
					break;
				}
			if(this.data)
				{
				this.content.innerHTML='<p><span id="'+this.name+'_nav"><a href="#" id="'+this.name+'_prev">&lt;</a> <span id="'+this.name+'_count">0/0</span> <a href="#" id="'+this.name+'_next">&gt;</a></span><br /><a href="#" id="'+this.name+'_link" onclick="window.open(this.href); return false;"><img src="" id="'+this.name+'_img" /><br /><strong id="'+this.name+'_title"></strong><br /></a></p>';
				this.setFirstItem();
				}
			}
		};

YJS.loadCurrentItem = function()
		{
		// title
		document.getElementById(this.name+'_title').innerHTML=this.data[this.current].title;
		// navigation
		document.getElementById(this.name+'_prev').setAttribute('href','javascript:YJS.goPreviousItem();');
		document.getElementById(this.name+'_next').setAttribute('href','javascript:YJS.goNextItem();');
		document.getElementById(this.name+'_count').innerHTML=this.getCurrentCount();
		// description
		document.getElementById(this.name+'_img').innerHTML=this.data[this.current]['media$group']['media$thumbnail'][1].url;
		// link
		var url='';
		var entry=this.data[i];
		for (var k = 0; k < entry.link.length; k++)
			{
			if (entry.link[k].rel == 'alternate')
				{
				url = entry.link[k].href;
				break;
				}
			}
		return url;
		document.getElementById(this.name+'_link').setAttribute('href',url);
		};

if(window.addEventListener)
	{
	window.addEventListener('load', function() { YJS.init(); }, false);
	}
else if(window.attachEvent)
	{
	window.attachEvent('onload', function() { YJS.init(); }, false);
	}